MDL-47494 gapselect: Eliminate integer and boolean in PHPdoc comments. should be...
[moodle.git] / question / type / gapselect / questionbase.php
CommitLineData
0d24b17a
TH
1<?php
2
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
0d24b17a 18/**
43df6cac
TH
19 * Definition class for embedded element in question text question. Parent of
20 * gap-select, drag and drop and possibly others.
0d24b17a 21 *
9df0480d 22 * @package qtype
0d24b17a 23 * @subpackage gapselect
9df0480d
TH
24 * @copyright 2011 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a
TH
26 */
27
28
b28ad86a
TH
29defined('MOODLE_INTERNAL') || die();
30
31
0d24b17a
TH
32/**
33 * Represents embedded element in question text question. Parent of drag and drop and select from
34 * drop down list and ?others?
35 *
9df0480d
TH
36 * @copyright 2011 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a 38 */
43df6cac 39abstract class qtype_gapselect_question_base extends question_graded_automatically_with_countback {
0d24b17a
TH
40 /** @var boolean Whether the question stems should be shuffled. */
41 public $shufflechoices;
42
43 public $correctfeedback;
44 public $partiallycorrectfeedback;
45 public $incorrectfeedback;
46
47 /** @var array of arrays. The keys are the choice group numbers. The values
48 * are arrays of qtype_gapselect_choice objects. */
49 public $choices;
50
51 /**
52 * @var array place number => group number of the places in the question
53 * text where choices can be put. Places are numbered from 1.
54 */
55 public $places;
56
57 /**
58 * @var array of strings, one longer than $places, which is achieved by
59 * indexing from 0. The bits of question text that go between the placeholders.
60 */
61 public $textfragments;
62
63 /** @var array index of the right choice for each stem. */
64 public $rightchoices;
65
66 /** @var array shuffled choice indexes. */
67 protected $choiceorder;
68
69 public function init_first_step(question_attempt_step $step) {
70 foreach ($this->choices as $group => $choices) {
71 $varname = '_choiceorder' . $group;
72
73 if ($step->has_qt_var($varname)) {
74 $choiceorder = explode(',', $step->get_qt_var($varname));
75
76 } else {
77 $choiceorder = array_keys($choices);
78 if ($this->shufflechoices) {
79 shuffle($choiceorder);
80 }
81 }
82
83 foreach ($choiceorder as $key => $value) {
84 $this->choiceorder[$group][$key + 1] = $value;
85 }
86
87 if (!$step->has_qt_var($varname)) {
88 $step->set_qt_var($varname, implode(',', $this->choiceorder[$group]));
89 }
90 }
91 }
92
93 public function get_question_summary() {
43df6cac 94 $question = $this->html_to_text($this->questiontext);
0d24b17a
TH
95 $groups = array();
96 foreach ($this->choices as $group => $choices) {
97 $cs = array();
98 foreach ($choices as $choice) {
43df6cac 99 $cs[] = $this->html_to_text($choice->text);
0d24b17a
TH
100 }
101 $groups[] = '[[' . $group . ']] -> {' . implode(' / ', $cs) . '}';
102 }
103 return $question . '; ' . implode('; ', $groups);
104 }
105
106 protected function get_selected_choice($group, $shuffledchoicenumber) {
107 $choiceno = $this->choiceorder[$group][$shuffledchoicenumber];
108 return $this->choices[$group][$choiceno];
109 }
110
111 public function summarise_response(array $response) {
112 $matches = array();
113 $allblank = true;
114 foreach ($this->places as $place => $group) {
115 if (array_key_exists($this->field($place), $response) &&
116 $response[$this->field($place)]) {
43df6cac
TH
117 $choices[] = '{' . $this->html_to_text($this->get_selected_choice(
118 $group, $response[$this->field($place)])->text) . '}';
0d24b17a
TH
119 $allblank = false;
120 } else {
121 $choices[] = '{}';
122 }
123 }
124 if ($allblank) {
125 return null;
126 }
127 return implode(' ', $choices);
128 }
129
130 public function get_random_guess_score() {
131 $accum = 0;
132
133 foreach ($this->places as $placegroup) {
134 $accum += 1 / count($this->choices[$placegroup]);
135 }
136
137 return $accum / count($this->places);
138 }
139
140 public function clear_wrong_from_response(array $response) {
141 foreach ($this->places as $place => $notused) {
142 if (array_key_exists($this->field($place), $response) &&
143 $response[$this->field($place)] != $this->get_right_choice_for($place)) {
144 $response[$this->field($place)] = '0';
145 }
146 }
147 return $response;
148 }
149
150 public function get_num_parts_right(array $response) {
151 $numright = 0;
152 foreach ($this->places as $place => $notused) {
153 if (!array_key_exists($this->field($place), $response)) {
154 continue;
155 }
156 if ($response[$this->field($place)] == $this->get_right_choice_for($place)) {
157 $numright += 1;
158 }
159 }
160 return array($numright, count($this->places));
161 }
162
163 /**
462c5539 164 * @param int $key stem number
0d24b17a
TH
165 * @return string the question-type variable name.
166 */
167 public function field($place) {
168 return 'p' . $place;
169 }
170
171 public function get_expected_data() {
172 $vars = array();
173 foreach ($this->places as $place => $notused) {
174 $vars[$this->field($place)] = PARAM_INTEGER;
175 }
176 return $vars;
177 }
178
179 public function get_correct_response() {
180 $response = array();
181 foreach ($this->places as $place => $notused) {
182 $response[$this->field($place)] = $this->get_right_choice_for($place);
183 }
184 return $response;
185 }
186
187 public function get_right_choice_for($place) {
188 $group = $this->places[$place];
189 foreach ($this->choiceorder[$group] as $choicekey => $choiceid) {
190 if ($this->rightchoices[$place] == $choiceid) {
191 return $choicekey;
192 }
193 }
194 }
195
196 public function get_ordered_choices($group) {
197 $choices = array();
198 foreach ($this->choiceorder[$group] as $choicekey => $choiceid) {
199 $choices[$choicekey] = $this->choices[$group][$choiceid];
200 }
201 return $choices;
202 }
203
204 public function is_complete_response(array $response) {
205 $complete = true;
206 foreach ($this->places as $place => $notused) {
207 $complete = $complete && !empty($response[$this->field($place)]);
208 }
209 return $complete;
210 }
211
212 public function is_gradable_response(array $response) {
213 foreach ($this->places as $place => $notused) {
214 if (!empty($response[$this->field($place)])) {
215 return true;
216 }
217 }
218 return false;
219 }
220
221 public function is_same_response(array $prevresponse, array $newresponse) {
222 foreach ($this->places as $place => $notused) {
223 $fieldname = $this->field($place);
224 if (!question_utils::arrays_same_at_key_integer(
225 $prevresponse, $newresponse, $fieldname)) {
226 return false;
227 }
228 }
229 return true;
230 }
231
232 public function get_validation_error(array $response) {
233 if ($this->is_complete_response($response)) {
234 return '';
235 }
236 return get_string('pleaseputananswerineachbox', 'qtype_gapselect');
237 }
238
239 public function grade_response(array $response) {
240 list($right, $total) = $this->get_num_parts_right($response);
241 $fraction = $right / $total;
242 return array($fraction, question_state::graded_state_for_fraction($fraction));
243 }
244
245 public function compute_final_grade($responses, $totaltries) {
246 $totalscore = 0;
247 foreach ($this->places as $place => $notused) {
248 $fieldname = $this->field($place);
249
250 $lastwrongindex = -1;
251 $finallyright = false;
252 foreach ($responses as $i => $response) {
253 if (!array_key_exists($fieldname, $response) ||
254 $response[$fieldname] != $this->get_right_choice_for($place)) {
255 $lastwrongindex = $i;
256 $finallyright = false;
257 } else {
258 $finallyright = true;
259 }
260 }
261
262 if ($finallyright) {
263 $totalscore += max(0, 1 - ($lastwrongindex + 1) * $this->penalty);
264 }
265 }
266
267 return $totalscore / count($this->places);
268 }
269
270 public function classify_response(array $response) {
271 $parts = array();
272 foreach ($this->places as $place => $group) {
273 if (!array_key_exists($this->field($place), $response) ||
274 !$response[$this->field($place)]) {
275 $parts[$place] = question_classified_response::no_response();
276 continue;
277 }
278
279 $fieldname = $this->field($place);
280 $choiceno = $this->choiceorder[$group][$response[$fieldname]];
281 $choice = $this->choices[$group][$choiceno];
282 $parts[$place] = new question_classified_response(
43df6cac 283 $choiceno, $this->html_to_text($choice->text),
0d24b17a
TH
284 $this->get_right_choice_for($place) == $response[$fieldname]);
285 }
286 return $parts;
287 }
e45d37bb
TH
288
289 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
290 if ($component == 'question' && in_array($filearea,
291 array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'))) {
292 return $this->check_combined_feedback_file_access($qa, $options, $filearea);
293
294 } else if ($component == 'question' && $filearea == 'hint') {
295 return $this->check_hint_file_access($qa, $options, $args);
296
297 } else {
298 return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
299 }
300 }
0d24b17a 301}