-
Notifications
You must be signed in to change notification settings - Fork 2
/
firstquestionselector.php
352 lines (318 loc) · 10.8 KB
/
firstquestionselector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class firstquestionselector.
*
* @package local_catquiz
* @copyright 2024 Wunderbyte GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_catquiz\teststrategy\preselect_task;
use local_catquiz\catcontext;
use local_catquiz\catquiz;
use local_catquiz\catscale;
use local_catquiz\local\model\model_item_param_list;
use local_catquiz\local\model\model_person_param_list;
use local_catquiz\local\model\model_responses;
use local_catquiz\local\model\model_strategy;
use local_catquiz\local\result;
use local_catquiz\local\status;
use local_catquiz\teststrategy\preselect_task;
use local_catquiz\teststrategy\progress;
use local_catquiz\wb_middleware;
use moodle_exception;
/**
* Test strategy firstquestionselector.
*
* @package local_catquiz
* @copyright 2024 Wunderbyte GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class firstquestionselector extends preselect_task implements wb_middleware {
/**
* @var int
*/
const MINIMUM_PARAMS_FOR_ESTIMATE = 50;
/**
* @var int
*/
const LEVEL_VERYEASY = -2;
/**
* @var int
*/
const LEVEL_EASY = -1;
/**
* @var int
*/
const LEVEL_NORMAL = 0;
/**
* @var int
*/
const LEVEL_DIFFICULT = 1;
/**
* @var int
*/
const LEVEL_VERYDIFFICULT = 2;
/**
* @var progress
*/
private progress $progress;
/**
* Run preselect task.
*
* @param array $context
* @param callable $next
*
* @return result
*
*/
public function run(array &$context, callable $next): result {
$this->progress = $context['progress'];
// Don't do anything if this is not the first question of the current attempt.
if (!$this->progress->is_first_question()) {
return $next($context);
}
// In the classic test, we do not change how the first question is selected.
if ($context['teststrategy'] == LOCAL_CATQUIZ_STRATEGY_CLASSIC) {
return $next($context);
}
if ($context['questions_ordered_by'] !== 'difficulty') {
return result::err(status::ERROR_FETCH_NEXT_QUESTION);
}
// If we select the first question based on its difficulty, then it can
// never be a pilot question.
$questionswithdifficulty = array_filter($context['questions'], fn($q) => !$q->is_pilot);
if (count($questionswithdifficulty) === 0) {
return result::err(status::ERROR_EMPTY_FIRST_QUESTION_LIST);
} else if (count($questionswithdifficulty) === 1) {
return result::ok($questionswithdifficulty[array_keys($questionswithdifficulty)[0]]);
}
$context['questions'] = $questionswithdifficulty;
if ($context['firstquestion_use_existing_data']) {
// We already have a person param for this user, so use it.
if ($this->has_ability()) {
return $next($context);
}
}
// User does not have an ability yet, so we try to take the average ability.
$meanability = $this->get_mean_ability();
if ($meanability === null) {
$startability = $this->set_start_ability($context['selectfirstquestion'], 0.0, 1.0);
$context['person_ability'][$this->context['catscaleid']] = $startability;
$context['progress']->set_ability($startability, $context['catscaleid']);
$context['se'][$this->context['catscaleid']] = 1.0;
return $next($context);
}
$items = $this->get_items();
$se = catscale::get_standarderror($meanability, $items, 1.0);
$context['person_ability'][$this->context['catscaleid']] = $this->set_start_ability(
$context['selectfirstquestion'],
$meanability,
$se
);
$context['se'][$this->context['catscaleid']] = $se;
return $next($context);
}
/**
* Get required context keys.
*
* @return array
*
*/
public function get_required_context_keys(): array {
return [
'selectfirstquestion',
'questions_ordered_by',
'testid',
'progress',
];
}
/**
* Returns the median ability of the given person parameters.
*
* @param array $personparams
*
* @return mixed
*
*/
public function get_median_ability_of_test(array $personparams) {
if (!$personparams) {
return 0.0;
}
$abilities = array_map(fn ($param) => floatval($param->ability), $personparams);
sort($abilities);
$index = count($abilities) / 2;
if ((int) $index == $index) {
return ($abilities[array_keys($abilities)[$index - 1]] + $abilities[array_keys($abilities)[$index]]) / 2;
}
return $abilities[array_keys($abilities)[floor($index)]];
}
/**
* Get easiest question.
*
* @param mixed $questions
*
* @return mixed
*
*/
private function get_easiest_question($questions) {
return $questions[array_key_first($questions)];
}
/**
* Get first question of second quintile.
*
* @param mixed $questions
*
* @return mixed
*
*/
private function get_first_question_of_second_quintile($questions) {
$index = $this->get_index_for_quantile(0.2, count($questions));
return $questions[array_keys($questions)[$index]];
}
/**
* Get first question of second quartile.
*
* @param mixed $questions
*
* @return mixed
*
*/
private function get_first_question_of_second_quartile($questions) {
$index = $this->get_index_for_quantile(0.25, count($questions));
return $questions[array_keys($questions)[$index]];
}
/**
* Get last question of second quartile.
*
* @param mixed $questions
*
* @return mixed
*
*/
private function get_last_question_of_second_quartile($questions) {
$index3rdquartile = $this->get_index_for_quantile(0.5, count($questions));
// We want to return the question that is right before the first of the third quartile.
$index = $index3rdquartile - 1;
return $questions[array_keys($questions)[$index]];
}
/**
* Get index for quantile.
*
* @param float $quantile
* @param int $len
*
* @return int
*
*/
private function get_index_for_quantile(float $quantile, int $len) {
$index = $quantile * $len;
$index -= 1; // Because we use zero-based indexing.
if ($index == (int) $index) {
// Theoretically, the quartile value is the average of the question difficulties
// at index i and i+1: ($questions[$index] + $questions[$index+1])/2
// But we need to return a real question at an existing index position.
// In this case, we err on the easy side and return the question at the lower index.
return $index;
}
return ceil($index);
}
/**
* Gets the person params from the database.
*
* Is overwritten in a _testing class, so that we do not need a database for testing.
* @param array $context
* @return array
* @throws moodle_exception
*/
protected function get_personparams_for_adaptivequiz_test(array $context) {
return catquiz::get_personparams_for_adaptivequiz_test($context['testid']);
}
/**
* Helper to get the user ability in the main scale
*
* @return bool
*/
protected function has_ability() {
boolval(catquiz::get_person_abilities(
$this->context['contextid'],
[$this->context['catscaleid']],
$this->context['userid']
));
}
/**
* Helper function to calculate the mean ability.
*
* If there are not enough abilities for a good estimate, returns null
*/
protected function get_mean_ability() {
$abilities = catquiz::get_person_abilities(
$this->context['contextid'],
[$this->context['catscaleid']]
);
if (!$abilities || count($abilities) < self::MINIMUM_PARAMS_FOR_ESTIMATE) {
return null;
}
$meanability = array_sum(array_map(fn ($pp) => floatval($pp->ability), $abilities)) / count($abilities);
return $meanability;
}
/**
* Helper function to calculate the start ability
*
* @param string $option
* @param float $mean
* @param float $se
*
* @return float
*/
private function set_start_ability(string $option, float $mean, float $se) {
$knownlevels = [
self::LEVEL_VERYEASY,
self::LEVEL_EASY,
self::LEVEL_NORMAL,
self::LEVEL_DIFFICULT,
self::LEVEL_VERYDIFFICULT,
];
if (!in_array($option, $knownlevels)) {
throw new \Exception(sprintf("Unknown option to select first question: %s"), $option);
}
return $mean + intval($option) * $se;
}
/**
* Helper function to return the item list for the main scale
*
* @return model_item_param_list
*/
private function get_items() {
// Create item list.
$catscaleids = [$this->context['catscaleid'], ...catscale::get_subscale_ids($this->context['catscaleid'])];
$responsedata = catcontext::getresponsedatafromdb($this->context['contextid'], $catscaleids);
$responses = (new model_responses())->setdata($responsedata);
$modelstrategy = new model_strategy($responses);
$catscalecontext = catscale::get_context_id($this->context['catscaleid']);
$itemparamlists = [];
$personparams = model_person_param_list::load_from_db($catscalecontext, $catscaleids);
foreach (array_keys($modelstrategy->get_installed_models()) as $model) {
$itemparamlists[$model] = model_item_param_list::load_from_db(
$catscalecontext,
$model,
$catscaleids
);
}
$items = $modelstrategy->select_item_model($itemparamlists, $personparams);
return $items;
}
}