PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/lesson/mod_form.php

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