-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathquestiondashboard.class.php
More file actions
349 lines (324 loc) · 14.5 KB
/
Copy pathquestiondashboard.class.php
File metadata and controls
349 lines (324 loc) · 14.5 KB
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
<?php
// This file is part of Stack - http://stack.maths.ed.ac.uk/
//
// Stack 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.
//
// Stack 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 STACK. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/questiontest.php');
require_once(__DIR__ . '/bulktester.class.php');
/**
* Loads and manipulates data for display on the STACK dashboard page.
*
* @package qtype_stack
* @copyright 2026 University of Edinburgh.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class stack_question_dashboard {
/**
* @var object Question being analysed.
*/
public $question;
/**
* @var object Question context.
*/
public $context;
/**
* @var object Question usage by activity.
*/
public $quba;
/**
* @var int Question slot
*/
public $slot;
/**
* @var object Variant progress bar.
*/
public $variantprogress;
/**
* @var object Test progress bar.
*/
public $testprogress;
/**
* Constructor
* @param object $question
* @param int|null $seed
* @param object $context
*/
public function __construct(object $question, int|null $seed, object $context) {
$this->question = $question;
$this->context = $context;
// We hard-wire decimals to be a full stop when testing questions.
$this->question->options->set_option('decimals', '.');
// Create the question usage we will use.
$this->quba = question_engine::make_questions_usage_by_activity('qtype_stack', $this->context);
$this->quba->set_preferred_behaviour('adaptive');
if (!is_null($seed)) {
// This is a bit of a hack to force the question to use a particular seed,
// even if it is not one of the deployed seeds.
$this->question->seed = $seed;
}
$this->slot = $this->quba->add_question($this->question, $this->question->defaultmark);
$this->quba->start_question($this->slot);
// Need to save question usage to allow display of files.
question_engine::save_questions_usage_by_activity($this->quba);
$this->question->castextprocessor = new castext2_qa_processor($this->quba->get_question_attempt($this->slot));
}
/**
* Get the basic display information for a question
* @return StdClass
*/
public function question_details() {
global $PAGE;
$renderer = $PAGE->get_renderer('qtype_stack');
$output = new StdClass();
$output->name = $this->question->name;
$generalfeedback = $this->question->get_generalfeedback_castext();
$output->rendergeneralfeedback = $renderer->general_feedback($this->quba->get_question_attempt($this->slot));
$output->generalfeedbackerr = $generalfeedback->get_errors();
$output->seed = $this->question->seed;
if (isset($this->question->metadata)) {
$output->metadata = $this->question->metadata;
}
$questiondescription = $this->question->get_questiondescription_castext();
$output->renderquestiondescription = $renderer->question_description($this->quba->get_question_attempt($this->slot));
// Not currently displayed.
$output->questiondescription = trim($questiondescription->get_errors());
// Prepare the display options.
$options = question_display_options();
// Store a rendered version of the blank question here.
// Runtime errors generated by test cases might change rendering later.
$output->renderquestion = $this->quba->render_question($this->slot, $options);
$output->questionnote = stack_ouput_castext($this->question->get_question_summary());
$output->questionvariablevalues = $this->question->get_question_session_keyval_representation();
$output->upgradeerrors = $this->question->validate_against_stackversion($this->context);
// Display a representation of the PRT for offline use.
$offlinemaxima = [];
foreach ($this->question->prts as $prt) {
$obj = new stdClass();
$obj->name = $prt->get_name();
$obj->prt = $prt->get_maxima_representation();
$offlinemaxima[] = $obj;
}
$output->prts = $offlinemaxima;
$output->stackversion = ($this->question->stackversion == null) ?
stack_string('stackversionnone') :
stack_string('stackversionedited', $this->question->stackversion) . ' ' .
stack_string('stackversionnow', get_config('qtype_stack', 'version'));
return $output;
}
/**
* Create the default test for a question assuming teacher's anser is correct.
* @return boolean
*/
public function create_default_test() {
$testscases = question_bank::get_qtype('stack')->load_question_tests($this->question->id);
if (empty($testscases) && $this->question->inputs !== []) {
$defaulttest = stack_bulk_tester::create_default_test($this->question);
question_bank::get_qtype('stack')->save_question_test($this->question->id, $defaulttest);
return true;
}
return false;
}
/**
* Set up progress bars for the dashboard.
* @return void
*/
public function create_progress_bars() {
$this->testprogress = new progress_bar('testingquestiontests', 500, true);
if (!empty($this->question->deployedseeds)) {
$this->variantprogress = new progress_bar('testingquestionvariants', 500, true);
}
}
/**
* Run all the test cases and return display information.
* @return StdClass
*/
public function run_test_cases() {
$output = new StdClass();
$output->results = [];
$output->demotest = false;
$output->allpassed = true;
$output->runtimeerrors = implode('<br />', array_keys($this->question->runtimeerrors));
$output->hasrandomvariants = $this->question->has_random_variants() ? true : false;
// Load the list of test cases.
$testscases = question_bank::get_qtype('stack')->load_question_tests($this->question->id);
$output->hasinputs = ($this->question->inputs !== []) ? true : false;
// Create a fake (unsaved) test if we don't have any. We use the demotest flag
// to let the mustache template know our one test is not real.
if (empty($testscases) && $output->hasinputs) {
$testscases[stack_string('runquestiontests_example')] = stack_bulk_tester::create_default_test($this->question);
$output->demotest = true;
}
$progressevery = (int) min(max(1, count($testscases) / 200), 100);
$a = ['total' => count($testscases), 'done' => 0];
// Execute the tests.
foreach ($testscases as $key => $testcase) {
// Summary output is used to create the summary table with test number, description and
// pass/fail for score, penalty and answer note.
$summaryoutput = new StdClass();
$summaryoutput->number = $testcase->testcase;
$summaryoutput->id = 'testcase-' . $testcase->testcase . '-' . $this->question->id;
$summaryoutput->description = $testcase->description;
$summaryoutput->scorepass = true;
$summaryoutput->penaltypass = true;
$summaryoutput->notepass = true;
$output->results[$key] = $testcase->test_question($this->question->id, $this->question->seed, $this->context);
$currentsummary = $output->results[$key]->passed_with_reasons();
if (!$currentsummary['passed']) {
$output->allpassed = false;
foreach ($currentsummary['outcomes'] as $prtoutcome) {
// If we've already failed a criterion we don't need to test it again.
if ($summaryoutput->scorepass && str_contains($prtoutcome['reason'], stack_string('score'))) {
$summaryoutput->scorepass = false;
}
if ($summaryoutput->penaltypass && str_contains($prtoutcome['reason'], stack_string('penalty'))) {
$summaryoutput->penaltypass = false;
}
if ($summaryoutput->notepass && str_contains($prtoutcome['reason'], stack_string('answernote'))) {
$summaryoutput->notepass = false;
}
}
}
$output->summary[] = $summaryoutput;
$a['done'] += 1;
if ($a['done'] % $progressevery == 0 || $a['done'] == $a['total']) {
core_php_time_limit::raise(60);
$this->testprogress->update($a['done'], $a['total'], get_string('testingquestiontests', 'qtype_stack', $a));
}
}
$output->hasresults = (!empty($output->results) && !$output->demotest) ? true : false;
return $output;
}
/**
* Get general variant information and details for each individual variant.
*
* @return StdClass
*/
public function list_variants() {
$output = new StdClass();
$output->duplicateerror = false;
$questionnotes = [];
$output->notes = [];
$output->deployedcount = 0;
$output->seed = $this->question->seed;
$output->hasrandomvariants = $this->question->has_random_variants() ? true : false;
// If the question does not have random variants, act as if we have a current seed that's
// been deployed.
$output->seedmatched = !$output->hasrandomvariants;
// Is the current variant (not necessarily seed) deployed?
$output->variantdeployed = false;
if (!empty($this->question->deployedseeds)) {
$output->deployedcount = count($this->question->deployedseeds);
$a = ['total' => $output->deployedcount, 'done' => 0];
$progressevery = (int) min(max(1, count($this->question->deployedseeds) / 500), 100);
foreach ($this->question->deployedseeds as $key => $deployedseed) {
$variant = $this->get_variant($key, $deployedseed);
if ($variant->iscurrentseed) {
$output->seedmatched = true;
}
if ($variant->samenoteascurrent) {
$output->variantdeployed = true;
}
$output->notes[] = $variant;
$questionnotes[] = $variant->questionnote;
$a['done'] += 1;
if ($a['done'] % $progressevery == 0 || $a['done'] == $a['total']) {
core_php_time_limit::raise(60);
$this->variantprogress->update(
$a['done'],
$a['total'],
get_string('testingquestionvariants', 'qtype_stack', $a)
);
}
}
} else {
$previewurl =
qbank_previewquestion\helper::question_preview_url($this->question->id, null, null, null, null, $this->context);
$output->previewurl = $previewurl->out();
}
usort($output->notes, [self::class, 'sort_by_note']);
if (count($questionnotes) != count(array_flip($questionnotes))) {
$output->duplicateerror = true;
}
return $output;
}
/**
* Get the details of a particular variant
* @param int $key the array key of the variant in the question
* @param string $deployedseed the seed
* @return StdClass
*/
public function get_variant($key, $deployedseed) {
global $PAGE;
$output = new StdClass();
$output->iscurrentseed = false;
$output->isdeployed = false;
$output->deployedseed = $deployedseed;
if (!is_null($this->question->seed) && $this->question->seed == $deployedseed) {
$output->iscurrentseed = true;
}
$previewurl = qbank_previewquestion\helper::question_preview_url(
$this->question->id,
null,
null,
null,
$key + 1,
$this->context
);
$output->previewurl = $previewurl->out();
$qurl = new moodle_url($PAGE->url, ['seed' => $deployedseed]);
$output->qurl = $qurl->out();
$bulktestresults = [false, ''];
if (optional_param('testall', null, PARAM_INT)) {
// Bulk test all variants.
$bulktester = new stack_bulk_tester();
// Load the list of test cases.
$testscases = question_bank::get_qtype('stack')->load_question_tests($this->question->id);
$bulktestresults = $bulktester->qtype_stack_test_question(
$this->context,
$this->question->id,
$testscases,
'web',
$deployedseed,
true
);
}
// Print out question notes of all deployed variants.
$qn = question_bank::load_question($this->question->id);
$qn->seed = (int) $deployedseed;
$cn = $qn->get_context();
$qunote = question_engine::make_questions_usage_by_activity('qtype_stack', $cn);
$qunote->set_preferred_behaviour('adaptive');
$slotnote = $qunote->add_question($qn, $qn->defaultmark);
$qunote->start_question($slotnote);
// Check for duplicate question notes.
$output->questionnote = $qn->get_question_summary();
$output->samenoteascurrent =
($output->questionnote == $this->question->get_question_summary()) ? true : false;
$output->questionnoterendered = stack_ouput_castext($output->questionnote);
$output->bulktestresultspass = $bulktestresults[0] ? true : false;
$output->bulktestresults = $bulktestresults[1];
return $output;
}
/**
* Sorting function for sorting by note
* @param mixed $a1
* @param mixed $b1
* @return int
*/
public static function sort_by_note($a1, $b1) {
$a = $a1->questionnoterendered;
$b = $b1->questionnoterendered;
return strcoll($a, $b);
}
}