3 * Form to define a new instance of lesson or edit an instance.
4 * It is used from /course/modedit.php.
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once($CFG->dirroot
.'/course/moodleform_mod.php');
12 require_once('locallib.php');
14 class mod_lesson_mod_form
extends moodleform_mod
{
16 function definition() {
17 global $CFG, $LESSON_NEXTPAGE_ACTION, $COURSE;
19 $mform =& $this->_form
;
21 //-------------------------------------------------------------------------------
22 $mform->addElement('header', 'general', get_string('general', 'form'));
24 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
25 if (!empty($CFG->formatstringstriptags
)) {
26 $mform->setType('name', PARAM_TEXT
);
28 $mform->setType('name', PARAM_CLEAN
);
30 $mform->addRule('name', null, 'required', null, 'client');
32 // Create a text box that can be enabled/disabled for lesson time limit
34 $timedgrp[] = &$mform->createElement('text', 'maxtime');
35 $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
36 $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
37 $mform->disabledIf('timedgrp', 'timed');
39 // Add numeric rule to text field
40 $timedgrprules = array();
41 $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
42 $mform->addGroupRule('timedgrp', $timedgrprules);
44 // Rest of group setup
45 $mform->setDefault('timed', 0);
46 $mform->setDefault('maxtime', 20);
47 $mform->setType('maxtime', PARAM_INT
);
48 $mform->setHelpButton('timedgrp', array('timed', get_string('timed', 'lesson'), 'lesson'));
51 for ($i=20; $i>1; $i--) {
54 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
55 $mform->setDefault('maxanswers', 4);
56 $mform->setHelpButton('maxanswers', array('maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), 'lesson'));
58 //-------------------------------------------------------------------------------
59 $mform->addElement('header', '', get_string('gradeoptions', 'lesson'));
61 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
62 $mform->setHelpButton('practice', array('practice', get_string('practice', 'lesson'), 'lesson'));
63 $mform->setDefault('practice', 0);
65 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
66 $mform->setHelpButton('custom', array('custom', get_string('customscoring', 'lesson'), 'lesson'));
67 $mform->setDefault('custom', 1);
70 for ($i=100; $i>=0; $i--) {
73 $mform->addElement('select', 'grade', get_string('maximumgrade'), $grades);
74 $mform->setDefault('grade', 0);
75 $mform->setHelpButton('grade', array('grade', get_string('maximumgrade'), 'lesson'));
77 $mform->addElement('selectyesno', 'retake', get_string('canretake', 'lesson', $COURSE->student
));
78 $mform->setHelpButton('retake', array('retake', get_string('canretake', 'lesson', $COURSE->student
), 'lesson'));
79 $mform->setDefault('retake', 0);
82 $options[0] = get_string('usemean', 'lesson');
83 $options[1] = get_string('usemaximum', 'lesson');
84 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
85 $mform->setHelpButton('usemaxgrade', array('handlingofretakes', get_string('handlingofretakes', 'lesson'), 'lesson'));
86 $mform->setDefault('usemaxgrade', 0);
88 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
89 $mform->setHelpButton('ongoing', array('ongoing', get_string('ongoing', 'lesson'), 'lesson'));
90 $mform->setDefault('ongoing', 0);
92 //-------------------------------------------------------------------------------
93 $mform->addElement('header', '', get_string('flowcontrol', 'lesson'));
95 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
96 $mform->setHelpButton('modattempts', array('modattempts', get_string('modattempts', 'lesson'), 'lesson'));
97 $mform->setDefault('modattempts', 0);
99 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
100 $mform->setHelpButton('review', array('review', get_string('displayreview', 'lesson'), 'lesson'));
101 $mform->setDefault('review', 0);
104 for ($i=10; $i>0; $i--) {
107 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
108 $mform->setHelpButton('maxattempts', array('maxattempts', get_string('maximumnumberofattempts', 'lesson'), 'lesson'));
109 $mform->setDefault('maxattempts', 1);
111 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $LESSON_NEXTPAGE_ACTION);
112 $mform->setHelpButton('nextpagedefault', array('nextpageaction', get_string('actionaftercorrectanswer', 'lesson'), 'lesson'));
113 $mform->setDefault('nextpagedefault', 0);
115 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
116 $mform->setHelpButton('feedback', array('feedback', get_string('displaydefaultfeedback', 'lesson'), 'lesson'));
117 $mform->setDefault('feedback', 0);
120 for ($i = 100; $i >= 0; $i--) {
123 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
124 $mform->setHelpButton('minquestions', array('minquestions', get_string('minimumnumberofquestions', 'lesson'), 'lesson'));
125 $mform->setDefault('minquestions', 0);
128 for ($i = 100; $i >= 0; $i--) {
131 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
132 $mform->setHelpButton('maxpages', array('maxpages', get_string('numberofpagestoshow', 'lesson'), 'lesson'));
133 $mform->setDefault('maxpages', 0);
135 //-------------------------------------------------------------------------------
136 $mform->addElement('header', '', get_string('lessonformating', 'lesson'));
138 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
139 $mform->setHelpButton('slideshow', array('slideshow', get_string('slideshow', 'lesson'), 'lesson'));
140 $mform->setDefault('slideshow', 0);
142 $mform->addElement('text', 'width', get_string('slideshowwidth', 'lesson'));
143 $mform->setDefault('width', 640);
144 $mform->addRule('width', null, 'required', null, 'client');
145 $mform->addRule('width', null, 'numeric', null, 'client');
146 $mform->setHelpButton('width', array('width', get_string('slideshowwidth', 'lesson'), 'lesson'));
147 $mform->setType('width', PARAM_INT
);
149 $mform->addElement('text', 'height', get_string('slideshowheight', 'lesson'));
150 $mform->setDefault('height', 480);
151 $mform->addRule('height', null, 'required', null, 'client');
152 $mform->addRule('height', null, 'numeric', null, 'client');
153 $mform->setHelpButton('height', array('height', get_string('slideshowheight', 'lesson'), 'lesson'));
154 $mform->setType('height', PARAM_INT
);
156 $mform->addElement('text', 'bgcolor', get_string('slideshowbgcolor', 'lesson'));
157 $mform->setDefault('bgcolor', '#FFFFFF');
158 $mform->addRule('bgcolor', null, 'required', null, 'client');
159 $mform->setHelpButton('bgcolor', array('bgcolor', get_string('slideshowbgcolor', 'lesson'), 'lesson'));
160 $mform->setType('bgcolor', PARAM_TEXT
);
162 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
163 $mform->setHelpButton('displayleft', array('displayleft', get_string('displayleftmenu', 'lesson'), 'lesson'));
164 $mform->setDefault('displayleft', 0);
167 for($i = 100; $i >= 0; $i--) {
168 $options[$i] = $i.'%';
170 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
171 $mform->setDefault('displayleftif', 0);
173 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
174 $mform->setHelpButton('progressbar', array('progressbar', get_string('progressbar', 'lesson'), 'lesson'));
175 $mform->setDefault('progressbar', 0);
178 //-------------------------------------------------------------------------------
179 $mform->addElement('header', '', get_string('accesscontrol', 'lesson'));
181 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
182 $mform->setHelpButton('usepassword', array('usepassword', get_string('usepassword', 'lesson'), 'lesson'));
183 $mform->setDefault('usepassword', 0);
185 $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
186 $mform->setHelpButton('password', array('password', get_string('password', 'lesson'), 'lesson'));
187 $mform->setDefault('password', '');
188 $mform->setType('password', PARAM_RAW
);
190 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
191 $mform->setDefault('available', 0);
193 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
194 $mform->setDefault('deadline', 0);
196 //-------------------------------------------------------------------------------
197 $mform->addElement('header', '', get_string('dependencyon', 'lesson'));
199 $options = array(0=>get_string('none'));
200 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
201 foreach($lessons as $lesson) {
202 if ($lesson->id
!= $this->_instance
){
203 $options[$lesson->id
] = format_string($lesson->name
, true);
208 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
209 $mform->setHelpButton('dependency', array('dependency', get_string('dependencyon', 'lesson'), 'lesson'));
210 $mform->setDefault('dependency', 0);
212 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
213 $mform->setDefault('timespent', 0);
214 $mform->setType('timespent', PARAM_INT
);
216 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
217 $mform->setDefault('completed', 0);
219 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
220 $mform->setDefault('gradebetterthan', 0);
221 $mform->setType('gradebetterthan', PARAM_INT
);
223 //-------------------------------------------------------------------------------
224 $mform->addElement('header', '', get_string('mediafile', 'lesson'));
226 $mform->addElement('choosecoursefile', 'mediafile', get_string('mediafile', 'lesson'), array('courseid'=>$COURSE->id
));
227 $mform->setHelpButton('mediafile', array('mediafile', get_string('mediafile', 'lesson'), 'lesson'));
228 $mform->setDefault('mediafile', '');
229 $mform->setType('mediafile', PARAM_RAW
);
231 $mform->addElement('selectyesno', 'mediaclose', get_string('mediaclose', 'lesson'));
232 $mform->setDefault('mediaclose', 0);
234 $mform->addElement('text', 'mediaheight', get_string('mediaheight', 'lesson'));
235 $mform->setHelpButton('mediaheight', array('mediaheight', get_string('mediaheight', 'lesson'), 'lesson'));
236 $mform->setDefault('mediaheight', 100);
237 $mform->addRule('mediaheight', null, 'required', null, 'client');
238 $mform->addRule('mediaheight', null, 'numeric', null, 'client');
239 $mform->setType('mediaheight', PARAM_INT
);
241 $mform->addElement('text', 'mediawidth', get_string('mediawidth', 'lesson'));
242 $mform->setHelpButton('mediawidth', array('mediawidth', get_string('mediawidth', 'lesson'), 'lesson'));
243 $mform->setDefault('mediawidth', 650);
244 $mform->addRule('mediawidth', null, 'required', null, 'client');
245 $mform->addRule('mediawidth', null, 'numeric', null, 'client');
246 $mform->setType('mediawidth', PARAM_INT
);
248 //-------------------------------------------------------------------------------
249 $mform->addElement('header', '', get_string('other', 'lesson'));
252 if ($mods = get_course_mods($COURSE->id
)) {
253 $modinstances = array();
254 foreach ($mods as $mod) {
256 // get the module name and then store it in a new array
257 if ($module = get_coursemodule_from_instance($mod->modname
, $mod->instance
, $COURSE->id
)) {
258 if (isset($this->_cm
->id
) and $this->_cm
->id
!= $mod->id
){
259 $modinstances[$mod->id
] = $mod->modname
.' - '.$module->name
;
263 asort($modinstances); // sort by module name
264 $modinstances=array(0=>get_string('none'))+
$modinstances;
266 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
267 $mform->setHelpButton('activitylink', array('activitylink', get_string('activitylink', 'lesson'), 'lesson'));
268 $mform->setDefault('activitylink', 0);
272 $mform->addElement('text', 'maxhighscores', get_string('maxhighscores', 'lesson'));
273 $mform->setHelpButton('maxhighscores', array('maxhighscores', get_string('maxhighscores', 'lesson'), 'lesson'));
274 $mform->setDefault('maxhighscores', 10);
275 $mform->addRule('maxhighscores', null, 'required', null, 'client');
276 $mform->addRule('maxhighscores', null, 'numeric', null, 'client');
277 $mform->setType('maxhighscores', PARAM_INT
);
279 $mform->addElement('selectyesno', 'lessondefault', get_string('lessondefault', 'lesson'));
280 $mform->setHelpButton('lessondefault', array('lessondefault', get_string('lessondefault', 'lesson'), 'lesson'));
281 $mform->setDefault('lessondefault', 0);
283 //-------------------------------------------------------------------------------
284 $features = new stdClass
;
285 $features->groups
= false;
286 $features->groupings
= true;
287 $features->groupmembersonly
= true;
288 $this->standard_coursemodule_elements($features);
289 //-------------------------------------------------------------------------------
291 $this->add_action_buttons();
295 * Enforce defaults here
297 * @param array $default_values Form defaults
300 function data_preprocessing(&$default_values) {
302 if (isset($default_values['conditions'])) {
303 $conditions = unserialize($default_values['conditions']);
304 $default_values['timespent'] = $conditions->timespent
;
305 $default_values['completed'] = $conditions->completed
;
306 $default_values['gradebetterthan'] = $conditions->gradebetterthan
;
308 // after this passwords are clear text, MDL-11090
309 if (isset($default_values['password']) and ($module->version
<2008112600)) {
310 unset($default_values['password']);
312 if (isset($default_values['add']) and $defaults = get_record('lesson_default', 'course', $default_values['course'])) {
313 foreach ($defaults as $fieldname => $default) {
314 switch ($fieldname) {
316 $conditions = unserialize($default);
317 $default_values['timespent'] = $conditions->timespent
;
318 $default_values['completed'] = $conditions->completed
;
319 $default_values['gradebetterthan'] = $conditions->gradebetterthan
;
322 $default_values[$fieldname] = $default;
330 * Enforce validation rules here
332 * @param object $data Post data to validate
335 function validation($data, $files) {
336 $errors = parent
::validation($data, $files);
338 if (empty($data['maxtime']) and !empty($data['timed'])) {
339 $errors['timedgrp'] = get_string('err_numeric', 'form');