MDL-53966 lesson: Allow maximum number of attempts to be unlimited
[moodle.git] / mod / lesson / mod_form.php
CommitLineData
86342d63 1<?php
0a4abb73
SH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
5f649aaa 18/**
19 * Form to define a new instance of lesson or edit an instance.
20 * It is used from /course/modedit.php.
21 *
9b24f68b 22 * @package mod_lesson
cc3dbaaa
PS
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
5f649aaa 25 **/
e24b7f85 26
1e7f8ea2 27defined('MOODLE_INTERNAL') || die();
bfebaf64 28
23a7718b 29require_once($CFG->dirroot.'/course/moodleform_mod.php');
0a4abb73 30require_once($CFG->dirroot.'/mod/lesson/locallib.php');
11bf5b83 31
f07b9627 32class mod_lesson_mod_form extends moodleform_mod {
5f649aaa 33
0a4abb73
SH
34 protected $course = null;
35
1a0df553 36 public function __construct($current, $section, $cm, $course) {
0a4abb73 37 $this->course = $course;
1a0df553
MG
38 parent::__construct($current, $section, $cm, $course);
39 }
40
41 /**
42 * Old syntax of class constructor. Deprecated in PHP7.
e04e4cdd 43 *
1a0df553
MG
44 * @deprecated since Moodle 3.1
45 */
46 public function mod_lesson_mod_form($current, $section, $cm, $course) {
47 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
48 self::__construct($current, $section, $cm, $course);
0a4abb73 49 }
2f67a9b3 50
e73457e4 51 function definition() {
c6f9f061 52 global $CFG, $COURSE, $DB, $OUTPUT;
11bf5b83 53
0a4abb73 54 $mform = $this->_form;
5f649aaa 55
a36eba45 56 $lessonconfig = get_config('mod_lesson');
d8389172 57
5f649aaa 58 $mform->addElement('header', 'general', get_string('general', 'form'));
59
0a4abb73
SH
60 /** Legacy slideshow width element to maintain backwards compatibility */
61 $mform->addElement('hidden', 'width');
62 $mform->setType('width', PARAM_INT);
a36eba45 63 $mform->setDefault('width', $lessonconfig->slideshowwidth);
0a4abb73
SH
64
65 /** Legacy slideshow height element to maintain backwards compatibility */
66 $mform->addElement('hidden', 'height');
67 $mform->setType('height', PARAM_INT);
a36eba45 68 $mform->setDefault('height', $lessonconfig->slideshowheight);
0a4abb73
SH
69
70 /** Legacy slideshow background color element to maintain backwards compatibility */
71 $mform->addElement('hidden', 'bgcolor');
72 $mform->setType('bgcolor', PARAM_TEXT);
a36eba45 73 $mform->setDefault('bgcolor', $lessonconfig->slideshowbgcolor);
0a4abb73
SH
74
75 /** Legacy media popup width element to maintain backwards compatibility */
76 $mform->addElement('hidden', 'mediawidth');
77 $mform->setType('mediawidth', PARAM_INT);
a36eba45 78 $mform->setDefault('mediawidth', $lessonconfig->mediawidth);
0a4abb73
SH
79
80 /** Legacy media popup height element to maintain backwards compatibility */
81 $mform->addElement('hidden', 'mediaheight');
82 $mform->setType('mediaheight', PARAM_INT);
a36eba45 83 $mform->setDefault('mediaheight', $lessonconfig->mediaheight);
0a4abb73
SH
84
85 /** Legacy media popup close button element to maintain backwards compatibility */
86 $mform->addElement('hidden', 'mediaclose');
87 $mform->setType('mediaclose', PARAM_BOOL);
a36eba45 88 $mform->setDefault('mediaclose', $lessonconfig->mediaclose);
0a4abb73 89
26de8d35 90 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
8eb1d25f 91 if (!empty($CFG->formatstringstriptags)) {
92 $mform->setType('name', PARAM_TEXT);
93 } else {
b8ea3041 94 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 95 }
e73457e4 96 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 97 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
6398ff53 98 $this->standard_intro_elements();
5f649aaa 99
d756d3fb
FM
100 // Appearance.
101 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
102
103 $filemanageroptions = array();
104 $filemanageroptions['filetypes'] = '*';
105 $filemanageroptions['maxbytes'] = $this->course->maxbytes;
106 $filemanageroptions['subdirs'] = 0;
107 $filemanageroptions['maxfiles'] = 1;
108
109 $mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions);
110 $mform->addHelpButton('mediafile', 'mediafile', 'lesson');
a36eba45 111 $mform->setAdvanced('mediafile', $lessonconfig->mediafile_adv);
d756d3fb
FM
112
113 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
114 $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
a36eba45
SB
115 $mform->setDefault('progressbar', $lessonconfig->progressbar);
116 $mform->setAdvanced('progressbar', $lessonconfig->progressbar_adv);
d756d3fb
FM
117
118 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
119 $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
a36eba45
SB
120 $mform->setDefault('ongoing', $lessonconfig->ongoing);
121 $mform->setAdvanced('ongoing', $lessonconfig->ongoing_adv);
d756d3fb
FM
122
123 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
124 $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
a36eba45
SB
125 $mform->setDefault('displayleft', $lessonconfig->displayleftmenu);
126 $mform->setAdvanced('displayleft', $lessonconfig->displayleftmenu_adv);
d756d3fb
FM
127
128 $options = array();
129 for($i = 100; $i >= 0; $i--) {
130 $options[$i] = $i.'%';
131 }
132 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
133 $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
a36eba45
SB
134 $mform->setDefault('displayleftif', $lessonconfig->displayleftif);
135 $mform->setAdvanced('displayleftif', $lessonconfig->displayleftif_adv);
d756d3fb
FM
136
137 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
138 $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
a36eba45
SB
139 $mform->setDefault('slideshow', $lessonconfig->slideshow);
140 $mform->setAdvanced('slideshow', $lessonconfig->slideshow_adv);
d756d3fb
FM
141
142 $numbers = array();
143 for ($i = 20; $i > 1; $i--) {
144 $numbers[$i] = $i;
145 }
146
147 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
a36eba45
SB
148 $mform->setDefault('maxanswers', $lessonconfig->maxanswers);
149 $mform->setAdvanced('maxanswers', $lessonconfig->maxanswers_adv);
d756d3fb
FM
150 $mform->setType('maxanswers', PARAM_INT);
151 $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
152
153 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
154 $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
a36eba45
SB
155 $mform->setDefault('feedback', $lessonconfig->defaultfeedback);
156 $mform->setAdvanced('feedback', $lessonconfig->defaultfeedback_adv);
d756d3fb
FM
157
158 // Get the modules.
159 if ($mods = get_course_mods($COURSE->id)) {
160 $modinstances = array();
161 foreach ($mods as $mod) {
a6736504 162 // Get the module name and then store it in a new array.
d756d3fb 163 if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
a6736504
PN
164 // Exclude this lesson, if it's already been saved.
165 if (!isset($this->_cm->id) || $this->_cm->id != $mod->id) {
d756d3fb
FM
166 $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
167 }
168 }
169 }
a6736504 170 asort($modinstances); // Sort by module name.
d756d3fb
FM
171 $modinstances=array(0=>get_string('none'))+$modinstances;
172
173 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
174 $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
175 $mform->setDefault('activitylink', 0);
a36eba45 176 $mform->setAdvanced('activitylink', $lessonconfig->activitylink_adv);
d756d3fb
FM
177 }
178
179 // Availability.
180 $mform->addElement('header', 'availabilityhdr', get_string('availability'));
181
182 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
183 $mform->setDefault('available', 0);
184
185 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
186 $mform->setDefault('deadline', 0);
187
a1acc001
JMV
188 // Time limit.
189 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'lesson'),
190 array('optional' => true));
191 $mform->addHelpButton('timelimit', 'timelimit', 'lesson');
a36eba45
SB
192 $mform->setAdvanced('timelimit', $lessonconfig->timelimit_adv);
193 $mform->setDefault('timelimit', $lessonconfig->timelimit);
5f649aaa 194
0a4abb73 195 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
4c80a990 196 $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
a36eba45
SB
197 $mform->setDefault('usepassword', $lessonconfig->password);
198 $mform->setAdvanced('usepassword', $lessonconfig->password_adv);
0a4abb73
SH
199
200 $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
0a4abb73 201 $mform->setDefault('password', '');
a36eba45 202 $mform->setAdvanced('password', $lessonconfig->password_adv);
0a4abb73 203 $mform->setType('password', PARAM_RAW);
385247d9
DS
204 $mform->hideIf('password', 'usepassword', 'eq', 0);
205 $mform->hideIf('passwordunmask', 'usepassword', 'eq', 0);
0a4abb73 206
d756d3fb 207 // Dependent on.
e8a434b3
JMV
208 if ($this->current && isset($this->current->dependency) && $this->current->dependency) {
209 $mform->addElement('header', 'dependencyon', get_string('prerequisitelesson', 'lesson'));
210 $mform->addElement('static', 'warningobsolete',
211 get_string('warning', 'lesson'),
212 get_string('prerequisiteisobsolete', 'lesson'));
213 $options = array(0 => get_string('none'));
214 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
215 foreach ($lessons as $lesson) {
216 if ($lesson->id != $this->_instance) {
217 $options[$lesson->id] = format_string($lesson->name, true);
218 }
5f649aaa 219
d756d3fb 220 }
d756d3fb 221 }
e8a434b3
JMV
222 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
223 $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
224 $mform->setDefault('dependency', 0);
225
226 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
227 $mform->setDefault('timespent', 0);
228 $mform->setType('timespent', PARAM_INT);
229 $mform->disabledIf('timespent', 'dependency', 'eq', 0);
230
231 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
232 $mform->setDefault('completed', 0);
233 $mform->disabledIf('completed', 'dependency', 'eq', 0);
234
235 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
236 $mform->setDefault('gradebetterthan', 0);
237 $mform->setType('gradebetterthan', PARAM_INT);
238 $mform->disabledIf('gradebetterthan', 'dependency', 'eq', 0);
239 } else {
240 $mform->addElement('hidden', 'dependency', 0);
241 $mform->setType('dependency', PARAM_INT);
242 $mform->addElement('hidden', 'timespent', 0);
243 $mform->setType('timespent', PARAM_INT);
244 $mform->addElement('hidden', 'completed', 0);
245 $mform->setType('completed', PARAM_INT);
246 $mform->addElement('hidden', 'gradebetterthan', 0);
247 $mform->setType('gradebetterthan', PARAM_INT);
248 $mform->setConstants(array('dependency' => 0, 'timespent' => 0,
249 'completed' => 0, 'gradebetterthan' => 0));
d756d3fb 250 }
5f649aaa 251
c6f9f061
JL
252 // Allow to enable offline lessons only if the Mobile services are enabled.
253 if ($CFG->enablemobilewebservice) {
254 $mform->addElement('selectyesno', 'allowofflineattempts', get_string('allowofflineattempts', 'lesson'));
255 $mform->addHelpButton('allowofflineattempts', 'allowofflineattempts', 'lesson');
256 $mform->setDefault('allowofflineattempts', 0);
257 $mform->setAdvanced('allowofflineattempts');
258 $mform->disabledIf('allowofflineattempts', 'timelimit[number]', 'neq', 0);
259
260 $mform->addElement('static', 'allowofflineattemptswarning', '',
261 $OUTPUT->notification(get_string('allowofflineattempts_help', 'lesson'), 'warning'));
262 $mform->setAdvanced('allowofflineattemptswarning');
263 } else {
264 $mform->addElement('hidden', 'allowofflineattempts', 0);
265 $mform->setType('allowofflineattempts', PARAM_INT);
266 }
267
d756d3fb 268 // Flow control.
0a4abb73 269 $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
5f649aaa 270
271 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
4c80a990 272 $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
a36eba45
SB
273 $mform->setDefault('modattempts', $lessonconfig->modattempts);
274 $mform->setAdvanced('modattempts', $lessonconfig->modattempts_adv);
5f649aaa 275
276 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
4c80a990 277 $mform->addHelpButton('review', 'displayreview', 'lesson');
a36eba45
SB
278 $mform->setDefault('review', $lessonconfig->displayreview);
279 $mform->setAdvanced('review', $lessonconfig->displayreview_adv);
5f649aaa 280
252e85be 281 $numbers = array('0' => get_string('unlimited'));
d756d3fb 282 for ($i = 10; $i > 0; $i--) {
5f649aaa 283 $numbers[$i] = $i;
284 }
285 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
4c80a990 286 $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
a36eba45
SB
287 $mform->setDefault('maxattempts', $lessonconfig->maximumnumberofattempts);
288 $mform->setAdvanced('maxattempts', $lessonconfig->maximumnumberofattempts_adv);
5f649aaa 289
0a4abb73 290 $defaultnextpages = array();
4c80a990
DM
291 $defaultnextpages[0] = get_string('normal', 'lesson');
292 $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
293 $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
0a4abb73 294 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
4c80a990 295 $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
a36eba45
SB
296 $mform->setDefault('nextpagedefault', $lessonconfig->defaultnextpage);
297 $mform->setAdvanced('nextpagedefault', $lessonconfig->defaultnextpage_adv);
5f649aaa 298
299 $numbers = array();
300 for ($i = 100; $i >= 0; $i--) {
301 $numbers[$i] = $i;
302 }
303 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
4c80a990 304 $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
a36eba45
SB
305 $mform->setDefault('maxpages', $lessonconfig->numberofpagestoshow);
306 $mform->setAdvanced('maxpages', $lessonconfig->numberofpagestoshow_adv);
5f649aaa 307
d756d3fb
FM
308 // Grade.
309 $this->standard_grading_coursemodule_elements();
5f649aaa 310
d756d3fb 311 // No header here, so that the following settings are displayed in the grade section.
5f649aaa 312
d756d3fb
FM
313 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
314 $mform->addHelpButton('practice', 'practice', 'lesson');
a36eba45
SB
315 $mform->setDefault('practice', $lessonconfig->practice);
316 $mform->setAdvanced('practice', $lessonconfig->practice_adv);
684c3be7 317
d756d3fb
FM
318 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
319 $mform->addHelpButton('custom', 'customscoring', 'lesson');
a36eba45
SB
320 $mform->setDefault('custom', $lessonconfig->customscoring);
321 $mform->setAdvanced('custom', $lessonconfig->customscoring_adv);
5f649aaa 322
d756d3fb
FM
323 $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
324 $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
a36eba45
SB
325 $mform->setDefault('retake', $lessonconfig->retakesallowed);
326 $mform->setAdvanced('retake', $lessonconfig->retakesallowed_adv);
5f649aaa 327
d756d3fb
FM
328 $options = array();
329 $options[0] = get_string('usemean', 'lesson');
330 $options[1] = get_string('usemaximum', 'lesson');
331 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
332 $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
a36eba45
SB
333 $mform->setDefault('usemaxgrade', $lessonconfig->handlingofretakes);
334 $mform->setAdvanced('usemaxgrade', $lessonconfig->handlingofretakes_adv);
385247d9 335 $mform->hideIf('usemaxgrade', 'retake', 'eq', '0');
e24b7f85 336
d756d3fb
FM
337 $numbers = array();
338 for ($i = 100; $i >= 0; $i--) {
339 $numbers[$i] = $i;
5f649aaa 340 }
d756d3fb
FM
341 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
342 $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
a36eba45
SB
343 $mform->setDefault('minquestions', $lessonconfig->minimumnumberofquestions);
344 $mform->setAdvanced('minquestions', $lessonconfig->minimumnumberofquestions_adv);
5f649aaa 345
5f649aaa 346//-------------------------------------------------------------------------------
42f103be 347 $this->standard_coursemodule_elements();
5f649aaa 348//-------------------------------------------------------------------------------
e73457e4 349// buttons
a23f0aaf 350 $this->add_action_buttons();
e73457e4 351 }
5f649aaa 352
11bf5b83 353 /**
354 * Enforce defaults here
355 *
10e2355f 356 * @param array $defaultvalues Form defaults
11bf5b83 357 * @return void
358 **/
10e2355f
JMV
359 public function data_preprocessing(&$defaultvalues) {
360 if (isset($defaultvalues['conditions'])) {
361 $conditions = unserialize($defaultvalues['conditions']);
362 $defaultvalues['timespent'] = $conditions->timespent;
363 $defaultvalues['completed'] = $conditions->completed;
364 $defaultvalues['gradebetterthan'] = $conditions->gradebetterthan;
5f649aaa 365 }
3d1b7452 366
10e2355f
JMV
367 // Set up the completion checkbox which is not part of standard data.
368 $defaultvalues['completiontimespentenabled'] =
369 !empty($defaultvalues['completiontimespent']) ? 1 : 0;
370
3d1b7452 371 if ($this->current->instance) {
66dbecef 372 // Editing existing instance - copy existing files into draft area.
684c3be7 373 $draftitemid = file_get_submitted_draft_itemid('mediafile');
3d1b7452 374 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
10e2355f 375 $defaultvalues['mediafile'] = $draftitemid;
11bf5b83 376 }
e73457e4 377 }
862bd9cd 378
379 /**
380 * Enforce validation rules here
381 *
382 * @param object $data Post data to validate
383 * @return array
384 **/
a78890d5 385 function validation($data, $files) {
386 $errors = parent::validation($data, $files);
862bd9cd 387
66cf47a0
JMV
388 // Check open and close times are consistent.
389 if ($data['available'] != 0 && $data['deadline'] != 0 &&
390 $data['deadline'] < $data['available']) {
391 $errors['deadline'] = get_string('closebeforeopen', 'lesson');
392 }
393
7b5abb50
AKA
394 if (!empty($data['usepassword']) && empty($data['password'])) {
395 $errors['password'] = get_string('emptypassword', 'lesson');
396 }
862bd9cd 397
a78890d5 398 return $errors;
862bd9cd 399 }
25345cb4
JMV
400
401 /**
402 * Display module-specific activity completion rules.
403 * Part of the API defined by moodleform_mod
404 * @return array Array of string IDs of added items, empty array if none
405 */
406 public function add_completion_rules() {
407 $mform = $this->_form;
408
409 $mform->addElement('checkbox', 'completionendreached', get_string('completionendreached', 'lesson'),
410 get_string('completionendreached_desc', 'lesson'));
e44d71a6
JP
411 // Enable this completion rule by default.
412 $mform->setDefault('completionendreached', 1);
d0445cf7
JMV
413
414 $group = array();
10e2355f
JMV
415 $group[] =& $mform->createElement('checkbox', 'completiontimespentenabled', '',
416 get_string('completiontimespent', 'lesson'));
63e4df60 417 $group[] =& $mform->createElement('duration', 'completiontimespent', '', array('optional' => false));
10e2355f
JMV
418 $mform->addGroup($group, 'completiontimespentgroup', get_string('completiontimespentgroup', 'lesson'), array(' '), false);
419 $mform->disabledIf('completiontimespent[number]', 'completiontimespentenabled', 'notchecked');
420 $mform->disabledIf('completiontimespent[timeunit]', 'completiontimespentenabled', 'notchecked');
421
422 return array('completionendreached', 'completiontimespentgroup');
25345cb4
JMV
423 }
424
425 /**
426 * Called during validation. Indicates whether a module-specific completion rule is selected.
427 *
428 * @param array $data Input data (not yet validated)
429 * @return bool True if one or more rules is enabled, false if none are.
430 */
431 public function completion_rule_enabled($data) {
10e2355f
JMV
432 return !empty($data['completionendreached']) || $data['completiontimespent'] > 0;
433 }
434
06cdda46
MG
435 /**
436 * Allows module to modify the data returned by form get_data().
437 * This method is also called in the bulk activity completion form.
438 *
439 * Only available on moodleform_mod.
440 *
273d3106 441 * @param stdClass $data the form data to be modified.
06cdda46 442 */
273d3106 443 public function data_postprocessing($data) {
06cdda46 444 parent::data_postprocessing($data);
10e2355f
JMV
445 // Turn off completion setting if the checkbox is not ticked.
446 if (!empty($data->completionunlocked)) {
447 $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
448 if (empty($data->completiontimespentenabled) || !$autocompletion) {
449 $data->completiontimespent = 0;
450 }
fb8b18a4
SB
451 if (empty($data->completionendreached) || !$autocompletion) {
452 $data->completionendreached = 0;
453 }
454 }
25345cb4 455 }
5f649aaa 456}
86342d63 457