You are here

choice.module in Quiz 6.6

The main file for choice.

Sponsored by: Norwegian Centre for Telemedicine Code: falcon

Multiplechoice question type for the Quiz module.

File

question_types/choice/choice.module
View source
<?php

/**
 * The main file for choice.
 *
 * Sponsored by: Norwegian Centre for Telemedicine
 * Code: falcon
 *
 * @file
 * Multiplechoice question type for the Quiz module.
 *
 */

/**
 * Implementation of hook_help().
 */
function choice_help($path, $args) {
  if ($path == 'admin/help#choice') {
    return t('This module provides a multiple choice question type for Quiz.');
  }
}

/**
 * Implementation of hook_quiz_question_info().
 */
function choice_quiz_question_info() {
  return array(
    'choice' => array(
      'name' => 'Choice question',
      'description' => 'This provides multiple choice questions for use by the Quiz module. Under development. Do not use on production servers.',
      'question provider' => 'ChoiceQuestion',
      'response provider' => 'ChoiceResponse',
      'module' => 'quiz_question',
    ),
  );
}
function choice_menu() {
  $items['choice/add_alternative_js'] = array(
    'page callback' => 'choice_add_alternative_js',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'create quiz question',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_autoload_info().
 */
function choice_autoload_info() {
  return array(
    'ChoiceQuestion' => array(
      'file' => 'choice.classes.inc',
    ),
    'ChoiceResponse' => array(
      'file' => 'choice.classes.inc',
    ),
  );
}
function choice_theme($existing, $type, $theme, $path) {
  return array(
    'choice_creation_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'path' => drupal_get_path('module', 'choice') . '/theme',
      'file' => 'choice.theme.inc',
    ),
    'choice_alternative_creation' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'path' => drupal_get_path('module', 'choice') . '/theme',
      'template' => 'choice-alternative-creation',
    ),
    'choice_alternative' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'path' => drupal_get_path('module', 'choice') . '/theme',
      'template' => 'choice-alternative',
    ),
  );
}
function choice_add_alternative_js() {
  include_once 'modules/node/node.pages.inc';
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];

  // Get the form from the cache.
  $form = form_get_cache($form_build_id, $form_state);
  $args = $form['#parameters'];
  $form_id = array_shift($args);

  // We will run some of the submit handlers so we need to disable redirecting.
  $form['#redirect'] = FALSE;

  // We need to process the form, prepare for that by setting a few internals
  // variables.
  $form['#post'] = $_POST;
  $form['#programmed'] = FALSE;
  $form_state['post'] = $_POST;

  // Build, validate and if possible, submit the form.
  _choice_skip_validation($form);
  drupal_process_form($form_id, $form, $form_state);

  /*if(form_get_errors()) {
      form_execute_handlers('submit', $form, $form_state);
    }*/

  // This call recreates the form relying solely on the form_state that the
  // drupal_process_form set up.
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  // Render the new output.
  $i = 0;
  while (isset($form['alternatives'][$i])) {
    $i++;
  }
  $i--;
  $new_choice = $form['alternatives'][$i];
  $new_choice['#collapsed'] = FALSE;
  $output = form_get_errors() ? '' : drupal_render($new_choice);
  $status = theme('status_messages');
  drupal_json(array(
    'status' => TRUE,
    'data' => $status . $output,
  ));
}
function choice_more_choices_submit($form, &$form_state) {

  // Set the form to rebuild and run submit handlers.
  node_form_submit_build_node($form, $form_state);
  $exists = 0;
  while (isset($form['alternatives'][$exists])) {
    $exists++;
  }

  // Make the changes we want to the form state.
  if ($form_state['values']['alternatives']['choice_add_alternative']) {
    $n = $_GET['q'] == 'choice/add_alternative_js' ? 1 : 3;
    $form_state['choice_count'] = $exists + $n;
  }
}

/**
 * Recursive helper function to set the validated property. (Taken from the skip validation module.)
 *
 * @param &$elements
 *   The elements that are currently being processed.
 */
function _choice_skip_validation(&$elements) {
  $elements['#validated'] = TRUE;
  foreach (element_children($elements) as $key) {
    _choice_skip_validation($elements[$key]);
  }
}

/**
 * Implementation of hook user.
 */
function choice_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'delete':
      $sql = 'DELETE FROM {quiz_choice_user_settings}
          WHERE uid = %d';
      db_query($sql, $account->uid);
      break;
  }
}

Functions

Namesort descending Description
choice_add_alternative_js
choice_autoload_info Implementation of hook_autoload_info().
choice_help Implementation of hook_help().
choice_menu
choice_more_choices_submit
choice_quiz_question_info Implementation of hook_quiz_question_info().
choice_theme
choice_user Implementation of hook user.
_choice_skip_validation Recursive helper function to set the validated property. (Taken from the skip validation module.)