PageRenderTime 32ms CodeModel.GetById 12ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/lesson/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 346 lines | 222 code | 66 blank | 58 comment | 20 complexity | f8059fcc090207624a21a558694e67f0 MD5 | raw file
  1<?php
  2
  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.
 14//
 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/>.
 17
 18/**
 19 * Form to define a new instance of lesson or edit an instance.
 20 * It is used from /course/modedit.php.
 21 *
 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 **/
 27
 28defined('MOODLE_INTERNAL') || die();
 29
 30require_once($CFG->dirroot.'/course/moodleform_mod.php');
 31require_once($CFG->dirroot.'/mod/lesson/locallib.php');
 32
 33class mod_lesson_mod_form extends moodleform_mod {
 34
 35    protected $course = null;
 36
 37    public function mod_lesson_mod_form($current, $section, $cm, $course) {
 38        $this->course = $course;
 39        parent::moodleform_mod($current, $section, $cm, $course);
 40    }
 41
 42    function definition() {
 43        global $CFG, $COURSE, $DB;
 44
 45        $mform    = $this->_form;
 46
 47//-------------------------------------------------------------------------------
 48        $mform->addElement('header', 'general', get_string('general', 'form'));
 49
 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);
 54
 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);
 59
 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);
 64
 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);
 69
 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);
 74
 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);
 79
 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);
 84
 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);
 90        }
 91        $mform->addRule('name', null, 'required', null, 'client');
 92        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 93
 94        // Appearance.
 95        $mform->addElement('header', 'appearancehdr', get_string('appearance'));
 96
 97        $filemanageroptions = array();
 98        $filemanageroptions['filetypes'] = '*';
 99        $filemanageroptions['maxbytes'] = $this->course->maxbytes;
