3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Form to define a new instance of lesson or edit an instance.
20 * It is used from /course/modedit.php.
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot.'/course/moodleform_mod.php');
30 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
32 class mod_lesson_mod_form extends moodleform_mod {
34 protected $course = null;
36 public function mod_lesson_mod_form($current, $section, $cm, $course) {
37 $this->course = $course;
38 parent::moodleform_mod($current, $section, $cm, $course);
41 function definition() {
42 global $CFG, $COURSE, $DB;
44 $mform = $this->_form;
46 $config = get_config('lesson');
48 $mform->addElement('header', 'general', get_string('general', 'form'));
50 /** Legacy slideshow width element to maintain backwards compatibility */
51 $mform->addElement('hidden', 'width');
52 $mform->setType('width', PARAM_INT);
53 $mform->setDefault('width', $CFG->lesson_slideshowwidth);
55 /** Legacy slideshow height element to maintain backwards compatibility */
56 $mform->addElement('hidden', 'height');
57 $mform->setType('height', PARAM_INT);
58 $mform->setDefault('height', $CFG->lesson_slideshowheight);
60 /** Legacy slideshow background color element to maintain backwards compatibility */
61 $mform->addElement('hidden', 'bgcolor');
62 $mform->setType('bgcolor', PARAM_TEXT);
63 $mform->setDefault('bgcolor', $CFG->lesson_slideshowbgcolor);
65 /** Legacy media popup width element to maintain backwards compatibility */
66 $mform->addElement('hidden', 'mediawidth');
67 $mform->setType('mediawidth', PARAM_INT);
68 $mform->setDefault('mediawidth', $CFG->lesson_mediawidth);
70 /** Legacy media popup height element to maintain backwards compatibility */
71 $mform->addElement('hidden', 'mediaheight');
72 $mform->setType('mediaheight', PARAM_INT);
73 $mform->setDefault('mediaheight', $CFG->lesson_mediaheight);
75 /** Legacy media popup close button element to maintain backwards compatibility */
76 $mform->addElement('hidden', 'mediaclose');
77 $mform->setType('mediaclose', PARAM_BOOL);
78 $mform->setDefault('mediaclose', $CFG->lesson_mediaclose);
80 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
81 if (!empty($CFG->formatstringstriptags)) {
82 $mform->setType('name', PARAM_TEXT);
84 $mform->setType('name', PARAM_CLEANHTML);
86 $mform->addRule('name', null, 'required', null, 'client');
87 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
88 $this->standard_intro_elements();
91 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
93 $filemanageroptions = array();
94 $filemanageroptions['filetypes'] = '*';
95 $filemanageroptions['maxbytes'] = $this->course->maxbytes;
96 $filemanageroptions['subdirs'] = 0;
97 $filemanageroptions['maxfiles'] = 1;
99 $mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions);
100 $mform->addHelpButton('mediafile', 'mediafile', 'lesson');
102 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
103 $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
104 $mform->setDefault('progressbar', 0);
106 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
107 $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
108 $mform->setDefault('ongoing', 0);
110 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
111 $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
112 $mform->setDefault('displayleft', 0);
115 for($i = 100; $i >= 0; $i--) {
116 $options[$i] = $i.'%';
118 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
119 $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
120 $mform->setDefault('displayleftif', 0);
122 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
123 $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
124 $mform->setDefault('slideshow', 0);
127 for ($i = 20; $i > 1; $i--) {
131 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
132 $mform->setDefault('maxanswers', $CFG->lesson_maxanswers);
133 $mform->setType('maxanswers', PARAM_INT);
134 $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
136 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
137 $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
138 $mform->setDefault('feedback', 0);
141 if ($mods = get_course_mods($COURSE->id)) {
142 $modinstances = array();
143 foreach ($mods as $mod) {
144 // Get the module name and then store it in a new array.
145 if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
146 // Exclude this lesson, if it's already been saved.
147 if (!isset($this->_cm->id) || $this->_cm->id != $mod->id) {
148 $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
152 asort($modinstances); // Sort by module name.
153 $modinstances=array(0=>get_string('none'))+$modinstances;
155 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
156 $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
157 $mform->setDefault('activitylink', 0);
161 $mform->addElement('header', 'availabilityhdr', get_string('availability'));
163 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
164 $mform->setDefault('available', 0);
166 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
167 $mform->setDefault('deadline', 0);
170 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'lesson'),
171 array('optional' => true));
172 $mform->addHelpButton('timelimit', 'timelimit', 'lesson');
174 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
175 $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
176 $mform->setDefault('usepassword', 0);
178 $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
179 $mform->setDefault('password', '');
180 $mform->setType('password', PARAM_RAW);
181 $mform->disabledIf('password', 'usepassword', 'eq', 0);
182 $mform->disabledIf('passwordunmask', 'usepassword', 'eq', 0);
185 if ($this->current && isset($this->current->dependency) && $this->current->dependency) {
186 $mform->addElement('header', 'dependencyon', get_string('prerequisitelesson', 'lesson'));
187 $mform->addElement('static', 'warningobsolete',
188 get_string('warning', 'lesson'),
189 get_string('prerequisiteisobsolete', 'lesson'));
190 $options = array(0 => get_string('none'));
191 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
192 foreach ($lessons as $lesson) {
193 if ($lesson->id != $this->_instance) {
194 $options[$lesson->id] = format_string($lesson->name, true);
199 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
200 $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
201 $mform->setDefault('dependency', 0);
203 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
204 $mform->setDefault('timespent', 0);
205 $mform->setType('timespent', PARAM_INT);
206 $mform->disabledIf('timespent', 'dependency', 'eq', 0);
208 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
209 $mform->setDefault('completed', 0);
210 $mform->disabledIf('completed', 'dependency', 'eq', 0);
212 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
213 $mform->setDefault('gradebetterthan', 0);
214 $mform->setType('gradebetterthan', PARAM_INT);
215 $mform->disabledIf('gradebetterthan', 'dependency', 'eq', 0);
217 $mform->addElement('hidden', 'dependency', 0);
218 $mform->setType('dependency', PARAM_INT);
219 $mform->addElement('hidden', 'timespent', 0);
220 $mform->setType('timespent', PARAM_INT);
221 $mform->addElement('hidden', 'completed', 0);
222 $mform->setType('completed', PARAM_INT);
223 $mform->addElement('hidden', 'gradebetterthan', 0);
224 $mform->setType('gradebetterthan', PARAM_INT);
225 $mform->setConstants(array('dependency' => 0, 'timespent' => 0,
226 'completed' => 0, 'gradebetterthan' => 0));
230 $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
232 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
233 $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
234 $mform->setDefault('modattempts', 0);
236 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
237 $mform->addHelpButton('review', 'displayreview', 'lesson');
238 $mform->setDefault('review', 0);
241 for ($i = 10; $i > 0; $i--) {
244 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
245 $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
246 $mform->setDefault('maxattempts', 1);
248 $defaultnextpages = array();
249 $defaultnextpages[0] = get_string('normal', 'lesson');
250 $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
251 $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
252 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
253 $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
254 $mform->setDefault('nextpagedefault', $CFG->lesson_defaultnextpage);
257 for ($i = 100; $i >= 0; $i--) {
260 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
261 $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
262 $mform->setDefault('maxpages', 0);
265 $this->standard_grading_coursemodule_elements();
267 // No header here, so that the following settings are displayed in the grade section.
269 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
270 $mform->addHelpButton('practice', 'practice', 'lesson');
271 $mform->setDefault('practice', 0);
273 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
274 $mform->addHelpButton('custom', 'customscoring', 'lesson');
275 $mform->setDefault('custom', 1);
277 $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
278 $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
279 $mform->setDefault('retake', 0);
282 $options[0] = get_string('usemean', 'lesson');
283 $options[1] = get_string('usemaximum', 'lesson');
284 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
285 $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
286 $mform->setDefault('usemaxgrade', 0);
287 $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
290 for ($i = 100; $i >= 0; $i--) {
293 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
294 $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
295 $mform->setDefault('minquestions', 0);
297 //-------------------------------------------------------------------------------
298 $this->standard_coursemodule_elements();
299 //-------------------------------------------------------------------------------
301 $this->add_action_buttons();
305 * Enforce defaults here
307 * @param array $defaultvalues Form defaults
310 public function data_preprocessing(&$defaultvalues) {
311 if (isset($defaultvalues['conditions'])) {
312 $conditions = unserialize($defaultvalues['conditions']);
313 $defaultvalues['timespent'] = $conditions->timespent;
314 $defaultvalues['completed'] = $conditions->completed;
315 $defaultvalues['gradebetterthan'] = $conditions->gradebetterthan;
318 // Set up the completion checkbox which is not part of standard data.
319 $defaultvalues['completiontimespentenabled'] =
320 !empty($defaultvalues['completiontimespent']) ? 1 : 0;
322 if ($this->current->instance) {
323 // Editing existing instance - copy existing files into draft area.
324 $draftitemid = file_get_submitted_draft_itemid('mediafile');
325 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
326 $defaultvalues['mediafile'] = $draftitemid;
331 * Enforce validation rules here
333 * @param object $data Post data to validate
336 function validation($data, $files) {
337 $errors = parent::validation($data, $files);
339 if (!empty($data['usepassword']) && empty($data['password'])) {
340 $errors['password'] = get_string('emptypassword', 'lesson');
347 * Display module-specific activity completion rules.
348 * Part of the API defined by moodleform_mod
349 * @return array Array of string IDs of added items, empty array if none
351 public function add_completion_rules() {
352 $mform = $this->_form;
354 $mform->addElement('checkbox', 'completionendreached', get_string('completionendreached', 'lesson'),
355 get_string('completionendreached_desc', 'lesson'));
358 $group[] =& $mform->createElement('checkbox', 'completiontimespentenabled', '',
359 get_string('completiontimespent', 'lesson'));
360 $group[] =& $mform->createElement('duration', 'completiontimespent', array('optional' => true));
361 $mform->addGroup($group, 'completiontimespentgroup', get_string('completiontimespentgroup', 'lesson'), array(' '), false);
362 $mform->disabledIf('completiontimespent[number]', 'completiontimespentenabled', 'notchecked');
363 $mform->disabledIf('completiontimespent[timeunit]', 'completiontimespentenabled', 'notchecked');
365 return array('completionendreached', 'completiontimespentgroup');
369 * Called during validation. Indicates whether a module-specific completion rule is selected.
371 * @param array $data Input data (not yet validated)
372 * @return bool True if one or more rules is enabled, false if none are.
374 public function completion_rule_enabled($data) {
375 return !empty($data['completionendreached']) || $data['completiontimespent'] > 0;
378 public function get_data() {
379 $data = parent::get_data();
383 // Turn off completion setting if the checkbox is not ticked.
384 if (!empty($data->completionunlocked)) {
385 $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
386 if (empty($data->completiontimespentenabled) || !$autocompletion) {
387 $data->completiontimespent = 0;