MDL-16464 qtype_multichoice: fix missed question -> questionid.
[moodle.git] / question / type / multichoice / questiontype.php
CommitLineData
aeb15530 1<?php
c9c989a0
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
1976496e 17/**
3e0647ff 18 * The questiontype class for the multiple choice question type.
19 *
d3603157 20 * @package qtype
c9c989a0 21 * @subpackage multichoice
d3603157
TH
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3e0647ff 24 */
516cf3eb 25
7764183a 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
603bd001
PS
29global $CFG;
30require_once($CFG->libdir . '/questionlib.php');
31
a17b297d 32
c9c989a0
TH
33/**
34 * The multiple choice question type.
35 *
7764183a
TH
36 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c9c989a0
TH
38 */
39class qtype_multichoice extends question_type {
40 public function get_question_options($question) {
fef8f84e 41 global $DB, $OUTPUT;
0aa04a7f
TH
42 $question->options = $DB->get_record('qtype_multichoice_options',
43 array('questionid' => $question->id), '*', MUST_EXIST);
c9c989a0 44 parent::get_question_options($question);
516cf3eb 45 }
46
c9c989a0 47 public function save_question_options($question) {
f34488b2 48 global $DB;
fe6ce234 49 $context = $question->context;
c9c989a0 50 $result = new stdClass();
69988ed4
TH
51
52 $oldanswers = $DB->get_records('question_answers',
53 array('question' => $question->id), 'id ASC');
516cf3eb 54
3d9645ae 55 // Following hack to check at least two answers exist.
516cf3eb 56 $answercount = 0;
69988ed4
TH
57 foreach ($question->answer as $key => $answer) {
58 if ($answer != '') {
516cf3eb 59 $answercount++;
60 }
61 }
3d9645ae 62 if ($answercount < 2) { // Check there are at lest 2 answers for multiple choice.
69988ed4 63 $result->notice = get_string('notenoughanswers', 'qtype_multichoice', '2');
516cf3eb 64 return $result;
65 }
66
3d9645ae 67 // Insert all the new answers.
516cf3eb 68 $totalfraction = 0;
69 $maxfraction = -1;
69988ed4 70 foreach ($question->answer as $key => $answerdata) {
2a6c5c52 71 if (trim($answerdata['text']) == '') {
69988ed4
TH
72 continue;
73 }
74
75 // Update an existing answer if possible.
76 $answer = array_shift($oldanswers);
77 if (!$answer) {
78 $answer = new stdClass();
79 $answer->question = $question->id;
80 $answer->answer = '';
81 $answer->feedback = '';
82 $answer->id = $DB->insert_record('question_answers', $answer);
83 }
84
3d9645ae 85 // Doing an import.
2a6c5c52 86 $answer->answer = $this->import_or_save_files($answerdata,
87 $context, 'question', 'answer', $answer->id);
88 $answer->answerformat = $answerdata['format'];
69988ed4
TH
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
94 $DB->update_record('question_answers', $answer);
516cf3eb 95
69988ed4
TH
96 if ($question->fraction[$key] > 0) {
97 $totalfraction += $question->fraction[$key];
516cf3eb 98 }
69988ed4
TH
99 if ($question->fraction[$key] > $maxfraction) {
100 $maxfraction = $question->fraction[$key];
101 }
102 }
103
104 // Delete any left over old answer records.
105 $fs = get_file_storage();
12039f2e 106 foreach ($oldanswers as $oldanswer) {
69988ed4
TH
107 $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id);
108 $DB->delete_records('question_answers', array('id' => $oldanswer->id));
516cf3eb 109 }
110
0aa04a7f 111 $options = $DB->get_record('qtype_multichoice_options', array('questionid' => $question->id));
307f045f 112 if (!$options) {
c9c989a0 113 $options = new stdClass();
0aa04a7f 114 $options->questionid = $question->id;
69988ed4
TH
115 $options->correctfeedback = '';
116 $options->partiallycorrectfeedback = '';
117 $options->incorrectfeedback = '';
0aa04a7f 118 $options->id = $DB->insert_record('qtype_multichoice_options', $options);
307f045f 119 }
69988ed4 120
307f045f 121 $options->single = $question->single;
69988ed4 122 if (isset($question->layout)) {
fe6ce234 123 $options->layout = $question->layout;
e5ebbd53 124 }
a5e8e6e5 125 $options->answernumbering = $question->answernumbering;
307f045f 126 $options->shuffleanswers = $question->shuffleanswers;
1c2ed7c5 127 $options = $this->save_combined_feedback_helper($options, $question, $context, true);
0aa04a7f 128 $DB->update_record('qtype_multichoice_options', $options);
516cf3eb 129
8b9dfc2b
TH
130 $this->save_hints($question, true);
131
3d9645ae 132 // Perform sanity checks on fractional grades.
516cf3eb 133 if ($options->single) {
134 if ($maxfraction != 1) {
12039f2e
TH
135 $result->noticeyesno = get_string('fractionsnomax', 'qtype_multichoice',
136 $maxfraction * 100);
516cf3eb 137 return $result;
138 }
139 } else {
69988ed4 140 $totalfraction = round($totalfraction, 2);
516cf3eb 141 if ($totalfraction != 1) {
12039f2e
TH
142 $result->noticeyesno = get_string('fractionsaddwrong', 'qtype_multichoice',
143 $totalfraction * 100);
516cf3eb 144 return $result;
145 }
146 }
516cf3eb 147 }
148
c9c989a0
TH
149 protected function make_question_instance($questiondata) {
150 question_bank::load_question_definition_classes($this->name());
151 if ($questiondata->options->single) {
152 $class = 'qtype_multichoice_single_question';
516cf3eb 153 } else {
c9c989a0 154 $class = 'qtype_multichoice_multi_question';
516cf3eb 155 }
c9c989a0 156 return new $class();
516cf3eb 157 }
158
c9c989a0
TH
159 protected function make_hint($hint) {
160 return question_hint_with_parts::load_from_record($hint);
516cf3eb 161 }
162
c9c989a0
TH
163 protected function initialise_question_instance(question_definition $question, $questiondata) {
164 parent::initialise_question_instance($question, $questiondata);
165 $question->shuffleanswers = $questiondata->options->shuffleanswers;
166 $question->answernumbering = $questiondata->options->answernumbering;
167 if (!empty($questiondata->options->layout)) {
168 $question->layout = $questiondata->options->layout;
516cf3eb 169 } else {
c9c989a0 170 $question->layout = qtype_multichoice_single_question::LAYOUT_VERTICAL;
516cf3eb 171 }
1c2ed7c5 172 $this->initialise_combined_feedback($question, $questiondata, true);
516cf3eb 173
35c9b652 174 $this->initialise_question_answers($question, $questiondata, false);
c9c989a0 175 }
fe6ce234 176
c9c989a0
TH
177 public function delete_question($questionid, $contextid) {
178 global $DB;
dba40d4e 179 $DB->delete_records('qtype_multichoice_options', array('questionid' => $questionid));
7b41a4a9 180
c9c989a0 181 parent::delete_question($questionid, $contextid);
516cf3eb 182 }
183
c9c989a0 184 public function get_random_guess_score($questiondata) {
a980739f
TH
185 if (!$questiondata->options->single) {
186 // Pretty much impossible to compute for _multi questions. Don't try.
187 return null;
188 }
189
190 // Single choice questions - average choice fraction.
c9c989a0
TH
191 $totalfraction = 0;
192 foreach ($questiondata->options->answers as $answer) {
193 $totalfraction += $answer->fraction;
f2ca88b0 194 }
c9c989a0 195 return $totalfraction / count($questiondata->options->answers);
f2ca88b0
TH
196 }
197
c7df5006 198 public function get_possible_responses($questiondata) {
c9c989a0
TH
199 if ($questiondata->options->single) {
200 $responses = array();
516cf3eb 201
c9c989a0 202 foreach ($questiondata->options->answers as $aid => $answer) {
e2b388c1
TH
203 $responses[$aid] = new question_possible_response(
204 question_utils::to_plain_text($answer->answer, $answer->answerformat),
6c23d0d4 205 $answer->fraction);
c9c989a0 206 }
f30bbcaf 207
c9c989a0
TH
208 $responses[null] = question_possible_response::no_response();
209 return array($questiondata->id => $responses);
210 } else {
211 $parts = array();
516cf3eb 212
c9c989a0 213 foreach ($questiondata->options->answers as $aid => $answer) {
e2b388c1
TH
214 $parts[$aid] = array($aid => new question_possible_response(
215 question_utils::to_plain_text($answer->answer, $answer->answerformat),
6c23d0d4 216 $answer->fraction));
516cf3eb 217 }
7b41a4a9 218
c9c989a0 219 return $parts;
6f51ed72 220 }
6f51ed72 221 }
c5d94c41 222
3e0647ff 223 /**
224 * @return array of the numbering styles supported. For each one, there
225 * should be a lang string answernumberingxxx in teh qtype_multichoice
226 * language file, and a case in the switch statement in number_in_style,
227 * and it should be listed in the definition of this column in install.xml.
228 */
c9c989a0
TH
229 public static function get_numbering_styles() {
230 $styles = array();
231 foreach (array('abc', 'ABCD', '123', 'iii', 'IIII', 'none') as $numberingoption) {
12039f2e
TH
232 $styles[$numberingoption] =
233 get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
3e0647ff 234 }
c9c989a0 235 return $styles;
b9bd6da4 236 }
fe6ce234 237
c7df5006 238 public function move_files($questionid, $oldcontextid, $newcontextid) {
5d548d3e
TH
239 parent::move_files($questionid, $oldcontextid, $newcontextid);
240 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid, true);
a23bda41 241 $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
d44480f6 242 $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
fe6ce234
DC
243 }
244
9203b705 245 protected function delete_files($questionid, $contextid) {
9203b705
TH
246 parent::delete_files($questionid, $contextid);
247 $this->delete_files_in_answers($questionid, $contextid, true);
a23bda41 248 $this->delete_files_in_combined_feedback($questionid, $contextid);
d44480f6 249 $this->delete_files_in_hints($questionid, $contextid);
9203b705 250 }
516cf3eb 251}