forked from itamart/moodle-mod_dataform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_form.php
461 lines (394 loc) · 18.3 KB
/
mod_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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?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/>.
/**
* @package mod_dataform
* @copyright 2013 Itamar Tzadok {@link http://substantialmethods.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* The Dataform has been developed as an enhanced counterpart
* of Moodle's Database activity module (1.9.11+ (20110323)).
* To the extent that Dataform code corresponds to Database code,
* certain copyrights on the Database module may obtain.
*/
defined('MOODLE_INTERNAL') or die;
require_once("$CFG->dirroot/course/moodleform_mod.php");
class mod_dataform_mod_form extends moodleform_mod {
public function definition() {
$mform = &$this->_form;
$this->add_action_buttons();
$this->definition_general();
$this->definition_appearance();
$this->definition_timing();
$this->definition_entry_settings();
$this->definition_grading();
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
/**
*
*/
protected function definition_general() {
global $CFG;
$mform = &$this->_form;
$mform->addElement('header', 'general', get_string('general', 'form'));
// Name.
$mform->addElement('text', 'name', get_string('name'), array('size' => '64'));
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->setDefault('name', get_string('dataformnew', 'dataform'));
// Intro.
$this->standard_intro_elements(false, get_string('description'));
}
/**
*
*/
protected function definition_appearance() {
global $COURSE;
// We want to hide that when using the singleactivity course format because it is confusing.
if (!$this->courseformat->has_view_page()) {
return;
}
$mform = &$this->_form;
$mform->addElement('header', 'coursedisplayhdr', get_string('appearance'));
// Activity icon.
$options = array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes, 'maxfiles' => 1, 'accepted_types' => array('image'));
$draftitemid = file_get_submitted_draft_itemid('activityicon');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_dataform', 'activityicon', 0, $options);
$mform->addElement('filemanager', 'activityicon', get_string('activityicon', 'dataform'), null, $options);
$mform->setDefault('activityicon', $draftitemid);
$mform->addHelpButton('activityicon', 'activityicon', 'mod_dataform');
// Displayed view.
$options = array(0 => get_string('choosedots'));
if ($this->_instance) {
if ($views = mod_dataform_view_manager::instance($this->_instance)->views_menu) {
$options = $options + $views;
}
}
$mform->addElement('select', 'inlineview', get_string('inlineview', 'mod_dataform'), $options);
$mform->addHelpButton('inlineview', 'inlineview', 'mod_dataform');
// Embedded.
$mform->addElement('selectyesno', 'embedded', get_string('embedded', 'mod_dataform'));
$mform->addHelpButton('embedded', 'embedded', 'mod_dataform');
$mform->disabledIf('embedded', 'inlineview', 'eq', 0);
}
/**
*
*/
protected function definition_timing() {
$mform = &$this->_form;
$mform->addElement('header', 'timinghdr', get_string('timing', 'dataform'));
// Time available.
$mform->addElement('date_time_selector', 'timeavailable', get_string('timeavailable', 'dataform'), array('optional' => true));
$mform->addHelpButton('timeavailable', 'timeavailable', 'mod_dataform');
// Time due.
$mform->addElement('date_time_selector', 'timedue', get_string('timedue', 'dataform'), array('optional' => true));
$mform->addHelpButton('timedue', 'timedue', 'mod_dataform');
$mform->disabledIf('timedue', 'interval', 'gt', 0);
// Activity interval.
$mform->addElement('duration', 'timeinterval', get_string('timeinterval', 'dataform'));
$mform->addHelpButton('timeinterval', 'timeinterval', 'mod_dataform');
$mform->disabledIf('timeinterval', 'timeavailable[enabled]', 'notchecked');
$mform->disabledIf('timeinterval', 'timedue[enabled]', 'checked');
// Number of intervals.
$mform->addElement('select', 'intervalcount', get_string('intervalcount', 'dataform'), array_combine(range(1, 100), range(1, 100)));
$mform->setDefault('intervalcount', 1);
$mform->addHelpButton('intervalcount', 'intervalcount', 'mod_dataform');
$mform->disabledIf('intervalcount', 'timeavailable[enabled]', 'notchecked');
$mform->disabledIf('intervalcount', 'timedue[enabled]', 'checked');
$mform->disabledIf('intervalcount', 'timeinterval[number]', 'eq', '');
$mform->disabledIf('intervalcount', 'timeinterval[number]', 'eq', 0);
}
/**
*
*/
protected function definition_entry_settings() {
global $CFG;
$mform = &$this->_form;
$mform->addElement('header', 'entrysettingshdr', get_string('entries', 'dataform'));
if ($CFG->dataform_maxentries > 0) {
// Admin limit, select from dropdown.
$maxoptions = (array_combine(range(0, $CFG->dataform_maxentries), range(0, $CFG->dataform_maxentries)));
// Max entries.
$mform->addElement('select', 'maxentries', get_string('entriesmax', 'dataform'), $maxoptions);
$mform->setDefault('maxentries', $CFG->dataform_maxentries);
// Required entries.
$mform->addElement('select', 'entriesrequired', get_string('entriesrequired', 'dataform'), array(0 => get_string('none')) + $maxoptions);
} else {
// No limit or no entries.
$admindeniesentries = (int) !$CFG->dataform_maxentries;
$mform->addElement('hidden', 'admindeniesentries', $admindeniesentries);
$mform->setType('admindeniesentries', PARAM_INT);
// Max entries.
$mform->addElement('text', 'maxentries', get_string('entriesmax', 'dataform'));
$mform->setDefault('maxentries', -1);
$mform->addRule('maxentries', null, 'numeric', null, 'client');
$mform->setType('maxentries', PARAM_INT);
$mform->disabledIf('maxentries', 'admindeniesentries', 'eq', 1);
// Required entries.
$mform->addElement('text', 'entriesrequired', get_string('entriesrequired', 'dataform'));
$mform->setDefault('entriesrequired', 0);
$mform->addRule('entriesrequired', null, 'numeric', null, 'client');
$mform->setType('entriesrequired', PARAM_INT);
$mform->disabledIf('entriesrequired', 'admindeniesentries', 'eq', 1);
}
$mform->addHelpButton('maxentries', 'entriesmax', 'mod_dataform');
$mform->addHelpButton('entriesrequired', 'entriesrequired', 'mod_dataform');
// Force separate participants.
$mform->addElement('selectyesno', 'individualized', get_string('separateparticipants', 'dataform'));
$mform->addHelpButton('individualized', 'separateparticipants', 'dataform');
// Force group entries.
$mform->addElement('selectyesno', 'grouped', get_string('groupentries', 'dataform'));
$mform->disabledIf('grouped', 'groupmode', 'eq', 0);
$mform->addHelpButton('grouped', 'groupentries', 'mod_dataform');
// Force anonymous entries.
if ($CFG->dataform_anonymous) {
$mform->addElement('selectyesno', 'anonymous', get_string('anonymousentries', 'dataform'));
$mform->setDefault('anonymous', 0);
$mform->addHelpButton('anonymous', 'anonymousentries', 'mod_dataform');
} else {
$mform->addElement('hidden', 'anonymous', 0);
$mform->setType('anonymous', PARAM_INT);
$mform->addElement('static', 'anonymousna', get_string('anonymousentries', 'dataform'), get_string('notapplicable', 'dataform'));
$mform->addHelpButton('anonymousna', 'anonymousentries', 'mod_dataform');
}
// Time limit to manage an entry.
$mform->addElement('text', 'timelimit', get_string('entrytimelimit', 'dataform'));
$mform->setType('timelimit', PARAM_INT);
$mform->setDefault('timelimit', -1);
$mform->addRule('timelimit', null, 'numeric', null, 'client');
$mform->addHelpButton('timelimit', 'entrytimelimit', 'mod_dataform');
}
/**
*
*/
protected function definition_grading() {
global $CFG;
$dataformid = !empty($this->current->id) ? $this->current->id : 0;
$mform = &$this->_form;
$displaymultilink = false;
if ($CFG->dataform_multigradeitems) {
if ($dataformid) {
$gitems = grade_item::fetch_all(array(
'itemtype' => 'mod',
'itemmodule' => 'dataform',
'iteminstance' => $dataformid,
'courseid' => $this->current->course
));
if ($gitems and count($gitems) > 1) {
$displaymultilink = true;
}
}
}
if ($displaymultilink) {
$mform->addElement('header', 'modstandardgrade', get_string('grade'));
$urlparams = array('id' => $this->current->update);
$url = new \moodle_url('/mod/dataform/grade/items.php', $urlparams);
$label = get_string('usegradeitemsform', 'mod_dataform', $url->out(false));
$mform->addElement('html', $label);
} else {
$this->standard_grading_coursemodule_elements();
$mform->setDefault('grade', 0);
$grademan = new \mod_dataform_grade_manager($dataformid);
// Grading rubric/guide.
$gradeguide = null;
if ($grademan->get_form_definition_grading_areas($mform, 'gradeguide', 'gradecalc')) {
$gradeguide = 'gradeguide';
$mform->disabledIf('gradeguide', 'grade[modgrade_type]', 'eq', 'none');
}
// Grading formula.
$grademan->get_form_definition_grading_calc($mform, 'gradecalc', $gradeguide);
$mform->disabledIf('gradecalc', 'grade[modgrade_type]', 'eq', 'none');
}
}
/**
*
*/
public function add_completion_rules() {
$mform =& $this->_form;
// Required entries.
$group = array();
$group[] =& $mform->createElement('checkbox', 'completionentriesenabled', '', get_string('completionentries', 'dataform'));
$group[] =& $mform->createElement('text', 'completionentries', '', array('size' => 3));
$mform->setType('completionentries', PARAM_INT);
$mform->addGroup($group, 'completionentriesgroup', get_string('completionentriesgroup', 'dataform'), array(' '), false);
$mform->disabledIf('completionentries', 'completionentriesenabled', 'notchecked');
// Required specific grade.
$specificgradestr = get_string('completionspecificgrade', 'dataform');
$specificgradegroupstr = get_string('completionspecificgradegroup', 'dataform');
$group = array();
$group[] = &$mform->createElement('checkbox', 'completionspecificgradeenabled', '', $specificgradestr);
$options = array();
if (!empty($this->current->grade)) {
if ($this->current->grade > 0) {
$range = range($this->current->grade, 1);
$options = array_combine($range, $range);
} else {
// Custom scale.
$scale = grade_scale::fetch(array('id' => -$this->current->grade));
$options = $scale->load_items();
}
}
$group[] = &$mform->createElement('select', 'completionspecificgrade', null, $options);
$mform->addGroup($group, 'completionspecificgradegroup', $specificgradegroupstr, array(' '), false);
$mform->disabledIf('completionspecificgradeenabled', 'grade', 'eq', 0);
$mform->disabledIf('completionspecificgrade', 'completionspecificgradeenabled', 'notchecked');
return array(
'completionentriesgroup',
'completionspecificgradegroup',
);
}
/**
*
*/
public function completion_rule_enabled($data) {
$completionentries = (!empty($data['completionentriesenabled']) and $data['completionentries'] != 0);
$completionspecificgrade = (!empty($data['completionspecificgradeenabled']) and $data['completionspecificgrade'] != 0);
return ($completionentries or $completionspecificgrade);
}
/**
*
*/
public function data_preprocessing(&$data) {
$data = (array) $data;
parent::data_preprocessing($data);
// Set up the completion checkboxes which aren't part of standard data.
$data['completionentriesenabled'] = (int) !empty($data['completionentries']);
$data['completionspecificgradeenabled'] = (int) !empty($data['completionspecificgrade']);
// Set up the grade calc and grade guide.
if ($this->_instance) {
$df = \mod_dataform_dataform::instance($this->_instance);
if ($gradeitems = $df->grade_items) {
if (!empty($gradeitems[0]['ca'])) {
$data['gradecalc'] = $gradeitems[0]['ca'];
}
if (!empty($gradeitems[0]['ru'])) {
$data['gradeguide'] = $gradeitems[0]['ru'];
}
}
}
}
/**
*
*/
public function set_data($data) {
$this->data_preprocessing($data);
parent::set_data($data);
}
/**
*
*/
public function get_data() {
$data = parent::get_data();
if (!$data) {
return false;
}
if (empty($data->timeavailable)) {
unset($data->timeinterval);
$data->intervalcount = 1;
}
if (empty($data->timeinterval)) {
$data->intervalcount = 1;
}
// Turn off completion settings if the checkboxes aren't ticked.
if (!empty($data->completionunlocked)) {
$autocompletion = (!empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC);
if (empty($data->completionentriesenabled) or !$autocompletion) {
$data->completionentries = 0;
}
if (empty($data->completionspecificgradeenabled) or !$autocompletion) {
$data->completionspecificgrade = 0;
}
if (empty($data->grade) and !empty($data->completionspecificgradeenabled)) {
unset($data->completionspecificgradeenabled);
$data->completionspecificgrade = 0;
}
if (!empty($data->grade) and !empty($data->completionspecificgrade)) {
if ($data->grade > 0) {
if ($data->completionspecificgrade > $data->grade) {
$data->completionspecificgrade = $data->grade;
}
} else {
// Custom scale.
$scale = grade_scale::fetch(array('id' => -$data->grade));
$numitems = count($scale->load_items());
if ($data->completionspecificgrade > $numitems) {
$data->completionspecificgrade = $numitems;
}
}
}
}
// Grade items.
$gradeitems = array();
if ($this->_instance) {
$gradeitems = \mod_dataform_dataform::instance($this->_instance)->grade_items;
}
$gradeitems[0] = empty($gradeitems[0]) ? array() : $gradeitems[0];
$updategradeitems = false;
// Check grade calc change.
$thiscalc = !empty($gradeitems[0]['ca']) ? $gradeitems[0]['ca'] : null;
$gradecalc = !empty($data->gradecalc) ? $data->gradecalc : null;
if ($thiscalc != $gradecalc) {
if (!$gradecalc) {
unset($gradeitems[0]['ca']);
} else {
$gradeitems[0]['ca'] = $gradecalc;
}
$updategradeitems = true;
}
// Check grade guide change.
$thisguide = !empty($gradeitems[0]['ru']) ? $gradeitems[0]['ru'] : null;
$gradeguide = !empty($data->gradeguide) ? $data->gradeguide : null;
if ($thisguide != $gradeguide) {
if (!$gradeguide) {
unset($gradeitems[0]['ru']);
} else {
$gradeitems[0]['ru'] = $gradeguide;
}
$updategradeitems = true;
}
if ($updategradeitems) {
$data->gradeitems = $gradeitems[0] ? serialize($gradeitems) : null;
}
return $data;
}
/**
*
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Completion.
if (!empty($errors['completion'])) {
return $errors;
}
$autocomletion = (isset($data['completion']) and $data['completion'] == COMPLETION_TRACKING_AUTOMATIC);
if ($autocomletion) {
// Automatic on-view completion can not work together with 'Inline view' option.
if (!empty($data['completionview']) and !empty($data['inlineview'])) {
$errors['completion'] = get_string('noautocompletioninline', 'mod_dataform');
}
}
return $errors;
}
protected function init_features() {
parent::init_features();
// Deny the advancedgrading so that it doesn't appear in the activity settings form.
$this->_features->advancedgrading = false;
}
}