You are here

short_answer.module in Quiz 8.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
 */

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

/**
 * Implements hook_permission().
 */
function short_answer_permission() {
  return array(
    'use regex for short answer' => array(
      'title' => t('use regex for short answer'),
      'description' => t('Use PHP "regular expressions" the advanced option for automated response evaluation.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_menu().
 */
function short_answer_menu() {
  $items['admin/config/quiz/score-short-answer'] = array(
    'title' => 'Score short answer questions',
    'description' => 'Score the answers from quizzes that use short answer questions.',
    'route_name' => 'short_answer.score_short_answer',
    'type' => MENU_NORMAL_ITEM,
  );

  // Pass vid and rid to this path.
  $items['admin/config/quiz/score-short-answer/%/%'] = array(
    'title' => 'Score short answer response',
    'description' => 'Score a response to a short answer question.',
    'route_name' => 'short_answer.score_short_answer_pages',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_quiz_question_info().
 */
function short_answer_quiz_question_info() {
  $config = \Drupal::config('node.type.short_answer');
  return array(
    'short_answer' => array(
      'name' => $config
        ->get('name'),
      'description' => $config
        ->get('description'),
      'question provider' => 'Drupal\\short_answer\\ShortAnswerQuestion',
      'response provider' => 'Drupal\\short_answer\\ShortAnswerResponse',
      'module' => 'quiz_question',
    ),
  );
}

/**
 * Implements hook_config().
 */
function short_answer_config() {
  $form['short_answer_default_max_score'] = array(
    '#type' => 'textfield',
    '#title' => t('Default max score'),
    '#description' => t('Choose the default maximum score for a short answer question.'),
    '#default_value' => \Drupal::config('short_answer.settings')
      ->get('short_answer_default_max_score'),
  );
  $form['#validate'][] = 'short_answer_config_validate';
  $form['#submit'][] = 'short_answer_config_submit';
  return $form;
}

/**
 * Validate the short_answer config form values
 */
function short_answer_config_validate($form, $form_state) {
  if ($form_state['values']['short_answer_default_max_score'] <= 0) {
    form_set_error('short_answer_default_max_score', $form_state, t('The default max score must be greater than 0'));
  }
}

/**
 * Submit the short_answer config form values
 */
function short_answer_config_submit($form, $form_state) {
  $config = \Drupal::config('short_answer.settings');
  $config
    ->set('short_answer_default_max_score', $form_state['values']['short_answer_default_max_score'])
    ->save();
}

/**
 * Implements hook_theme().
 */
function short_answer_theme($existing, $type, $theme, $path) {
  $module_path = drupal_get_path('module', 'short_answer');
  return array(
    'short_answer_view_unscored' => array(
      'variables' => array(
        'unscored' => array(),
      ),
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_response_form' => array(
      'render element' => 'form',
      'file' => 'short_answer.theme.inc',
    ),
    'short_answer_user_answer' => array(
      'variables' => array(
        'answer' => NULL,
        'correct' => NULL,
      ),
      'file' => 'short_answer.theme.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.
 * @param $update_total
 *  Shall the total score for a quiz 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) {
  extract($values);

  // When we set the score we make sure that the max score in the quiz the question belongs to is considered
  $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
      ->getRevisionId(),
    ':cvid' => $vid,
  ))
    ->fetchField();
  $changed = db_update('quiz_short_answer_user_answers')
    ->fields(array(
    'score' => $score * $question_max_score / $quiz_max_score,
    'is_evaluated' => 1,
    'answer_feedback' => empty($answer_feedback) ? '' : $answer_feedback,
  ))
    ->condition('question_nid', $nid)
    ->condition('question_vid', $vid)
    ->condition('result_id', $rid)
    ->execute();

  // 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_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 > 0.5 ? 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;
}

/**
 * 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) {
  $results = db_query('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
    FROM {quiz_short_answer_user_answers} sa
    JOIN {quiz_node_results} qnr ON (sa.result_id = qnr.result_id)
    JOIN {quiz_node_relationship} rel ON (qnr.vid = rel.parent_vid AND rel.child_vid = sa.question_vid)
    WHERE sa.question_nid = :qnid AND sa.question_vid = :qvid AND sa.result_id = :rid', array(
    ':qnid' => $question_nid,
    ':qvid' => $question_vid,
    ':rid' => $result_id,
  ))
    ->fetchAssoc();
  return $results ? $results : FALSE;
}

/**
 * Implements hook_field_extra_fields().
 */
function short_answer_field_extra_fields() {
  $extra = array();
  foreach (node_type_get_types() as $bundle_name => $bundle) {
    if ($bundle_name == 'short_answer') {
      $extra['node'][$bundle->type]['form']['answer'] = array(
        'label' => t('Answer'),
        'description' => t('Provide the answer and the method by which the answer will be evaluated.'),
        'weight' => -4,
      );
    }
  }
  return $extra;
}

Functions

Namesort descending Description
short_answer_config Implements hook_config().
short_answer_config_submit Submit the short_answer config form values
short_answer_config_validate Validate the short_answer config form values
short_answer_field_extra_fields Implements hook_field_extra_fields().
short_answer_get_answer Get the answer for a question.
short_answer_help Implements hook_help().
short_answer_menu Implements hook_menu().
short_answer_permission Implements hook_permission().
short_answer_quiz_question_info Implements 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 Implements hook_theme().