forked from maths/moodle-qtype_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.php
144 lines (120 loc) · 5.34 KB
/
deploy.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
<?php
// This file is part of Stack - http://stack.bham.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.
*
* @copyright 2012 the Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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);
// Load the necessary data.
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST);
$question = question_bank::load_question($questionid);
// Process any other URL parameters, and do require_login.
list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($question);
// Check permissions.
question_require_capability_on($questiondata, 'edit');
require_sesskey();
// Initialise $PAGE.
$nexturl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams);
$PAGE->set_url($nexturl); // Since this script always ends in a redirect.
$PAGE->set_heading($COURSE->fullname);
$PAGE->set_pagelayout('admin');
// Process deploy if applicable.
$deploy = optional_param('deploy', null, PARAM_INT);
if (!is_null($deploy)) {
$question->deploy_variant($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);
}
$deploy = optional_param('deploymany', null, PARAM_INT);
$deploytxt = optional_param('deploymany', null, PARAM_TEXT);
if (!is_null($deploy)) {
if (0 == $deploy) {
$nexturl->param('deployfeedbackerr', stack_string('deploymanyerror', array('err' => $deploytxt)));
redirect($nexturl);
}
if ($deploy > 100) {
$nexturl->param('deployfeedbackerr', stack_string('deploytoomanyerror'));
redirect($nexturl);
}
$maxfailedattempts = 3;
$failedattempts = 0;
$numberdeployed = 0;
while ($failedattempts < $maxfailedattempts && $numberdeployed < $deploy) {
// 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);
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 = array();
$allpassed = true;
foreach ($testscases as $key => $testcase) {
$testresults[$key] = $testcase->test_question($quba, $question, $seed);
if (!$testresults[$key]->passed()) {
$nexturl->param('seed', $seed);
$nexturl->param('deployfeedback', stack_string('deploymanysuccess', array('no' => $numberdeployed)));
$nexturl->param('deployfeedbackerr', stack_string('stackInstall_testsuite_fail'));
redirect($nexturl);
}
}
// Actually deploy the question.
$question->deploy_variant($seed);
$numberdeployed++;
}
}
$nexturl->param('deployfeedback', stack_string('deploymanysuccess', array('no' => $numberdeployed)));
$nexturl->param('seed', $seed);
if ($failedattempts >= $maxfailedattempts) {
$nexturl->param('deployfeedbackerr', stack_string('deploymanynonew'));
}
redirect($nexturl);
}