MDL-32188 quiz/question: behaviours can add to the review summary.
[moodle.git] / question / type / multianswer / question.php
CommitLineData
ab50232b 1<?php
ab50232b
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
17
18/**
19 * Multianswer question definition class.
20 *
21 * @package qtype
22 * @subpackage multianswer
23 * @copyright 2010 Pierre Pichet
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
28require_once($CFG->dirroot . '/question/type/numerical/question.php');
29require_once($CFG->dirroot . '/question/type/multichoice/question.php');
30
31
32/**
33 * Represents a multianswer question.
34 *
35 * A multi-answer question is made of of several subquestions of various types.
36 * You can think of it as an application of the composite pattern to qusetion
37 * types.
38 *
39 * @copyright 2010 Pierre Pichet
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
c2f056a9 42class qtype_multianswer_question extends question_graded_automatically_with_countback {
ab50232b
TH
43 /** @var array of question_graded_automatically. */
44 public $subquestions = array();
45
42a5b055
TH
46 /**
47 * @var array place number => insex in the $subquestions array. Places are
48 * numbered from 1.
49 */
50 public $places;
51
52 /**
53 * @var array of strings, one longer than $places, which is achieved by
54 * indexing from 0. The bits of question text that go between the subquestions.
55 */
56 public $textfragments;
57
ab50232b
TH
58 /**
59 * Get a question_attempt_step_subquestion_adapter
60 * @param question_attempt_step $step the step to adapt.
61 * @param int $i the subquestion index.
62 * @return question_attempt_step_subquestion_adapter.
63 */
64 protected function get_substep($step, $i) {
65 return new question_attempt_step_subquestion_adapter($step, 'sub' . $i . '_');
66 }
67
1da821bb 68 public function start_attempt(question_attempt_step $step, $variant) {
ab50232b 69 foreach ($this->subquestions as $i => $subq) {
1da821bb 70 $subq->start_attempt($this->get_substep($step, $i), $variant);
ab50232b
TH
71 }
72 }
73
74 public function apply_attempt_state(question_attempt_step $step) {
75 foreach ($this->subquestions as $i => $subq) {
76 $subq->apply_attempt_state($this->get_substep($step, $i));
ab50232b
TH
77 }
78 }
79
fa6c8620
TH
80 public function get_question_summary() {
81 $summary = $this->html_to_text($this->questiontext, $this->questiontextformat);
82 foreach ($this->subquestions as $i => $subq) {
83 switch ($subq->qtype->name()) {
84 case 'multichoice':
85 $choices = array();
86 $dummyqa = new question_attempt($subq, $this->contextid);
87 foreach ($subq->get_order($dummyqa) as $ansid) {
88 $choices[] = $this->html_to_text($subq->answers[$ansid]->answer,
89 $subq->answers[$ansid]->answerformat);
90 }
91 $answerbit = '{' . implode('; ', $choices) . '}';
92 break;
93 case 'numerical':
94 case 'shortanswer':
95 $answerbit = '_____';
96 break;
97 default:
98 $answerbit = '{ERR unknown sub-question type}';
99 }
100 $summary = str_replace('{#' . $i . '}', $answerbit, $summary);
101 }
102 return $summary;
103 }
ab50232b
TH
104
105 public function get_min_fraction() {
106 $fractionsum = 0;
107 $fractionmax = 0;
108 foreach ($this->subquestions as $i => $subq) {
109 $fractionmax += $subq->defaultmark;
110 $fractionsum += $subq->defaultmark * $subq->get_min_fraction();
111 }
112 return $fractionsum / $fractionmax;
113 }
114
115 public function get_expected_data() {
116 $expected = array();
117 foreach ($this->subquestions as $i => $subq) {
118 $substep = $this->get_substep(null, $i);
119 foreach ($subq->get_expected_data() as $name => $type) {
7ac7977c 120 if ($subq->qtype->name() == 'multichoice' &&
dcedbb0e 121 $subq->layout == qtype_multichoice_base::LAYOUT_DROPDOWN) {
7ac7977c
TH
122 // Hack or MC inline does not work.
123 $expected[$substep->add_prefix($name)] = PARAM_RAW;
124 } else {
125 $expected[$substep->add_prefix($name)] = $type;
126 }
ab50232b
TH
127 }
128 }
129 return $expected;
130 }
131
132 public function get_correct_response() {
133 $right = array();
134 foreach ($this->subquestions as $i => $subq) {
135 $substep = $this->get_substep(null, $i);
fa6c8620 136 foreach ($subq->get_correct_response() as $name => $type) {
ab50232b
TH
137 $right[$substep->add_prefix($name)] = $type;
138 }
139 }
140 return $right;
141 }
142
a3c5150b
JP
143 public function prepare_simulated_post_data($simulatedresponse) {
144 $postdata = array();
145 foreach ($this->subquestions as $i => $subq) {
146 $substep = $this->get_substep(null, $i);
147 foreach ($subq->prepare_simulated_post_data($simulatedresponse[$i]) as $name => $value) {
148 $postdata[$substep->add_prefix($name)] = $value;
149 }
150 }
151 return $postdata;
152 }
153
ab50232b
TH
154 public function is_complete_response(array $response) {
155 foreach ($this->subquestions as $i => $subq) {
156 $substep = $this->get_substep(null, $i);
157 if (!$subq->is_complete_response($substep->filter_array($response))) {
158 return false;
159 }
160 }
161 return true;
162 }
163
164 public function is_gradable_response(array $response) {
165 foreach ($this->subquestions as $i => $subq) {
166 $substep = $this->get_substep(null, $i);
167 if ($subq->is_gradable_response($substep->filter_array($response))) {
168 return true;
169 }
170 }
171 return false;
172 }
173
174 public function is_same_response(array $prevresponse, array $newresponse) {
175 foreach ($this->subquestions as $i => $subq) {
176 $substep = $this->get_substep(null, $i);
177 if (!$subq->is_same_response($substep->filter_array($prevresponse),
178 $substep->filter_array($newresponse))) {
179 return false;
180 }
181 }
182 return true;
183 }
184
185 public function get_validation_error(array $response) {
186 $errors = array();
187 foreach ($this->subquestions as $i => $subq) {
188 $substep = $this->get_substep(null, $i);
189 $errors[] = $subq->get_validation_error($substep->filter_array($response));
190 }
191 return implode('<br />', $errors);
192 }
193
194 /**
195 * Used by grade_response to combine the states of the subquestions.
196 * The combined state is accumulates in $overallstate. That will be right
197 * if all the separate states are right; and wrong if all the separate states
198 * are wrong, otherwise, it will be partially right.
199 * @param question_state $overallstate the result so far.
200 * @param question_state $newstate the new state to add to the combination.
201 * @return question_state the new combined state.
202 */
203 protected function combine_states($overallstate, $newstate) {
204 if (is_null($overallstate)) {
205 return $newstate;
206 } else if ($overallstate == question_state::$gaveup &&
207 $newstate == question_state::$gaveup) {
208 return question_state::$gaveup;
209 } else if ($overallstate == question_state::$gaveup &&
210 $newstate == question_state::$gradedwrong) {
211 return question_state::$gradedwrong;
212 } else if ($overallstate == question_state::$gradedwrong &&
213 $newstate == question_state::$gaveup) {
214 return question_state::$gradedwrong;
215 } else if ($overallstate == question_state::$gradedwrong &&
216 $newstate == question_state::$gradedwrong) {
217 return question_state::$gradedwrong;
218 } else if ($overallstate == question_state::$gradedright &&
219 $newstate == question_state::$gradedright) {
220 return question_state::$gradedright;
221 } else {
222 return question_state::$gradedpartial;
223 }
224 }
225
226 public function grade_response(array $response) {
227 $overallstate = null;
228 $fractionsum = 0;
229 $fractionmax = 0;
230 foreach ($this->subquestions as $i => $subq) {
231 $fractionmax += $subq->defaultmark;
232 $substep = $this->get_substep(null, $i);
233 $subresp = $substep->filter_array($response);
234 if (!$subq->is_gradable_response($subresp)) {
235 $overallstate = $this->combine_states($overallstate, question_state::$gaveup);
236 } else {
237 list($subfraction, $newstate) = $subq->grade_response($subresp);
238 $fractionsum += $subfraction * $subq->defaultmark;
239 $overallstate = $this->combine_states($overallstate, $newstate);
240 }
241 }
242 return array($fractionsum / $fractionmax, $overallstate);
243 }
244
e9af6091
JMV
245 public function clear_wrong_from_response(array $response) {
246 foreach ($this->subquestions as $i => $subq) {
247 $substep = $this->get_substep(null, $i);
248 $subresp = $substep->filter_array($response);
249 list($subfraction, $newstate) = $subq->grade_response($subresp);
250 if ($newstate != question_state::$gradedright) {
251 foreach ($subresp as $ind => $resp) {
d55399a0
JMV
252 if ($subq->qtype == 'multichoice' && ($subq->layout == qtype_multichoice_base::LAYOUT_VERTICAL
253 || $subq->layout == qtype_multichoice_base::LAYOUT_HORIZONTAL)) {
e9af6091
JMV
254 $response[$substep->add_prefix($ind)] = '-1';
255 } else {
256 $response[$substep->add_prefix($ind)] = '';
257 }
258 }
259 }
260 }
261 return $response;
262 }
263
264 public function get_num_parts_right(array $response) {
265 $numright = 0;
266 foreach ($this->subquestions as $i => $subq) {
267 $substep = $this->get_substep(null, $i);
268 $subresp = $substep->filter_array($response);
269 list($subfraction, $newstate) = $subq->grade_response($subresp);
270 if ($newstate == question_state::$gradedright) {
271 $numright += 1;
272 }
273 }
274 return array($numright, count($this->subquestions));
275 }
276
c2f056a9
JMV
277 public function compute_final_grade($responses, $totaltries) {
278 $fractionsum = 0;
279 $fractionmax = 0;
280 foreach ($this->subquestions as $i => $subq) {
281 $fractionmax += $subq->defaultmark;
282
283 $lastresponse = array();
284 $lastchange = 0;
285 $subfraction = 0;
286 foreach ($responses as $responseindex => $response) {
287 $substep = $this->get_substep(null, $i);
288 $subresp = $substep->filter_array($response);
289 if ($subq->is_same_response($lastresponse, $subresp)) {
290 continue;
291 }
292 $lastresponse = $subresp;
293 $lastchange = $responseindex;
294 list($subfraction, $newstate) = $subq->grade_response($subresp);
295 }
296
297 $fractionsum += $subq->defaultmark * max(0, $subfraction - $lastchange * $this->penalty);
298 }
299
300 return $fractionsum / $fractionmax;
301 }
302
ab50232b
TH
303 public function summarise_response(array $response) {
304 $summary = array();
305 foreach ($this->subquestions as $i => $subq) {
306 $substep = $this->get_substep(null, $i);
307 $a = new stdClass();
308 $a->i = $i;
309 $a->response = $subq->summarise_response($substep->filter_array($response));
310 $summary[] = get_string('subqresponse', 'qtype_multianswer', $a);
311 }
312
313 return implode('; ', $summary);
314 }
315
316 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
03b2b8fa
TH
317 if ($component == 'question' && $filearea == 'answer') {
318 return true;
319
320 } else if ($component == 'question' && $filearea == 'answerfeedback') {
321 // Full logic to control which feedbacks a student can see is too complex.
322 // Just allow access to all images. There is a theoretical chance the
323 // students could see files they are not meant to see by guessing URLs,
324 // but it is remote.
325 return $options->feedback;
326
327 } else if ($component == 'question' && $filearea == 'hint') {
328 return $this->check_hint_file_access($qa, $options, $args);
329
330 } else {
331 return parent::check_file_access($qa, $options, $component, $filearea,
332 $args, $forcedownload);
333 }
ab50232b
TH
334 }
335}