PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/lesson/mod_form.php

https://bitbucket.org/ngmares/moodle
PHP | 349 lines | 228 code | 64 blank | 57 comment | 21 complexity | 6fa3e02803779e5033573cbe42b11a83 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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
  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. // Create a text box that can be enabled/disabled for lesson time limit
  75. $timedgrp = array();
  76. $timedgrp[] = &$mform->createElement('text', 'maxtime');
  77. $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
  78. $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
  79. $mform->disabledIf('timedgrp', 'timed');
  80. // Add numeric rule to text field
  81. $timedgrprules = array();
  82. $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
  83. $mform->addGroupRule('timedgrp', $timedgrprules);
  84. // Rest of group setup
  85. $mform->setDefault('timed', 0);
  86. $mform->setDefault('maxtime', 20);
  87. $mform->setType('maxtime', PARAM_INT);
  88. $numbers = array();
  89. for ($i=20; $i>1; $i--) {
  90. $numbers[$i] = $i;
  91. }
  92. $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
  93. $mform->setDefault('available', 0);
  94. $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
  95. $mform->setDefault('deadline', 0);
  96. $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
  97. $mform->setDefault('maxanswers', $CFG->lesson_maxanswers);
  98. $mform->setType('maxanswers', PARAM_INT);
  99. $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
  100. $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
  101. $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
  102. $mform->setDefault('usepassword', 0);
  103. $mform->setAdvanced('usepassword');
  104. $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
  105. $mform->setDefault('password', '');
  106. $mform->setType('password', PARAM_RAW);
  107. $mform->setAdvanced('password');
  108. $mform->disabledIf('password', 'usepassword', 'eq', 0);
  109. $this->standard_grading_coursemodule_elements();
  110. //-------------------------------------------------------------------------------
  111. $mform->addElement('header', 'gradeoptions', get_string('gradeoptions', 'lesson'));
  112. $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
  113. $mform->addHelpButton('practice', 'practice', 'lesson');
  114. $mform->setDefault('practice', 0);
  115. $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
  116. $mform->addHelpButton('custom', 'customscoring', 'lesson');
  117. $mform->setDefault('custom', 1);
  118. $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
  119. $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
  120. $mform->setDefault('retake', 0);
  121. $options = array();
  122. $options[0] = get_string('usemean', 'lesson');
  123. $options[1] = get_string('usemaximum', 'lesson');
  124. $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
  125. $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
  126. $mform->setDefault('usemaxgrade', 0);
  127. $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
  128. $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
  129. $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
  130. $mform->setDefault('ongoing', 0);
  131. //-------------------------------------------------------------------------------
  132. $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
  133. $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
  134. $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
  135. $mform->setDefault('modattempts', 0);
  136. $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
  137. $mform->addHelpButton('review', 'displayreview', 'lesson');
  138. $mform->setDefault('review', 0);
  139. $numbers = array();
  140. for ($i=10; $i>0; $i--) {
  141. $numbers[$i] = $i;
  142. }
  143. $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
  144. $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
  145. $mform->setDefault('maxattempts', 1);
  146. $defaultnextpages = array();
  147. $defaultnextpages[0] = get_string('normal', 'lesson');
  148. $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
  149. $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
  150. $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
  151. $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
  152. $mform->setDefault('nextpagedefault', $CFG->lesson_defaultnextpage);
  153. $mform->setAdvanced('nextpagedefault');
  154. $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
  155. $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
  156. $mform->setDefault('feedback', 0);
  157. $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
  158. $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
  159. $mform->setDefault('progressbar', 0);
  160. $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
  161. $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
  162. $mform->setDefault('displayleft', 0);
  163. $options = array();
  164. for($i = 100; $i >= 0; $i--) {
  165. $options[$i] = $i.'%';
  166. }
  167. $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
  168. $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
  169. $mform->setDefault('displayleftif', 0);
  170. $mform->setAdvanced('displayleftif');
  171. $numbers = array();
  172. for ($i = 100; $i >= 0; $i--) {
  173. $numbers[$i] = $i;
  174. }
  175. $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
  176. $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
  177. $mform->setDefault('minquestions', 0);
  178. $mform->setAdvanced('minquestions');
  179. $numbers = array();
  180. for ($i = 100; $i >= 0; $i--) {
  181. $numbers[$i] = $i;
  182. }
  183. $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
  184. $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
  185. $mform->setAdvanced('maxpages');
  186. $mform->setDefault('maxpages', 0);
  187. $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
  188. $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
  189. $mform->setDefault('slideshow', 0);
  190. $mform->setAdvanced('slideshow');
  191. // get the modules
  192. if ($mods = get_course_mods($COURSE->id)) {
  193. $modinstances = array();
  194. foreach ($mods as $mod) {
  195. // get the module name and then store it in a new array
  196. if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
  197. if (isset($this->_cm->id) and $this->_cm->id != $mod->id){
  198. $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
  199. }
  200. }
  201. }
  202. asort($modinstances); // sort by module name
  203. $modinstances=array(0=>get_string('none'))+$modinstances;
  204. $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
  205. $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
  206. $mform->setDefault('activitylink', 0);
  207. $mform->setAdvanced('activitylink');
  208. }
  209. //-------------------------------------------------------------------------------
  210. $mform->addElement('header', 'mediafileheader', get_string('mediafile', 'lesson'));
  211. $filepickeroptions = array();
  212. $filepickeroptions['filetypes'] = '*';
  213. $filepickeroptions['maxbytes'] = $this->course->maxbytes;
  214. $mform->addElement('filepicker', 'mediafilepicker', get_string('mediafile', 'lesson'), null, $filepickeroptions);
  215. $mform->addHelpButton('mediafilepicker', 'mediafile', 'lesson');
  216. //-------------------------------------------------------------------------------
  217. $mform->addElement('header', 'dependencyon', get_string('dependencyon', 'lesson'));
  218. $options = array(0=>get_string('none'));
  219. if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
  220. foreach($lessons as $lesson) {
  221. if ($lesson->id != $this->_instance){
  222. $options[$lesson->id] = format_string($lesson->name, true);
  223. }
  224. }
  225. }
  226. $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
  227. $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
  228. $mform->setDefault('dependency', 0);
  229. $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
  230. $mform->setDefault('timespent', 0);
  231. $mform->setType('timespent', PARAM_INT);
  232. $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
  233. $mform->setDefault('completed', 0);
  234. $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
  235. $mform->setDefault('gradebetterthan', 0);
  236. $mform->setType('gradebetterthan', PARAM_INT);
  237. //-------------------------------------------------------------------------------
  238. $this->standard_coursemodule_elements();
  239. //-------------------------------------------------------------------------------
  240. // buttons
  241. $this->add_action_buttons();
  242. }
  243. /**
  244. * Enforce defaults here
  245. *
  246. * @param array $default_values Form defaults
  247. * @return void
  248. **/
  249. function data_preprocessing(&$default_values) {
  250. global $DB;
  251. global $module;
  252. if (isset($default_values['conditions'])) {
  253. $conditions = unserialize($default_values['conditions']);
  254. $default_values['timespent'] = $conditions->timespent;
  255. $default_values['completed'] = $conditions->completed;
  256. $default_values['gradebetterthan'] = $conditions->gradebetterthan;
  257. }
  258. // after this passwords are clear text, MDL-11090
  259. if (isset($default_values['password']) and ($module->version<2008112600)) {
  260. unset($default_values['password']);
  261. }
  262. if ($this->current->instance) {
  263. // editing existing instance - copy existing files into draft area
  264. $draftitemid = file_get_submitted_draft_itemid('mediafilepicker');
  265. file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
  266. $default_values['mediafilepicker'] = $draftitemid;
  267. }
  268. }
  269. /**
  270. * Enforce validation rules here
  271. *
  272. * @param object $data Post data to validate
  273. * @return array
  274. **/
  275. function validation($data, $files) {
  276. $errors = parent::validation($data, $files);
  277. if (empty($data['maxtime']) and !empty($data['timed'])) {
  278. $errors['timedgrp'] = get_string('err_numeric', 'form');
  279. }
  280. if (!empty($data['usepassword']) && empty($data['password'])) {
  281. $errors['password'] = get_string('emptypassword', 'lesson');
  282. }
  283. return $errors;
  284. }
  285. }