-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathtidyquestionform.php
175 lines (145 loc) · 6.47 KB
/
tidyquestionform.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
<?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 used by the tidy question script.
*
* @package qtype_stack
* @copyright 2013 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');
require_once($CFG->dirroot . '/question/type/stack/stack/graphlayout/graph.php');
/**
* The editing form used by the tidy question script.
*
* @package qtype_stack
* @copyright 2013 the Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_stack_tidy_question_form extends moodleform {
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
protected function definition() {
$mform = $this->_form;
$question = $this->_customdata;
// Inputs.
$mform->addElement('header', 'inputsheader', stack_string('inputs'));
foreach ($question->inputs as $inputname => $input) {
$mform->addElement('text', 'inputname_' . $inputname,
stack_string('newnameforx', $inputname), ['size' => 20]);
$mform->setDefault('inputname_' . $inputname, $inputname);
$mform->setType('inputname_' . $inputname, PARAM_RAW); // Validated in the validation method.
}
// PRTs.
$mform->addElement('header', 'prtsheader', stack_string('prts'));
foreach ($question->prts as $prtname => $prt) {
$mform->addElement('text', 'prtname_' . $prtname,
stack_string('newnameforx', $prtname), ['size' => 20]);
$mform->setDefault('prtname_' . $prtname, $prtname);
$mform->setType('prtname_' . $prtname, PARAM_RAW); // Validated in the validation method.
}
// PRT nodes.
foreach ($question->prts as $prtname => $prt) {
$mform->addElement('header', 'prtnodesheader' . $prtname,
stack_string('prtnodesheading', $prtname));
$graph = $this->get_prt_graph($prt);
$newnames = $graph->get_suggested_node_names();
$mform->addElement('static', $prtname . 'graph', '',
stack_abstract_graph_svg_renderer::render($graph, $prtname . 'graphsvg'));
$nodes = $prt->get_nodes_summary();
uasort($nodes, fn($a, $b) => $a->nodename - $b->nodename);
foreach ($nodes as $nodekey => $notused) {
$mform->addElement('text', 'nodename_' . $prtname . '_' . $nodekey,
stack_string('newnameforx', $nodekey + 1), ['size' => 20]);
$mform->setDefault('nodename_' . $prtname . '_' . $nodekey, $newnames[$nodekey + 1]);
$mform->setType('nodename_' . $prtname . '_' . $nodekey, PARAM_INT);
}
}
// Submit buttons.
$this->add_action_buttons(true, stack_string('renamequestionparts'));
}
/**
* Get a stack_abstract_graph representation of a PRT.
* @return stack_abstract_graph.
*/
protected function get_prt_graph($prt) {
$graph = new stack_abstract_graph();
foreach ($prt->get_nodes_summary() as $nodekey => $summary) {
if ($summary->truenextnode == -1) {
$left = null;
} else {
$left = $summary->truenextnode + 1;
}
if ($summary->falsenextnode == -1) {
$right = null;
} else {
$right = $summary->falsenextnode + 1;
}
$graph->add_node($nodekey + 1, $summary->description, $left, $right,
$summary->truescoremode . stack_utils::fix_trailing_zeros($summary->truescore),
$summary->falsescoremode . stack_utils::fix_trailing_zeros($summary->falsescore));
}
$graph->layout();
return $graph;
}
// phpcs:ignore moodle.Commenting.MissingDocblock.Function
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$question = $this->_customdata;
// Inputs.
$inputnames = [];
foreach ($question->inputs as $inputname => $notused) {
$field = 'inputname_' . $inputname;
$proposedname = $data[$field];
if (!stack_utils::is_valid_name($proposedname)) {
$errors[$field] = stack_string('notavalidname');
} else if (array_key_exists($proposedname, $inputnames)) {
$errors[$field] = stack_string('namealreadyused');
} else {
$inputnames[$proposedname] = $inputname;
}
}
// PRTs.
$prtnames = [];
foreach ($question->prts as $prtname => $notused) {
$field = 'prtname_' . $prtname;
$proposedname = $data[$field];
if (!stack_utils::is_valid_name($proposedname)) {
$errors[$field] = stack_string('notavalidname');
} else if (array_key_exists($proposedname, $prtnames)) {
$errors[$field] = stack_string('namealreadyused');
} else {
$prtnames[$proposedname] = $prtname;
}
}
foreach ($question->prts as $prtname => $prt) {
$nodenames = [];
$nodes = $prt->get_nodes_summary();
foreach ($nodes as $nodekey => $notused) {
$field = 'nodename_' . $prtname . '_' . $nodekey;
$proposedname = $data[$field];
if ($proposedname < 1) {
$errors[$field] = stack_string('notavalidname');
} else if (array_key_exists($proposedname, $nodenames)) {
$errors[$field] = stack_string('namealreadyused');
} else {
$nodenames[$proposedname] = $nodekey;
}
}
}
return $errors;
}
}