You are here

long_answer.module in Quiz 6.6

This module defines a long answer question type for quizzes.

Long answer questions make use of the quiz question framework (quiz_question.module). The functions in this file are largely used for grading long answer questions. Most of the real logic is in long_answer.classes.inc.

File

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

/**
 * This module defines a long answer question type for quizzes.
 *
 * Long answer questions make use of the quiz question framework (quiz_question.module).
 * The functions in this file are largely used for grading long answer questions. Most
 * of the real logic is in long_answer.classes.inc.
 *
 * @file
 */

/**
 * Implementation of hook_help().
 */
function long_answer_help($path, $arg) {
  if ($path == 'admin/help#long_answer') {
    return '<p>' . t('This module provides long-answer (essay, multi-paragraph) questions to the quiz module.') . '</p><p>' . t('A long-answer question is designed to provide the quiz taker a lengthy area to expand on ideas.
      Common forms of long-answer questions include essays, single paragraph responses, hypothesis design problems,
      outlines and summaries, and lengthier math problems
      where the focus is on showing work rather than simply getting the correct answer.') . '</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function long_answer_menu() {

  /*
  $items['admin/quiz/long_answer'] = array(
    'title' => t('Long-answer configuration'),
    'description' => t('Configure long-answer questions for users.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('long_answer_admin_settings_form'),
    'access arguments' => array(QUIZ_PERM_ADMIN_CONFIG),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'long_answer.admin.inc',
  );
  */
  $items['admin/quiz/score-long-answer'] = array(
    'title' => t('Score long-answer questions'),
    'description' => t('Score the answers from quizzes that use long answer questions.'),
    'page callback' => 'long_answer_view_unscored',
    //'page arguments' => array('long_answer_admin_settings_form'),
    'access arguments' => array(
      'score long answer',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'long_answer.admin.inc',
  );

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

/**
 * Implementation of hook_quiz_question_info().
 */
function long_answer_quiz_question_info() {
  return array(
    'long_answer' => array(
      'name' => 'Long answer question',
      'description' => 'Quiz questions that allow a user to enter multiple paragraphs of text.',
      'question provider' => 'LongAnswerQuestion',
      'response provider' => 'LongAnswerResponse',
      'module' => 'quiz_question',
    ),
  );
}

/**
 * Implementation of hook_autoload_info().
 */
function long_answer_autoload_info() {
  return array(
    'LongAnswerQuestion' => array(
      'file' => 'long_answer.classes.inc',
    ),
    'LongAnswerResponse' => array(
      'file' => 'long_answer.classes.inc',
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function long_answer_theme() {
  return array(
    'long_answer_report' => array(
      'arguments' => array(
        'question' => NULL,
        'show_points' => NULL,
        'show_feedback' => NULL,
      ),
      'file' => 'long_answer.theme.inc',
    ),
    'long_answer_feedback' => array(
      'arguments' => array(
        'quiz' => NULL,
        'report' => NULL,
      ),
      'file' => 'long_answer.theme.inc',
    ),
    'long_answer_view_unscored' => array(
      'arguments' => array(
        'unscored' => array(),
      ),
      'file' => 'long_answer.admin.inc',
    ),
  );
}

/**
 * Set a score for a long answer question.
 *
 * This stores a score for a long 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 long_answer_score_an_answer($quiz, $nid, $vid, $rid, $score) {

  // Quiz scoring information is spread out across three tables:
  // 1. The module should retain its own scoring information in any case where scoring is non-trivial.
  // 2. The Quiz module (needlessly?) retains a limited amount of scoring information. This appears to be used only
  //    infrequently.
  // 3. The Quiz module retains an overall score for a quiz. This is the percentage score for the combination of all
  //    questions on the quiz.
  //
  // We update all three.
  // First, we update the long answer table
  db_query("UPDATE {quiz_long_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) {

    // Second, we update the main quiz answers table
    // 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_long_answer_node_properties} WHERE vid = %d', $vid));
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $score * 100 / $max > 50 ? 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 long 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 long_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_long_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 long_answer_perm() {
  return array(
    'score long answer',
  );
}

/**
 * Given a quiz, return a list of all of the unscored answers.
 *
 * @param $nid
 *  Node ID for the quiz to check.
 * @param $vid
 *  Version ID for the quiz to check.
 * @param $count
 *  Number of items to return (default: 50).
 * @param $offset
 *  Where in the results we should start (default: 0).
 *
 * @return
 *  Indexed array of result IDs that need to be scored.
 */
function long_answer_get_unscored_answers_by_question($nid, $vid, $count = 50, $offset = 0) {
  $results = db_query_range('SELECT result_id FROM {quiz_long_answer_user_answers} WHERE is_evaluated = 0 AND question_nid = %d AND question_vid = %d', $nid, $vid, $offset, $count);
  $unscored = array();
  foreach (db_fetch_object($results) as $row) {
    $unscored[] = $row->result_id;
  }
  return $unscored;
}

/**
 * Get all quiz scores that have not yet been evaluated.
 *
 * @param $count
 *  Number of items to return (default: 50).
 * @param $offset
 *  Where in the results we should start (default: 0).
 *
 * @return
 *  Array of objects describing unanswered questions. Each object will have result_id, question_nid, and question_vid.
 */
function long_answer_get_all_unscored_answers($count = 50, $offset = 0) {
  $sql = 'SELECT a.result_id, a.question_nid, a.question_vid, r.title, n.time_end, n.time_start, n.uid
    FROM {quiz_long_answer_user_answers} AS a
    INNER JOIN {node_revisions} AS r ON a.question_vid = r.vid
    INNER JOIN {quiz_node_results} AS n ON a.result_id = n.result_id
    WHERE is_evaluated = 0';
  $results = db_query_range($sql, $offset, $count);
  $unscored = array();
  if ($results) {
    while ($row = db_fetch_object($results)) {
      $unscored[] = $row;
    }
  }
  return $unscored;
}

/*
 * @function
 * To generate a list of short answer questions node
 * @return
 * array containing nid as key and title as value
 */
function long_answer_questions_list() {
  $questions = array();
  $type = 'long_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
long_answer_autoload_info Implementation of hook_autoload_info().
long_answer_get_all_unscored_answers Get all quiz scores that have not yet been evaluated.
long_answer_get_answer Set the answer for a question.
long_answer_get_unscored_answers_by_question Given a quiz, return a list of all of the unscored answers.
long_answer_help Implementation of hook_help().
long_answer_menu Implementation of hook_menu().
long_answer_perm Implementation of hook_perm().
long_answer_questions_list
long_answer_quiz_question_info Implementation of hook_quiz_question_info().
long_answer_score_an_answer Set a score for a long answer question.
long_answer_theme Implementation of hook_theme().