forked from maths/moodle-qtype_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questiontestform.php
129 lines (104 loc) · 4.67 KB
/
questiontestform.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
<?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.
*
* @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 question tests.
*
* @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 {
protected function definition() {
$mform = $this->_form;
$question = $this->_customdata['question'];
// 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 = $prt->get_required_variables($allinputs);
$inputsused = ': [' . implode(', ' , $inputsused) . ']';
$elements = array(
$mform->createElement('text', $prtname . 'score',
stack_string('score'), array('size' => 2)),
$mform->createElement('text', $prtname . 'penalty',
stack_string('penalty'), array('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']);
}
public function definition_after_data() {
if ($this->_form->exportValue('complete')) {
$this->complete_passing_testcase();
}
if ($this->_form->exportValue('teacher')) {
$this->complete_teacher_testcase();
}
}
protected function complete_passing_testcase() {
$mform = $this->_form;
$question = $this->_customdata['question'];
$inputs = array();
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->answernotes;
$mform->getElement($prtname . 'group')->setValue(array(
$prtname . 'score' => $result->score,
$prtname . 'penalty' => $result->penalty,
$prtname . 'answernote' => end($answernotes)));
}
}
protected function complete_teacher_testcase() {
$mform = $this->_form;
$question = $this->_customdata['question'];
$inputs = array();
foreach ($question->inputs as $inputname => $input) {
$ta = $input->get_teacher_answer_testcase();
$mform->getElement($inputname)->setValue($ta);
}
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
}