You are here

function riddler_captcha in Captcha Riddler 8

Same name and namespace in other branches
  1. 5 riddler.module \riddler_captcha()
  2. 6 riddler.module \riddler_captcha()
  3. 7 riddler.module \riddler_captcha()

Implements hook_captcha().

File

./riddler.module, line 19

Code

function riddler_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
  $riddles = \Drupal::config('riddler.settings')
    ->get('riddles');
  switch ($op) {
    case 'list':
      return [
        'Riddler',
      ];
      break;
    case 'generate':
      if ($captcha_type == 'Riddler') {

        // Generate a CAPTCHA code.
        $key = array_rand($riddles);
        $riddle = $riddles[$key];

        // Build the result to return.
        $result = [];
        $result['solution'] = $riddle['response'];
        $result['form']['captcha_riddle'] = [
          '#markup' => '<p><strong>' . $riddle['question'] . '</strong></p>',
        ];
        $result['form']['captcha_response'] = [
          '#type' => 'textfield',
          '#title' => t('Answer the question here:'),
          '#weight' => 0,
          '#required' => TRUE,
          '#size' => 15,
          '#attributes' => [
            'autocomplete' => 'off',
          ],
          '#cache' => [
            'max-age' => 0,
          ],
        ];

        // Use custom validation so multiple answers can be tested.
        $result['captcha_validate'] = 'riddler_captcha_validate';
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
        return $result;
      }
      break;
  }
}