MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / lesson / mod_form.php
blob43e790693b6ac212d97269e5a725923666486a40
1 <?php // $Id$
2 /**
3 * Form to define a new instance of lesson or edit an instance.
4 * It is used from /course/modedit.php.
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package lesson
9 **/
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 $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 $mform->setType('name', PARAM_TEXT);
26 $mform->addRule('name', null, 'required', null, 'client');
28 // Create a text box that can be enabled/disabled for lesson time limit
29 $timedgrp = array();
30 $timedgrp[] = &$mform->createElement('text', 'maxtime');
31 $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
32 $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
33 $mform->disabledIf('timedgrp', 'timed');
35 // Add numeric rule to text field
36 $timedgrprules = array();
37 $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
38 $mform->addGroupRule('timedgrp', $timedgrprules);
40 // Rest of group setup
41 $mform->setDefault('timed', 0);
42 $mform->setDefault('maxtime', 20);
43 $mform->setType('maxtime', PARAM_INT);
44 $mform->setHelpButton('timedgrp', array('timed', get_string('timed', 'lesson'), 'lesson'));
46 $numbers = array();
47 for ($i=20; $i>1; $i--) {
48 $numbers[$i] = $i;
50 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
51 $mform->setDefault('maxanswers', 4);
52 $mform->setHelpButton('maxanswers', array('maxanswers', get_string('displayformat', 'lesson'), 'lesson'));
54 //-------------------------------------------------------------------------------
55 $mform->addElement('header', '', get_string('gradeoptions', 'lesson'));
57 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
58 $mform->setHelpButton('practice', array('practice', get_string('practice', 'lesson'), 'lesson'));
59 $mform->setDefault('practice', 0);
61 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
62 $mform->setHelpButton('custom', array('custom', get_string('customscoring', 'lesson'), 'lesson'));
63 $mform->setDefault('custom', 1);
65 $grades = array();
66 for ($i=100; $i>=0; $i--) {
67 $grades[$i] = $i;
69 $mform->addElement('select', 'grade', get_string('maximumgrade'), $grades);
70 $mform->setDefault('grade', 0);
71 $mform->setHelpButton('grade', array('grade', get_string('maximumgrade', 'lesson'), 'lesson'));
73 $mform->addElement('selectyesno', 'retake', get_string('canretake', 'lesson', $COURSE->student));
74 $mform->setHelpButton('retake', array('retake', get_string('canretake', 'lesson', $COURSE->student), 'lesson'));
75 $mform->setDefault('retake', 0);
77 $options = array();
78 $options[0] = get_string('usemean', 'lesson');
79 $options[1] = get_string('usemaximum', 'lesson');
80 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
81 $mform->setHelpButton('usemaxgrade', array('handlingofretakes', get_string('handlingofretakes', 'lesson'), 'lesson'));
82 $mform->setDefault('usemaxgrade', 0);
84 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
85 $mform->setHelpButton('ongoing', array('ongoing', get_string('ongoing', 'lesson'), 'lesson'));
86 $mform->setDefault('ongoing', 0);
88 //-------------------------------------------------------------------------------
89 $mform->addElement('header', '', get_string('flowcontrol', 'lesson'));
91 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
92 $mform->setHelpButton('modattempts', array('modattempts', get_string('modattempts', 'lesson'), 'lesson'));
93 $mform->setDefault('modattempts', 0);
95 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
96 $mform->setHelpButton('review', array('review', get_string('displayreview', 'lesson'), 'lesson'));
97 $mform->setDefault('review', 0);
99 $numbers = array();
100 for ($i=10; $i>0; $i--) {
101 $numbers[$i] = $i;
103 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
104 $mform->setHelpButton('maxattempts', array('maxattempts', get_string('maximumnumberofattempts', 'lesson'), 'lesson'));
105 $mform->setDefault('maxattempts', 1);
107 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $LESSON_NEXTPAGE_ACTION);
108 $mform->setHelpButton('nextpagedefault', array('nextpageaction', get_string('actionaftercorrectanswer', 'lesson'), 'lesson'));
109 $mform->setDefault('nextpagedefault', 0);
111 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
112 $mform->setHelpButton('feedback', array('feedback', get_string('displaydefaultfeedback', 'lesson'), 'lesson'));
113 $mform->setDefault('feedback', 0);
115 $numbers = array();
116 for ($i = 100; $i >= 0; $i--) {
117 $numbers[$i] = $i;
119 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
120 $mform->setHelpButton('minquestions', array('minquestions', get_string('minimumnumberofquestions', 'lesson'), 'lesson'));
121 $mform->setDefault('minquestions', 0);
123 $numbers = array();
124 for ($i = 100; $i >= 0; $i--) {
125 $numbers[$i] = $i;
127 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
128 $mform->setHelpButton('maxpages', array('maxpages', get_string('numberofpagestoshow', 'lesson'), 'lesson'));
129 $mform->setDefault('maxpages', 0);
131 //-------------------------------------------------------------------------------
132 $mform->addElement('header', '', get_string('lessonformating', 'lesson'));
134 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
135 $mform->setHelpButton('slideshow', array('slideshow', get_string('slideshow', 'lesson'), 'lesson'));
136 $mform->setDefault('slideshow', 0);
138 $mform->addElement('text', 'width', get_string('slideshowwidth', 'lesson'));
139 $mform->setDefault('width', 640);
140 $mform->addRule('width', null, 'required', null, 'client');
141 $mform->addRule('width', null, 'numeric', null, 'client');
142 $mform->setHelpButton('width', array('width', get_string('slideshowwidth', 'lesson'), 'lesson'));
143 $mform->setType('width', PARAM_INT);
145 $mform->addElement('text', 'height', get_string('slideshowheight', 'lesson'));
146 $mform->setDefault('height', 480);
147 $mform->addRule('height', null, 'required', null, 'client');
148 $mform->addRule('height', null, 'numeric', null, 'client');
149 $mform->setHelpButton('height', array('height', get_string('slideshowheight', 'lesson'), 'lesson'));
150 $mform->setType('height', PARAM_INT);
152 $mform->addElement('text', 'bgcolor', get_string('slideshowbgcolor', 'lesson'));
153 $mform->setDefault('bgcolor', '#FFFFFF');
154 $mform->addRule('bgcolor', null, 'required', null, 'client');
155 $mform->setHelpButton('bgcolor', array('bgcolor', get_string('slideshowbgcolor', 'lesson'), 'lesson'));
156 $mform->setType('bgcolor', PARAM_TEXT);
158 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
159 $mform->setHelpButton('displayleft', array('displayleft', get_string('displayleftmenu', 'lesson'), 'lesson'));
160 $mform->setDefault('displayleft', 0);
162 $options = array();
163 for($i = 100; $i >= 0; $i--) {
164 $options[$i] = $i.'%';
166 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
167 $mform->setDefault('displayleftif', 0);
169 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
170 $mform->setHelpButton('progressbar', array('progressbar', get_string('progressbar', 'lesson'), 'lesson'));
171 $mform->setDefault('progressbar', 0);
174 //-------------------------------------------------------------------------------
175 $mform->addElement('header', '', get_string('accesscontrol', 'lesson'));
177 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
178 $mform->setHelpButton('usepassword', array('usepassword', get_string('usepassword', 'lesson'), 'lesson'));
179 $mform->setDefault('usepassword', 0);
181 $mform->addElement('text', 'password', get_string('password', 'lesson'));
182 $mform->setHelpButton('password', array('password', get_string('password', 'lesson'), 'lesson'));
183 $mform->setDefault('password', '');
184 //never displayed converted to md5
185 $mform->setType('password', PARAM_RAW);
187 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
188 $mform->setDefault('available', 0);
190 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
191 $mform->setDefault('deadline', 0);
193 //-------------------------------------------------------------------------------
194 $mform->addElement('header', '', get_string('dependencyon', 'lesson'));
196 $options = array(0=>get_string('none'));
197 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
198 foreach($lessons as $lesson) {
199 if ($lesson->id != $this->_instance){
200 $options[$lesson->id] = format_string($lesson->name, true);
205 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
206 $mform->setHelpButton('dependency', array('dependency', get_string('dependency', 'lesson'), 'lesson'));
207 $mform->setDefault('dependency', 0);
209 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
210 $mform->setDefault('timespent', 0);
211 $mform->setType('timespent', PARAM_INT);
213 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
214 $mform->setDefault('completed', 0);
216 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
217 $mform->setDefault('gradebetterthan', 0);
218 $mform->setType('gradebetterthan', PARAM_INT);
220 //-------------------------------------------------------------------------------
221 $mform->addElement('header', '', get_string('mediafile', 'lesson'));
223 $mform->addElement('choosecoursefile', 'mediafile', get_string('mediafile', 'lesson'), array('courseid'=>$COURSE->id));
224 $mform->setHelpButton('mediafile', array('mediafile', get_string('mediafile', 'lesson'), 'lesson'));
225 $mform->setDefault('mediafile', '');
226 $mform->setType('mediafile', PARAM_RAW);
228 $mform->addElement('selectyesno', 'mediaclose', get_string('mediaclose', 'lesson'));
229 $mform->setDefault('mediaclose', 0);
231 $mform->addElement('text', 'mediaheight', get_string('mediaheight', 'lesson'));
232 $mform->setHelpButton('mediaheight', array('mediaheight', get_string('mediaheight', 'lesson'), 'lesson'));
233 $mform->setDefault('mediaheight', 100);
234 $mform->addRule('mediaheight', null, 'required', null, 'client');
235 $mform->addRule('mediaheight', null, 'numeric', null, 'client');
236 $mform->setType('mediaheight', PARAM_INT);
238 $mform->addElement('text', 'mediawidth', get_string('mediawidth', 'lesson'));
239 $mform->setHelpButton('mediawidth', array('mediawidth', get_string('mediawidth', 'lesson'), 'lesson'));
240 $mform->setDefault('mediawidth', 650);
241 $mform->addRule('mediawidth', null, 'required', null, 'client');
242 $mform->addRule('mediawidth', null, 'numeric', null, 'client');
243 $mform->setType('mediawidth', PARAM_INT);
245 //-------------------------------------------------------------------------------
246 $mform->addElement('header', '', get_string('other', 'lesson'));
248 // get the modules
249 if ($mods = get_course_mods($COURSE->id)) {
250 $modinstances = array();
251 foreach ($mods as $mod) {
253 // get the module name and then store it in a new array
254 if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
255 if (isset($this->_cm->id) and $this->_cm->id != $mod->id){
256 $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
260 asort($modinstances); // sort by module name
261 $modinstances=array(0=>get_string('none'))+$modinstances;
263 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
264 $mform->setHelpButton('activitylink', array('activitylink', get_string('activitylink', 'lesson'), 'lesson'));
265 $mform->setDefault('activitylink', 0);
269 $mform->addElement('text', 'maxhighscores', get_string('maxhighscores', 'lesson'));
270 $mform->setHelpButton('maxhighscores', array('maxhighscores', get_string('maxhighscores', 'lesson'), 'lesson'));
271 $mform->setDefault('maxhighscores', 10);
272 $mform->addRule('maxhighscores', null, 'required', null, 'client');
273 $mform->addRule('maxhighscores', null, 'numeric', null, 'client');
274 $mform->setType('maxhighscores', PARAM_INT);
276 $mform->addElement('selectyesno', 'lessondefault', get_string('lessondefault', 'lesson'));
277 $mform->setHelpButton('lessondefault', array('lessondefault', get_string('lessondefault', 'lesson'), 'lesson'));
278 $mform->setDefault('lessondefault', 0);
280 //-------------------------------------------------------------------------------
281 $features = new stdClass;
282 $features->groups = false;
283 $features->groupings = true;
284 $features->groupmembersonly = true;
285 $this->standard_coursemodule_elements($features);
286 //-------------------------------------------------------------------------------
287 // buttons
288 $this->add_action_buttons();
292 * Enforce defaults here
294 * @param array $default_values Form defaults
295 * @return void
297 function data_preprocessing(&$default_values) {
298 if (isset($default_values['conditions'])) {
299 $conditions = unserialize($default_values['conditions']);
300 $default_values['timespent'] = $conditions->timespent;
301 $default_values['completed'] = $conditions->completed;
302 $default_values['gradebetterthan'] = $conditions->gradebetterthan;
304 if (isset($default_values['password'])) {
305 unset($default_values['password']);
307 if (isset($default_values['add']) and $defaults = get_record('lesson_default', 'course', $default_values['course'])) {
308 foreach ($defaults as $fieldname => $default) {
309 switch ($fieldname) {
310 case 'conditions':
311 $conditions = unserialize($default);
312 $default_values['timespent'] = $conditions->timespent;
313 $default_values['completed'] = $conditions->completed;
314 $default_values['gradebetterthan'] = $conditions->gradebetterthan;
315 break;
316 default:
317 $default_values[$fieldname] = $default;
318 break;
325 * Enforce validation rules here
327 * @param object $data Post data to validate
328 * @return array
330 function validation($data) {
331 $errors = parent::validation($data);
332 if ($errors === true) {
333 $errors = array();
336 if (empty($data['maxtime']) and !empty($data['timed'])) {
337 $errors['timedgrp'] = get_string('err_numeric', 'form');
340 if (count($errors) == 0) {
341 return true;
342 } else {
343 return $errors;