You are here

short_answer.module in Quiz 6.6

The main file for short_answer.

Short answer is structurally similar to long answer. However, the module mechanism makes it very difficult for these two modules (either one of which may be disabled) to effectively share code.

File

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

/**
 * The main file for short_answer.
 *
 * Short answer is structurally similar to long answer. However, the module
 * mechanism makes it very difficult for these two modules (either one of
 * which may be disabled) to effectively share code.
 * @file
 */

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

/**
 * Implementation of hook_menu().
 */
function short_answer_menu() {
  $items['admin/quiz/score-short-answer'] = array(
    'title' => t('Score short-answer questions'),
    'description' => t('Score the answers from quizzes that use short answer questions.'),
    'page callback' => 'short_answer_view_unscored',
    //'page arguments' => array('short_answer_admin_settings_form'),
    'access arguments' => array(
      'score short answer',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'short_answer.admin.inc',
  );

  // Pass vid and rid to this path.
  $items['admin/quiz/score-short-answer/%/%'] = array(
    'title' => t('Score short-answer response'),
    'description' => t('Score a response to a short-answer question.'),
    'page callback' => 'short_answer_edit_score',
    'page arguments' => array(
      3,
      4,
    ),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'score short answer',
    ),
    'file' => 'short_answer.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_quiz_question_info().
 */
function short_answer_quiz_question_info() {
  return array(
    'short_answer' => array(
      'name' => 'Short answer question',
      'description' => 'Quiz questions that allow a user to enter a line of text.',
      'question provider' => 'ShortAnswerQuestion',
      'response provider' => 'ShortAnswerResponse',
      'module' => 'quiz_question',
    ),
  );
}

/**
 * Implementation of hook_autoload_info().
 */
function short_answer_autoload_info() {
  return array(
    'ShortAnswerQuestion' => array(
      'file' => 'short_answer.classes.inc',
    ),
    'ShortAnswerResponse' => array(
      'file' => 'short_answer.classes.inc',
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function short_answer_theme() {
  return array(
    'short_answer_report' => array(
      'arguments' => array(
        'question' => NULL,
        'show_points' => NULL,
        'show_feedback' => NULL,
      ),
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_feedback' => array(
      'arguments' => array(
        'quiz' => NULL,
        'report' => NULL,
      ),
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_view_unscored' => array(
      'arguments' => array(
        'unscored' => array(),
      ),
      'file' => 'short_answer.admin.inc',
    ),
  );
}

/**
 * Set a score for a short answer question.
 *
 * This stores a score for a short answer question and marks that question as having been evaluated.
 * The function updates all of the necessary data sources so that the individual answer results should be
 * reflected in the total scoring table.
 *
 * @param $quiz
 *  Quiz node.
 * @param $nid
 *  Node ID of question.
 * @param $vid
 *  Version ID of question.
 * @param $rid
 *  Result ID for the quiz results.
 * @param $score
 *  The numeric score to assign the result.
 *
 * @return int
 *  Number of scores adjusted. If a change was made, this should be 1.
 */
function short_answer_score_an_answer($quiz, $nid, $vid, $rid, $score) {

  // See long_answer.module for details on how this works.
  db_query("UPDATE {quiz_short_answer_user_answers} SET score = %d, is_evaluated = 1 WHERE question_nid = %d AND question_vid = %d AND result_id = %d", $score, $nid, $vid, $rid);
  $changed = db_affected_rows();
  if ($changed > 0) {

    // What do we do about the quiz_node_results_answers table? It assumes strict
    // bivalence (is_correct). I guess we consider any essay with over 50% to be correct?
    $max = db_result(db_query('SELECT maximum_score FROM {quiz_short_answer_node_properties} WHERE vid = %d', $vid));
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $score / $max > 0.5 ? 1 : 0;
      $points_awarded = $score;
    }
    $sql = 'UPDATE {quiz_node_results_answers}
      SET points_awarded = %d, is_correct = %d
      WHERE question_vid = %d AND result_id = %d';
    db_query($sql, $points_awarded, $is_correct, $vid, $rid);

    // Third, we update the main quiz results table
    quiz_update_total_score($quiz, $rid);
  }
  return $changed;
}

/**
 * Set the answer for a question.
 *
 * This stores a score for a short answer question and marks that question as having been evaluated.
 * @param $nid
 *  Node ID of question.
 * @param $vid
 *  Version ID of question.
 * @param $rid
 *  Result ID for the quiz results.
 *
 * @return Assoc array
 *  An array if successful, or FALSE if no result could be found. The array contains the following properties:
 *  <code>
 *  answer_id; // The answer ID
 *  answer; // The full text of the answer
 *  is_evaluated; // 0 if the question has not been evaluated, 1 if it has
 *  score; // The score the evaluator gave the user; this should be 0 if is_evaluated is 0.
 *  question_vid
 *  question_nid
 *  result_id
 *  </code>
 */
function short_answer_get_answer($question_nid, $question_vid, $result_id) {
  $sql = "SELECT answer_id, answer, is_evaluated, score, question_vid, question_nid, result_id\n    FROM {quiz_short_answer_user_answers}\n    WHERE question_nid = %d AND question_vid = %d AND result_id = %d";
  $results = db_query($sql, $question_nid, $question_vid, $result_id);
  if (!$results) {
    return FALSE;
  }
  return db_fetch_array($results);
}

/**
 * Implementation of hook_perm().
 */
function short_answer_perm() {
  return array(
    'score short answer',
  );
}

/*
 * @function
 * To generate a list of short answer questions node
 * @return
 * array containing nid as key and title as value
 */
function short_answer_questions_list() {
  $questions = array();
  $type = 'short_answer';
  $sql = "SELECT nid, title FROM {node} WHERE type = '%s'";
  $results = db_query($sql, $type);
  while ($result = db_fetch_object($results)) {
    $questions[$result->nid] = check_plain($result->title);
  }
  return $questions;
}

Functions

Namesort descending Description
short_answer_autoload_info Implementation of hook_autoload_info().
short_answer_get_answer Set the answer for a question.
short_answer_help Implementation of hook_help().
short_answer_menu Implementation of hook_menu().
short_answer_perm Implementation of hook_perm().
short_answer_questions_list
short_answer_quiz_question_info Implementation of hook_quiz_question_info().
short_answer_score_an_answer Set a score for a short answer question.
short_answer_theme Implementation of hook_theme().