-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgradesettings_form.php
269 lines (221 loc) · 13.1 KB
/
gradesettings_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?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/>.
/**
* Grade settings form
*
* @package report_trainingsessions
* @category report
* @version moodle 2.x
* @author Valery Fremaux (valery.fremaux@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot.'/report/trainingsessions/locallib.php');
require_once($CFG->dirroot.'/report/trainingsessions/lib.php');
class TrainingsessionsGradeSettingsForm extends moodleform {
protected $linkablemodules;
public function definition() {
global $COURSE, $OUTPUT;
$rt = \report\trainingsessions\trainingsessions::instance();
$this->linkablemodules = $rt->get_linkable_modules($COURSE->id);
$config = get_config('report_trainingsessions');
$mform = $this->_form;
$mform->addElement('hidden', 'id', $COURSE->id);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'from');
$mform->setType('from', PARAM_INT);
$mform->addElement('hidden', 'to');
$mform->setType('to', PARAM_INT);
$mform->addElement('header', 'coursegradehead', get_string('coursegrade', 'report_trainingsessions'));
$formgroup = array();
$formgroup[] = &$mform->createElement('checkbox', 'coursegrade', get_string('addcoursegrade', 'report_trainingsessions'));
$formgroup[] = &$mform->createElement('text', 'courselabel', '', array('size' => 60, 'maxlength' => 60));
$mform->setType('courselabel', PARAM_TEXT);
$label = get_string('enablecoursescore', 'report_trainingsessions');
$seps = array(get_string('courselabel', 'report_trainingsessions').' ');
$mform->addGroup($formgroup, 'coursegroup', $label, $seps, false);
$mform->addElement('header', 'modulegrades', get_string('modulegrades', 'report_trainingsessions'));
$mform->addHelpButton('modulegrades', 'modulegrades', 'report_trainingsessions');
/*
* The linked modules portion goes here, but is forced in in the 'definition_after_data' function so
* that we can get any elements added in the form and not overwrite them with what's in the database.
*/
$mform->addElement('submit', 'addmodule', get_string('addmodulelabel', 'report_trainingsessions'),
array('title' => get_string('addmoduletitle', 'report_trainingsessions')));
$mform->registerNoSubmitButton('addmodule');
if (report_trainingsessions_supports_feature('calculation/specialgrades')) {
$mform->addElement('header', 'specialgrades', get_string('specialgrades', 'report_trainingsessions'));
$mform->addElement('html', $OUTPUT->box_start('trainingsessions-fieldset'));
$label = get_string('noextragrade', 'report_trainingsessions');
$mform->addElement('radio', 'specialgrade', '', $label, TR_TIMEGRADE_DISABLED);
$label = get_string('addtimegrade', 'report_trainingsessions');
$mform->addElement('radio', 'specialgrade', '', $label, TR_TIMEGRADE_GRADE);
$options = array(TR_GRADE_MODE_BINARY => get_string('binary', 'report_trainingsessions'),
TR_GRADE_MODE_DISCRETE => get_string('discrete', 'report_trainingsessions'),
TR_GRADE_MODE_CONTINUOUS => get_string('continuous', 'report_trainingsessions'));
$mform->addElement('select', 'timegrademode', get_string('timegrademode', 'report_trainingsessions'), $options);
$mform->setDefault('timegrademode', $config->timegrademodedefault);
$mform->disabledIf('timegrademode', 'specialgrade', 'neq', TR_TIMEGRADE_GRADE);
$mform->addHelpButton('timegrademode', 'grademodes', 'report_trainingsessions');
$label = get_string('addtimebonus', 'report_trainingsessions');
$mform->addElement('radio', 'specialgrade', '', $label, TR_TIMEGRADE_BONUS);
$options = array(TR_GRADE_MODE_DISCRETE => get_string('discrete', 'report_trainingsessions'),
TR_GRADE_MODE_CONTINUOUS => get_string('continuous', 'report_trainingsessions'));
$mform->addElement('select', 'bonusgrademode', get_string('bonusgrademode', 'report_trainingsessions'), $options);
$mform->setDefault('bonusgrademode', $config->bonusgrademodedefault);
$mform->disabledIf('bonusgrademode', 'specialgrade', 'neq', TR_TIMEGRADE_BONUS);
$mform->addHelpButton('timegrademode', 'grademodes', 'report_trainingsessions');
$mform->addElement('html', $OUTPUT->box_end());
$options = array(TR_GRADE_SOURCE_COURSE => get_string('elapsed', 'report_trainingsessions'),
TR_GRADE_SOURCE_COURSE_EXT => get_string('extelapsed', 'report_trainingsessions'),
TR_GRADE_SOURCE_ACTIVITIES => get_string('activitytime', 'report_trainingsessions'));
$mform->addElement('select', 'timegradesource', get_string('timesource', 'report_trainingsessions'), $options);
$mform->setDefault('timegradesource', $config->timegradesourcedefault);
$mform->disabledIf('timegradesource', 'specialgrade', 'eq', TR_TIMEGRADE_DISABLED);
$mform->addElement('modgrade', 'timegrade', get_string('timegrade', 'report_trainingsessions'));
$attrs = array('size' => 80, 'maxlength' => 254);
$mform->addElement('text', 'timegraderanges', get_string('timegraderanges', 'report_trainingsessions'), $attrs);
$mform->addHelpButton('timegraderanges', 'timegraderanges', 'report_trainingsessions');
$mform->setType('timegraderanges', PARAM_TEXT);
}
if (report_trainingsessions_supports_feature('xls/calculated')) {
// Preliminary implementation. Not finished yet.
$label = get_string('calculatedcolumns', 'report_trainingsessions');
$mform->addElement('header', 'calculatedcolumnshead', $label, '');
$formulastr = get_string('xlsformula', 'report_trainingsessions');
$formulalabelstr = get_string('formulalabel', 'report_trainingsessions');
for ($i = 1; $i <= 3; $i++) {
$attrs = array('cols' => 60, 'rows' => 4, 'maxlength' => 254);
$mform->addElement('textarea', 'calculated'.$i, $formulastr.' '.$i, $attrs);
$mform->setType('calculated'.$i, PARAM_RAW);
$mform->addHelpButton('calculated'.$i, 'calculated', 'report_trainingsessions');
$attrs = array('size' => 20, 'maxlength' => 254);
$mform->addElement('text', 'calculated'.$i.'label', $formulalabelstr.' '.$i, $attrs);
$mform->setType('calculated'.$i.'label', PARAM_RAW);
}
$attrs = array('size' => 80, 'maxlength' => 254);
$label = get_string('lineaggregators', 'report_trainingsessions');
$mform->addElement('text', 'lineaggregators', $label, $attrs);
$mform->addHelpButton('lineaggregators', 'lineaggregators', 'report_trainingsessions');
$mform->setType('lineaggregators', PARAM_TEXT);
}
$this->add_action_buttons(true);
}
/**
* Add the added activities portion only after the entire form has been created. That way,
* we can act on previous added values that haven't been committed to the database.
* Check for an 'addmodule' button. If the linked activities fields are all full, add an empty one.
*/
public function definition_after_data() {
global $COURSE;
// Start process core datas (conditions, etc.).
parent::definition_after_data();
/*
* This gets called more than once, and there's no way to tell which time this is, so set a
* variable to make it as called so we only do this processing once.
*/
if (!empty($this->def_after_data_done)) {
return;
}
$this->def_after_data_done = true;
$mform =& $this->_form;
$fdata = $mform->getSubmitValues();
/*
* Get the existing linked activities from the database, unless this form has resubmitted itself, in
* which case they will be in the form already.
*/
$moduleids = array();
$rt = \report\trainingsessions\trainingsessions::instance();
if (empty($fdata)) {
if ($linkedmodules = $rt->get_graded_modules($COURSE->id)) {
foreach ($linkedmodules as $cidx => $cmid) {
if ($cmid > 0) {
$moduleids[$cidx] = $cmid;
}
}
}
} else {
if (isset($fdata['moduleid'])) {
foreach ($fdata['moduleid'] as $cidx => $cmid) {
if ($cmid > 0) {
$moduleids[$cidx] = $cmid;
}
}
}
}
if (isset($fdata['linkablemodules']) && is_array($fdata['linkablemodules'])) {
foreach ($fdata['linkablemodules'] as $linkablemodule) {
$moduleids[] = $linkablemodule;
}
}
$moduleids = array_unique($moduleids);
$ix = 0;
foreach ($moduleids as $cidx => $modid) {
$formgroup = array();
$choices = array(
'' => get_string('disabled', 'report_trainingsessions'),
$modid => $this->linkablemodules[$modid]
);
$formgroup[] = &$mform->createElement('select', 'moduleid['.$ix.']', '', $choices);
$mform->setDefault('moduleid['.$ix.']', $modid);
$formgroup[] = & $mform->createElement('text', 'scorelabel['.$ix.']', '', array('maxlength' => 60));
$mform->setType('scorelabel['.$ix.']', PARAM_TEXT);
$label = get_string('modgrade', 'report_trainingsessions', ($ix + 1));
$padding = array(' '.get_string('columnname', 'report_trainingsessions'));
$group =& $mform->createElement('group', 'modgrade'.$ix, $label, $formgroup, $padding, false);
$mform->insertElementBefore($group, 'addmodule');
$ix++;
}
$availablemodules = $this->linkablemodules;
unset($availablemodules[0]);
$label = get_string('availableactivities', 'report_trainingsessions');
$linkablemodules = $mform->createElement('select', 'linkablemodules', $label, $availablemodules, array('size' => 10));
$linkablemodules->setMultiple(true);
$formgroup[] = $linkablemodules;
$mform->insertElementBefore($linkablemodules, 'addmodule');
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (($data['specialgrade'] == TR_TIMEGRADE_GRADE) &&
($data['timegrademode'] == TR_GRADE_MODE_CONTINUOUS) &&
($data['timegrade'] < 0)) {
// Scales cannot be used in continuous mode.
$errors['timegrademode'] = get_string('errorcontinuousscale', 'report_trainingsessions');
$errors['timegrade'] = get_string('errorcontinuousscale', 'report_trainingsessions');
}
if (($data['specialgrade'] == TR_TIMEGRADE_BONUS) &&
($data['bonusgrademode'] == TR_GRADE_MODE_CONTINUOUS) &&
($data['timegrade'] < 0)) {
// Scales cannot be used in continuous mode.
$errors['bonusgrademode'] = get_string('errorcontinuousscale', 'report_trainingsessions');
$errors['timegrade'] = get_string('errorcontinuousscale', 'report_trainingsessions');
}
if (($data['specialgrade'] == TR_TIMEGRADE_GRADE) &&
($data['timegrademode'] < TR_GRADE_MODE_CONTINUOUS) &&
(empty($data['timegraderanges']))) {
$errors['timegrademode'] = get_string('errordiscretenoranges', 'report_trainingsessions');
$errors['timegraderanges'] = get_string('errordiscretenoranges', 'report_trainingsessions');
}
if (($data['specialgrade'] == TR_TIMEGRADE_BONUS) &&
($data['bonusgrademode'] < TR_GRADE_MODE_CONTINUOUS) &&
(empty($data['timegraderanges']))) {
$errors['bonusgrademode'] = get_string('errordiscretenoranges', 'report_trainingsessions');
$errors['timegraderanges'] = get_string('errordiscretenoranges', 'report_trainingsessions');
}
return $errors;
}
}