PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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
  21. * @subpackage lesson
  22. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
  24. **/
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->dirroot.'/course/moodleform_mod.php');
  27. require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  28. class mod_lesson_mod_form extends moodleform_mod {
  29. protected $course = null;
  30. public function mod_lesson_mod_form($current, $section, $cm, $course) {
  31. $this->course = $course;
  32. parent::moodleform_mod($current, $section, $cm, $course);
  33. }
  34. function definition() {
  35. global $CFG, $COURSE, $DB;
  36. $mform = $this->_form;
  37. //-------------------------------------------------------------------------------
  38. $mform->addElement('header', 'general', get_string('general', 'form'));
  39. /** Legacy slideshow width element to maintain backwards compatibility */
  40. $mform->addElement('hidden', 'width');
  41. $mform->setType('width', PARAM_INT);
  42. $mform->setDefault('width', $CFG->lesson_slideshowwidth);
  43. /** Legacy slideshow height element to maintain backwards compatibility */
  44. $mform->addElement('hidden', 'height');
  45. $mform->setType('height', PARAM_INT);
  46. $mform->setDefault('height', $CFG->lesson_slideshowheight);
  47. /** Legacy slideshow background color element to maintain backwards compatibility */
  48. $mform->addElement('hidden', 'bgcolor');
  49. $mform->setType('bgcolor', PARAM_TEXT);
  50. $mform->setDefault('bgcolor', $CFG->lesson_slideshowbgcolor);
  51. /** Legacy media popup width element to maintain backwards compatibility */
  52. $mform->addElement('hidden', 'mediawidth');
  53. $mform->setType('mediawidth', PARAM_INT);
  54. $mform->setDefault('mediawidth', $CFG->lesson_mediawidth);
  55. /** Legacy media popup height element to maintain backwards compatibility */
  56. $mform->addElement('hidden', 'mediaheight');
  57. $mform->setType('mediaheight', PARAM_INT);
  58. $mform->setDefault('mediaheight', $CFG->lesson_mediaheight);
  59. /** Legacy media popup close button element to maintain backwards compatibility */
  60. $mform->addElement('hidden', 'mediaclose');
  61. $mform->setType('mediaclose', PARAM_BOOL);
  62. $mform->setDefault('mediaclose', $CFG->lesson_mediaclose);
  63. /** Legacy maximum highscores element to maintain backwards compatibility */
  64. $mform->addElement('hidden', 'maxhighscores');
  65. $mform->setType('maxhighscores', PARAM_INT);
  66. $mform->setDefault('maxhighscores', $CFG->lesson_maxhighscores);
  67. $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  68. if (!empty($CFG->formatstringstriptags)) {
  69. $mform->setType('name', PARAM_TEXT);
  70. } else {
  71. $mform->setType('name', PARAM_CLEANHTML);
  72. }
  73. $mform->addRule('name', null, 'required', null, 'client');
  74. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  75. // Appearance.
  76. $mform->addElement('header', 'appearancehdr', get_string('appearance'));
  77. $filemanageroptions = array();
  78. $filemanageroptions['filetypes'] = '*';
  79. $filemanageroptions['maxbytes'] = $this->course->maxbytes;
  80. $filemanageroptions['subdirs'] = 0;
  81. $filemanageroptions['maxfiles'] = 1;
  82. $mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions);
  83. $mform->addHelpButton('mediafile', 'mediafile', 'lesson');
  84. $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
  85. $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
  86. $mform->setDefault('progressbar', 0);
  87. $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
  88. $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
  89. $mform->setDefault('ongoing', 0);
  90. $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
  91. $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
  92. $mform->setDefault('displayleft', 0);
  93. $options = array();
  94. for($i = 100; $i >= 0; $i--) {
  95. $options[$i] = $i.'%';
  96. }
  97. $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
  98. $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
  99. $mform->setDefault('displayleftif', 0);
  100. $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
  101. $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
  102. $mform->setDefault('slideshow', 0);
  103. $numbers = array();
  104. for ($i = 20; $i > 1; $i--) {
  105. $numbers[$i] = $i;
  106. }
  107. $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
  108. $mform->setDefault('maxanswers', $CFG->lesson_maxanswers);
  109. $mform->setType('maxanswers', PARAM_INT);
  110. $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
  111. $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
  112. $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
  113. $mform->setDefault('feedback', 0);
  114. // Get the modules.
  115. if ($mods = get_course_mods($COURSE->id)) {
  116. $modinstances = array();
  117. foreach ($mods as $mod) {
  118. // get the module name and then store it in a new array
  119. if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
  120. if (isset($this->_cm->id) and $this->_cm->id != $mod->id){
  121. $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
  122. }
  123. }
  124. }
  125. asort($modinstances); // sort by module name
  126. $modinstances=array(0=>get_string('none'))+$modinstances;
  127. $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
  128. $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
  129. $mform->setDefault('activitylink', 0);
  130. }
  131. // Availability.
  132. $mform->addElement('header', 'availabilityhdr', get_string('availability'));
  133. $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
  134. $mform->setDefault('available', 0);
  135. $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
  136. $mform->setDefault('deadline', 0);
  137. // Create a text box that can be enabled/disabled for lesson time limit
  138. $timedgrp = array();
  139. $timedgrp[] = &$mform->createElement('text', 'maxtime');
  140. $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
  141. $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
  142. $mform->disabledIf('timedgrp', 'timed');
  143. // Add numeric rule to text field
  144. $timedgrprules = array();
  145. $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
  146. $mform->addGroupRule('timedgrp', $timedgrprules);
  147. // Rest of group setup
  148. $mform->setDefault('timed', 0);
  149. $mform->setDefault('maxtime', 20);
  150. $mform->setType('maxtime', PARAM_INT);
  151. $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
  152. $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
  153. $mform->setDefault('usepassword', 0);
  154. $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
  155. $mform->setDefault('password', '');
  156. $mform->setType('password', PARAM_RAW);
  157. $mform->disabledIf('password', 'usepassword', 'eq', 0);
  158. $mform->disabledIf('passwordunmask', 'usepassword', 'eq', 0);
  159. // Dependent on.
  160. $mform->addElement('header', 'dependencyon', get_string('prerequisitelesson', 'lesson'));
  161. $options = array(0=>get_string('none'));
  162. if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
  163. foreach($lessons as $lesson) {
  164. if ($lesson->id != $this->_instance){
  165. $options[$lesson->id] = format_string($lesson->name, true);
  166. }
  167. }
  168. }
  169. $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
  170. $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
  171. $mform->setDefault('dependency', 0);
  172. $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
  173. $mform->setDefault('timespent', 0);
  174. $mform->setType('timespent', PARAM_INT);
  175. $mform->disabledIf('timespent', 'dependency', 'eq', 0);
  176. $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
  177. $mform->setDefault('completed', 0);
  178. $mform->disabledIf('completed', 'dependency', 'eq', 0);
  179. $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
  180. $mform->setDefault('gradebetterthan', 0);
  181. $mform->setType('gradebetterthan', PARAM_INT);
  182. $mform->disabledIf('gradebetterthan', 'dependency', 'eq', 0);
  183. // Flow control.
  184. $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
  185. $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
  186. $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
  187. $mform->setDefault('modattempts', 0);
  188. $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
  189. $mform->addHelpButton('review', 'displayreview', 'lesson');
  190. $mform->setDefault('review', 0);
  191. $numbers = array();
  192. for ($i = 10; $i > 0; $i--) {
  193. $numbers[$i] = $i;
  194. }
  195. $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
  196. $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
  197. $mform->setDefault('maxattempts', 1);
  198. $defaultnextpages = array();
  199. $defaultnextpages[0] = get_string('normal', 'lesson');
  200. $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
  201. $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
  202. $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
  203. $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
  204. $mform->setDefault('nextpagedefault', $CFG->lesson_defaultnextpage);
  205. $numbers = array();
  206. for ($i = 100; $i >= 0; $i--) {
  207. $numbers[$i] = $i;
  208. }
  209. $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
  210. $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
  211. $mform->setDefault('maxpages', 0);
  212. // Grade.
  213. $this->standard_grading_coursemodule_elements();
  214. // No header here, so that the following settings are displayed in the grade section.
  215. $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
  216. $mform->addHelpButton('practice', 'practice', 'lesson');
  217. $mform->setDefault('practice', 0);
  218. $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
  219. $mform->addHelpButton('custom', 'customscoring', 'lesson');
  220. $mform->setDefault('custom', 1);
  221. $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
  222. $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
  223. $mform->setDefault('retake', 0);
  224. $options = array();
  225. $options[0] = get_string('usemean', 'lesson');
  226. $options[1] = get_string('usemaximum', 'lesson');
  227. $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
  228. $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
  229. $mform->setDefault('usemaxgrade', 0);
  230. $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
  231. $numbers = array();
  232. for ($i = 100; $i >= 0; $i--) {
  233. $numbers[$i] = $i;
  234. }
  235. $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
  236. $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
  237. $mform->setDefault('minquestions', 0);
  238. //-------------------------------------------------------------------------------
  239. $this->standard_coursemodule_elements();
  240. //-------------------------------------------------------------------------------
  241. // buttons
  242. $this->add_action_buttons();
  243. }
  244. /**
  245. * Enforce defaults here
  246. *
  247. * @param array $default_values Form defaults
  248. * @return void
  249. **/
  250. function data_preprocessing(&$default_values) {
  251. if (isset($default_values['conditions'])) {
  252. $conditions = unserialize($default_values['conditions']);
  253. $default_values['timespent'] = $conditions->timespent;
  254. $default_values['completed'] = $conditions->completed;
  255. $default_values['gradebetterthan'] = $conditions->gradebetterthan;
  256. }
  257. if ($this->current->instance) {
  258. // Editing existing instance - copy existing files into draft area.
  259. $draftitemid = file_get_submitted_draft_itemid('mediafile');
  260. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
  261. $default_values['mediafile'] = $draftitemid;
  262. }
  263. }
  264. /**
  265. * Enforce validation rules here
  266. *
  267. * @param object $data Post data to validate
  268. * @return array
  269. **/
  270. function validation($data, $files) {
  271. $errors = parent::validation($data, $files);
  272. if (empty($data['maxtime']) and !empty($data['timed'])) {
  273. $errors['timedgrp'] = get_string('err_numeric', 'form');
  274. }
  275. if (!empty($data['usepassword']) && empty($data['password'])) {
  276. $errors['password'] = get_string('emptypassword', 'lesson');
  277. }
  278. return $errors;
  279. }
  280. }