MDL-29199 dml: fix query logging on Oracle
[moodle.git] / mod / lesson / mod_form.php
blob3b2f1a20a71204d3b13aa41d369a304cc0a318d9
1 <?php
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.
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/>.
18 /**
19 * Form to define a new instance of lesson or edit an instance.
20 * It is used from /course/modedit.php.
22 * @package mod
23 * @subpackage lesson
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
26 **/
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot.'/course/moodleform_mod.php');
31 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
33 class mod_lesson_mod_form extends moodleform_mod {
35 protected $course = null;
37 public function mod_lesson_mod_form($current, $section, $cm, $course) {
38 $this->course = $course;
39 parent::moodleform_mod($current, $section, $cm, $course);
42 function definition() {
43 global $CFG, $COURSE, $DB;
45 $mform = $this->_form;
47 //-------------------------------------------------------------------------------
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 /** Legacy maximum highscores element to maintain backwards compatibility */
81 $mform->addElement('hidden', 'maxhighscores');
82 $mform->setType('maxhighscores', PARAM_INT);
83 $mform->setDefault('maxhighscores', $CFG->lesson_maxhighscores);
85 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
86 if (!empty($CFG->formatstringstriptags)) {
87 $mform->setType('name', PARAM_TEXT);
88 } else {
89 $mform->setType('name', PARAM_CLEANHTML);
91 $mform->addRule('name', null, 'required', null, 'client');
93 // Create a text box that can be enabled/disabled for lesson time limit
94 $timedgrp = array();
95 $timedgrp[] = &$mform->createElement('text', 'maxtime');
96 $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
97 $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
98 $mform->disabledIf('timedgrp', 'timed');
100 // Add numeric rule to text field
101 $timedgrprules = array();
102 $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
103 $mform->addGroupRule('timedgrp', $timedgrprules);
105 // Rest of group setup
106 $mform->setDefault('timed', 0);
107 $mform->setDefault('maxtime', 20);
108 $mform->setType('maxtime', PARAM_INT);
110 $numbers = array();
111 for ($i=20; $i>1; $i--) {
112 $numbers[$i] = $i;
115 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
116 $mform->setDefault('available', 0);
118 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
119 $mform->setDefault('deadline', 0);
121 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
122 $mform->setDefault('maxanswers', $CFG->lesson_maxanswers);
123 $mform->setType('maxanswers', PARAM_INT);
124 $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
126 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
127 $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
128 $mform->setDefault('usepassword', 0);
129 $mform->setAdvanced('usepassword');
131 $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
132 $mform->setDefault('password', '');
133 $mform->setType('password', PARAM_RAW);
134 $mform->setAdvanced('password');
135 $mform->disabledIf('password', 'usepassword', 'eq', 0);
137 $this->standard_grading_coursemodule_elements();
139 //-------------------------------------------------------------------------------
140 $mform->addElement('header', 'gradeoptions', get_string('gradeoptions', 'lesson'));
142 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
143 $mform->addHelpButton('practice', 'practice', 'lesson');
144 $mform->setDefault('practice', 0);
146 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
147 $mform->addHelpButton('custom', 'customscoring', 'lesson');
148 $mform->setDefault('custom', 1);
150 $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
151 $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
152 $mform->setDefault('retake', 0);
154 $options = array();
155 $options[0] = get_string('usemean', 'lesson');
156 $options[1] = get_string('usemaximum', 'lesson');
157 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
158 $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
159 $mform->setDefault('usemaxgrade', 0);
160 $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
162 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
163 $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
164 $mform->setDefault('ongoing', 0);
166 //-------------------------------------------------------------------------------
167 $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
169 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
170 $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
171 $mform->setDefault('modattempts', 0);
173 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
174 $mform->addHelpButton('review', 'displayreview', 'lesson');
175 $mform->setDefault('review', 0);
177 $numbers = array();
178 for ($i=10; $i>0; $i--) {
179 $numbers[$i] = $i;
181 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
182 $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
183 $mform->setDefault('maxattempts', 1);
185 $defaultnextpages = array();
186 $defaultnextpages[0] = get_string('normal', 'lesson');
187 $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
188 $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
189 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
190 $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
191 $mform->setDefault('nextpagedefault', $CFG->lesson_defaultnextpage);
192 $mform->setAdvanced('nextpagedefault');
194 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
195 $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
196 $mform->setDefault('feedback', 0);
198 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
199 $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
200 $mform->setDefault('progressbar', 0);
202 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
203 $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
204 $mform->setDefault('displayleft', 0);
206 $options = array();
207 for($i = 100; $i >= 0; $i--) {
208 $options[$i] = $i.'%';
210 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
211 $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
212 $mform->setDefault('displayleftif', 0);
213 $mform->setAdvanced('displayleftif');
215 $numbers = array();
216 for ($i = 100; $i >= 0; $i--) {
217 $numbers[$i] = $i;
219 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
220 $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
221 $mform->setDefault('minquestions', 0);
222 $mform->setAdvanced('minquestions');
224 $numbers = array();
225 for ($i = 100; $i >= 0; $i--) {
226 $numbers[$i] = $i;
228 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
229 $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
230 $mform->setAdvanced('maxpages');
231 $mform->setDefault('maxpages', 0);
233 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
234 $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
235 $mform->setDefault('slideshow', 0);
236 $mform->setAdvanced('slideshow');
238 // get the modules
239 if ($mods = get_course_mods($COURSE->id)) {
240 $modinstances = array();
241 foreach ($mods as $mod) {
243 // get the module name and then store it in a new array
244 if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
245 if (isset($this->_cm->id) and $this->_cm->id != $mod->id){
246 $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
250 asort($modinstances); // sort by module name
251 $modinstances=array(0=>get_string('none'))+$modinstances;
253 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
254 $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
255 $mform->setDefault('activitylink', 0);
256 $mform->setAdvanced('activitylink');
259 //-------------------------------------------------------------------------------
260 $mform->addElement('header', 'mediafileheader', get_string('mediafile', 'lesson'));
262 $filepickeroptions = array();
263 $filepickeroptions['filetypes'] = '*';
264 $filepickeroptions['maxbytes'] = $this->course->maxbytes;
265 $mform->addElement('filepicker', 'mediafilepicker', get_string('mediafile', 'lesson'), null, $filepickeroptions);
266 $mform->addHelpButton('mediafilepicker', 'mediafile', 'lesson');
268 //-------------------------------------------------------------------------------
269 $mform->addElement('header', 'dependencyon', get_string('dependencyon', 'lesson'));
271 $options = array(0=>get_string('none'));
272 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
273 foreach($lessons as $lesson) {
274 if ($lesson->id != $this->_instance){
275 $options[$lesson->id] = format_string($lesson->name, true);
280 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
281 $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
282 $mform->setDefault('dependency', 0);
284 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
285 $mform->setDefault('timespent', 0);
286 $mform->setType('timespent', PARAM_INT);
288 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
289 $mform->setDefault('completed', 0);
291 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
292 $mform->setDefault('gradebetterthan', 0);
293 $mform->setType('gradebetterthan', PARAM_INT);
295 //-------------------------------------------------------------------------------
296 $this->standard_coursemodule_elements();
297 //-------------------------------------------------------------------------------
298 // buttons
299 $this->add_action_buttons();
303 * Enforce defaults here
305 * @param array $default_values Form defaults
306 * @return void
308 function data_preprocessing(&$default_values) {
309 global $DB;
310 global $module;
311 if (isset($default_values['conditions'])) {
312 $conditions = unserialize($default_values['conditions']);
313 $default_values['timespent'] = $conditions->timespent;
314 $default_values['completed'] = $conditions->completed;
315 $default_values['gradebetterthan'] = $conditions->gradebetterthan;
317 // after this passwords are clear text, MDL-11090
318 if (isset($default_values['password']) and ($module->version<2008112600)) {
319 unset($default_values['password']);
322 if ($this->current->instance) {
323 // editing existing instance - copy existing files into draft area
324 $draftitemid = file_get_submitted_draft_itemid('mediafilepicker');
325 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
326 $default_values['mediafilepicker'] = $draftitemid;
331 * Enforce validation rules here
333 * @param object $data Post data to validate
334 * @return array
336 function validation($data, $files) {
337 $errors = parent::validation($data, $files);
339 if (empty($data['maxtime']) and !empty($data['timed'])) {
340 $errors['timedgrp'] = get_string('err_numeric', 'form');
342 if (!empty($data['usepassword']) && empty($data['password'])) {
343 $errors['password'] = get_string('emptypassword', 'lesson');
346 return $errors;