You are here

function quiz_form in Quiz 6.6

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_form()
  2. 5 quiz.module \quiz_form()
  3. 6.2 quiz.module \quiz_form()
  4. 6.3 quiz.module \quiz_form()
  5. 6.4 quiz.module \quiz_form()
  6. 6.5 quiz.module \quiz_form()
  7. 7.6 quiz.module \quiz_form()
  8. 7 quiz.module \quiz_form()
  9. 7.4 quiz.module \quiz_form()
  10. 7.5 quiz.module \quiz_form()

Implementation of hook_form().

This is an admin form used to build a new quiz. It is called as part of the node edit form.

File

./quiz.module, line 552
Quiz Module

Code

function quiz_form(&$node) {
  $form = array();
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $node->title,
    '#description' => t('The name of the @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )),
    '#required' => TRUE,
  );
  $form['body_field']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $node->body,
    '#description' => t('A description of what the @quiz entails', array(
      '@quiz' => QUIZ_NAME,
    )),
    '#required' => FALSE,
  );
  $form['body_field']['format'] = filter_form($node->format);
  $form['shuffle'] = array(
    '#type' => 'checkbox',
    '#title' => t('Shuffle questions'),
    '#default_value' => isset($node->shuffle) ? $node->shuffle : 1,
    '#description' => t('Whether to shuffle/randomize the questions on the @quiz', array(
      '@quiz' => QUIZ_NAME,
    )),
  );

  /*
   * Added the action as a dropdown for selection with specific quizzes
   * This allows you to choose a defined action from the actions module for use when
   * a user completes the quiz.
   */
  $form['aid'] = array(
    '#title' => t('Assign Action'),
    '#description' => t('Select an action to be preformed after a user has completed this @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )),
    '#type' => 'select',
    /*
     * An idea here would be to add a system conf variable into the quiz_action_options() function that
     * could filter the type of actions you could display on your quizzes.  For Example: you create
     * a custom module that defines some actions that you only want a user to choose when creating
     * a quiz and selecting an action from the dropdown.  You setup your actions with type 'quiz' and
     * then add in that variable into the function and it will automatically filter and show only
     * those specific actions.  @note:  In doing this you loose your default "Choose an Action"
     * option.  Review actions and the quiz_action_options() function for further explaination.
     */
    '#options' => quiz_action_options(variable_get('quiz_action_type', 'all')),
    '#default_value' => MD5($node->aid),
  );
  $form['backwards_navigation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Backwards navigation'),
    '#default_value' => $node->backwards_navigation,
    '#description' => t('Whether to allow user to go back and revisit their answers'),
  );
  $form['feedback_time'] = array(
    '#title' => t('Feedback Time'),
    '#type' => 'radios',
    '#default_value' => isset($node->feedback_time) ? $node->feedback_time : QUIZ_FEEDBACK_END,
    '#options' => _quiz_get_feedback_options(),
    '#description' => t('Indicates at what point feedback for each question will be given to the user'),
  );

  // Set up the availability options.
  $form['quiz_availability'] = array(
    '#type' => 'fieldset',
    '#title' => t('Availability options'),
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
  );
  $form['quiz_availability']['quiz_always'] = array(
    '#type' => 'checkbox',
    '#title' => t('Always Available'),
    '#default_value' => $node->quiz_always,
    '#description' => t('Click this option to ignore the open and close dates.'),
  );
  $form['quiz_availability']['quiz_open'] = array(
    '#type' => 'date',
    '#title' => t('Open Date'),
    '#default_value' => _quiz_form_prepare_date($node->quiz_open),
    '#description' => t('The date this @quiz will become available.', array(
      '@quiz' => QUIZ_NAME,
    )),
  );
  $form['quiz_availability']['quiz_close'] = array(
    '#type' => 'date',
    '#title' => t('Close Date'),
    '#default_value' => _quiz_form_prepare_date($node->quiz_close, variable_get('quiz_default_close', 30)),
    '#description' => t('The date this @quiz will cease to be available.', array(
      '@quiz' => QUIZ_NAME,
    )),
  );
  $options = array(
    t('Unlimited'),
  );
  for ($i = 1; $i < 10; $i++) {
    $options[$i] = $i;
  }
  $form['takes'] = array(
    '#type' => 'select',
    '#title' => t('Number of takes'),
    '#default_value' => $node->takes,
    '#options' => $options,
    '#description' => t('The number of times a user is allowed to take the @quiz', array(
      '@quiz' => QUIZ_NAME,
    )),
  );
  $form['addons'] = array(
    '#type' => 'fieldset',
    '#title' => t('Quiz Addons Properties'),
    '#description' => t('Configure Quiz  !url and their Properties', array(
      '!url' => l(t('Addons'), 'admin/quiz/settings'),
    )),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  if (function_exists('jquery_countdown_add') && variable_get('quiz_has_timer', 0)) {
    $form['addons']['time_limit'] = array(
      '#type' => 'textfield',
      '#title' => t(' Time Limit'),
      '#default_value' => isset($node->time_limit) ? $node->time_limit : 0,
      '#description' => t('Set the maximum allowed time in seconds for this @quiz. Use 0 for no limit.', array(
        '@quiz' => QUIZ_NAME,
      )),
    );
  }
  else {
    $form['addons']['time_limit'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
  }
  if (module_exists('userpoints') && variable_get('quiz_has_userpoints', 0)) {
    $form['addons']['has_userpoints'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($node->has_userpoints) ? $node->has_userpoints : 1,
      '#title' => t('Enable UserPoints Module Integration'),
      '#description' => t('If checked, marks scored in this @quiz will be credited to userpoints. For each correct answer 1 point will be added to user\'s point.', array(
        '@quiz' => QUIZ_NAME,
      )),
    );
  }

  // Quiz summary options.
  $form['summaryoptions'] = array(
    '#type' => 'fieldset',
    '#title' => t('@quiz Summary Options', array(
      '@quiz' => QUIZ_NAME,
    )),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // If pass/fail option is checked, present the form elements.
  if (variable_get('quiz_use_passfail', 1)) {

    // New nodes get the default.
    if (empty($node->nid)) {
      $node->pass_rate = variable_get('quiz_default_pass_rate', 75);
    }
    $form['summaryoptions']['pass_rate'] = array(
      '#type' => 'textfield',
      '#title' => t('Pass rate for @quiz (%)', array(
        '@quiz' => QUIZ_NAME,
      )),
      '#default_value' => $node->pass_rate,
      '#description' => t('Pass rate for the @quiz as a percentage score. (For personality quiz enter 0, and use result options.)', array(
        '@quiz' => QUIZ_NAME,
      )),
      '#required' => FALSE,
    );
    $form['summaryoptions']['summary_pass'] = array(
      '#type' => 'textarea',
      '#title' => t('Summary text if passed'),
      '#default_value' => $node->summary_pass,
      '#cols' => 60,
      '#description' => t("Summary for when the user gets enough correct answers to pass the @quiz. Leave blank if you don't want to give different summary text if they passed or if you are not using the 'percent to pass' option above. If you don't use the 'Percentage needed to pass' field above, this text will not be used.", array(
        '@quiz' => QUIZ_NAME,
      )),
    );
  }
  else {
    $form['summaryoptions']['pass_rate'] = array(
      '#type' => 'hidden',
      '#value' => variable_get('quiz_default_pass_rate', 75),
      '#required' => FALSE,
    );
  }
  $form['summaryoptions']['summary_default'] = array(
    '#type' => 'textarea',
    '#title' => t('Default summary text'),
    '#default_value' => $node->summary_default,
    '#cols' => 60,
    '#description' => t("Default summary. Leave blank if you don't want to give a summary."),
  );
  $num_rand = isset($node->number_of_random_questions) ? $node->number_of_random_questions : 0;
  $form['number_of_random_questions'] = array(
    '#type' => 'value',
    '#value' => $num_rand,
  );
  $form['resultoptions'] = array(
    '#type' => 'fieldset',
    '#title' => t('!quiz Results', array(
      '!quiz' => QUIZ_NAME,
    )),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $options = !empty($node->resultoptions) ? $node->resultoptions : array();
  $num_options = max(3, !empty($options) ? count($options) : variable_get('quiz_max_result_options', 5));
  for ($i = 0; $i < $num_options; $i++) {
    $option = count($options) > 0 ? array_shift($options) : NULL;

    // grab each option in the array
    $form['resultoptions'][$i] = array(
      '#type' => 'fieldset',
      '#title' => t('Result Option ') . ($i + 1),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['resultoptions'][$i]['option_name'] = array(
      '#type' => 'textfield',
      '#title' => t('The name of the result'),
      '#description' => t('Not displayed on personality !quiz.', array(
        '!quiz' => QUIZ_NAME,
      )),
      '#default_value' => $option['option_name'],
      '#maxlength' => 40,
      '#size' => 40,
    );
    $form['resultoptions'][$i]['option_start'] = array(
      '#type' => 'textfield',
      '#title' => t('Percentage Start Range'),
      '#description' => t('Show this result for scored quizzes in this range (0-100). Leave blank for personality quizzes.'),
      '#default_value' => $option['option_start'],
      '#size' => 5,
    );
    $form['resultoptions'][$i]['option_end'] = array(
      '#type' => 'textfield',
      '#title' => t('Percentage End Range'),
      '#description' => t('Show this result for scored quizzes in this range (0-100). Leave blank for personality quizzes.'),
      '#default_value' => $option['option_end'],
      '#size' => 5,
    );
    $form['resultoptions'][$i]['option_summary'] = array(
      '#type' => 'textarea',
      '#title' => t('Display text for the result'),
      '#default_value' => $option['option_summary'],
      '#description' => t('Result summary. This is the summary that is displayed when the user falls in this result set determined by his/her responses.'),
    );
    if ($option['option_id']) {
      $form['resultoptions'][$i]['option_id'] = array(
        '#type' => 'hidden',
        '#value' => $option['option_id'],
      );
    }
  }
  return $form;
}