-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathdeploy.php
More file actions
368 lines (324 loc) · 13.5 KB
/
Copy pathdeploy.php
File metadata and controls
368 lines (324 loc) · 13.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?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 handles the various deploy/undeploy actions from questiontestrun.php.
*
* @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__ . '/locallib.php');
// Get the parameters from the URL.
$questionid = required_param('questionid', PARAM_INT);
[$qversion, $questionid] = get_latest_question_version($questionid);
// Load the necessary data.
$questiondata = $DB->get_record('question', ['id' => $questionid], '*', MUST_EXIST);
$question = question_bank::load_question($questionid);
// Process any other URL parameters, and do require_login.
[$context, $seed, $urlparams] = qtype_stack_setup_question_test_page($question);
$PAGE->set_context($context);
// Check permissions.
question_require_capability_on($questiondata, 'edit');
require_sesskey();
// Initialise $PAGE.
$nexturl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams, 'variants-pane');
$nexturl->param('hidetests', 1);
$PAGE->set_url($nexturl); // Since this script always ends in a redirect.
$PAGE->set_heading($COURSE->fullname);
$PAGE->set_pagelayout('popup');
require_login();
// Deploying lots of variants is time consuming, and we need a progress bar in some cases.
$numforprogressbar = 10;
/**
* Run tests on a given seed and stop deployment if a test fails.
* Stop deployment if we have a question runtime error.
* @param mixed $question
* @param mixed $seed
* @param mixed $context
* @param mixed $nexturl
* @param mixed $numberdeployed Number of variants deployed so far/
* @return void
*/
function testseed($question, $seed, $context, $nexturl, $numberdeployed) {
$deployhalt = optional_param('deployhalt', null, PARAM_TEXT);
if (!isset($deployhalt)) {
return;
}
$testscases = question_bank::get_qtype('stack')->load_question_tests($question->id);
// Exectue the tests.
$testresults = [];
foreach ($testscases as $key => $testcase) {
$testresults[$key] = $testcase->test_question($question->id, $seed, $context);
if (!$testresults[$key]->passed()) {
$nexturl->param('seed', $seed);
$nexturl->param('deployfeedback', stack_string('deploymanysuccess', ['no' => $numberdeployed]));
$nexturl->param('deployfeedbackerr', stack_string('stackInstall_testsuite_fail'));
redirect($nexturl);
}
}
}
// Process deploy if applicable.
$deploy = optional_param('deploy', null, PARAM_INT);
$deploybtn = optional_param('deploysinglebtn', null, PARAM_TEXT);
if (!is_null($deploy) && $deploybtn) {
testseed($question, $deploy, $context, $nexturl, 0);
$question->deploy_variant($deploy);
$nexturl->param('seed', $deploy);
redirect($nexturl);
}
// Process undeploy if applicable.
$undeploy = optional_param('undeploy', null, PARAM_INT);
if (!is_null($undeploy)) {
$question->undeploy_variant($undeploy);
// As we redirect, switch to the undeployed variant, so it easy to re-deploy if you just made a mistake.
$nexturl->param('seed', $undeploy);
redirect($nexturl);
}
// Process undeploy selected if applicable.
$undeployselected = optional_param('deleteselectedbtn', null, PARAM_TEXT);
if (!is_null($undeployselected)) {
foreach ($question->deployedseeds as $deployedseed) {
$selected = optional_param("selectvariant-{$deployedseed}", null, PARAM_TEXT);
if (isset($selected)) {
$question->undeploy_variant($deployedseed);
}
}
$nexturl->param('seed', $seed);
redirect($nexturl);
}
// Process undeployall if applicable.
$undeploy = optional_param('undeployall', null, PARAM_INT);
if (!is_null($undeploy) && $question->deployedseeds) {
foreach ($question->deployedseeds as $seed) {
$question->undeploy_variant($seed);
}
$nexturl->param('seed', $seed);
redirect($nexturl);
}
$listbtn = optional_param('deployfromlistbtn', null, PARAM_TEXT);
$systbtn = optional_param('deploysystbtn', null, PARAM_TEXT);
$manybtn = optional_param('deploymanybtn', null, PARAM_TEXT);
// Process undeployall if applicable.
$deployfromlist = optional_param('deployfromlist', null, PARAM_TEXT);
$deploysystematic = optional_param('deploysystematic', null, PARAM_INT);
$deploysystematicfrom = optional_param('deploysystematicfrom', null, PARAM_INT);
$deploysystematicto = optional_param('deploysystematicto', null, PARAM_INT);
$usefromtofeature = false;
if (
($deployfromlist && $listbtn) ||
($deploysystematicfrom && $deploysystematicto && $systbtn)
) {
// Check data integrity.
$dataproblem = false;
if ($deployfromlist && $listbtn) {
$deploytxt = optional_param('deployfromlist', null, PARAM_TEXT);
$baseseeds = explode("\n", trim($deploytxt));
} else {
$baseseeds = range($deploysystematicfrom, $deploysystematicto);
$usefromtofeature = true;
}
$newseeds = [];
foreach ($baseseeds as $newseed) {
// Now also explode over commas.
$newseed = explode(",", trim($newseed));
foreach ($newseed as $seed) {
// Clean up whitespace.
// Force the entry to be a positive integer.
if (trim($seed) !== '') {
$seed = (int) (trim($seed));
if ($seed <= 0) {
$dataproblem = true;
} else {
$newseeds[] = (string) ($seed);
}
}
}
}
// No action to take?
if ($newseeds === $question->deployedseeds) {
redirect($nexturl);
}
if (count($newseeds) > 100) {
$nexturl->param('deployfeedbackerr', stack_string('deploymanyerror', ['err' => count($newseeds)]));
redirect($nexturl);
}
// Check the entries are all different.
if (count($newseeds) !== count(array_flip($newseeds))) {
// TO-DO: specific feedback for each error.
$dataproblem = true;
}
if ($dataproblem) {
$nexturl->param('deployfeedbackerr', stack_string('deployfromlisterror'));
redirect($nexturl);
}
// If deploying a list and user has selected to delete existing variants,
// undeploy all existing variants not on the list.
$deleteexisting = optional_param('deleteexisting', null, PARAM_TEXT);
if ($deleteexisting && $listbtn) {
foreach ($question->deployedseeds as $seed) {
if (!in_array($seed, $newseeds)) {
$question->undeploy_variant($seed);
}
}
}
// Deploy all new variants.
$numberdeployed = 0;
$newseeds = array_diff($newseeds, $question->deployedseeds);
$deployhalt = optional_param('deployhalt', null, PARAM_TEXT);
$newcount = count($newseeds);
// Output something if we need a progress bar.
// (We have 10+ variants and we are running the tests.)
if ($newcount >= $numforprogressbar && isset($deployhalt)) {
echo $OUTPUT->header();
flush();
$a = ['total' => $newcount, 'done' => 0];
$progressevery = (int) min(max(1, $newcount / 500), 100);
$pbar = new progress_bar('deployedprogress', 500, true);
}
foreach ($newseeds as $seed) {
testseed($question, $seed, $context, $nexturl, $numberdeployed);
$question->deploy_variant($seed);
$numberdeployed++;
if ($newcount >= $numforprogressbar && isset($deployhalt)) {
$a['done'] += 1;
if ($a['done'] % $progressevery == 0 || $a['done'] == $a['total']) {
core_php_time_limit::raise(60);
$pbar->update($a['done'], $a['total'], get_string('deployedprogress', 'qtype_stack', $a));
}
}
}
$nexturl->param('deployfeedback', stack_string('deploymanysuccess', ['no' => $numberdeployed]));
redirect($nexturl);
}
$deploy = optional_param('deploymany', null, PARAM_INT);
$deploytxt = optional_param('deploymany', null, PARAM_TEXT);
$starttime = time();
// The number of seconds we devote to deploying before moving on. Prevents system hangging.
// Note, in "safe mode" the set time limit function has no effect.
$maxtime = 180;
core_php_time_limit::raise($maxtime); // Prevent PHP timeouts.
gc_collect_cycles(); // Because PHP's default memory management is rubbish.
if (!is_null($deploy) && $manybtn) {
if (0 == $deploy) {
$nexturl->param('deployfeedbackerr', stack_string('deploymanyerror', ['err' => $deploytxt]));
redirect($nexturl);
}
if ($deploy > 100) {
$nexturl->param('deployfeedbackerr', stack_string('deploytoomanyerror'));
redirect($nexturl);
}
$maxfailedattempts = 10;
$failedattempts = 0;
$numberdeployed = 0;
$errmessage = [];
$allok = true;
// Output something if we need a progress bar.
if ($deploy >= $numforprogressbar) {
echo $OUTPUT->header();
flush();
$a = ['total' => $deploy, 'done' => 0];
$progressevery = (int) min(max(1, $deploy / 500), 100);
$pbar = new progress_bar('deployedprogress', 500, true);
}
while ($failedattempts < $maxfailedattempts && $numberdeployed < $deploy && time() - $starttime < $maxtime) {
// Genrate a new seed.
$seed = mt_rand();
$variantdeployed = false;
// Reload the question to ensure any new deployed version is included.
$question = question_bank::load_question($questionid);
$question->seed = (int) $seed;
$quba = question_engine::make_questions_usage_by_activity('qtype_stack', $context);
$quba->set_preferred_behaviour('adaptive');
$slot = $quba->add_question($question, $question->defaultmark);
$quba->start_question($slot);
// Do not deploy questions with runtime errors (could be from s_assert).
if (!empty($question->runtimeerrors)) {
$errmessage['deployruntimeerr'] = stack_string('deployruntimeerr', ['no' => $seed]);
$allok = false;
continue;
}
foreach ($question->deployedseeds as $key => $deployedseed) {
$qn = question_bank::load_question($questionid);
$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 if the question note has already been deployed.
if ($qn->get_question_summary() == $question->get_question_summary()) {
$variantdeployed = true;
$failedattempts++;
}
}
if (!$variantdeployed) {
// Load the list of test cases.
$testscases = question_bank::get_qtype('stack')->load_question_tests($question->id);
// Exectue the tests.
$testresults = [];
$allpassed = true;
testseed($question, $seed, $context, $nexturl, $numberdeployed);
// Actually deploy the question.
$question->deploy_variant($seed);
$numberdeployed++;
flush();
}
if ($deploy >= $numforprogressbar) {
$a['done'] += 1;
if ($a['done'] % $progressevery == 0 || $a['done'] == $a['total']) {
core_php_time_limit::raise(60);
$pbar->update($a['done'], $a['total'], get_string('deployedprogress', 'qtype_stack', $a));
}
}
}
// If we quit the while loop early we should set progress to 100%.
if ($deploy >= $numforprogressbar) {
$pbar->update($a['total'], $a['total'], get_string('deployedprogress', 'qtype_stack', $a));
}
$message = stack_string('deploymanysuccess', ['no' => $numberdeployed]);
$nexturl->param('deployfeedback', $message);
$nexturl->param('seed', $seed);
if ($failedattempts >= $maxfailedattempts) {
$allok = false;
$errmessage[] = stack_string('deploymanynonew');
$nexturl->param('deployfeedbackerr', implode(' ', $errmessage));
if ($deploy < $numforprogressbar) {
redirect($nexturl);
}
}
if (time() - $starttime >= $maxtime) {
$allok = false;
$errmessage[] = stack_string('deployoutoftime', ['time' => time() - $starttime]);
$nexturl->param('deployfeedbackerr', implode(' ', $errmessage));
if ($deploy < $numforprogressbar) {
redirect($nexturl);
}
}
if ($deploy >= $numforprogressbar) {
\core\notification::success(stack_string('deploymanysuccess', ['no' => $numberdeployed]));
if (!$allok) {
echo html_writer::tag('p', implode(' ', $errmessage), ['class' => 'overallresult fail']);
}
echo $OUTPUT->continue_button($nexturl);
echo $OUTPUT->footer();
exit;
}
redirect($nexturl);
}
redirect($nexturl);