Merge pull request #40 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Daniel Hensby 2015-11-21 12:06:58 +00:00
commit e692437b7c
2 changed files with 168 additions and 156 deletions

View File

@ -4,20 +4,22 @@
* @package mathspamprotection
*/
class MathSpamProtector implements SpamProtector {
class MathSpamProtector implements SpamProtector
{
/**
* Returns the {@link MathSpamProtectorField} associated with this protector
*
* @return MathSpamProtectorField
*/
public function getFormField($name = null, $title = null, $value = null) {
public function getFormField($name = null, $title = null, $value = null)
{
return new MathSpamProtectorField($name, $title, $value);
}
/**
* Not used by MathSpamProtector
*/
public function setFieldMapping($fieldMapping) {}
public function setFieldMapping($fieldMapping)
{
}
}

View File

@ -6,8 +6,8 @@
* @package mathspamprotection
*/
class MathSpamProtectorField extends TextField {
class MathSpamProtectorField extends TextField
{
/**
* @config
*
@ -29,16 +29,18 @@ class MathSpamProtectorField extends TextField {
*/
private static $allow_numeric_answer = true;
public function Field($properties = array()) {
if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
public function Field($properties = array())
{
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return parent::Field($properties);
}
return null;
}
public function FieldHolder($properties = array()) {
if(Config::inst()->get('MathSpamProtectorField', 'enabled')) {
public function FieldHolder($properties = array())
{
if (Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return parent::FieldHolder($properties);
}
@ -50,10 +52,11 @@ class MathSpamProtectorField extends TextField {
*
* @return string
*/
public function Title() {
public function Title()
{
$prefix = Config::inst()->get('MathSpamProtection', 'question_prefix');
if(!$prefix) {
if (!$prefix) {
$prefix = _t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s");
}
@ -70,12 +73,13 @@ class MathSpamProtectorField extends TextField {
*
* @return bool
*/
public function validate($validator) {
if(!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
public function validate($validator)
{
if (!Config::inst()->get('MathSpamProtectorField', 'enabled')) {
return true;
}
if(!self::correct_answer($this->Value())) {
if (!self::correct_answer($this->Value())) {
$validator->validationError(
$this->name,
_t(
@ -97,20 +101,21 @@ class MathSpamProtectorField extends TextField {
*
* @return string
*/
public static function get_math_question() {
if(!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) {
$v1 = rand(1,9);
$v2 = rand(1,9);
public static function get_math_question()
{
if (!Session::get("mathQuestionV1") && !Session::get("mathQuestionV2")) {
$v1 = rand(1, 9);
$v2 = rand(1, 9);
Session::set("mathQuestionV1",$v1);
Session::set("mathQuestionV2",$v2);
Session::set("mathQuestionV1", $v1);
Session::set("mathQuestionV2", $v2);
} else {
$v1 = Session::get("mathQuestionV1");
$v2 = Session::get("mathQuestionV2");
}
return sprintf(
_t('MathSpamProtection.WHATIS',"What is %s plus %s?"),
_t('MathSpamProtection.WHATIS', "What is %s plus %s?"),
MathSpamProtectorField::digit_to_word($v1),
MathSpamProtectorField::digit_to_word($v2)
);
@ -124,7 +129,8 @@ class MathSpamProtectorField extends TextField {
*
* @return bool
*/
public static function correct_answer($answer){
public static function correct_answer($answer)
{
$v1 = Session::get("mathQuestionV1");
$v2 = Session::get("mathQuestionV2");
@ -141,7 +147,8 @@ class MathSpamProtectorField extends TextField {
*
* @return string
*/
public static function digit_to_word($num){
public static function digit_to_word($num)
{
$numbers = array(_t('MathSpamProtection.ZERO', 'zero'),
_t('MathSpamProtection.ONE', 'one'),
_t('MathSpamProtection.TWO', 'two'),
@ -162,12 +169,15 @@ class MathSpamProtectorField extends TextField {
_t('MathSpamProtection.SEVENTEEN', 'seventeen'),
_t('MathSpamProtection.EIGHTEEN', 'eighteen'));
if($num < 0) return "minus ".($numbers[-1*$num]);
if ($num < 0) {
return "minus ".($numbers[-1*$num]);
}
return $numbers[$num];
}
public function Type() {
public function Type()
{
return 'mathspamprotector text';
}
}