forked from maths/moodle-qtype_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questiontestedit.php
190 lines (158 loc) · 7.05 KB
/
questiontestedit.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
<?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 create or edit question tests for a question.
*
* @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');
require_once(__DIR__ . '/questiontestform.php');
require_once(__DIR__ . '/stack/questiontest.php');
require_once(__DIR__ . '/stack/potentialresponsetreestate.class.php');
// Get the parameters from the URL.
$questionid = required_param('questionid', PARAM_INT);
$testcase = optional_param('testcase', null, PARAM_INT);
$confirmthistestcase = optional_param('confirmthistestcase', null, PARAM_INT);
// Load the necessary data.
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST);
$question = question_bank::load_question($questionid);
if ($testcase || $confirmthistestcase) {
$qtest = question_bank::get_qtype('stack')->load_question_test($questionid, $testcase);
}
// 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');
// Work out whether we are adding or editing.
if (!is_null($testcase)) {
$title = stack_string('editingtestcase',
array('no' => $testcase, 'question' => format_string($question->name)));
$submitlabel = get_string('savechanges');
} else {
$title = stack_string('addingatestcase', format_string($question->name));
$submitlabel = stack_string('createtestcase');
}
// Create the question usage we will use.
$quba = question_engine::make_questions_usage_by_activity('qtype_stack', $context);
$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.
$question->seed = $seed;
}
$slot = $quba->add_question($question, $question->defaultmark);
$quba->start_question($slot);
// Initialise $PAGE.
$backurl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams);
if (!is_null($testcase)) {
$urlparams['testcase'] = $testcase;
}
$PAGE->set_url('/question/type/stack/questiontestedit.php', $urlparams);
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->set_pagelayout('popup');
require_login();
// Create the editing form.
$mform = new qtype_stack_question_test_form($PAGE->url,
array('submitlabel' => $submitlabel, 'question' => $question));
// Send current data to the form.
if ($testcase) {
$currentdata = new stdClass();
$currentdata->description = $qtest->description;
foreach ($qtest->inputs as $name => $value) {
$currentdata->{$name} = $value;
}
foreach ($qtest->expectedresults as $prtname => $expected) {
if (is_null($expected->score)) {
$currentdata->{$prtname . 'score'} = '';
} else {
$currentdata->{$prtname . 'score'} = $expected->score + 0; // Fix excessive DPs.
}
if (is_null($expected->penalty)) {
$currentdata->{$prtname . 'penalty'} = '';
} else {
$currentdata->{$prtname . 'penalty'} = $expected->penalty + 0; // Fix excessive DPs.
}
$currentdata->{$prtname . 'answernote'} = $expected->answernotes[0];
}
$mform->set_data($currentdata);
}
// Process the form.
if ($mform->is_cancelled()) {
unset($urlparams['testcase']);
redirect($backurl);
} else if ($confirmthistestcase) {
$inputs = array();
foreach ($qtest->inputs as $name => $value) {
$inputs[$name] = $value;
}
$qtest = new stack_question_test($qtest->description, $inputs);
$response = stack_question_test::compute_response($question, $inputs);
foreach ($question->prts as $prtname => $prt) {
$result = $question->get_prt_result($prtname, $response, false);
// For testing purposes we just take the last note.
$answernotes = $result->get_answernotes();
$answernote = array(end($answernotes));
$qtest->add_expected_result($prtname, new stack_potentialresponse_tree_state(
1, true, $result->get_score(), $result->get_penalty(), '', $answernote));
}
question_bank::get_qtype('stack')->save_question_test($questionid, $qtest, $testcase);
redirect($backurl);
} else if ($data = $mform->get_data()) {
// Process form submission.
$inputs = array();
foreach ($question->inputs as $inputname => $notused) {
$inputs[$inputname] = $data->$inputname;
}
$qtest = new stack_question_test($data->description, $inputs);
foreach ($question->prts as $prtname => $notused) {
$qtest->add_expected_result($prtname, new stack_potentialresponse_tree_state(
1, true, $data->{$prtname . 'score'}, $data->{$prtname . 'penalty'},
'', array($data->{$prtname . 'answernote'})));
}
question_bank::get_qtype('stack')->save_question_test($questionid, $qtest, $testcase);
redirect($backurl);
}
// Prepare the display options.
$options = new question_display_options();
$options->readonly = true;
$options->flags = question_display_options::HIDDEN;
$options->suppressruntestslink = true;
// Display the page.
echo $OUTPUT->header();
// Show the question read-only.
echo $quba->render_question($slot, $options);
// Display the question variables.
echo $OUTPUT->heading(stack_string('questionvariables'), 3);
echo html_writer::start_tag('div', array('class' => 'questionvariables'));
echo html_writer::tag('pre', $question->questionvariables);
echo html_writer::end_tag('div');
echo $OUTPUT->heading(stack_string('questionvariablevalues'), 3);
echo html_writer::start_tag('div', array('class' => 'questionvariables'));
echo html_writer::tag('pre', $question->get_question_session_keyval_representation());
echo html_writer::end_tag('div');
// Display the question text.
// We need this as well as the rendered view above so that teachers can see the names of variables used.
// This helps when writing question tests using those variables to reflect randomization.
echo $OUTPUT->heading(stack_string('questiontext'), 3);
echo html_writer::tag('pre', $question->questiontext, array('class' => 'questiontext'));
echo html_writer::tag('p', stack_string('testinputsimpwarning'));
// Show the form.
$mform->display();
echo $OUTPUT->footer();