You are here

quiz_question.module in Quiz 6.4

Quiz Question module. This module provides the basic facilities for adding quiz question types to a quiz.

File

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

/**
 * Quiz Question module.
 * This module provides the basic facilities for adding quiz question types to a quiz.
 * @file
 */

/*
 * The system remembers what quizzes a user has been involved in lately. This constant determines
 * how many quizzes the system will remember for each user
 */
define('QUIZ_QUESTION_NUM_LATEST', 10);

/**
 * Implementation of hook_help().
 */
function quiz_question_help($path, $args) {
  if ($path == 'admin/help#quiz_quesion') {
    return t('Support for Quiz question types.');
  }
}

/**
 * Implementation of hook_autoload_info().
 */
function quiz_question_autoload_info() {
  return array(
    // Base interfaces and classes:
    'QuizQuestion' => array(
      'file' => 'quiz_question.core.inc',
    ),
    'AbstractQuizQuestion' => array(
      'file' => 'quiz_question.core.inc',
    ),
    'QuizQuestionResponse' => array(
      'file' => 'quiz_question.core.inc',
    ),
    'AbstractQuizQuestionResponse' => array(
      'file' => 'quiz_question.core.inc',
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function quiz_question_menu() {
  $items = array();
  $items['quiz_question/%/%/revision_actions'] = array(
    'title' => 'Revision actions',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'quiz_question_revision_actions',
      1,
      2,
    ),
    'access arguments' => array(
      'manual quiz revisioning',
    ),
    'file' => 'quiz_question.pages.inc',
    'type' => MENU_NORMAL_ITEM,
  );

  // Menu items for admin view of each question type.
  $items['admin/quiz/settings/questions_settings'] = array(
    'title' => 'Question configuration',
    'description' => 'Configure the question types.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'quiz_question_config',
    ),
    'access arguments' => array(
      'administer quiz configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function quiz_question_theme() {
  return array(
    'quiz_question_creation_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'quiz_question.theme.inc',
    ),
    'quiz_question_navigation_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'quiz_question.theme.inc',
    ),
  );
}

/**
 * Implementation of hook_node_info().
 */
function quiz_question_node_info() {
  $types = _quiz_question_get_implementations();
  $info = array();
  $defaults = array(
    'module' => 'quiz_question',
    'has_body' => TRUE,
    'has_title' => TRUE,
    'body_label' => t('Question'),
  );
  foreach ($types as $type => $definition) {
    $node_info = array(
      'name' => $definition['name'],
      'description' => $definition['description'],
    );
    $info[$type] = $node_info + $defaults;
  }
  return $info;
}

/**
 * Implementation of hook_access().
 */
function quiz_question_access($op, $node, $account) {
  switch ($op) {
    case 'view':
      return user_access('view quiz question outside of a quiz');
    case 'create':
      return user_access('create quiz', $account);
    case 'update':
      if (user_access('edit any quiz', $account) || user_access('edit own quiz', $account) && $account->uid == $node->uid) {
        return TRUE;
      }
    case 'delete':
      if (user_access('delete any quiz', $account) || user_access('delete own quiz', $account) && $account->uid == $node->uid) {
        return TRUE;
      }
  }
}

/**
 * Figure out if a user has access to score a certain result
 *
 * @param $vid
 *  Question version id
 * @param $rid
 *  Result id
 * @return
 *  True if the user has access to score the result
 */
function quiz_question_access_to_score($vid, $rid) {
  global $user;
  $sql = 'SELECT *
          FROM {quiz_node_results_answers}
          WHERE result_id = %d AND question_vid = %d';
  if (!db_fetch_object(db_query($sql, $rid, $vid))) {
    return FALSE;
  }
  if (user_access('score any quiz')) {
    return TRUE;
  }
  if (user_access('score own quiz')) {
    $sql = 'SELECT r.uid
            FROM {node_revisions} r
            WHERE r.nid = (
              SELECT nid
              FROM {quiz_node_results}
              WHERE result_id = %d
            )';
    return db_result(db_query($sql, $rid)) == $user->uid;
  }
}

/**
 * Implementation of hook_form().
 */
function quiz_question_form(&$node, $form_state) {
  $question = _quiz_question_get_instance($node);
  $form = $question
    ->getNodeForm($form_state);
  return $form;
}

/**
 * Implementation of hook_validate().
 */
function quiz_question_validate($node, &$form) {

  // Check to make sure that there is a question.
  if (empty($node->body)) {
    form_set_error('body', t('Question text is empty.'));
  }
  _quiz_question_get_instance($node)
    ->validateNode($form);
}

/**
 * Get the form to show to the quiz taker.
 */
function quiz_question_answering_form($form_state, $node, $include_nid_in_id = FALSE) {
  $question = _quiz_question_get_instance($node);
  $form = $question
    ->getAnsweringForm($form_state, isset($node->rid) ? $node->rid : NULL);
  $quiz = quiz_type_access_load(arg(1));
  $form['#attributes']['class'] = 'answering-form';
  $is_last = _quiz_is_last_question();
  $form['navigation']['#theme'] = 'quiz_question_navigation_form';
  if (!empty($quiz->backwards_navigation) && !empty($node->question_number)) {
    $form['navigation']['back'] = array(
      '#type' => 'submit',
      '#value' => t('Back'),
      '#attributes' => array(
        'class' => 'q-back-button',
      ),
    );
    if ($is_last) {
      drupal_set_message(t('This is the last question. If you would like to go back and check some of your answers, click "Back", otherwise click the "Finish" button.'), 'status', FALSE);
    }
  }

  // Add navigation at the bottom:
  $form['navigation']['submit'] = array(
    '#type' => 'submit',
    '#value' => $is_last ? t('Finish') : t('Next'),
  );
  if ($node->allow_skipping) {
    $form['navigation']['op'] = array(
      '#type' => 'submit',
      '#value' => $is_last ? t('Leave blank and finish') : t('Leave blank'),
      '#attributes' => array(
        'class' => 'q-skip-button',
      ),
    );
    if ($quiz->allow_jumping) {
      $form['jump_to_question'] = array(
        '#type' => 'hidden',
        '#default_value' => 0,
      );
    }
  }
  return $form;
}

/**
 * Form for teaser display
 *
 * @param $node
 *  The question node
 * @return
 *  Content array
 */
function _quiz_question_teaser_content($node) {
  $content['question'] = array(
    '#type' => 'markup',
    '#value' => check_markup($node->body, $node->format, FALSE),
  );
  $type = node_get_types('type', $node);
  $content['question_type'] = array(
    '#type' => 'markup',
    '#value' => '<div class="question_type_name">' . $type->name . '</div>',
    '#weight' => -1,
  );
  return $content;
}

/**
 * Implementation of Quiz's hook_evaluate_question().
 *
 * @param $question
 *  The question node
 * @param $result_id
 *  Result id
 * @param $answer
 *  The users answer to the question
 * @return
 *  Object with nid, vid, rid, score, is_correct flags set.
 */
function quiz_question_evaluate_question($question, $result_id, $answer = NULL) {
  if (empty($answer)) {

    // FIXME this use of POST array is hacky. We will try to use FAPI mor accurately in Quiz 5.x
    $answer = $_POST['tries'];
  }
  unset($_POST['tries']);
  $response = _quiz_question_response_get_instance($result_id, $question, $answer);

  // If a result_id is set, we are taking a quiz.
  if ($result_id && isset($answer)) {

    // We don't know whether or not the user has gone back a question. However,
    // we do know that deleting a question for this result set should be safe in
    // the case where the user has not gone back (since there will be no entries
    // to delete). So we always run the delete.
    $response
      ->delete();
    $response
      ->saveResult();
  }

  // Convert the response to a bare object.
  return $response
    ->toBareObject();
}

/**
 * Implementation of quiz hook_skip_question().
 */
function quiz_question_skip_question($question, $result_id) {
  unset($_POST['tries']);

  // Unset any answer that might have been set.
  // Delete any old answers for this question (for backwards nav).
  _quiz_question_response_get_instance($result_id, $question)
    ->delete();

  // This is the standard response:
  $response = new stdClass();
  $response->nid = $question->nid;
  $response->vid = $question->vid;
  $response->rid = $result_id;
  $response->is_skipped = TRUE;
  return $response;
}

/**
 * Implementation of hook_list_questions().
 */
function quiz_question_list_questions($count = 0, $offset = 0) {
  $sql = "SELECT n.nid, n.vid, r.body, r.format\n    FROM {node} AS n\n    INNER JOIN {node_revisions} AS r USING(vid)\n    WHERE n.type IN (%s) ORDER BY n.type, n.changed";
  $types = array();
  foreach (array_keys(_quiz_question_get_implementations()) as $key) {
    $types[] = "'" . $key . "'";
  }
  $type = implode(',', $types);
  if ($count == 0) {

    // Return all results
    $result = db_query(db_rewrite_sql($sql), $type);
  }
  else {

    // return only $count results
    $result = db_query_range(db_rewrite_sql($sql), $type, $offset, $count);
  }
  $questions = array();
  while ($question = db_fetch_object($result)) {
    $question->question = check_markup($question->body, $question->format, FALSE);
    $questions[] = $question;
  }
  return $questions;
}

/**
 * Imlementation of hook_get_report().
 *
 * @return
 *  Node containing all of the items from the question plus the user's answer.
 */
function quiz_question_get_report($nid, $vid, $rid) {
  $response_instance = _quiz_question_response_get_instance($rid, NULL, NULL, $nid, $vid);
  if (!$response_instance) {
    drupal_set_message(t('Unable to load question with nid %nid and vid %vid', array(
      '%nid' => $nid,
      '%vid' => $vid,
    )), 'error');
    return FALSE;
  }
  $result = $response_instance
    ->getReport();
  $response_instance->question->answers[$result['answer_id']] = $result;
  $response_instance->question->correct = $result['is_correct'];
  return $response_instance->question;
}
function quiz_question_has_been_answered($node) {
  $question_instance = _quiz_question_get_instance($node, true);
  return $question_instance
    ->hasBeenAnswered();
}

/**
 * Implementation of hook_quiz_question_score().
 */
function quiz_question_quiz_question_score($quiz, $question_nid, $question_vid = NULL, $rid = NULL) {
  if (!isset($quiz) && !isset($rid)) {
    return quiz_question_get_max_score($question_nid, $question_vid);
  }

  // We avoid using node_load to increase performance...
  $dummy_node = new stdClass();
  $dummy_node->nid = $question_nid;
  $dummy_node->vid = $question_vid;
  $question = _quiz_question_get_instance($dummy_node, TRUE);
  if (!$question) {
    return FALSE;
  }
  $score = new stdClass();
  $score->possible = $question
    ->getMaximumScore();
  $score->question_nid = $question->node->nid;
  $score->question_vid = $question->node->vid;
  if (isset($rid)) {
    $response = _quiz_question_response_get_instance($rid, $question->node);
    $score->attained = $score->possible > 0 ? $response
      ->getScore() : 0;
    $score->possible = $response
      ->getMaxScore();
    $score->is_evaluated = $response
      ->isEvaluated();
  }
  return $score;
}

/**
 * Implementation of hook_delete_result
 *
 * @param $rid
 *  Result id
 * @param $nid
 *  Question node id
 * @param $vid
 *  Question node version id
 */
function quiz_question_delete_result($rid, $nid, $vid) {
  $response = _quiz_question_response_get_instance($rid, NULL, NULL, $nid, $vid);
  if ($response) {
    $response
      ->delete();
  }
  else {
    drupal_set_message(t('Unable to delete result. A constructor could not be found for the question-type'), 'error');
  }
}

/**
 * Get the configuration form for all enabled question types.
 */
function quiz_question_config($context) {
  $q_types = _quiz_question_get_implementations();
  $form = array();

  // Go through all question types and merge their config forms
  foreach ($q_types as $type => $values) {
    $function = $type . '_config';
    if ($admin_form = $function()) {
      $form[$type] = $admin_form;
      $form[$type]['#type'] = 'fieldset';
      $form[$type]['#title'] = $values['name'];
      $form[$type]['#collapsible'] = TRUE;
      $form[$type]['#collapsed'] = TRUE;
      if (isset($admin_form['#validate'])) {
        $form['#validate'][] = $admin_form['#validate'];
      }
    }
  }
  return system_settings_form($form);
}

// NODE API

/**
 * Implementation of hook_nodeapi().
 */
function quiz_question_nodeapi(&$node, $op) {

  // If a question is deleted it should also be removed from quizzes it belongs to.
  if ($op == 'delete revision') {
    $q_types = _quiz_question_get_implementations();
    foreach ($q_types as $q_type => $info) {
      if ($node->type == $q_type) {
        _quiz_delete_question($node, TRUE);

        // true for only this version
      }
    }
  }
  elseif ($op == 'presave') {
    $q_types = _quiz_question_get_implementations();
    foreach ($q_types as $q_type => $info) {
      if ($node->type == $q_type) {
        if (drupal_strlen($node->title) == 0 || !user_access('edit question titles')) {
          if (drupal_strlen(strip_tags(check_markup($node->body, $node->format, FALSE))) > variable_get('quiz_autotitle_length', 50)) {
            $node->title = drupal_substr(strip_tags(check_markup($node->body, $node->format, FALSE)), 0, variable_get('quiz_autotitle_length', 50) - 3) . '...';
          }
          else {
            $node->title = strip_tags(check_markup($node->body, $node->format, FALSE));
          }
        }
      }
    }
  }
  elseif ($node->type == 'quiz') {
    switch ($op) {
      case 'insert':
      case 'update':
        quiz_question_refresh_latest_quizzes($node->nid);
        break;
      case 'delete':
        quiz_question_remove_latest_quizzes($node->nid);
        break;
    }
  }
}

/**
 * Implementation of hook_insert()
 */
function quiz_question_insert(stdClass $node) {
  _quiz_question_get_instance($node)
    ->save(TRUE);
  if (isset($node->quiz_nid) && $node->quiz_nid > 0) {
    quiz_question_refresh_latest_quizzes($node->quiz_nid);
  }
}

/**
 * Implementation of hook_view()
 */
function quiz_question_view($node, $teaser = FALSE, $page = FALSE) {
  if ($node->build_mode == NODE_BUILD_SEARCH_INDEX && !variable_get('quiz_index_questions', 1)) {
    $node->body = '';
    $node->content = array();
    $node->title = '';
    $node->taxonomy = array();
    return $node;
  }
  $content = '';
  if ($teaser) {
    $content = _quiz_question_teaser_content($node);
  }
  elseif (_quiz_is_taking_context()) {

    /*
     * @todo: I see no reason why this should be a part of view anymore.
     * In quiz 5 we should stop using hook_view to view the answering form
     */
    $form_markup = drupal_get_form('quiz_question_answering_form', $node);
  }
  else {

    // normal node view
    $question = _quiz_question_get_instance($node, TRUE);
    $content = $question
      ->getNodeView();
  }

  // put it into the node->content
  if (!empty($content)) {
    if (isset($node->content)) {
      $node->content += $content;
    }
    else {
      $node->content = $content;
    }
  }
  if (!empty($form_markup)) {
    $node->content['body']['#value'] = $form_markup;
  }
  return $node;
}

/**
 * Implementation of hook_update().
 */
function quiz_question_update($node) {
  _quiz_question_get_instance($node)
    ->save();
}

/**
 * Implementation of hook_delete().
 */
function quiz_question_delete(&$node) {
  _quiz_delete_question($node, FALSE);
}

/**
 * Delete the question node from the db, and mark its identifiers in the quiz
 * linking table as "NEVER".  This is safer than deleting them
 * and allows for same tracing of what's happened if a question was deleted unintentionally.
 *
 * @param $node the question node
 * @param $only_this_version whether to delete only the specific revision of the question
 */
function _quiz_delete_question(&$node, $only_this_version) {

  // let each question class delete its own stuff
  _quiz_question_get_instance($node, TRUE)
    ->delete($only_this_version);

  // FIXME QuizQuestion class makes these relationships, so it should handle their 'deletion' too
  // FIXME alternately, move the relationship handling out of QuizQuestion class
  // @todo reconsider this QUESTION_NEVER status, since the node is actually gone
  // then remove it from {quiz_node_relationship} linking table
  $base_sql = "UPDATE {quiz_node_relationship} SET question_status = " . QUESTION_NEVER;
  $select_sql = "SELECT parent_vid FROM {quiz_node_relationship}";
  if ($only_this_version) {
    $filter_sql = ' WHERE child_nid = %d AND child_vid = %d';
  }
  else {
    $filter_sql = ' WHERE child_nid = %d';
  }
  $res = db_query($select_sql . $filter_sql, $node->nid, $node->vid);
  db_query($base_sql . $filter_sql, $node->nid, $node->vid);
  $quizzes_to_update = array();
  while ($quizzes_to_update[] = db_result($res)) {
  }
  quiz_update_max_score_properties($quizzes_to_update);
}

/**
 * Implementation of hook_load().
 */
function quiz_question_load($node) {
  return _quiz_question_get_instance($node, TRUE)
    ->getNodeProperties();
}

// END NODE API

/**
 * Get an instance of a quiz question.
 *
 * Get information about the class and use it to construct a new
 * object of the appropriate type.
 *
 * @param $node
 *  Question node
 * @param $use_cached
 *  Can we use a cached version of the node?
 * @return
 *  The appropriate QuizQuestion extension instance
 */
function _quiz_question_get_instance(&$node, $use_cached = FALSE) {

  // We use static caching to improve performance
  static $question_instances = array();
  $using_dummy_node = FALSE;
  if (is_object($node)) {
    $vid = isset($node->vid) ? $node->vid : 0;
    if ($use_cached && isset($question_instances[$vid])) {

      // We just return a cached instance of the QuizQuestion
      return $question_instances[$vid];
    }

    // If $node don't have a type it is a dummy node
    if (!isset($node->type)) {

      // To substanitally improve performance(especially on the result page) we avoid node_load()...
      $sql = 'SELECT n.type, r.nid, r.vid, r.title, r.body, p.max_score, r.format
              FROM {node_revisions} r
              JOIN {node} n
              ON r.nid = n.nid
              JOIN {quiz_question_properties} p
              ON r.vid = p.vid
              WHERE r.vid = %d';
      $res = db_query($sql, $node->vid);
      $node = db_fetch_object($res);
      $using_dummy_node = TRUE;
    }
    $name = $node->type;
  }
  elseif (is_array($node)) {
    $name = $node['type'];
    $vid = $node['vid'];
    if ($use_cached && isset($question_instances[$vid])) {

      // We return a cached instance of the appropriate QuizQuestion
      return $question_instances[$vid];
    }
  }

  // No cached instance of QuizQuestion has been returned. We construct a new instance
  $info = _quiz_question_get_implementations();
  $constructor = $info[$name]['question provider'];
  if (empty($constructor)) {
    return FALSE;
  }

  // We create a new instance of QuizQuestion
  $to_return = new $constructor($node);
  if (!$to_return instanceof QuizQuestion) {

    // Make sure the constructor is creating an extension of QuizQuestion
    drupal_set_message(t('The question-type %name isn\'t a QuizQuestion. It needs to extend the QuizQuestion class.', array(
      '%name' => $name,
    )), 'error', FALSE);
  }

  // If we're using a dummy node we have to run getNodeProperties, and populate the node with those properties
  if ($using_dummy_node) {
    $props = $to_return
      ->getNodeProperties();
    foreach ($props as $key => $value) {
      $to_return->node->{$key} = $value;
    }
  }

  // Cache the node
  $question_instances[$vid] = $to_return;
  return $to_return;
}

/**
 * Get an instance of a quiz question responce.
 *
 * Get information about the class and use it to construct a new
 * object of the appropriate type.
 *
 * @param $rid
 *  Result id
 * @param $question
 *  The question node(not a QuizQuestion instance)
 * @param $answer
 *  Resonce to the answering form.
 * @param $nid
 *  Question node id
 * @param $vid
 *  Question node version id
 * @return
 *  The appropriate QuizQuestionResponce extension instance
 */
function _quiz_question_response_get_instance($rid, $question, $answer = NULL, $nid = NULL, $vid = NULL) {

  // We cache responses to improve performance
  static $quiz_responses = array();
  if (is_object($question) && isset($quiz_responses[$rid][$question->vid])) {

    // We refresh the question node in case it has been changed since we cached the response
    $quiz_responses[$rid][$question->vid]
      ->refreshQuestionNode($question);
    if ($quiz_responses[$rid][$question->vid]->is_skipped !== FALSE) {

      // Return a cached instance of the response
      return $quiz_responses[$rid][$question->vid];
    }
  }
  elseif (isset($quiz_responses[$rid][$vid])) {
    if ($quiz_responses[$rid][$vid]->is_skipped !== FALSE) {

      // Return a cached instance of the response
      return $quiz_responses[$rid][$vid];
    }
  }
  if (!isset($quiz_responses[$rid])) {

    // Prepare to cache responses for this result id
    $quiz_responses[$rid] = array();
  }

  // If the question node isn't set we fetch it from the QuizQuestion instance this responce belongs to
  if (!isset($question)) {
    $dummy_node = new stdClass();
    $dummy_node->nid = $nid;
    $dummy_node->vid = $vid;
    $question = _quiz_question_get_instance($dummy_node, TRUE)->node;
  }
  if (!$question) {
    return FALSE;
  }
  $info = _quiz_question_get_implementations();
  $constructor = $info[$question->type]['response provider'];
  $to_return = new $constructor($rid, $question, $answer);

  // All responce classes must extend QuizQuestionResponse
  if (!$to_return instanceof QuizQuestionResponse) {
    drupal_set_message(t('The question-response isn\'t a QuizQuestionResponse. It needs to extend the QuizQuestionResponse interface, or extend the abstractQuizQuestionResponse class.'), 'error', FALSE);
  }

  // Cache the responce instance
  $quiz_responses[$rid][$question->vid] = $to_return;
  return $to_return;
}

/**
 * Get the information about various implementations of quiz questions.
 *
 * @param $reset
 *  If this is true, the cache will be reset.
 * @return
 *  An array of information about quiz question implementations.
 * @see quiz_question_quiz_question_info() for an example of a quiz question info hook.
 */
function _quiz_question_get_implementations($name = NULL, $reset = FALSE) {
  static $info = array();
  if (empty($info) || $reset) {
    $qtypes = module_invoke_all('quiz_question_info');
    foreach ($qtypes as $type => $definition) {

      // We only want the ones with classes.
      if (!empty($definition['question provider'])) {

        // Cache the info
        $info[$type] = $definition;
      }
    }
  }
  return $info;
}

/**
 * Refreshes the quiz_question_latest_quizzes table when a user has modified a new quiz.
 *
 * The latest quizzes table is used to know what quizzes the user has been using lately.
 *
 * @param $nid
 *   nid of the last quiz the current user modified
 */
function quiz_question_refresh_latest_quizzes($nid) {
  global $user;

  // Delete entry if it allready exists
  $sql = 'DELETE FROM {quiz_question_latest_quizzes}
          WHERE uid = %d AND quiz_nid = %d';
  db_query($sql, $user->uid, $nid);

  // Inserts as new entry to get new id. Latest quizzes are ordered by id(descending)
  $sql = 'INSERT INTO {quiz_question_latest_quizzes}
          (uid, quiz_nid)
          VALUES (%d, %d)';
  db_query($sql, $user->uid, $nid);

  // If we have to many entries for current user, delete the oldest entries...
  $sql = 'SELECT COUNT(*)
          FROM {quiz_question_latest_quizzes}
          WHERE uid = %d';
  $count = db_result(db_query($sql, $user->uid));
  $n_to_delete = (int) $count - QUIZ_QUESTION_NUM_LATEST;
  if ($n_to_delete > 0) {
    $sql = 'DELETE FROM {quiz_question_latest_quizzes}
            WHERE uid = %d
            ORDER BY id LIMIT %d';
    db_query($sql, $user->uid, $n_to_delete);
  }
}

/**
 * Removes a quiz from the quiz_question_latest_quizzes table.
 *
 * @param $nid
 *   the nid of a quiz that shall be removed
 */
function quiz_question_remove_latest_quizzes($nid) {
  $sql = 'DELETE FROM {quiz_question_latest_quizzes}
          WHERE quiz_nid = %d';
  db_query($sql, $nid);
}

/**
 * Get the max score for a question
 *
 * @param $nid
 *  Question node id
 * @param $vid
 *  Question node version id
 * @return
 *  Max score(int)
 */
function quiz_question_get_max_score($nid, $vid) {
  $sql = 'SELECT max_score
          FROM {quiz_question_properties}
          WHERE nid = %d AND vid = %d';
  return db_result(db_query($sql, $nid, $vid));
}

/**
 * Implementation of hook_content_extra_fields(cck)
 */
function quiz_question_content_extra_fields($type_name) {
  $extra = array();
  $question_types = array_keys(_quiz_question_get_implementations());
  if (in_array($type_name, $question_types)) {
    $extra['add_directly'] = array(
      'label' => t('Add directly'),
      'description' => t('Fieldset for adding a question directly into quizzes'),
      'weight' => -3,
    );
  }
  return $extra;
}

/**
 * Implementation of hook_content_extra_fields_alter(cck)
 */
function quiz_question_content_extra_fields_alter(&$extras, $type_name) {
  $question_types = array_keys(_quiz_question_get_implementations());
  if (in_array($type_name, $question_types)) {
    $extras['body_field']['weight'] = -15;
  }
}

/**
 * Returns a result report for a question response.
 *
 * The retaurned value is a form array because in some contexts the scores in the form
 * is editable
 *
 * @param $question
 *  The question node
 * @param $showpoints
 * @param $showfeedback
 * @param $allow_scoring
 * @return
 *  FAPI form array
 */
function quiz_question_report_form($question, $showpoints, $showfeedback, $allow_scoring = FALSE) {
  $answer = $question->answers[0];
  $response_instance = _quiz_question_response_get_instance($answer['result_id'], $question, $answer);

  // If need to specify the score weight if it isn't already specified.
  if (!isset($response_instance->question->score_weight)) {
    $sql = 'SELECT qnr.max_score
            FROM {quiz_node_relationship} qnr
            WHERE qnr.child_vid = %d
            AND qnr.parent_vid = (
              SELECT vid
              FROM {quiz_node_results}
              WHERE result_id = %d
            )';
    $qnr_max_score = db_result(db_query($sql, $question->vid, $answer['result_id']));
    if ($qnr_max_score === FALSE) {
      $qnr_max_score = db_result(db_query('SELECT qt.max_score
         FROM {quiz_node_results} qnr
         JOIN {quiz_node_results_answers} qnra ON (qnr.result_id = qnra.result_id)
         JOIN {quiz_terms} qt ON (qt.vid = qnr.vid AND qt.tid = qnra.tid)
         WHERE qnr.result_id = %d AND qnra.question_nid = %d AND qnra.question_vid = %d', $answer['result_id'], $question->nid, $question->vid));
    }
    if ($qnr_max_score == 0) {
      $weight = 0;
    }
    else {
      $weight = $qnr_max_score / $response_instance->question->max_score;
    }
    $response_instance->question->score_weight = $weight;
  }
  return $response_instance
    ->getReportForm($showpoints, $showfeedback, $allow_scoring);
}

Functions

Namesort descending Description
quiz_question_access Implementation of hook_access().
quiz_question_access_to_score Figure out if a user has access to score a certain result
quiz_question_answering_form Get the form to show to the quiz taker.
quiz_question_autoload_info Implementation of hook_autoload_info().
quiz_question_config Get the configuration form for all enabled question types.
quiz_question_content_extra_fields Implementation of hook_content_extra_fields(cck)
quiz_question_content_extra_fields_alter Implementation of hook_content_extra_fields_alter(cck)
quiz_question_delete Implementation of hook_delete().
quiz_question_delete_result Implementation of hook_delete_result
quiz_question_evaluate_question Implementation of Quiz's hook_evaluate_question().
quiz_question_form Implementation of hook_form().
quiz_question_get_max_score Get the max score for a question
quiz_question_get_report Imlementation of hook_get_report().
quiz_question_has_been_answered
quiz_question_help Implementation of hook_help().
quiz_question_insert Implementation of hook_insert()
quiz_question_list_questions Implementation of hook_list_questions().
quiz_question_load Implementation of hook_load().
quiz_question_menu Implementation of hook_menu().
quiz_question_nodeapi Implementation of hook_nodeapi().
quiz_question_node_info Implementation of hook_node_info().
quiz_question_quiz_question_score Implementation of hook_quiz_question_score().
quiz_question_refresh_latest_quizzes Refreshes the quiz_question_latest_quizzes table when a user has modified a new quiz.
quiz_question_remove_latest_quizzes Removes a quiz from the quiz_question_latest_quizzes table.
quiz_question_report_form Returns a result report for a question response.
quiz_question_skip_question Implementation of quiz hook_skip_question().
quiz_question_theme Implementation of hook_theme().
quiz_question_update Implementation of hook_update().
quiz_question_validate Implementation of hook_validate().
quiz_question_view Implementation of hook_view()
_quiz_delete_question Delete the question node from the db, and mark its identifiers in the quiz linking table as "NEVER". This is safer than deleting them and allows for same tracing of what's happened if a question was deleted unintentionally.
_quiz_question_get_implementations Get the information about various implementations of quiz questions.
_quiz_question_get_instance Get an instance of a quiz question.
_quiz_question_response_get_instance Get an instance of a quiz question responce.
_quiz_question_teaser_content Form for teaser display

Constants