-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathquestiontestform.php
More file actions
146 lines (120 loc) · 5.51 KB
/
questiontestform.php
File metadata and controls
146 lines (120 loc) · 5.51 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
<?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 file defines the editing form for editing question tests.
*
* @package qtype_stack
* @copyright 2012 the Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
/**
* The editing form for editing STACK question tests.
*
* @package qtype_stack
* @copyright 2012 the Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_stack_question_test_form extends moodleform {
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
protected function definition() {
$mform = $this->_form;
$question = $this->_customdata['question'];
$mform->addElement('text', 'description', stack_string('description'), ['size' => 64]);
$mform->setType('description', PARAM_RAW);
// Inputs.
$mform->addElement('header', 'inputsheader', stack_string('testinputs'));
foreach ($question->inputs as $input) {
// We do not require these to be filled in, (or contain valid input), as the teacher may want to test such cases.
$input->add_to_moodleform_testinput($mform);
}
$mform->addElement('submit', 'teacher', stack_string('teacheranswercase'));
$mform->registerNoSubmitButton('teacher');
$mform->addElement('submit', 'complete', stack_string('completetestcase'));
$mform->registerNoSubmitButton('complete');
// Expected outcome.
$mform->addElement('header', 'prtsheader', stack_string('expectedoutcomes'));
$allinputs = array_keys($question->inputs);
foreach ($question->prts as $prtname => $prt) {
$inputsused = array_keys($question->get_cached('required')[$prtname]);
$inputsused = ': [' . implode(', ' , $inputsused) . ']';
$elements = [
$mform->createElement('text', $prtname . 'score',
stack_string('score'), ['size' => 2]),
$mform->createElement('text', $prtname . 'penalty',
stack_string('penalty'), ['size' => 2]),
$mform->createElement('select', $prtname . 'answernote',
stack_string('answernote'), $prt->get_all_answer_notes()),
];
$mform->addGroup($elements, $prtname . 'group', $prtname . $inputsused, ' ', false);
$mform->setType($prtname . 'score', PARAM_RAW);
$mform->setType($prtname . 'penalty', PARAM_RAW);
$mform->setType($prtname . 'answernote', PARAM_RAW);
}
// Submit buttons.
$this->add_action_buttons(true, $this->_customdata['submitlabel']);
}
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
public function definition_after_data() {
if ($this->_form->exportValue('complete')) {
$this->complete_passing_testcase();
}
if ($this->_form->exportValue('teacher')) {
$this->complete_teacher_testcase();
}
}
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
protected function complete_passing_testcase() {
$mform = $this->_form;
$question = $this->_customdata['question'];
$inputs = [];
foreach ($question->inputs as $inputname => $input) {
$inputs[$inputname] = $mform->exportValue($inputname);
}
$response = stack_question_test::compute_response($question, $inputs);
foreach ($question->prts as $prtname => $prt) {
$result = $question->get_prt_result($prtname, $response, false);
$answernotes = $result->get_answernotes();
// In automatic test case generation set penalties as the default unless they differ.
// If they are the same as the detault, and you want this, you can change it later.
$prtpenalty = $result->get_penalty();
if ($prtpenalty == $question->penalty) {
$prtpenalty = '';
}
$mform->getElement($prtname . 'group')->setValue([
$prtname . 'score' => $result->get_score(),
$prtname . 'penalty' => $prtpenalty,
$prtname . 'answernote' => end($answernotes),
]);
}
}
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
protected function complete_teacher_testcase() {
$mform = $this->_form;
$question = $this->_customdata['question'];
$inputs = [];
foreach ($question->inputs as $inputname => $input) {
$ta = $input->get_teacher_answer_testcase();
$mform->getElement($inputname)->setValue($ta);
}
}
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
}