summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaslouski Yauheni2011-10-06 22:47:40 (GMT)
committer Maslouski Yauheni2011-10-06 22:47:40 (GMT)
commit15621dd1f7a33d8efb51595206baae759d3e0bd6 (patch)
treeb9eefa9bd5e92946982bb1b432e1be43f6b67e66
parent0b5db0b6767e16482491b4b78d2e428bdf766046 (diff)
by Spleshka: math captcha was ported to 7.x
-rw-r--r--math_captcha/math_captcha.admin.inc20
-rw-r--r--math_captcha/math_captcha.challenge.inc36
-rw-r--r--math_captcha/math_captcha.info7
-rw-r--r--math_captcha/math_captcha.install8
-rw-r--r--math_captcha/math_captcha.module17
5 files changed, 53 insertions, 35 deletions
diff --git a/math_captcha/math_captcha.admin.inc b/math_captcha/math_captcha.admin.inc
index 799abb3..95b3f86 100644
--- a/math_captcha/math_captcha.admin.inc
+++ b/math_captcha/math_captcha.admin.inc
@@ -1,6 +1,11 @@
<?php
/**
+ * @file
+ * Functionality and helper functions for MATH CAPTCHA administration.
+ */
+
+/**
* Math CAPTCHA settings form
*/
function math_captcha_settings_form() {
@@ -46,7 +51,7 @@ function math_captcha_settings_form() {
'#title' => t('Allow negative values.'),
'#default_value' => variable_get('math_captcha_addition_allow_negative', FALSE),
);
- // subtraction challenge
+ // Subtraction challenge
$form['math_captcha_subtraction'] = array(
'#type' => 'fieldset',
'#title' => t('Subtraction challenge: x - y = z'),
@@ -63,7 +68,7 @@ function math_captcha_settings_form() {
'#title' => t('Allow negative values.'),
'#default_value' => variable_get('math_captcha_subtraction_allow_negative', FALSE),
);
- // multiplication challenge
+ // Multiplication challenge
$form['math_captcha_multiplication'] = array(
'#type' => 'fieldset',
'#title' => t('Multiplication challenge: x * y = z'),
@@ -80,8 +85,9 @@ function math_captcha_settings_form() {
'#title' => t('Allow negative values.'),
'#default_value' => variable_get('math_captcha_multiplication_allow_negative', FALSE),
);
- $form['#validate'] = array('math_captcha_settings_form_validate');
- // add buttons and return
+
+ // Add validation function
+ $form['#validate'][] = 'math_captcha_settings_form_validate';
return system_settings_form($form);
}
@@ -89,14 +95,14 @@ function math_captcha_settings_form() {
* Validation function for Math CAPTCHA settings form
*/
function math_captcha_settings_form_validate($form, &$form_state) {
- // check enabled challenges
+ // Check enabled challenges
if (count(array_filter($form_state['values']['math_captcha_enabled_challenges'])) < 1) {
form_set_error('math_captcha_enabled_challenges', t('You should select at least one type of math challenges.'));
}
- // check argmax's
+ // Check argmax's
$argmaxs = array('math_captcha_addition_argmax', 'math_captcha_subtraction_argmax', 'math_captcha_multiplication_argmax');
foreach ($argmaxs as $argmax) {
- if (!is_numeric($form_state['values'][$argmax])) {
+ if (!ctype_digit($form_state['values'][$argmax])) {
form_set_error($argmax, t('Maximum value should be an integer.'));
}
else {
diff --git a/math_captcha/math_captcha.challenge.inc b/math_captcha/math_captcha.challenge.inc
index 43bc352..50d19f4 100644
--- a/math_captcha/math_captcha.challenge.inc
+++ b/math_captcha/math_captcha.challenge.inc
@@ -1,7 +1,12 @@
<?php
/**
- * function for addition challenges
+ * @file
+ * Provides challenge functions for MATH CAPTCHA administration.
+ */
+
+/**
+ * Function for addition challenges
*/
function _math_captcha_addition_challenge() {
$argmax = intval(variable_get('math_captcha_addition_argmax', 10));
@@ -19,7 +24,7 @@ function _math_captcha_addition_challenge() {
}
/**
- * function for subtraction challenges
+ * Function for subtraction challenges
*/
function _math_captcha_subtraction_challenge() {
$argmax = intval(variable_get('math_captcha_subtraction_argmax', 10));
@@ -37,7 +42,7 @@ function _math_captcha_subtraction_challenge() {
}
/**
- * function for multiplication challenges
+ * Function for multiplication challenges
*/
function _math_captcha_multiplication_challenge() {
$argmax = intval(variable_get('math_captcha_multiplication_argmax', 5));
@@ -53,9 +58,9 @@ function _math_captcha_multiplication_challenge() {
}
/**
- * helper function to build a math CAPTCHA form item
+ * Helper function to build a math CAPTCHA form item
*/
-function _math_captcha_build_captcha($x, $y, $operator, $result, $maxlength=3) {
+function _math_captcha_build_captcha($x, $y, $operator, $result, $maxlength = 3) {
$form_item = array();
$form_item['form']['captcha_response'] = array(
'#type' => 'textfield',
@@ -68,16 +73,16 @@ function _math_captcha_build_captcha($x, $y, $operator, $result, $maxlength=3) {
switch (mt_rand(0, 2)) {
case 0: // question like "x + y = ?"
$form_item['solution'] = "$result";
- $form_item['form']['captcha_response']['#field_prefix'] = _math_captcha_repr($x, TRUE) .' '. _math_captcha_repr_op($operator) .' '. _math_captcha_repr($y, TRUE) .' '. _math_captcha_repr_op('=');
+ $form_item['form']['captcha_response']['#field_prefix'] = _math_captcha_repr($x, TRUE) . ' ' . _math_captcha_repr_op($operator) . ' ' . _math_captcha_repr($y, TRUE) . ' ' . _math_captcha_repr_op('=');
break;
case 1: // question like "x + ? = z"
$form_item['solution'] = "$y";
- $form_item['form']['captcha_response']['#field_prefix'] = _math_captcha_repr($x, TRUE) .' '. _math_captcha_repr_op($operator) .' ';
- $form_item['form']['captcha_response']['#field_suffix'] = ' '. _math_captcha_repr_op('=') .' '. _math_captcha_repr($result);
+ $form_item['form']['captcha_response']['#field_prefix'] = _math_captcha_repr($x, TRUE) . ' ' . _math_captcha_repr_op($operator) . ' ';
+ $form_item['form']['captcha_response']['#field_suffix'] = ' ' . _math_captcha_repr_op('=') . ' ' . _math_captcha_repr($result);
break;
case 2: // question like "? + y = z"
$form_item['solution'] = "$x";
- $form_item['form']['captcha_response']['#field_suffix'] = ' '. _math_captcha_repr_op($operator) .' '. _math_captcha_repr($y, TRUE) .' '. _math_captcha_repr_op('=') .' '. _math_captcha_repr($result);
+ $form_item['form']['captcha_response']['#field_suffix'] = ' ' . _math_captcha_repr_op($operator) . ' ' . _math_captcha_repr($y, TRUE) . ' ' . _math_captcha_repr_op('=') . ' ' . _math_captcha_repr($result);
break;
}
return $form_item;
@@ -86,10 +91,10 @@ function _math_captcha_build_captcha($x, $y, $operator, $result, $maxlength=3) {
/**
* Helper function for transforming a number to a textual representation
*/
-function _math_captcha_repr($n, $add_paratheses_when_negative=FALSE) {
- // start with no textual representation
+function _math_captcha_repr($n, $add_paratheses_when_negative = FALSE) {
+ // Start with no textual representation
$t = "$n";
- // if enabled and available: do textual representation
+ // If enabled and available: do textual representation
if (variable_get('math_captcha_textual_numbers', TRUE)) {
$repr_map = array(
0 => t('zero'), 1 => t('one'), 2 => t('two'), 3 => t('three'),
@@ -110,10 +115,13 @@ function _math_captcha_repr($n, $add_paratheses_when_negative=FALSE) {
return $t;
}
+/**
+ * Helper function for transforming a operator to a textual representation
+ */
function _math_captcha_repr_op($op) {
- // start with no textual representation
+ // Start with no textual representation
$t = "$op";
- // if enabled and available: do textual representation
+ // If enabled and available: do textual representation
if (variable_get('math_captcha_textual_operators', FALSE)) {
$repr_map = array('+' => t('plus'), '-' => t('minus'), '*' => t('times'), '=' => t('equals'));
if (array_key_exists($op, $repr_map)) {
diff --git a/math_captcha/math_captcha.info b/math_captcha/math_captcha.info
index 4cdcd2d..8d43cff 100644
--- a/math_captcha/math_captcha.info
+++ b/math_captcha/math_captcha.info
@@ -1,5 +1,6 @@
-name = "Math CAPTCHA"
-description = "Provide math CAPTCHAs like 'two + three = ?' and '2 times ? = 6'"
+name = Math CAPTCHA
+description = Provide math CAPTCHAs like 'two + three = ?' and '2 times ? = 6'
package = "Spam control"
dependencies[] = captcha
-core = 6.x
+core = 7.x
+configure = admin/config/people/captcha/math_captcha \ No newline at end of file
diff --git a/math_captcha/math_captcha.install b/math_captcha/math_captcha.install
index 8905bb1..ab9a677 100644
--- a/math_captcha/math_captcha.install
+++ b/math_captcha/math_captcha.install
@@ -1,7 +1,13 @@
<?php
/**
- * On uninstall: remove module variables and clear variable cache
+ * @file
+ * Install, update and uninstall functions for the MATH CAPTCHA module.
+ */
+
+/**
+ * Implements hook_uninstall().
+ * On uninstall: remove module variables and clear variables cache
*/
function math_captcha_uninstall() {
db_query("DELETE FROM {variable} WHERE name LIKE 'math_captcha_%'");
diff --git a/math_captcha/math_captcha.module b/math_captcha/math_captcha.module
index 52a7cfc..698054d 100644
--- a/math_captcha/math_captcha.module
+++ b/math_captcha/math_captcha.module
@@ -1,15 +1,11 @@
<?php
/**
- * Implementation of a math CAPTCHA, for use with the CAPTCHA module
- */
-
-/**
- * Implementation of hook_menu().
+ * Implements hook_menu().
*/
function math_captcha_menu() {
$items = array();
- $items['admin/user/captcha/math_captcha'] = array(
+ $items['admin/config/people/captcha/math_captcha'] = array(
'title' => 'Math CAPTCHA',
'file' => 'math_captcha.admin.inc',
'page callback' => 'drupal_get_form',
@@ -33,17 +29,18 @@ function _math_captcha_enabled_challenges() {
}
/**
- * Implementation of hook_captcha().
+ * Implements hook_captcha().
*/
-function math_captcha_captcha($op, $captcha_type='') {
+function math_captcha_captcha($op, $captcha_type = '') {
switch ($op) {
case 'list':
return array('Math CAPTCHA');
case 'generate':
require_once('math_captcha.challenge.inc');
if ($captcha_type == 'Math CAPTCHA') {
- // get the available challenges
- $challenges = array_filter(_math_captcha_enabled_challenges());
+ // Get the available challenges
+ $enabled_challenges = _math_captcha_enabled_challenges();
+ $challenges = array_filter($enabled_challenges);
$challenge = $challenges[array_rand($challenges)];
$form_item = call_user_func("_math_captcha_{$challenge}_challenge");
return $form_item;