You are here

function riddler_settings in Captcha Riddler 7

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

Settings form definition.

1 string reference to 'riddler_settings'
riddler_menu in ./riddler.module
Implementation of hook_menu().

File

./riddler.module, line 57
Adds a question and answer type to the Captcha module.

Code

function riddler_settings($form, &$form_state) {
  $i = 0;

  // Load the questions.
  $riddles = riddler_get_riddles();
  $saved_riddles = count($riddles);
  $form = array();
  $form['riddler_weight'] = array(
    '#type' => 'select',
    '#title' => t('Weight'),
    '#default_value' => variable_get('riddler_weight', 0),
    '#options' => drupal_map_assoc(range(-10, 10)),
    '#description' => t('Weight of the Riddler form element'),
    '#required' => TRUE,
  );
  $form['riddler_groups'] = array(
    '#type' => 'fieldset',
    '#title' => t('Riddles'),
    '#prefix' => '<div id="riddler-groups">',
    '#suffix' => '</div>',
  );

  // If the add another button is clicked, add an empty array element.
  if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == 'add-riddle') {

    // Make sure there are enough unsaved riddle fieldsets.
    for ($x = 0; $x <= $form_state['values']['riddles'] - $saved_riddles; $x++) {
      $riddles[] = array(
        'question' => '',
        'answer' => '',
      );
    }
  }
  foreach ($riddles as $key => $riddle) {
    $i = $key + 1;
    $access = TRUE;

    // Is ajax rebuilding the form?
    if (isset($form_state['values'])) {

      // Is this question deleted?
      if (isset($form_state['values']['riddler_delete_' . $i]) && $form_state['values']['riddler_delete_' . $i]) {
        $access = FALSE;
      }
    }
    $collapse = isset($form_state['values']) ? isset($form_state['values']['riddler_question_' . $i]) && !empty($form_state['values']['riddler_question_' . $i]) : TRUE;
    $title = isset($form_state['values']) ? !empty($form_state['values']['riddler_question_' . $i]) ? t('Riddle') . ' ' . $i . ': ' . $form_state['values']['riddler_question_' . $i] : t('Riddle !i', array(
      '!i' => $i,
    )) : t('Riddle') . ' ' . $i . ': ' . $riddle['question'];
    $form['riddler_groups']['riddler_group_' . $i] = array(
      '#type' => 'fieldset',
      '#title' => $title,
      '#collapsible' => TRUE,
      '#collapsed' => $collapse,
      '#access' => $access,
    );
    $form['riddler_groups']['riddler_group_' . $i]['riddler_question_' . $i] = array(
      '#type' => 'textfield',
      '#title' => t('Question'),
      '#description' => t('A question that you require anonymous users to answer'),
      '#default_value' => isset($form_state['values']['riddler_question_' . $i]) ? $form_state['values']['riddler_question_' . $i] : $riddle['question'],
      '#required' => FALSE,
      '#access' => $access,
    );
    $form['riddler_groups']['riddler_group_' . $i]['riddler_answer_' . $i] = array(
      '#type' => 'textfield',
      '#title' => t('Answer'),
      '#default_value' => isset($form_state['values']['riddler_answer_' . $i]) ? $form_state['values']['riddler_answer_' . $i] : $riddle['answer'],
      '#description' => t('Answer to the above question. You may allow more than one correct answer by entering a comma or space-separated list. Answers are not case sensitive.  Answers must be only one word.'),
      '#access' => $access,
      '#required' => FALSE,
    );
    $form['riddler_groups']['riddler_group_' . $i]['riddler_delete_' . $i] = array(
      '#type' => 'checkbox',
      '#title' => t('Delete'),
      '#description' => t('Permanently delete this riddle/answer pair?'),
      '#required' => FALSE,
      '#default_value' => isset($form_state['values']['riddler_delete_' . $i]) ? $form_state['values']['riddler_delete_' . $i] : 0,
      '#ajax' => array(
        'callback' => 'riddler_ajax_add_riddle',
        'wrapper' => 'riddler-groups',
        'method' => 'replace',
        'effect' => 'fade',
        '#access' => $access,
      ),
    );
  }
  $form['riddler_groups']['riddler_add'] = array(
    '#type' => 'button',
    '#value' => t('Add another'),
    '#name' => 'add-riddle',
    '#ajax' => array(
      'callback' => 'riddler_ajax_add_riddle',
      'wrapper' => 'riddler-groups',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['riddles'] = array(
    '#type' => 'value',
    '#value' => $i,
  );
  $form['#validate'][] = 'riddler_settings_validate';
  $form['#submit'][] = 'riddler_settings_submit';
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save settings'),
  );
  return $form;
}