You are here

function _phrase_captcha_word_question_alphabetical_misplaced in CAPTCHA Pack 5

Same name and namespace in other branches
  1. 8 text_captcha/modules/phrase_captcha/phrase_captcha.module \_phrase_captcha_word_question_alphabetical_misplaced()
  2. 6 text_captcha/phrase_captcha/phrase_captcha.inc \_phrase_captcha_word_question_alphabetical_misplaced()
  3. 7 text_captcha/phrase_captcha/phrase_captcha.module \_phrase_captcha_word_question_alphabetical_misplaced()

File

text_captcha/phrase_captcha/phrase_captcha.module, line 213

Code

function _phrase_captcha_word_question_alphabetical_misplaced($words) {

  // sort the words
  mt_rand(0, 1) ? sort($words) : rsort($words);

  // pick a word and its new destination
  // new destination has to be at least 2 places from the original place,
  // otherwise it could lead to something like swapping two neighbours,
  // in which case there is no unique answer.
  $from = $to = 0;
  while (abs($from - $to) < 2) {
    $from = array_rand($words, 1);
    $to = array_rand($words, 1);
  }

  // get the word
  $answer = $words[$from];

  // move the word from $from to $to
  unset($words[$from]);
  array_splice($words, $to, 0, $answer);

  // build the description
  $description = t('Which word does not follow the alphabetical order in the CAPTCHA phrase above?');
  return array(
    $words,
    $description,
    $answer,
  );
}