weekly release 2.1beta
[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 */
42class qtype_multianswer_question extends question_graded_automatically {
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
143 public function is_complete_response(array $response) {
144 foreach ($this->subquestions as $i => $subq) {
145 $substep = $this->get_substep(null, $i);
146 if (!$subq->is_complete_response($substep->filter_array($response))) {
147 return false;
148 }
149 }
150 return true;
151 }
152
153 public function is_gradable_response(array $response) {
154 foreach ($this->subquestions as $i => $subq) {
155 $substep = $this->get_substep(null, $i);
156 if ($subq->is_gradable_response($substep->filter_array($response))) {
157 return true;
158 }
159 }
160 return false;
161 }
162
163 public function is_same_response(array $prevresponse, array $newresponse) {
164 foreach ($this->subquestions as $i => $subq) {
165 $substep = $this->get_substep(null, $i);
166 if (!$subq->is_same_response($substep->filter_array($prevresponse),
167 $substep->filter_array($newresponse))) {
168 return false;
169 }
170 }
171 return true;
172 }
173
174 public function get_validation_error(array $response) {
175 $errors = array();
176 foreach ($this->subquestions as $i => $subq) {
177 $substep = $this->get_substep(null, $i);
178 $errors[] = $subq->get_validation_error($substep->filter_array($response));
179 }
180 return implode('<br />', $errors);
181 }
182
183 /**
184 * Used by grade_response to combine the states of the subquestions.
185 * The combined state is accumulates in $overallstate. That will be right
186 * if all the separate states are right; and wrong if all the separate states
187 * are wrong, otherwise, it will be partially right.
188 * @param question_state $overallstate the result so far.
189 * @param question_state $newstate the new state to add to the combination.
190 * @return question_state the new combined state.
191 */
192 protected function combine_states($overallstate, $newstate) {
193 if (is_null($overallstate)) {
194 return $newstate;
195 } else if ($overallstate == question_state::$gaveup &&
196 $newstate == question_state::$gaveup) {
197 return question_state::$gaveup;
198 } else if ($overallstate == question_state::$gaveup &&
199 $newstate == question_state::$gradedwrong) {
200 return question_state::$gradedwrong;
201 } else if ($overallstate == question_state::$gradedwrong &&
202 $newstate == question_state::$gaveup) {
203 return question_state::$gradedwrong;
204 } else if ($overallstate == question_state::$gradedwrong &&
205 $newstate == question_state::$gradedwrong) {
206 return question_state::$gradedwrong;
207 } else if ($overallstate == question_state::$gradedright &&
208 $newstate == question_state::$gradedright) {
209 return question_state::$gradedright;
210 } else {
211 return question_state::$gradedpartial;
212 }
213 }
214
215 public function grade_response(array $response) {
216 $overallstate = null;
217 $fractionsum = 0;
218 $fractionmax = 0;
219 foreach ($this->subquestions as $i => $subq) {
220 $fractionmax += $subq->defaultmark;
221 $substep = $this->get_substep(null, $i);
222 $subresp = $substep->filter_array($response);
223 if (!$subq->is_gradable_response($subresp)) {
224 $overallstate = $this->combine_states($overallstate, question_state::$gaveup);
225 } else {
226 list($subfraction, $newstate) = $subq->grade_response($subresp);
227 $fractionsum += $subfraction * $subq->defaultmark;
228 $overallstate = $this->combine_states($overallstate, $newstate);
229 }
230 }
231 return array($fractionsum / $fractionmax, $overallstate);
232 }
233
234 public function summarise_response(array $response) {
235 $summary = array();
236 foreach ($this->subquestions as $i => $subq) {
237 $substep = $this->get_substep(null, $i);
238 $a = new stdClass();
239 $a->i = $i;
240 $a->response = $subq->summarise_response($substep->filter_array($response));
241 $summary[] = get_string('subqresponse', 'qtype_multianswer', $a);
242 }
243
244 return implode('; ', $summary);
245 }
246
247 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
248 // TODO
249 }
250}