100        $filemanageroptions['subdirs'] = 0;
101        $filemanageroptions['maxfiles'] = 1;
102
103        $mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions);
104        $mform->addHelpButton('mediafile', 'mediafile', 'lesson');
105
106        $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
107        $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
108        $mform->setDefault('progressbar', 0);
109
110        $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
111        $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
112        $mform->setDefault('ongoing', 0);
113
114        $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
115        $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
116        $mform->setDefault('displayleft', 0);
117
118        $options = array();
119        for($i = 100; $i >= 0; $i--) {
120            $options[$i] = $i.'%';
121        }
122        $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
123        $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
124        $mform->setDefault('displayleftif', 0);
125
126        $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
127        $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
128        $mform->setDefault('slideshow', 0);
129
130        $numbers = array();
131        for ($i = 20; $i > 1; $i--) {
132            $numbers[$i] = $i;
133        }
134
135        $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
136        $mform->setDefault('maxanswers', $CFG->lesson_maxanswers);
137        $mform->setType('maxanswers', PARAM_INT);
138        $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
139
140        $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
141        $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
142        $mform->setDefault('feedback', 0);
143
144        // Get the modules.
145        if ($mods = get_course_mods($COURSE->id)) {
146            $modinstances = array();
147            foreach ($mods as $mod) {
148
149                // get the module name and then store it in a new array
150                if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
151                    if (isset($this->_cm->id) and $this->_cm->id != $mod->id){
152                        $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
153                    }
154                }
155            }
156            asort($modinstances); // sort by module name
157            $modinstances=array(0=>get_string('none'))+$modinstances;
158
159            $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
160            $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
161            $mform->setDefault('activitylink', 0);
162        }
163
164        // Availability.
165        $mform->addElement('header', 'availabilityhdr', get_string('availability'));
166
167        $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
168        $mform->setDefault('available', 0);
169
170        $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
171        $mform->setDefault('deadline', 0);
172
173        // Create a text box that can be enabled/disabled for lesson time limit
174        $timedgrp = array();
175        $timedgrp[] = &$mform->createElement('text', 'maxtime');
176        $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
177        $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
178        $mform->disabledIf('timedgrp', 'timed');
179
180        // Add numeric rule to text field
181        $timedgrprules = array();
182        $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
183        $mform->addGroupRule('timedgrp', $timedgrprules);
184
185        // Rest of group setup
186        $mform->setDefault('timed', 0);
187        $mform->setDefault('maxtime', 20);
188        $mform->setType('maxtime', PARAM_INT);
189
190        $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
191        $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
192        $mform->setDefault('usepassword', 0);
193
194        $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
195        $mform->setDefault('password', '');
196        $mform->setType('password', PARAM_RAW);
197        $mform->disabledIf('password', 'usepassword', 'eq', 0);
198        $mform->disabledIf('passwordunmask', 'usepassword', 'eq', 0);
199
200        // Dependent on.
201        $mform->addElement('header', 'dependencyon', get_string('prerequisitelesson', 'lesson'));
202
203        $options = array(0=>get_string('none'));
204        if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
205            foreach($lessons as $lesson) {
206                if ($lesson->id != $this->_instance){
207                    $options[$lesson->id] = format_string($lesson->name, true);
208                }
209
210            }
211        }
212        $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
213        $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
214        $mform->setDefault('dependency', 0);
215
216        $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
217        $mform->setDefault('timespent', 0);
218        $mform->setType('timespent', PARAM_INT);
219        $mform->disabledIf('timespent', 'dependency', 'eq', 0);
220
221        $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
222        $mform->setDefault('completed', 0);
223        $mform->disabledIf('completed', 'dependency', 'eq', 0);
224
225        $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
226        $mform->setDefault('gradebetterthan', 0);
227        $mform->setType('gradebetterthan', PARAM_INT);
228        $mform->disabledIf('gradebetterthan', 'dependency', 'eq', 0);
229
230        // Flow control.
231        $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
232
233        $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
234        $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
235        $mform->setDefault('modattempts', 0);
236
237        $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
238        $mform->addHelpButton('review', 'displayreview', 'lesson');
239        $mform->setDefault('review', 0);
240
241        $numbers = array();
242        for ($i = 10; $i > 0; $i--) {
243            $numbers[$i] = $i;
244        }
245        $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
246        $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
247        $mform->setDefault('maxattempts', 1);
248
249        $defaultnextpages = array();
250        $defaultnextpages[0] = get_string('normal', 'lesson');
251        $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
252        $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
253        $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
254        $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
255        $mform->setDefault('nextpagedefault', $CFG->lesson_defaultnextpage);
256
257        $numbers = array();
258        for ($i = 100; $i >= 0; $i--) {
259            $numbers[$i] = $i;
260        }
261        $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
262        $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
263        $mform->setDefault('maxpages', 0);
264
265        // Grade.
266        $this->standard_grading_coursemodule_elements();
267
268        // No header here, so that the following settings are displayed in the grade section.
269
270        $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
271        $mform->addHelpButton('practice', 'practice', 'lesson');
272        $mform->setDefault('practice', 0);
273
274        $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
275        $mform->addHelpButton('custom', 'customscoring', 'lesson');
276        $mform->setDefault('custom', 1);
277
278        $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
279        $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
280        $mform->setDefault('retake', 0);
281
282        $options = array();
283        $options[0] = get_string('usemean', 'lesson');
284        $options[1] = get_string('usemaximum', 'lesson');
285        $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
286        $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
287        $mform->setDefault('usemaxgrade', 0);
288        $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
289
290        $numbers = array();
291        for ($i = 100; $i >= 0; $i--) {
292            $numbers[$i] = $i;
293        }
294        $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
295        $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
296        $mform->setDefault('minquestions', 0);
297
298//-------------------------------------------------------------------------------
299        $this->standard_coursemodule_elements();
300//-------------------------------------------------------------------------------
301// buttons
302        $this->add_action_buttons();
303    }
304
305    /**
306     * Enforce defaults here
307     *
308     * @param array $default_values Form defaults
309     * @return void
310     **/
311    function data_preprocessing(&$default_values) {
312        if (isset($default_values['conditions'])) {
313            $conditions = unserialize($default_values['conditions']);
314            $default_values['timespent'] = $conditions->timespent;
315            $default_values['completed'] = $conditions->completed;
316            $default_values['gradebetterthan'] = $conditions->gradebetterthan;
317        }
318
319        if ($this->current->instance) {
320            // Editing existing instance - copy existing files into draft area.
321            $draftitemid = file_get_submitted_draft_itemid('mediafile');
322            file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
323            $default_values['mediafile'] = $draftitemid;
324        }
325    }
326
327    /**
328     * Enforce validation rules here
329     *
330     * @param object $data Post data to validate
331     * @return array
332     **/
333    function validation($data, $files) {
334        $errors = parent::validation($data, $files);
335
336        if (empty($data['maxtime']) and !empty($data['timed'])) {
337            $errors['timedgrp'] = get_string('err_numeric', 'form');
338        }
339        if (!empty($data['usepassword']) && empty($data['password'])) {
340            $errors['password'] = get_string('emptypassword', 'lesson');
341        }
342
343        return $errors;
344    }
345}
346