MDL-20636 Fix @package names in question/behaviour, question/engine and question...
[moodle.git] / question / type / shortanswer / questiontype.php
CommitLineData
aeb15530 1<?php
516cf3eb 2
fe6ce234
DC
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
516cf3eb 18
41a89a07 19/**
068b4594
TH
20 * Question type class for the short answer question type.
21 *
22 * @package qtype_shortanswer
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53a4d39f 25 */
516cf3eb 26
516cf3eb 27
068b4594
TH
28require_once($CFG->libdir . '/questionlib.php');
29require_once($CFG->dirroot . '/question/engine/lib.php');
30require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
516cf3eb 31
869309b8 32
068b4594
TH
33/**
34 * The short answer question type.
35 *
36 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_shortanswer extends question_type {
40 public function extra_question_fields() {
fe6ce234 41 return array('question_shortanswer', 'answers', 'usecase');
d001dac7 42 }
516cf3eb 43
068b4594 44 protected function questionid_column_name() {
d001dac7 45 return 'question';
516cf3eb 46 }
47
5d548d3e
TH
48 function move_files($questionid, $oldcontextid, $newcontextid) {
49 parent::move_files($questionid, $oldcontextid, $newcontextid);
50 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
fe6ce234
DC
51 }
52
9203b705
TH
53 protected function delete_files($questionid, $contextid) {
54 parent::delete_files($questionid, $contextid);
55 $this->delete_files_in_answers($questionid, $contextid);
56 }
57
068b4594 58 public function save_question_options($question) {
f34488b2 59 global $DB;
0ff4bd08 60 $result = new stdClass();
90a36f8c 61
fe6ce234
DC
62 $context = $question->context;
63
69988ed4
TH
64 $oldanswers = $DB->get_records('question_answers',
65 array('question' => $question->id), 'id ASC');
516cf3eb 66
67 $answers = array();
68 $maxfraction = -1;
068b4594
TH
69
70 // Insert all the new answers
69988ed4
TH
71 foreach ($question->answer as $key => $answerdata) {
72 // Check for, and ignore, completely blank answer from the form.
73 if (trim($answerdata) == '' && $question->fraction[$key] == 0 &&
fe6ce234 74 html_is_blank($question->feedback[$key]['text'])) {
94a6d656 75 continue;
76 }
77
69988ed4
TH
78 // Update an existing answer if possible.
79 $answer = array_shift($oldanswers);
80 if (!$answer) {
81 $answer = new stdClass();
82 $answer->question = $question->id;
83 $answer->answer = '';
84 $answer->feedback = '';
85 $answer->id = $DB->insert_record('question_answers', $answer);
cde2709a 86 }
fe6ce234 87
69988ed4
TH
88 $answer->answer = trim($answerdata);
89 $answer->fraction = $question->fraction[$key];
90 $answer->feedback = $this->import_or_save_files($question->feedback[$key],
91 $context, 'question', 'answerfeedback', $answer->id);
92 $answer->feedbackformat = $question->feedback[$key]['format'];
93 $DB->update_record('question_answers', $answer);
fe6ce234 94
94a6d656 95 $answers[] = $answer->id;
96 if ($question->fraction[$key] > $maxfraction) {
97 $maxfraction = $question->fraction[$key];
98 }
516cf3eb 99 }
100
068b4594
TH
101 $question->answers = implode(',', $answers);
102 $parentresult = parent::save_question_options($question);
103 if ($parentresult !== null) {
104 // Parent function returns null if all is OK
105 return $parentresult;
106 }
107
69988ed4
TH
108 // Delete any left over old answer records.
109 $fs = get_file_storage();
110 foreach($oldanswers as $oldanswer) {
111 $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id);
112 $DB->delete_records('question_answers', array('id' => $oldanswer->id));
113 }
114
068b4594 115 $this->save_hints($question);
516cf3eb 116
69988ed4 117 // Perform sanity checks on fractional grades
516cf3eb 118 if ($maxfraction != 1) {
69988ed4 119 $result->noticeyesno = get_string('fractionsnomax', 'quiz', $maxfraction * 100);
516cf3eb 120 return $result;
516cf3eb 121 }
516cf3eb 122 }
123
068b4594
TH
124 protected function initialise_question_instance(question_definition $question, $questiondata) {
125 parent::initialise_question_instance($question, $questiondata);
126 $question->usecase = $questiondata->options->usecase;
127 $this->initialise_question_answers($question, $questiondata);
516cf3eb 128 }
1a1293ed 129
068b4594
TH
130 public function get_random_guess_score($questiondata) {
131 foreach ($questiondata->options->answers as $aid => $answer) {
132 if ('*' == trim($answer->answer)) {
6f51ed72 133 return $answer->fraction;
134 }
135 }
136 return 0;
137 }
516cf3eb 138
068b4594
TH
139 public function get_possible_responses($questiondata) {
140 $responses = array();
90a36f8c 141
068b4594
TH
142 foreach ($questiondata->options->answers as $aid => $answer) {
143 $responses[$aid] = new question_possible_response($answer->answer,
144 $answer->fraction);
93a501c1 145 }
068b4594 146 $responses[null] = question_possible_response::no_response();
b9bd6da4 147
068b4594 148 return array($questiondata->id => $responses);
b9bd6da4 149 }
516cf3eb 150}