Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 $suffix = $this->get_suffix(); 369 $completiontimespentenabledel = 'completiontimespentenabled' . $suffix; 370 $completiontimespentel = 'completiontimespent' . $suffix; 371 $defaultvalues[$completiontimespentenabledel] = !empty($defaultvalues[$completiontimespentel]) ? 1 : 0; 372 373 if ($this->current->instance) { 374 // Editing existing instance - copy existing files into draft area. 375 $draftitemid = file_get_submitted_draft_itemid('mediafile'); 376 file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1)); 377 $defaultvalues['mediafile'] = $draftitemid; 378 } 379 } 380 381 /** 382 * Enforce validation rules here 383 * 384 * @param object $data Post data to validate 385 * @return array 386 **/ 387 function validation($data, $files) { 388 $errors = parent::validation($data, $files); 389 390 // Check open and close times are consistent. 391 if ($data['available'] != 0 && $data['deadline'] != 0 && 392 $data['deadline'] < $data['available']) { 393 $errors['deadline'] = get_string('closebeforeopen', 'lesson'); 394 } 395 396 if (!empty($data['usepassword']) && empty($data['password'])) { 397 $errors['password'] = get_string('emptypassword', 'lesson'); 398 } 399 400 return $errors; 401 } 402 403 /** 404 * Display module-specific activity completion rules. 405 * Part of the API defined by moodleform_mod 406 * @return array Array of string IDs of added items, empty array if none 407 */ 408 public function add_completion_rules() { 409 $mform = $this->_form; 410 411 $suffix = $this->get_suffix(); 412 $completionendreachedel = 'completionendreached' . $suffix; 413 $mform->addElement( 414 'checkbox', $completionendreachedel, 415 '', 416 get_string('completionendreached', 'lesson') 417 ); 418 // Enable this completion rule by default. 419 $mform->setDefault($completionendreachedel, 1); 420 421 $group = []; 422 $completiontimespentenabledel = 'completiontimespentenabled' . $suffix; 423 $group[] =& $mform->createElement( 424 'checkbox', 425 $completiontimespentenabledel, 426 '', 427 get_string('completiontimespentgroup', 'lesson') 428 ); 429 $completiontimespentel = 'completiontimespent' . $suffix; 430 $group[] =& $mform->createElement('duration', $completiontimespentel, '', ['optional' => false]); 431 $completiontimespentgroupel = 'completiontimespentgroup' . $suffix; 432 $mform->addGroup($group, $completiontimespentgroupel, '', ' ', false); 433 $mform->hideIf($completiontimespentel . '[number]', $completiontimespentenabledel, 'notchecked'); 434 $mform->hideIf($completiontimespentel . '[timeunit]', $completiontimespentenabledel, 'notchecked'); 435 436 return [$completionendreachedel, $completiontimespentgroupel]; 437 } 438 439 /** 440 * Called during validation. Indicates whether a module-specific completion rule is selected. 441 * 442 * @param array $data Input data (not yet validated) 443 * @return bool True if one or more rules is enabled, false if none are. 444 */ 445 public function completion_rule_enabled($data) { 446 $suffix = $this->get_suffix(); 447 return !empty($data['completionendreached' . $suffix]) || $data['completiontimespent' . $suffix] > 0; 448 } 449 450 /** 451 * Allows module to modify the data returned by form get_data(). 452 * This method is also called in the bulk activity completion form. 453 * 454 * Only available on moodleform_mod. 455 * 456 * @param stdClass $data the form data to be modified. 457 */ 458 public function data_postprocessing($data) { 459 parent::data_postprocessing($data); 460 // Turn off completion setting if the checkbox is not ticked. 461 if (!empty($data->completionunlocked)) { 462 $suffix = $this->get_suffix(); 463 $completion = $data->{'completion' . $suffix}; 464 $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC; 465 if (empty($data->{'completiontimespentenabled' . $suffix}) || !$autocompletion) { 466 $data->{'completiontimespent' . $suffix} = 0; 467 } 468 if (empty($data->{'completionendreached' . $suffix}) || !$autocompletion) { 469 $data->{'completionendreached' . $suffix} = 0; 470 } 471 } 472 } 473 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body