2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Multianswer question definition class.
22 * @subpackage multianswer
23 * @copyright 2010 Pierre Pichet
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
28 require_once($CFG->dirroot . '/question/type/numerical/question.php');
29 require_once($CFG->dirroot . '/question/type/multichoice/question.php');
33 * Represents a multianswer question.
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
39 * @copyright 2010 Pierre Pichet
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class qtype_multianswer_question extends question_graded_automatically_with_countback {
43 /** @var array of question_graded_automatically. */
44 public $subquestions = array();
47 * @var array place number => insex in the $subquestions array. Places are
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.
56 public $textfragments;
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.
64 protected function get_substep($step, $i) {
65 return new question_attempt_step_subquestion_adapter($step, 'sub' . $i . '_');
68 public function start_attempt(question_attempt_step $step, $variant) {
69 foreach ($this->subquestions as $i => $subq) {
70 $subq->start_attempt($this->get_substep($step, $i), $variant);
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));
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()) {
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);
91 $answerbit = '{' . implode('; ', $choices) . '}';
98 $answerbit = '{ERR unknown sub-question type}';
100 $summary = str_replace('{#' . $i . '}', $answerbit, $summary);
105 public function get_min_fraction() {
108 foreach ($this->subquestions as $i => $subq) {
109 $fractionmax += $subq->defaultmark;
110 $fractionsum += $subq->defaultmark * $subq->get_min_fraction();
112 return $fractionsum / $fractionmax;
115 public function get_max_fraction() {
118 foreach ($this->subquestions as $i => $subq) {
119 $fractionmax += $subq->defaultmark;
120 $fractionsum += $subq->defaultmark * $subq->get_max_fraction();
122 return $fractionsum / $fractionmax;
125 public function get_expected_data() {
127 foreach ($this->subquestions as $i => $subq) {
128 $substep = $this->get_substep(null, $i);
129 foreach ($subq->get_expected_data() as $name => $type) {
130 if ($subq->qtype->name() == 'multichoice' &&
131 $subq->layout == qtype_multichoice_base::LAYOUT_DROPDOWN) {
132 // Hack or MC inline does not work.
133 $expected[$substep->add_prefix($name)] = PARAM_RAW;
135 $expected[$substep->add_prefix($name)] = $type;
142 public function get_correct_response() {
144 foreach ($this->subquestions as $i => $subq) {
145 $substep = $this->get_substep(null, $i);
146 foreach ($subq->get_correct_response() as $name => $type) {
147 $right[$substep->add_prefix($name)] = $type;
153 public function prepare_simulated_post_data($simulatedresponse) {
155 foreach ($this->subquestions as $i => $subq) {
156 $substep = $this->get_substep(null, $i);
157 foreach ($subq->prepare_simulated_post_data($simulatedresponse[$i]) as $name => $value) {
158 $postdata[$substep->add_prefix($name)] = $value;
164 public function get_student_response_values_for_simulation($postdata) {
165 $simulatedresponse = array();
166 foreach ($this->subquestions as $i => $subq) {
167 $substep = $this->get_substep(null, $i);
168 $subqpostdata = $substep->filter_array($postdata);
169 $subqsimulatedresponse = $subq->get_student_response_values_for_simulation($subqpostdata);
170 foreach ($subqsimulatedresponse as $subresponsekey => $responsevalue) {
171 $simulatedresponse[$i.'.'.$subresponsekey] = $responsevalue;
174 ksort($simulatedresponse);
175 return $simulatedresponse;
178 public function is_complete_response(array $response) {
179 foreach ($this->subquestions as $i => $subq) {
180 $substep = $this->get_substep(null, $i);
181 if (!$subq->is_complete_response($substep->filter_array($response))) {
188 public function is_gradable_response(array $response) {
189 foreach ($this->subquestions as $i => $subq) {
190 $substep = $this->get_substep(null, $i);
191 if ($subq->is_gradable_response($substep->filter_array($response))) {
198 public function is_same_response(array $prevresponse, array $newresponse) {
199 foreach ($this->subquestions as $i => $subq) {
200 $substep = $this->get_substep(null, $i);
201 if (!$subq->is_same_response($substep->filter_array($prevresponse),
202 $substep->filter_array($newresponse))) {
209 public function get_validation_error(array $response) {
210 if ($this->is_complete_response($response)) {
213 return get_string('pleaseananswerallparts', 'qtype_multianswer');
217 * Used by grade_response to combine the states of the subquestions.
218 * The combined state is accumulates in $overallstate. That will be right
219 * if all the separate states are right; and wrong if all the separate states
220 * are wrong, otherwise, it will be partially right.
221 * @param question_state $overallstate the result so far.
222 * @param question_state $newstate the new state to add to the combination.
223 * @return question_state the new combined state.
225 protected function combine_states($overallstate, $newstate) {
226 if (is_null($overallstate)) {
228 } else if ($overallstate == question_state::$gaveup &&
229 $newstate == question_state::$gaveup) {
230 return question_state::$gaveup;
231 } else if ($overallstate == question_state::$gaveup &&
232 $newstate == question_state::$gradedwrong) {
233 return question_state::$gradedwrong;
234 } else if ($overallstate == question_state::$gradedwrong &&
235 $newstate == question_state::$gaveup) {
236 return question_state::$gradedwrong;
237 } else if ($overallstate == question_state::$gradedwrong &&
238 $newstate == question_state::$gradedwrong) {
239 return question_state::$gradedwrong;
240 } else if ($overallstate == question_state::$gradedright &&
241 $newstate == question_state::$gradedright) {
242 return question_state::$gradedright;
244 return question_state::$gradedpartial;
248 public function grade_response(array $response) {
249 $overallstate = null;
252 foreach ($this->subquestions as $i => $subq) {
253 $fractionmax += $subq->defaultmark;
254 $substep = $this->get_substep(null, $i);
255 $subresp = $substep->filter_array($response);
256 if (!$subq->is_gradable_response($subresp)) {
257 $overallstate = $this->combine_states($overallstate, question_state::$gaveup);
259 list($subfraction, $newstate) = $subq->grade_response($subresp);
260 $fractionsum += $subfraction * $subq->defaultmark;
261 $overallstate = $this->combine_states($overallstate, $newstate);
264 return array($fractionsum / $fractionmax, $overallstate);
267 public function clear_wrong_from_response(array $response) {
268 foreach ($this->subquestions as $i => $subq) {
269 $substep = $this->get_substep(null, $i);
270 $subresp = $substep->filter_array($response);
271 list($subfraction, $newstate) = $subq->grade_response($subresp);
272 if ($newstate != question_state::$gradedright) {
273 foreach ($subresp as $ind => $resp) {
274 if ($subq->qtype == 'multichoice' && ($subq->layout == qtype_multichoice_base::LAYOUT_VERTICAL
275 || $subq->layout == qtype_multichoice_base::LAYOUT_HORIZONTAL)) {
276 $response[$substep->add_prefix($ind)] = '-1';
278 $response[$substep->add_prefix($ind)] = '';
286 public function get_num_parts_right(array $response) {
288 foreach ($this->subquestions as $i => $subq) {
289 $substep = $this->get_substep(null, $i);
290 $subresp = $substep->filter_array($response);
291 list($subfraction, $newstate) = $subq->grade_response($subresp);
292 if ($newstate == question_state::$gradedright) {
296 return array($numright, count($this->subquestions));
299 public function compute_final_grade($responses, $totaltries) {
302 foreach ($this->subquestions as $i => $subq) {
303 $fractionmax += $subq->defaultmark;
305 $lastresponse = array();
308 foreach ($responses as $responseindex => $response) {
309 $substep = $this->get_substep(null, $i);
310 $subresp = $substep->filter_array($response);
311 if ($subq->is_same_response($lastresponse, $subresp)) {
314 $lastresponse = $subresp;
315 $lastchange = $responseindex;
316 list($subfraction, $newstate) = $subq->grade_response($subresp);
319 $fractionsum += $subq->defaultmark * max(0, $subfraction - $lastchange * $this->penalty);
322 return $fractionsum / $fractionmax;
325 public function summarise_response(array $response) {
327 foreach ($this->subquestions as $i => $subq) {
328 $substep = $this->get_substep(null, $i);
331 $a->response = $subq->summarise_response($substep->filter_array($response));
332 $summary[] = get_string('subqresponse', 'qtype_multianswer', $a);
335 return implode('; ', $summary);
338 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
339 if ($component == 'question' && $filearea == 'answer') {
342 } else if ($component == 'question' && $filearea == 'answerfeedback') {
343 // Full logic to control which feedbacks a student can see is too complex.
344 // Just allow access to all images. There is a theoretical chance the
345 // students could see files they are not meant to see by guessing URLs,
347 return $options->feedback;
349 } else if ($component == 'question' && $filearea == 'hint') {
350 return $this->check_hint_file_access($qa, $options, $args);
353 return parent::check_file_access($qa, $options, $component, $filearea,
354 $args, $forcedownload);