Differences Between: [Versions 310 and 311] [Versions 311 and 403] [Versions 39 and 311]
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_lesson 23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late 25 **/ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 require_once($CFG->dirroot.'/course/moodleform_mod.php'); 30 require_once($CFG->dirroot.'/mod/lesson/locallib.php'); 31 32 class mod_lesson_mod_form extends moodleform_mod { 33 34 protected $course = null; 35 36 public function __construct($current, $section, $cm, $course) { 37 $this->course = $course; 38 parent::__construct($current, $section, $cm, $course); 39 } 40 41 /** 42 * Old syntax of class constructor. Deprecated in PHP7. 43 * 44 * @deprecated since Moodle 3.1 45 */ 46 public function mod_lesson_mod_form($current, $section, $cm, $course) { 47 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 48 self::__construct($current, $section, $cm, $course); 49 } 50 51 function definition() { 52 global $CFG, $COURSE, $DB, $OUTPUT; 53 54 $mform = $this->_form; 55 56 $lessonconfig = get_config('mod_lesson'); 57 58 $mform->addElement('header', 'general', get_string('general', 'form')); 59 60 /** Legacy slideshow width element to maintain backwards compatibility */ 61 $mform->addElement('hidden', 'width'); 62 $mform->setType('width', PARAM_INT); 63 $mform->setDefault('width', $lessonconfig->slideshowwidth); 64 65 /** Legacy slideshow height element to maintain backwards compatibility */ 66 $mform->addElement('hidden', 'height'); 67 $mform->setType('height', PARAM_INT); 68 $mform->setDefault('height', $lessonconfig->slideshowheight); 69 70 /** Legacy slideshow background color element to maintain backwards compatibility */ 71 $mform->addElement('hidden', 'bgcolor'); 72 $mform->setType('bgcolor', PARAM_TEXT); 73 $mform->setDefault('bgcolor', $lessonconfig->slideshowbgcolor); 74 75 /** Legacy media popup width element to maintain backwards compatibility */ 76 $mform->addElement('hidden', 'mediawidth'); 77 $mform->setType('mediawidth', PARAM_INT); 78 $mform->setDefault('mediawidth', $lessonconfig->mediawidth); 79 80 /** Legacy media popup height element to maintain backwards compatibility */ 81 $mform->addElement('hidden', 'mediaheight'); 82 $mform->setType('mediaheight', PARAM_INT); 83 $mform->setDefault('mediaheight', $lessonconfig->mediaheight); 84 85 /** Legacy media popup close button element to maintain backwards compatibility */ 86 $mform->addElement('hidden', 'mediaclose'); 87 $mform->setType('mediaclose', PARAM_BOOL); 88 $mform->setDefault('mediaclose', $lessonconfig->mediaclose); 89 90 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64')); 91 if (!empty($CFG->formatstringstriptags)) { 92 $mform->setType('name', PARAM_TEXT); 93 } else { 94 $mform->setType('name', PARAM_CLEANHTML); 95 } 96 $mform->addRule('name', null, 'required', null, 'client'); 97 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); 98 $this->standard_intro_elements(); 99 100 // Appearance. 101 $mform->addElement('header', 'appearancehdr', get_string('appearance')); 102 103 $filemanageroptions = array(); 104 $filemanageroptions['filetypes'] = '*'; 105 $filemanageroptions['maxbytes'] = $this->course->maxbytes; 106 $filemanageroptions['subdirs'] = 0; 107 $filemanageroptions['maxfiles'] = 1; 108 109 $mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions); 110 $mform->addHelpButton('mediafile', 'mediafile', 'lesson'); 111 $mform->setAdvanced('mediafile', $lessonconfig->mediafile_adv); 112 113 $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson')); 114 $mform->addHelpButton('progressbar', 'progressbar', 'lesson'); 115 $mform->setDefault('progressbar', $lessonconfig->progressbar); 116 $mform->setAdvanced('progressbar', $lessonconfig->progressbar_adv); 117 118 $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson')); 119 $mform->addHelpButton('ongoing', 'ongoing', 'lesson'); 120 $mform->setDefault('ongoing', $lessonconfig->ongoing); 121 $mform->setAdvanced('ongoing', $lessonconfig->ongoing_adv); 122 123 $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson')); 124 $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson'); 125 $mform->setDefault('displayleft', $lessonconfig->displayleftmenu); 126 $mform->setAdvanced('displayleft', $lessonconfig->displayleftmenu_adv); 127 128 $options = array(); 129 for($i = 100; $i >= 0; $i--) { 130 $options[$i] = $i.'%'; 131 } 132 $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options); 133 $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson'); 134 $mform->setDefault('displayleftif', $lessonconfig->displayleftif); 135 $mform->setAdvanced('displayleftif', $lessonconfig->displayleftif_adv); 136 137 $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson')); 138 $mform->addHelpButton('slideshow', 'slideshow', 'lesson'); 139 $mform->setDefault('slideshow', $lessonconfig->slideshow); 140 $mform->setAdvanced('slideshow', $lessonconfig->slideshow_adv); 141 142 $numbers = array(); 143 for ($i = 20; $i > 1; $i--) { 144 $numbers[$i] = $i; 145 } 146 147 $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers); 148 $mform->setDefault('maxanswers', $lessonconfig->maxanswers); 149 $mform->setAdvanced('maxanswers', $lessonconfig->maxanswers_adv); 150 $mform->setType('maxanswers', PARAM_INT); 151 $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson'); 152 153 $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson')); 154 $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson'); 155 $mform->setDefault('feedback', $lessonconfig->defaultfeedback); 156 $mform->setAdvanced('feedback', $lessonconfig->defaultfeedback_adv); 157 158 // Get the modules. 159 if ($mods = get_course_mods($COURSE->id)) { 160 $modinstances = array(); 161 foreach ($mods as $mod) { 162 // Get the module name and then store it in a new array. 163 if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) { 164 // Exclude this lesson, if it's already been saved. 165 if (!isset($this->_cm->id) || $this->_cm->id != $mod->id) { 166 $modinstances[$mod->id] = $mod->modname.' - '.$module->name; 167 } 168 } 169 } 170 asort($modinstances); // Sort by module name. 171 $modinstances=array(0=>get_string('none'))+$modinstances; 172 173 $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances); 174 $mform->addHelpButton('activitylink', 'activitylink', 'lesson'); 175 $mform->setDefault('activitylink', 0); 176 $mform->setAdvanced('activitylink', $lessonconfig->activitylink_adv); 177 } 178 179 // Availability. 180 $mform->addElement('header', 'availabilityhdr', get_string('availability')); 181 182 $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true)); 183 $mform->setDefault('available', 0); 184 185 $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true)); 186 $mform->setDefault('deadline', 0); 187 188 // Time limit. 189 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'lesson'), 190 array('optional' => true)); 191 $mform->addHelpButton('timelimit', 'timelimit', 'lesson'); 192 $mform->setAdvanced('timelimit', $lessonconfig->timelimit_adv); 193 $mform->setDefault('timelimit', $lessonconfig->timelimit); 194 195 $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson')); 196 $mform->addHelpButton('usepassword', 'usepassword', 'lesson'); 197 $mform->setDefault('usepassword', $lessonconfig->password); 198 $mform->setAdvanced('usepassword', $lessonconfig->password_adv); 199 200 $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson')); 201 $mform->setDefault('password', ''); 202 $mform->setAdvanced('password', $lessonconfig->password_adv); 203 $mform->setType('password', PARAM_RAW); 204 $mform->hideIf('password', 'usepassword', 'eq', 0); 205 $mform->hideIf('passwordunmask', 'usepassword', 'eq', 0); 206 207 // Dependent on. 208 if ($this->current && isset($this->current->dependency) && $this->current->dependency) { 209 $mform->addElement('header', 'dependencyon', get_string('prerequisitelesson', 'lesson')); 210 $mform->addElement('static', 'warningobsolete', 211 get_string('warning', 'lesson'), 212 get_string('prerequisiteisobsolete', 'lesson')); 213 $options = array(0 => get_string('none')); 214 if ($lessons = get_all_instances_in_course('lesson', $COURSE)) { 215 foreach ($lessons as $lesson) { 216 if ($lesson->id != $this->_instance) { 217 $options[$lesson->id] = format_string($lesson->name, true); 218 } 219 220 } 221 } 222 $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options); 223 $mform->addHelpButton('dependency', 'dependencyon', 'lesson'); 224 $mform->setDefault('dependency', 0); 225 226 $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson')); 227 $mform->setDefault('timespent', 0); 228 $mform->setType('timespent', PARAM_INT); 229 $mform->disabledIf('timespent', 'dependency', 'eq', 0); 230 231 $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson')); 232 $mform->setDefault('completed', 0); 233 $mform->disabledIf('completed', 'dependency', 'eq', 0); 234 235 $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson')); 236 $mform->setDefault('gradebetterthan', 0); 237 $mform->setType('gradebetterthan', PARAM_INT); 238 $mform->disabledIf('gradebetterthan', 'dependency', 'eq', 0); 239 } else { 240 $mform->addElement('hidden', 'dependency', 0); 241 $mform->setType('dependency', PARAM_INT); 242 $mform->addElement('hidden', 'timespent', 0); 243 $mform->setType('timespent', PARAM_INT); 244 $mform->addElement('hidden', 'completed', 0); 245 $mform->setType('completed', PARAM_INT); 246 $mform->addElement('hidden', 'gradebetterthan', 0); 247 $mform->setType('gradebetterthan', PARAM_INT); 248 $mform->setConstants(array('dependency' => 0, 'timespent' => 0, 249 'completed' => 0, 'gradebetterthan' => 0)); 250 } 251 252 // Allow to enable offline lessons only if the Mobile services are enabled. 253 if ($CFG->enablemobilewebservice) { 254 $mform->addElement('selectyesno', 'allowofflineattempts', get_string('allowofflineattempts', 'lesson')); 255 $mform->addHelpButton('allowofflineattempts', 'allowofflineattempts', 'lesson'); 256 $mform->setDefault('allowofflineattempts', 0); 257 $mform->setAdvanced('allowofflineattempts'); 258 $mform->disabledIf('allowofflineattempts', 'timelimit[number]', 'neq', 0); 259 260 $mform->addElement('static', 'allowofflineattemptswarning', '', 261 $OUTPUT->notification(get_string('allowofflineattempts_help', 'lesson'), 'warning')); 262 $mform->setAdvanced('allowofflineattemptswarning'); 263 } else { 264 $mform->addElement('hidden', 'allowofflineattempts', 0); 265 $mform->setType('allowofflineattempts', PARAM_INT); 266 } 267 268 // Flow control. 269 $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson')); 270 271 $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson')); 272 $mform->addHelpButton('modattempts', 'modattempts', 'lesson'); 273 $mform->setDefault('modattempts', $lessonconfig->modattempts); 274 $mform->setAdvanced('modattempts', $lessonconfig->modattempts_adv); 275 276 $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson')); 277 $mform->addHelpButton('review', 'displayreview', 'lesson'); 278 $mform->setDefault('review', $lessonconfig->displayreview); 279 $mform->setAdvanced('review', $lessonconfig->displayreview_adv); 280 281 $numbers = array('0' => get_string('unlimited')); 282 for ($i = 10; $i > 0; $i--) { 283 $numbers[$i] = $i; 284 } 285 $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers); 286 $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson'); 287 $mform->setDefault('maxattempts', $lessonconfig->maximumnumberofattempts); 288 $mform->setAdvanced('maxattempts', $lessonconfig->maximumnumberofattempts_adv); 289 290 $defaultnextpages = array(); 291 $defaultnextpages[0] = get_string('normal', 'lesson'); 292 $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson'); 293 $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson'); 294 $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages); 295 $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson'); 296 $mform->setDefault('nextpagedefault', $lessonconfig->defaultnextpage); 297 $mform->setAdvanced('nextpagedefault', $lessonconfig->defaultnextpage_adv); 298 299 $numbers = array(); 300 for ($i = 100; $i >= 0; $i--) { 301 $numbers[$i] = $i; 302 } 303 $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers); 304 $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson'); 305 $mform->setDefault('maxpages', $lessonconfig->numberofpagestoshow); 306 $mform->setAdvanced('maxpages', $lessonconfig->numberofpagestoshow_adv); 307 308 // Grade. 309 $this->standard_grading_coursemodule_elements(); 310 311 // No header here, so that the following settings are displayed in the grade section. 312 313 $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson')); 314 $mform->addHelpButton('practice', 'practice', 'lesson'); 315 $mform->setDefault('practice', $lessonconfig->practice); 316 $mform->setAdvanced('practice', $lessonconfig->practice_adv); 317 318 $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson')); 319 $mform->addHelpButton('custom', 'customscoring', 'lesson'); 320 $mform->setDefault('custom', $lessonconfig->customscoring); 321 $mform->setAdvanced('custom', $lessonconfig->customscoring_adv); 322 323 $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson')); 324 $mform->addHelpButton('retake', 'retakesallowed', 'lesson'); 325 $mform->setDefault('retake', $lessonconfig->retakesallowed); 326 $mform->setAdvanced('retake', $lessonconfig->retakesallowed_adv); 327 328 $options = array(); 329 $options[0] = get_string('usemean', 'lesson'); 330 $options[1] = get_string('usemaximum', 'lesson'); 331 $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options); 332 $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson'); 333 $mform->setDefault('usemaxgrade', $lessonconfig->handlingofretakes); 334 $mform->setAdvanced('usemaxgrade', $lessonconfig->handlingofretakes_adv); 335 $mform->hideIf('usemaxgrade', 'retake', 'eq', '0'); 336 337 $numbers = array(); 338 for ($i = 100; $i >= 0; $i--) { 339 $numbers[$i] = $i; 340 } 341 $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers); 342 $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson'); 343 $mform->setDefault('minquestions', $lessonconfig->minimumnumberofquestions); 344 $mform->setAdvanced('minquestions', $lessonconfig->minimumnumberofquestions_adv); 345 346 //------------------------------------------------------------------------------- 347 $this->standard_coursemodule_elements(); 348 //------------------------------------------------------------------------------- 349 // buttons 350 $this->add_action_buttons(); 351 } 352 353 /** 354 * Enforce defaults here 355 * 356 * @param array $defaultvalues Form defaults 357 * @return void 358 **/ 359 public function data_preprocessing(&$defaultvalues) { 360 if (isset($defaultvalues['conditions'])) { 361 $conditions = unserialize_object($defaultvalues['conditions']); 362 $defaultvalues['timespent'] = $conditions->timespent ?? 0; 363 $defaultvalues['completed'] = !empty($conditions->completed); 364 $defaultvalues['gradebetterthan'] = $conditions->gradebetterthan ?? 0; 365 } 366 367 // Set up the completion checkbox which is not part of standard data. 368 $defaultvalues['completiontimespentenabled'] = 369 !empty($defaultvalues['completiontimespent']) ? 1 : 0; 370 371 if ($this->current->instance) { 372 // Editing existing instance - copy existing files into draft area. 373 $draftitemid = file_get_submitted_draft_itemid('mediafile'); 374 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1)); 375 $defaultvalues['mediafile'] = $draftitemid; 376 } 377 } 378 379 /** 380 * Enforce validation rules here 381 * 382 * @param object $data Post data to validate 383 * @return array 384 **/ 385 function validation($data, $files) { 386 $errors = parent::validation($data, $files); 387 388 // Check open and close times are consistent. 389 if ($data['available'] != 0 && $data['deadline'] != 0 && 390 $data['deadline'] < $data['available']) { 391 $errors['deadline'] = get_string('closebeforeopen', 'lesson'); 392 } 393 394 if (!empty($data['usepassword']) && empty($data['password'])) { 395 $errors['password'] = get_string('emptypassword', 'lesson'); 396 } 397 398 return $errors; 399 } 400 401 /** 402 * Display module-specific activity completion rules. 403 * Part of the API defined by moodleform_mod 404 * @return array Array of string IDs of added items, empty array if none 405 */ 406 public function add_completion_rules() { 407 $mform = $this->_form; 408 409 $mform->addElement('checkbox', 'completionendreached', get_string('completionendreached', 'lesson'), 410 get_string('completionendreached_desc', 'lesson')); 411 // Enable this completion rule by default. 412 $mform->setDefault('completionendreached', 1); 413 414 $group = array(); 415 $group[] =& $mform->createElement('checkbox', 'completiontimespentenabled', '', 416 get_string('completiontimespent', 'lesson')); 417 $group[] =& $mform->createElement('duration', 'completiontimespent', '', array('optional' => false)); 418 $mform->addGroup($group, 'completiontimespentgroup', get_string('completiontimespentgroup', 'lesson'), array(' '), false); 419 $mform->disabledIf('completiontimespent[number]', 'completiontimespentenabled', 'notchecked'); 420 $mform->disabledIf('completiontimespent[timeunit]', 'completiontimespentenabled', 'notchecked'); 421 422 return array('completionendreached', 'completiontimespentgroup'); 423 } 424 425 /** 426 * Called during validation. Indicates whether a module-specific completion rule is selected. 427 * 428 * @param array $data Input data (not yet validated) 429 * @return bool True if one or more rules is enabled, false if none are. 430 */ 431 public function completion_rule_enabled($data) { 432 return !empty($data['completionendreached']) || $data['completiontimespent'] > 0; 433 } 434 435 /** 436 * Allows module to modify the data returned by form get_data(). 437 * This method is also called in the bulk activity completion form. 438 * 439 * Only available on moodleform_mod. 440 * 441 * @param stdClass $data the form data to be modified. 442 */ 443 public function data_postprocessing($data) { 444 parent::data_postprocessing($data); 445 // Turn off completion setting if the checkbox is not ticked. 446 if (!empty($data->completionunlocked)) { 447 $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC; 448 if (empty($data->completiontimespentenabled) || !$autocompletion) { 449 $data->completiontimespent = 0; 450 } 451 if (empty($data->completionendreached) || !$autocompletion) { 452 $data->completionendreached = 0; 453 } 454 } 455 } 456 } 457
title
Description
Body
title
Description
Body
title
Description
Body
title
Body