You are here

short_answer.module in Quiz 6.4

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_perm
 */
function short_answer_perm() {
  return array(
    'use regex for short answer',
  );
}

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

  // Pass vid and rid to this path.
  $items['admin/quiz/reports/score-short-answer/%/%'] = array(
    'title' => 'Score short answer response',
    'description' => 'Score a response to a short answer question.',
    'page callback' => 'short_answer_edit_score',
    'page arguments' => array(
      4,
      5,
    ),
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      4,
      5,
    ),
    'access callback' => 'quiz_question_access_to_score',
    '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' => t('Short answer question'),
      'description' => t('Quiz questions that allow a user to enter a line of text.'),
      'question provider' => 'ShortAnswerQuestion',
      'response provider' => 'ShortAnswerResponse',
      'module' => 'quiz_question',
    ),
  );
}

/**
 * Implementation of the quiz_question hook_config.
 */
function short_answer_config() {
  return FALSE;

  // No config options available for the short answer question type
}

/**
 * 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_view_unscored' => array(
      'arguments' => array(
        'unscored' => array(),
      ),
      'path' => drupal_get_path('module', 'short_answer') . '/theme',
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_response_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'path' => drupal_get_path('module', 'short_answer') . '/theme',
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_user_answer' => array(
      'arguments' => array(
        'answer' => NULL,
        'correct' => NULL,
      ),
      'path' => drupal_get_path('module', 'short_answer') . '/theme',
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_answering_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'path' => drupal_get_path('module', 'short_answer') . '/theme',
      'template' => 'short-answer-answering-form',
    ),
  );
}

/**
 * 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 array $values
 *  quiz => Quiz node,
 *  nid => Node ID of question.
 *  vid => Version ID of question.
 *  rid => Result ID for the quiz results.
 *  score => The numeric score to assign the result.
 *  max_score => The max score for this question
 * @param $update_total
 *  Shall the total score be updated?
 *
 * @return int
 *  Number of scores adjusted. If a change was made, this should be 1.
 */
function short_answer_score_an_answer($values, $update_total = TRUE) {

  // When we set the score we make sure that the max score in the quiz the question belongs to is considered
  db_query("UPDATE {quiz_short_answer_user_answers}\n            SET score = %d * (\n              SELECT max_score\n              FROM {quiz_question_properties}\n              WHERE vid = %d\n            ) / %d, is_evaluated = 1, answer_feedback = '%s'\n            WHERE question_nid = %d\n            AND question_vid = %d\n            AND result_id = %d", $values['score'], $values['vid'], $values['max_score'], $values['answer_feedback'], $values['nid'], $values['vid'], $values['rid']);
  $changed = db_affected_rows();

  // Now the short answer user data has been updated. We also need to update the data in the quiz tables
  if ($changed > 0) {
    $max = db_result(db_query('SELECT max_score FROM {quiz_question_properties} WHERE vid = %d', $values['vid']));
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $values['score'] / $max > 0.5 ? 1 : 0;
      $points_awarded = $values['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, $values['vid'], $values['rid']);

    // Third, we update the main quiz results table
    if ($update_total) {
      quiz_update_total_score($values['quiz'], $values['rid']);
    }
  }
  return $changed;
}

/**
 * Validate the result report for short answer
 */
function short_answer_report_validate($values, $form_key) {

  // Check to make sure that entered score is not higher than max allowed score.
  if (!_quiz_is_int($values['score'], 0, (int) $values['max_score'])) {
    form_set_error($form_key . '][score', t('The score needs to be a number between 0 and @max', array(
      '@max' => (int) $values['max_score'],
    )));
  }
}

/**
 * Submit the result report for short answer
 */
function short_answer_report_submit($values) {
  short_answer_score_an_answer($values, FALSE);
}

/**
 * Get the answer for a question.
 *
 * This stores a score for a short answer question and marks that question as having been evaluated.
 * @param $question_nid
 *  Node ID of question.
 * @param $question_vid
 *  Version ID of question.
 * @param $result_id
 *  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 sa.answer_id, sa.answer, sa.is_evaluated, sa.score, sa.question_vid, sa.question_nid, sa.result_id, sa.answer_feedback, rel.max_score AS rel_max_score, qt.max_score AS term_max_score, qnra.tid\n    FROM {quiz_short_answer_user_answers} sa\n    JOIN {quiz_node_results} qnr ON (sa.result_id = qnr.result_id)\n    JOIN {quiz_node_results_answers} qnra ON (qnr.result_id = qnra.result_id AND sa.question_nid = qnra.question_nid AND sa.question_vid = qnra.question_vid)\n    LEFT OUTER JOIN {quiz_node_relationship} rel ON (qnr.vid = rel.parent_vid AND rel.child_vid = sa.question_vid)\n    LEFT OUTER JOIN {quiz_terms} qt ON qnr.nid = qt.nid AND qnr.vid = qt.vid AND qnra.tid = qt.tid\n    WHERE sa.question_nid = %d AND sa.question_vid = %d AND sa.result_id = %d";
  $results = db_query($sql, $question_nid, $question_vid, $result_id);
  if (!$results) {
    return FALSE;
  }
  $answer = db_fetch_object($results);
  if ($answer->rel_max_score === NULL) {
    $answer->rel_max_score = $answer->term_max_score;
  }
  return $answer;
}

Functions

Namesort descending Description
short_answer_autoload_info Implementation of hook_autoload_info().
short_answer_config Implementation of the quiz_question hook_config.
short_answer_get_answer Get 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_quiz_question_info Implementation of hook_quiz_question_info().
short_answer_report_submit Submit the result report for short answer
short_answer_report_validate Validate the result report for short answer
short_answer_score_an_answer Set a score for a short answer question.
short_answer_theme Implementation of hook_theme().