-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathquestiontestrun.php
More file actions
294 lines (268 loc) · 12.3 KB
/
Copy pathquestiontestrun.php
File metadata and controls
294 lines (268 loc) · 12.3 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
<?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/>.
/**
* This script lets the user test a question using any question tests defined
* in the database. It also displays some of the internal workings of questions.
*
* Users with moodle/question:view capability can use this script to view the
* results of the tests.
*
* Users with moodle/question:edit can edit the test cases and deployed variant,
* as well as just run them.
*
* The script takes one parameter id which is a questionid as a parameter.
* In can optionally also take a random seed.
*
* @package qtype_stack
* @copyright 2012 the Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/questionlib.php');
require_once(__DIR__ . '/vle_specific.php');
require_once(__DIR__ . '/locallib.php');
require_once(__DIR__ . '/stack/questiontest.php');
require_once(__DIR__ . '/stack/bulktester.class.php');
require_once(__DIR__ . '/stack/questiondashboard.class.php');
// Get the parameters from the URL.
$questionid = required_param('questionid', PARAM_INT);
$courseid = optional_param('courseid', null, PARAM_INT);
$cmid = optional_param('cmid', null, PARAM_INT);
$historic = optional_param('historic', false, PARAM_BOOL);
[$latestqversion, $latestquestionid, $qbeid, $versions] = get_latest_question_version($questionid);
if (!$historic) {
$questionid = $latestquestionid;
}
// Load the necessary data.
$questiondata = question_bank::load_question_data($questionid);
if (!$questiondata) {
throw new stack_exception('questiondoesnotexist');
}
$question = question_bank::load_question($questionid);
$questioncategory = $DB->get_record('question_categories', ['id' => $questiondata->category], 'id,name');
// Process any other URL parameters, and do require_login.
[$context, $seed, $urlparams] = qtype_stack_setup_question_test_page($question);
// Check permissions.
question_require_capability_on($questiondata, 'view');
$caneditpermission = question_has_capability_on($questiondata, 'edit');
$canedit = $caneditpermission && !$historic;
// Initialise $PAGE.
$PAGE->set_url('/question/type/stack/questiontestrun.php', $urlparams);
$title = stack_string('testingquestion', format_string($question->name));
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->set_pagelayout('popup');
require_login();
$dashboard = new stack_question_dashboard($question, $seed, $context);
// Create some other useful links.
$qbankparams = $urlparams;
unset($qbankparams['questionid']);
unset($qbankparams['seed']);
$editparams = $qbankparams;
$editparams['id'] = $question->id;
$qbankparams['qperpage'] = 1000; // Should match MAXIMUM_QUESTIONS_PER_PAGE but that constant is not easily accessible.
$qbankparams['category'] = $questiondata->category . ',' . $question->contextid;
$qbankparams['lastchanged'] = $question->id;
if (property_exists($questiondata, 'hidden') && $questiondata->hidden) {
$qbankparams['showhidden'] = 1;
}
$todoparams = $qbankparams;
$todoparams['contextid'] = $question->contextid;
$exportparams = $urlparams;
$exportparams['id'] = $question->id;
$sesskey = sesskey();
$questionbanklinkedit = new moodle_url('/question/type/stack/questioneditlatest.php', $editparams);
$questionxmllink = new moodle_url('/question/type/stack/questionxmledit.php', $editparams);
$questionbanklink = new moodle_url('/question/edit.php', $qbankparams);
$exportquestionlink = new moodle_url('/question/bank/exporttoxml/exportone.php', $exportparams);
$exportquestionlink->param('sesskey', $sesskey);
$todolink = new moodle_url('/question/type/stack/adminui/todo.php', $todoparams);
$reportlink = new moodle_url('/question/type/stack/questiontestreport.php', $urlparams);
$bulktestlink = new moodle_url('/question/type/stack/questionbulktest.php', $urlparams);
$pagelink = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams);
$historyparams = $urlparams;
unset($historyparams['questionid']);
$historyparams['entryid'] = $qbeid;
$historyparams['returnurl'] = $pagelink->out(false);
$historylink = new moodle_url('/question/bank/history/history.php', $historyparams);
// We've chosen not to send a specific seed since it is helpful to test the general feedback in a random context.
$chatparams = $urlparams;
$chatparams['initialise'] = true;
$chatlink = new moodle_url('/question/type/stack/adminui/caschat.php', $chatparams);
// Start output.
echo $OUTPUT->header();
// Create object containing the display information.
$initialdata = new StdClass();
$initialdata->question = $dashboard->question_details();
$initialdata->general = new Stdclass();
// Create default test if requested and save it to the question.
if (optional_param('defaulttestcase', null, PARAM_INT) && $canedit && $question->inputs !== []) {
$initialdata->general->testcreated = $dashboard->create_default_test();
} else {
$initialdata->general->testcreated = false;
}
// Add all the display information that's based on this page's paramaters.
$initialdata->question->versions = [];
$versionparams = $urlparams;
$versionparams['questionid'] = $latestquestionid;
$versionlink = new moodle_url('/question/type/stack/questiontestrun.php', $versionparams);
$latestoption = new StdClass();
$latestoption->version = stack_string('latest');
$latestoption->url = $versionlink;
$latestoption->active = false;
$initialdata->question->versions[] = $latestoption;
$versionparams['historic'] = true;
foreach ($versions as $v => $id) {
$option = new StdClass();
$option->version = $v;
$versionparams['questionid'] = $id;
$versionlink = new moodle_url('/question/type/stack/questiontestrun.php', $versionparams);
$option->url = $versionlink;
$option->active = ($id == $questionid) ? true : false;
$initialdata->question->versions[] = $option;
};
$initialdata->general->editquestionlink = $questionbanklinkedit->out();
$initialdata->general->editxmllink = $questionxmllink->out();
$initialdata->general->questionbanklink = $questionbanklink->out();
$initialdata->general->chatlink = $chatlink->out();
$initialdata->general->tidylink = $question->qtype->get_tidy_question_url($question);
$initialdata->general->exportquestionlink = $exportquestionlink->out();
$initialdata->general->reportlink = $reportlink->out();
$initialdata->general->todolink = $todolink->out();
$initialdata->general->bulktestlink = $bulktestlink->out();
$initialdata->general->historylink = $historylink->out();
$initialdata->general->caneditpermission = $caneditpermission;
$initialdata->general->canedit = $canedit;
$initialdata->general->courseid = $courseid;
$initialdata->general->cmid = $cmid;
$initialdata->general->questionid = $questionid;
$initialdata->general->seed = $seed;
$initialdata->general->categoryname = $questioncategory ? $questioncategory->name ?? '' : '';
$coursefordisplayid = $courseid;
if (!$coursefordisplayid && $context->contextlevel == CONTEXT_COURSE) {
$coursefordisplayid = $context->instanceid;
}
if (!$coursefordisplayid && $context->contextlevel == CONTEXT_MODULE) {
$cm = get_coursemodule_from_id('', $context->instanceid, 0, false, 'id,course');
if ($cm) {
$coursefordisplayid = $cm->course;
}
}
$initialdata->general->coursename = '';
if ($coursefordisplayid) {
$courserecord = $DB->get_record('course', ['id' => $coursefordisplayid], 'id,fullname');
if ($courserecord) {
$initialdata->general->coursename = $courserecord->fullname ?? '';
}
}
$initialdata->general->hidetests = optional_param('hidetests', '', PARAM_INT);
// Output the progress bars first.
$dashboard->create_progress_bars();
// Render the navigation links and low cost question information before running tests and processing variants.
echo $OUTPUT->render_from_template('qtype_stack/questiontestrun', $initialdata);
flush();
// Create test data.
$initialdata->tests = $dashboard->run_test_cases();
$testeditlink = new moodle_url('/question/type/stack/questiontestedit.php', $urlparams);
$initialdata->tests->testeditlink = $testeditlink->out();
$defaulttestlink = new moodle_url(
'/question/type/stack/questiontestrun.php',
array_merge($urlparams, ['defaulttestcase' => '1', 'sesskey' => $sesskey])
);
$initialdata->tests->defaulttestlink = $defaulttestlink->out();
$initialdata->tests->output = [];
$initialdata->tests->hasheadlineerror =
($initialdata->question->generalfeedbackerr || $initialdata->tests->runtimeerrors) ? true : false;
// Create display info for individual tests.
foreach ($initialdata->tests->results as $key => $result) {
$test = new StdClass();
$test->output = $result->html_output($question, $key);
$test->number = $key;
$testeditlink = new moodle_url('/question/type/stack/questiontestedit.php', array_merge($urlparams, ['testcase' => $key]));
$testconfirmlink = new moodle_url(
'/question/type/stack/questiontestedit.php',
array_merge($urlparams, ['testcase' => $key, 'confirmthistestcase' => true])
);
$testdeletelink = new moodle_url('/question/type/stack/questiontestdelete.php', array_merge($urlparams, ['testcase' => $key]));
$test->editlink = $testeditlink->out();
$test->confirmlink = $testconfirmlink->out();
$test->deletelink = $testdeletelink->out();
$test->canedit = $canedit;
$initialdata->tests->output[] = $test;
}
// We no longer need the in-depth results.
unset($initialdata->tests->results);
// Display test information.
echo $OUTPUT->render_from_template('qtype_stack/questiontestruntests', $initialdata);
flush();
// Create variant data for display.
$variantdata = $dashboard->list_variants();
$variantdata->deployfeedback = optional_param('deployfeedback', null, PARAM_TEXT);
$variantdata->deployfeedbackerr = optional_param('deployfeedbackerr', null, PARAM_TEXT);
if ($variantdata->deployfeedback || $variantdata->deployfeedbackerr) {
$PAGE->set_url('/question/type/stack/questiontestrun.php#variants-pane', $urlparams);
}
$variantdata->canedit = $canedit;
$variantdata->passed = 0;
$variantdata->failed = 0;
// Create varaiant action links.
foreach ($variantdata->notes as $variant) {
$variant->canedit = $canedit;
$vdeletelink = new moodle_url(
'/question/type/stack/deploy.php',
$urlparams + ['undeploy' => $variant->deployedseed, 'sesskey' => $sesskey],
'variants-pane'
);
$variant->deletelink = $vdeletelink->out();
if ($variant->bulktestresultspass) {
$variantdata->passed += 1;
} else {
$variantdata->failed += 1;
}
}
$deploylink = new moodle_url(
'/question/type/stack/deploy.php',
[],
'variants-pane'
);
$variantdata->deploylink = $deploylink->out();
$undeployalllink = new moodle_url(
'/question/type/stack/deploy.php',
$urlparams + ['undeployall' => true, 'sesskey' => $sesskey],
'variants-pane'
);
$variantdata->undeployalllink = $undeployalllink->out();
$pagelink = new moodle_url('/question/type/stack/questiontestrun.php', []);
$variantdata->pagelink = $pagelink->out();
$variantdata->courseid = $courseid;
$variantdata->cmid = $cmid;
$variantdata->questionid = $questionid;
$variantdata->sesskey = $sesskey;
$variantdata->historic = $historic ? 1 : 0;
$variantdata->newseed = mt_rand();
$variantdata->deployedseeds = $question->deployedseeds;
$variantdata->hastests = optional_param('testall', null, PARAM_INT);
$testalllink = new moodle_url(
'/question/type/stack/questiontestrun.php',
$urlparams + ['testall' => '1', 'sesskey' => $sesskey, 'nocache' => time(), 'historic' => $historic, 'hidetests' => 1],
'variants-pane'
);
$variantdata->testalllink = $testalllink->out();
$variantdata->questionnote = $initialdata->question->questionnote;
// Finally about variant data.
echo $OUTPUT->render_from_template('qtype_stack/questiontestrunvariants', $variantdata);
echo $OUTPUT->footer();