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 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
31 require_once($CFG->dirroot
.'/course/moodleform_mod.php');
32 require_once($CFG->dirroot
.'/mod/lesson/locallib.php');
34 class mod_lesson_mod_form
extends moodleform_mod
{
36 protected $course = null;
38 public function mod_lesson_mod_form($current, $section, $cm, $course) {
39 $this->course
= $course;
40 parent
::moodleform_mod($current, $section, $cm, $course);
43 function definition() {
44 global $CFG, $COURSE, $DB;
46 $mform = $this->_form
;
48 //-------------------------------------------------------------------------------
49 $mform->addElement('header', 'general', get_string('general', 'form'));
51 /** Legacy slideshow width element to maintain backwards compatibility */
52 $mform->addElement('hidden', 'width');
53 $mform->setType('width', PARAM_INT
);
54 $mform->setDefault('width', $CFG->lesson_slideshowwidth
);
56 /** Legacy slideshow height element to maintain backwards compatibility */
57 $mform->addElement('hidden', 'height');
58 $mform->setType('height', PARAM_INT
);
59 $mform->setDefault('height', $CFG->lesson_slideshowheight
);
61 /** Legacy slideshow background color element to maintain backwards compatibility */
62 $mform->addElement('hidden', 'bgcolor');
63 $mform->setType('bgcolor', PARAM_TEXT
);
64 $mform->setDefault('bgcolor', $CFG->lesson_slideshowbgcolor
);
66 /** Legacy media popup width element to maintain backwards compatibility */
67 $mform->addElement('hidden', 'mediawidth');
68 $mform->setType('mediawidth', PARAM_INT
);
69 $mform->setDefault('mediawidth', $CFG->lesson_mediawidth
);
71 /** Legacy media popup height element to maintain backwards compatibility */
72 $mform->addElement('hidden', 'mediaheight');
73 $mform->setType('mediaheight', PARAM_INT
);
74 $mform->setDefault('mediaheight', $CFG->lesson_mediaheight
);
76 /** Legacy media popup close button element to maintain backwards compatibility */
77 $mform->addElement('hidden', 'mediaclose');
78 $mform->setType('mediaclose', PARAM_BOOL
);
79 $mform->setDefault('mediaclose', $CFG->lesson_mediaclose
);
81 /** Legacy maximum highscores element to maintain backwards compatibility */
82 $mform->addElement('hidden', 'maxhighscores');
83 $mform->setType('maxhighscores', PARAM_INT
);
84 $mform->setDefault('maxhighscores', $CFG->lesson_maxhighscores
);
86 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
87 if (!empty($CFG->formatstringstriptags
)) {
88 $mform->setType('name', PARAM_TEXT
);
90 $mform->setType('name', PARAM_CLEAN
);
92 $mform->addRule('name', null, 'required', null, 'client');
94 // Create a text box that can be enabled/disabled for lesson time limit
96 $timedgrp[] = &$mform->createElement('text', 'maxtime');
97 $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
98 $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
99 $mform->disabledIf('timedgrp', 'timed');
101 // Add numeric rule to text field
102 $timedgrprules = array();
103 $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
104 $mform->addGroupRule('timedgrp', $timedgrprules);
106 // Rest of group setup
107 $mform->setDefault('timed', 0);
108 $mform->setDefault('maxtime', 20);
109 $mform->setType('maxtime', PARAM_INT
);
112 for ($i=20; $i>1; $i--) {
116 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
117 $mform->setDefault('available', 0);
119 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
120 $mform->setDefault('deadline', 0);
122 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
123 $mform->setDefault('maxanswers', $CFG->lesson_maxanswers
);
124 $mform->setType('maxanswers', PARAM_INT
);
125 $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
127 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
128 $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
129 $mform->setDefault('usepassword', 0);
130 $mform->setAdvanced('usepassword');
132 $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
133 $mform->setDefault('password', '');
134 $mform->setType('password', PARAM_RAW
);
135 $mform->setAdvanced('password');
136 $mform->disabledIf('password', 'usepassword', 'eq', 0);
138 //-------------------------------------------------------------------------------
139 $mform->addElement('header', 'gradeoptions', get_string('gradeoptions', 'lesson'));
141 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
142 $mform->addHelpButton('practice', 'practice', 'lesson');
143 $mform->setDefault('practice', 0);
145 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
146 $mform->addHelpButton('custom', 'customscoring', 'lesson');
147 $mform->setDefault('custom', 1);
150 for ($i=100; $i>=0; $i--) {
153 $mform->addElement('select', 'grade', get_string('maxgrade', 'lesson'), $grades);
154 $mform->setDefault('grade', 0);
155 $mform->addHelpButton('grade', 'maxgrade', 'lesson');
156 $mform->disabledIf('grade', 'practice', 'eq', '1');
158 $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
159 $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
160 $mform->setDefault('retake', 0);
163 $options[0] = get_string('usemean', 'lesson');
164 $options[1] = get_string('usemaximum', 'lesson');
165 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
166 $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
167 $mform->setDefault('usemaxgrade', 0);
168 $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
170 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
171 $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
172 $mform->setDefault('ongoing', 0);
174 //-------------------------------------------------------------------------------
175 $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
177 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
178 $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
179 $mform->setDefault('modattempts', 0);
181 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
182 $mform->addHelpButton('review', 'displayreview', 'lesson');
183 $mform->setDefault('review', 0);
186 for ($i=10; $i>0; $i--) {
189 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
190 $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
191 $mform->setDefault('maxattempts', 1);
193 $defaultnextpages = array();
194 $defaultnextpages[0] = get_string('normal', 'lesson');
195 $defaultnextpages[LESSON_UNSEENPAGE
] = get_string('showanunseenpage', 'lesson');
196 $defaultnextpages[LESSON_UNANSWEREDPAGE
] = get_string('showanunansweredpage', 'lesson');
197 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
198 $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
199 $mform->setDefault('nextpagedefault', $CFG->lesson_defaultnextpage
);
200 $mform->setAdvanced('nextpagedefault');
202 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
203 $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
204 $mform->setDefault('feedback', 0);
206 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
207 $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
208 $mform->setDefault('progressbar', 0);
210 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
211 $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
212 $mform->setDefault('displayleft', 0);
215 for($i = 100; $i >= 0; $i--) {
216 $options[$i] = $i.'%';
218 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
219 $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
220 $mform->setDefault('displayleftif', 0);
221 $mform->setAdvanced('displayleftif');
224 for ($i = 100; $i >= 0; $i--) {
227 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
228 $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
229 $mform->setDefault('minquestions', 0);
230 $mform->setAdvanced('minquestions');
233 for ($i = 100; $i >= 0; $i--) {
236 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
237 $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
238 $mform->setAdvanced('maxpages');
239 $mform->setDefault('maxpages', 0);
241 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
242 $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
243 $mform->setDefault('slideshow', 0);
244 $mform->setAdvanced('slideshow');
247 if ($mods = get_course_mods($COURSE->id
)) {
248 $modinstances = array();
249 foreach ($mods as $mod) {
251 // get the module name and then store it in a new array
252 if ($module = get_coursemodule_from_instance($mod->modname
, $mod->instance
, $COURSE->id
)) {
253 if (isset($this->_cm
->id
) and $this->_cm
->id
!= $mod->id
){
254 $modinstances[$mod->id
] = $mod->modname
.' - '.$module->name
;
258 asort($modinstances); // sort by module name
259 $modinstances=array(0=>get_string('none'))+
$modinstances;
261 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
262 $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
263 $mform->setDefault('activitylink', 0);
264 $mform->setAdvanced('activitylink');
267 //-------------------------------------------------------------------------------
268 $mform->addElement('header', 'mediafileheader', get_string('mediafile', 'lesson'));
270 $filepickeroptions = array();
271 $filepickeroptions['filetypes'] = '*';
272 $filepickeroptions['maxbytes'] = $this->course
->maxbytes
;
273 $mform->addElement('filepicker', 'mediafile', get_string('mediafile', 'lesson'), null, $filepickeroptions);
274 $mform->addHelpButton('mediafile', 'mediafile', 'lesson');
275 $mform->setDefault('mediafile', '');
277 //-------------------------------------------------------------------------------
278 $mform->addElement('header', 'dependencyon', get_string('dependencyon', 'lesson'));
280 $options = array(0=>get_string('none'));
281 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
282 foreach($lessons as $lesson) {
283 if ($lesson->id
!= $this->_instance
){
284 $options[$lesson->id
] = format_string($lesson->name
, true);
289 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
290 $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
291 $mform->setDefault('dependency', 0);
293 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
294 $mform->setDefault('timespent', 0);
295 $mform->setType('timespent', PARAM_INT
);
297 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
298 $mform->setDefault('completed', 0);
300 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
301 $mform->setDefault('gradebetterthan', 0);
302 $mform->setType('gradebetterthan', PARAM_INT
);
304 //-------------------------------------------------------------------------------
305 $this->standard_coursemodule_elements();
306 //-------------------------------------------------------------------------------
308 $this->add_action_buttons();
312 * Enforce defaults here
314 * @param array $default_values Form defaults
317 function data_preprocessing(&$default_values) {
320 if (isset($default_values['conditions'])) {
321 $conditions = unserialize($default_values['conditions']);
322 $default_values['timespent'] = $conditions->timespent
;
323 $default_values['completed'] = $conditions->completed
;
324 $default_values['gradebetterthan'] = $conditions->gradebetterthan
;
326 // after this passwords are clear text, MDL-11090
327 if (isset($default_values['password']) and ($module->version
<2008112600)) {
328 unset($default_values['password']);
330 if (!empty($this->_cm
) && !empty($default_values['mediafile'])) {
331 $context = get_context_instance(CONTEXT_MODULE
, $this->_cm
->id
);
332 $draftitemid = file_get_submitted_draft_itemid('mediafile');
333 file_prepare_draft_area($draftitemid, $context->id
, 'lesson_media_file', $this->_cm
->instance
, array('subdirs' => 0, 'maxbytes' => $this->course
->maxbytes
, 'maxfiles' => 1));
334 $default_values['mediafile'] = $draftitemid;
339 * Enforce validation rules here
341 * @param object $data Post data to validate
344 function validation($data, $files) {
345 $errors = parent
::validation($data, $files);
347 if (empty($data['maxtime']) and !empty($data['timed'])) {
348 $errors['timedgrp'] = get_string('err_numeric', 'form');