short_answer.module in Quiz 7.6
Same filename and directory in other branches
- 8.4 question_types/short_answer/short_answer.module
- 6.6 question_types/short_answer/short_answer.module
- 6.3 question_types/short_answer/short_answer.module
- 6.4 question_types/short_answer/short_answer.module
- 6.5 question_types/short_answer/short_answer.module
- 7 question_types/short_answer/short_answer.module
- 7.4 question_types/short_answer/short_answer.module
- 7.5 question_types/short_answer/short_answer.module
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.moduleView 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/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',
'score taken quiz answer',
),
'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;
}
/**
* Implements 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',
),
);
}
/**
* Implements hook_quiz_question_config().
*/
function short_answer_quiz_question_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' => variable_get('short_answer_default_max_score', 5),
);
$form['#validate'][] = 'short_answer_config_validate';
return $form;
}
/**
* Validate the long_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', t('The default max score must be greater than 0'));
}
}
/**
* 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(),
),
'path' => $module_path . '/theme',
'file' => 'short_answer.theme.inc',
),
'short_answer_response_form' => array(
'render element' => 'form',
'path' => $module_path . '/theme',
'file' => 'short_answer.theme.inc',
),
'short_answer_user_answer' => array(
'variables' => array(
'answer' => NULL,
'correct' => NULL,
),
'path' => $module_path . '/theme',
'file' => 'short_answer.theme.inc',
),
'short_answer_answering_form' => array(
'render element' => 'form',
'path' => $module_path . '/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 $quiz
* Quiz node.
* @param $nid
* Node ID of question.
* @param $vid
* Version ID of question.
* @param $result_id
* 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->vid,
':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['value']) ? '' : $answer_feedback['value'],
'answer_feedback_format' => empty($answer_feedback['format']) ? '' : $answer_feedback['format'],
))
->condition('question_nid', $nid)
->condition('question_vid', $vid)
->condition('result_id', $result_id)
->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', $result_id)
->execute();
// Third, we update the main quiz results table
if ($update_total) {
quiz_update_total_score($quiz, $result_id);
}
}
return $changed;
}
/**
* 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, sa.answer_feedback_format, 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['node']['short_answer'] = array(
'form' => array(
'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
Name![]() |
Description |
---|---|
short_answer_config_validate | Validate the long_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_config | Implements hook_quiz_question_config(). |
short_answer_quiz_question_info | Implements hook_quiz_question_info(). |
short_answer_report_submit | Submit 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(). |