You are here

long_answer.module in Quiz 7

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
 */

/**
 * Implements 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>';
  }
}

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

  // Pass vid and rid to this path.
  $items['admin/quiz/reports/score-long-answer/%/%'] = array(
    'title' => 'Score long answer response',
    'description' => 'Score a response to a long answer question.',
    'page callback' => 'long_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' => 'long_answer.admin.inc',
  );
  return $items;
}

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

/**
 * Implements hook_config().
 */
function long_answer_config() {
  return FALSE;
}

/**
 * Implements hook_theme().
 */
function long_answer_theme() {
  return array(
    'long_answer_view_unscored' => array(
      'variables' => array(
        'unscored' => array(),
      ),
      'path' => drupal_get_path('module', 'long_answer') . '/theme',
      'file' => 'long_answer.theme.inc',
    ),
    'long_answer_response_form' => array(
      'render element' => 'form',
      'path' => drupal_get_path('module', 'long_answer') . '/theme',
      'file' => 'long_answer.theme.inc',
    ),
    'long_answer_answering_form' => array(
      'render element' => 'form',
      'path' => drupal_get_path('module', 'long_answer') . '/theme',
      'template' => 'long-answer-answering-form',
    ),
  );
}

/**
 * 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.
 * @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 long_answer_score_an_answer($quiz, $nid, $vid, $rid, $score, $update_total = TRUE) {

  // 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 moduleretains a limited amount of scoring information.
  // 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
  $question_max_score = db_query('SELECT max_score FROM {quiz_question_properties} WHERE nid = :nid AND vid = :vid', array(
    ':nid' => $nid,
    ':vid' => $vid,
  ))
    ->FetchField();
  $quiz_max_score = db_query('SELECT max_score FROM {quiz_node_relationship} WHERE parent_vid = :pvid AND child_vid = :cvid', array(
    ':pvid' => $quiz->vid,
    ':cvid' => $vid,
  ))
    ->fetchField();
  $changed = db_update('quiz_long_answer_user_answers')
    ->fields(array(
    'score' => $question_max_score / $quiz_max_score,
    'is_evaluated' => 1,
  ))
    ->condition('question_nid', $nid)
    ->condition('question_vid', $vid)
    ->condition('result_id', $rid)
    ->execute();
  $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_query('SELECT max_score FROM {quiz_question_properties} WHERE vid = :vid', array(
      ':vid' => $vid,
    ))
      ->fetchField();
    if ($max <= 0) {
      $is_correct = 0;
      $points_awarded = 0;
    }
    else {
      $is_correct = $score == $max ? 1 : 0;
      $points_awarded = $score;
    }
    db_update('quiz_node_results_answers')
      ->fields(array(
      'points_awarded' => $points_awarded,
      'is_correct' => $is_correct,
    ))
      ->condition('question_vid', $vid)
      ->condition('result_id', $rid)
      ->execute();

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

/**
 * Get 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) {
  $result = db_query('SELECT answer_id, answer, la.is_evaluated, la.score, question_vid, question_nid, la.result_id, rel.max_score AS rel_max_score FROM {quiz_long_answer_user_answers} la
    JOIN {quiz_node_results} qnr ON (la.result_id = qnr.result_id)
    JOIN {quiz_node_relationship} rel ON (qnr.vid = rel.parent_vid AND rel.child_vid = la.question_vid)
    WHERE question_nid = :qnid AND question_vid = :qvid AND la.result_id = :rid', array(
    ':qnid' => $question_nid,
    ':qvid' => $question_vid,
    ':rid' => $result_id,
  ))
    ->fetchAssoc();
  return $result ? $result : FALSE;
}

/**
 * Given a quiz, return a list of all 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('SELECT result_id FROM {quiz_long_answer_user_answers}
    WHERE is_evaluated = :is_evaluated AND question_nid = :question_nid AND question_vid = :question_vid', array(
    ':is_evaluated' => 0,
    ':question_nid' => $nid,
    ':question_vid' => $vid,
  ));
  $unscored = array();
  foreach ($results as $result) {
    $unscored[] = $result->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) {
  $query = db_select('quiz_long_answer_user_answers', 'a');
  $query
    ->fields('a', array(
    'result_id',
    'question_nid',
    'question_vid',
  ));
  $query
    ->fields('r', array(
    'title',
  ));
  $query
    ->fields('qnr', array(
    'time_end',
    'time_start',
    'uid',
  ));
  $query
    ->join('node_revision', 'r', 'a.question_vid = r.vid');
  $query
    ->join('quiz_node_results', 'qnr', 'a.result_id = qnr.result_id');
  $query
    ->join('node', 'n', 'qnr.nid = n.nid');
  $query
    ->condition('a.is_evaluated', 0);
  if (!user_access('score any quiz')) {
    $query
      ->condition('n.uid', $_GLOBALS['user']->uid);
  }
  $unscored = array();
  foreach ($query
    ->execute() as $result) {
    $unscored[] = $result;
  }
  return $unscored;
}

/**
 * Generate a list of long answer questions
 *
 * @return
 *  array containing nid as key and title as value
 */
function long_answer_questions_list() {
  $questions = array();
  $results = db_query('SELECT nid, title FROM {node} WHERE type = :type', array(
    ':type' => 'long_answer',
  ));
  foreach ($results as $result) {
    $questions[$result->nid] = check_plain($result->title);
  }
  return $questions;
}

/**
 * Submit function for the report form
 *
 * @param $values
 *   The FAPI $form_state['values']
 */
function long_answer_report_submit($values) {
  long_answer_score_an_answer($values['quiz'], $values['nid'], $values['vid'], $values['rid'], (int) $values['score'], FALSE);
}

/**
 * Validation function for the report form
 *
 * @param $values
 *  The FAPI $form_state['values']
 * @param $form_key
 *  Array key to add errors to
 */
function long_answer_report_validate($values, $form_key) {
  $max = (int) $values['max_score'];

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

Functions

Namesort descending Description
long_answer_config Implements hook_config().
long_answer_get_all_unscored_answers Get all quiz scores that have not yet been evaluated.
long_answer_get_answer Get the answer for a question.
long_answer_get_unscored_answers_by_question Given a quiz, return a list of all the unscored answers.
long_answer_help Implements hook_help().
long_answer_menu Implements hook_menu().
long_answer_questions_list Generate a list of long answer questions
long_answer_quiz_question_info Implements hook_quiz_question_info().
long_answer_report_submit Submit function for the report form
long_answer_report_validate Validation function for the report form
long_answer_score_an_answer Set a score for a long answer question.
long_answer_theme Implements hook_theme().