mirrored from git://git.moodle.org/moodle.git
/
datasetdefinitions_form.php
195 lines (178 loc) · 8.01 KB
/
datasetdefinitions_form.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
191
192
193
194
195
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the editing form for the calculated question data set definitions.
*
* @package qtype
* @subpackage calculated
* @copyright 2007 Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Calculated question data set definitions editing form definition.
*
* @copyright 2007 Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_dataset_dependent_definitions_form extends moodleform {
/**
* Question object with options and answers already loaded by get_question_options
* Be careful how you use this it is needed sometimes to set up the structure of the
* form in definition_inner but data is always loaded into the form with set_defaults.
*
* @var object
*/
protected $question;
/**
* Reference to question type object
*
* @var question_dataset_dependent_questiontype
*/
protected $qtypeobj;
/**
* Add question-type specific form fields.
*
* @param MoodleQuickForm $mform the form being built.
*/
public function __construct($submiturl, $question) {
global $DB;
$this->question = $question;
$this->qtypeobj = $QTYPES[$this->question->qtype];
// Validate the question category.
if (!$category = $DB->get_record('question_categories',
array('id' => $question->category))) {
print_error('categorydoesnotexist', 'question', $returnurl);
}
$this->category = $category;
$this->categorycontext = get_context_instance_by_id($category->contextid);
parent::__construct($submiturl);
}
protected function definition() {
global $SESSION;
$mform = $this->_form;
$possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
$mandatorydatasets = array();
if (isset($this->question->options->answers)) {
foreach ($this->question->options->answers as $answer) {
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
}
} else {
foreach ($SESSION->calculated->questionform->answers as $answer) {
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
}
}
$key = 0;
$datadefscat= array();
$datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
$datasetmenus = array();
$label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
// explaining the role of datasets so other strings can be shortened
$mform->addElement('html', $label);
$mform->addElement('header', 'mandatoryhdr',
get_string('mandatoryhdr', 'qtype_calculated'));
$labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
foreach ($mandatorydatasets as $datasetname) {
if (!isset($datasetmenus[$datasetname])) {
list($options, $selected) =
$this->qtypeobj->dataset_options($this->question, $datasetname);
unset($options['0']); // Mandatory...
$label = get_string('wildcard', 'qtype_calculated').
" <strong>$datasetname</strong> ";
$mform->addElement('select', "dataset[$key]", $label, $options);
if (isset($datadefscat[$datasetname])) {
$mform->addElement('static', "there is a category",
get_string('sharedwildcard', 'qtype_calculated', $datasetname),
get_string('dataitemdefined', 'qtype_calculated',
$datadefscat[$datasetname]));
}
$mform->setDefault("dataset[$key]", $selected);
$datasetmenus[$datasetname] = '';
$key++;
}
}
$mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
foreach ($possibledatasets as $datasetname) {
if (!isset($datasetmenus[$datasetname])) {
list($options, $selected) = $this->qtypeobj->dataset_options(
$this->question, $datasetname, false);
$label = get_string('wildcard', 'qtype_calculated') .
" <strong>$datasetname</strong> ";
$mform->addElement('select', "dataset[$key]", $label, $options);
if (isset($datadefscat[$datasetname])) {
$mform->addElement('static', "there is a category",
get_string('sharedwildcard', 'qtype_calculated', $datasetname),
get_string('dataitemdefined', 'qtype_calculated',
$datadefscat[$datasetname]));
}
$mform->setDefault("dataset[$key]", $selected);
$datasetmenus[$datasetname] = '';
$key++;
}
}
// temporary strings
$mform->addElement('header', 'synchronizehdr',
get_string('synchronize', 'qtype_calculated'));
$mform->addElement('radio', 'synchronize', '',
get_string('synchronizeno', 'qtype_calculated'), 0);
$mform->addElement('radio', 'synchronize', '',
get_string('synchronizeyes', 'qtype_calculated'), 1);
$mform->addElement('radio', 'synchronize', '',
get_string('synchronizeyesdisplay', 'qtype_calculated'), 2);
if (isset($this->question->options) &&
isset($this->question->options->synchronize)) {
$mform->setDefault('synchronize', $this->question->options->synchronize);
} else {
$mform->setDefault('synchronize', 0);
}
$this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
// Hidden elements
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
$mform->setDefault('returnurl', 0);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'category');
$mform->setType('category', PARAM_RAW);
$mform->setDefault('category', array('contexts' => array($this->categorycontext)));
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
$mform->setDefault('courseid', 0);
$mform->addElement('hidden', 'cmid');
$mform->setType('cmid', PARAM_INT);
$mform->setDefault('cmid', 0);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'wizard', 'datasetitems');
$mform->setType('wizard', PARAM_ALPHA);
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$datasets = $data['dataset'];
$countvalid = 0;
foreach ($datasets as $key => $dataset) {
if ($dataset != '0') {
$countvalid++;
}
}
if (!$countvalid) {
foreach ($datasets as $key => $dataset) {
$errors['dataset['.$key.']'] =
get_string('atleastonerealdataset', 'qtype_calculated');
}
}
return $errors;
}
}