PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/MoodleWebRole/mod/lesson/mod_form.php

#
PHP | 345 lines | 246 code | 62 blank | 37 comment | 20 complexity | 27b14a3b1efe2b3466490fd5bba51b0e MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php // $Id: mod_form.php,v 1.20.2.4 2008/11/26 14:30:22 thepurpleblob Exp $
  2. /**
  3. * Form to define a new instance of lesson or edit an instance.
  4. * It is used from /course/modedit.php.
  5. *
  6. * @version $Id: mod_form.php,v 1.20.2.4 2008/11/26 14:30:22 thepurpleblob Exp $
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  8. * @package lesson
  9. **/
  10. require_once($CFG->dirroot.'/course/moodleform_mod.php');
  11. require_once('locallib.php');
  12. class mod_lesson_mod_form extends moodleform_mod {
  13. function definition() {
  14. global $CFG, $LESSON_NEXTPAGE_ACTION, $COURSE;
  15. $mform =& $this->_form;
  16. //-------------------------------------------------------------------------------
  17. $mform->addElement('header', 'general', get_string('general', 'form'));
  18. $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  19. if (!empty($CFG->formatstringstriptags)) {
  20. $mform->setType('name', PARAM_TEXT);
  21. } else {
  22. $mform->setType('name', PARAM_CLEAN);
  23. }
  24. $mform->addRule('name', null, 'required', null, 'client');
  25. // Create a text box that can be enabled/disabled for lesson time limit
  26. $timedgrp = array();
  27. $timedgrp[] = &$mform->createElement('text', 'maxtime');
  28. $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable'));
  29. $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false);
  30. $mform->disabledIf('timedgrp', 'timed');
  31. // Add numeric rule to text field
  32. $timedgrprules = array();
  33. $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client');
  34. $mform->addGroupRule('timedgrp', $timedgrprules);
  35. // Rest of group setup
  36. $mform->setDefault('timed', 0);
  37. $mform->setDefault('maxtime', 20);
  38. $mform->setType('maxtime', PARAM_INT);
  39. $mform->setHelpButton('timedgrp', array('timed', get_string('timed', 'lesson'), 'lesson'));
  40. $numbers = array();
  41. for ($i=20; $i>1; $i--) {
  42. $numbers[$i] = $i;
  43. }
  44. $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
  45. $mform->setDefault('maxanswers', 4);
  46. $mform->setHelpButton('maxanswers', array('maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), 'lesson'));
  47. //-------------------------------------------------------------------------------
  48. $mform->addElement('header', '', get_string('gradeoptions', 'lesson'));
  49. $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
  50. $mform->setHelpButton('practice', array('practice', get_string('practice', 'lesson'), 'lesson'));
  51. $mform->setDefault('practice', 0);
  52. $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
  53. $mform->setHelpButton('custom', array('custom', get_string('customscoring', 'lesson'), 'lesson'));
  54. $mform->setDefault('custom', 1);
  55. $grades = array();
  56. for ($i=100; $i>=0; $i--) {
  57. $grades[$i] = $i;
  58. }
  59. $mform->addElement('select', 'grade', get_string('maximumgrade'), $grades);
  60. $mform->setDefault('grade', 0);
  61. $mform->setHelpButton('grade', array('grade', get_string('maximumgrade'), 'lesson'));
  62. $mform->addElement('selectyesno', 'retake', get_string('canretake', 'lesson', $COURSE->student));
  63. $mform->setHelpButton('retake', array('retake', get_string('canretake', 'lesson', $COURSE->student), 'lesson'));
  64. $mform->setDefault('retake', 0);
  65. $options = array();
  66. $options[0] = get_string('usemean', 'lesson');
  67. $options[1] = get_string('usemaximum', 'lesson');
  68. $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
  69. $mform->setHelpButton('usemaxgrade', array('handlingofretakes', get_string('handlingofretakes', 'lesson'), 'lesson'));
  70. $mform->setDefault('usemaxgrade', 0);
  71. $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
  72. $mform->setHelpButton('ongoing', array('ongoing', get_string('ongoing', 'lesson'), 'lesson'));
  73. $mform->setDefault('ongoing', 0);
  74. //-------------------------------------------------------------------------------
  75. $mform->addElement('header', '', get_string('flowcontrol', 'lesson'));
  76. $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
  77. $mform->setHelpButton('modattempts', array('modattempts', get_string('modattempts', 'lesson'), 'lesson'));
  78. $mform->setDefault('modattempts', 0);
  79. $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
  80. $mform->setHelpButton('review', array('review', get_string('displayreview', 'lesson'), 'lesson'));
  81. $mform->setDefault('review', 0);
  82. $numbers = array();
  83. for ($i=10; $i>0; $i--) {
  84. $numbers[$i] = $i;
  85. }
  86. $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
  87. $mform->setHelpButton('maxattempts', array('maxattempts', get_string('maximumnumberofattempts', 'lesson'), 'lesson'));
  88. $mform->setDefault('maxattempts', 1);
  89. $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $LESSON_NEXTPAGE_ACTION);
  90. $mform->setHelpButton('nextpagedefault', array('nextpageaction', get_string('actionaftercorrectanswer', 'lesson'), 'lesson'));
  91. $mform->setDefault('nextpagedefault', 0);
  92. $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
  93. $mform->setHelpButton('feedback', array('feedback', get_string('displaydefaultfeedback', 'lesson'), 'lesson'));
  94. $mform->setDefault('feedback', 0);
  95. $numbers = array();
  96. for ($i = 100; $i >= 0; $i--) {
  97. $numbers[$i] = $i;
  98. }
  99. $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
  100. $mform->setHelpButton('minquestions', array('minquestions', get_string('minimumnumberofquestions', 'lesson'), 'lesson'));
  101. $mform->setDefault('minquestions', 0);
  102. $numbers = array();
  103. for ($i = 100; $i >= 0; $i--) {
  104. $numbers[$i] = $i;
  105. }
  106. $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
  107. $mform->setHelpButton('maxpages', array('maxpages', get_string('numberofpagestoshow', 'lesson'), 'lesson'));
  108. $mform->setDefault('maxpages', 0);
  109. //-------------------------------------------------------------------------------
  110. $mform->addElement('header', '', get_string('lessonformating', 'lesson'));
  111. $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
  112. $mform->setHelpButton('slideshow', array('slideshow', get_string('slideshow', 'lesson'), 'lesson'));
  113. $mform->setDefault('slideshow', 0);
  114. $mform->addElement('text', 'width', get_string('slideshowwidth', 'lesson'));
  115. $mform->setDefault('width', 640);
  116. $mform->addRule('width', null, 'required', null, 'client');
  117. $mform->addRule('width', null, 'numeric', null, 'client');
  118. $mform->setHelpButton('width', array('width', get_string('slideshowwidth', 'lesson'), 'lesson'));
  119. $mform->setType('width', PARAM_INT);
  120. $mform->addElement('text', 'height', get_string('slideshowheight', 'lesson'));
  121. $mform->setDefault('height', 480);
  122. $mform->addRule('height', null, 'required', null, 'client');
  123. $mform->addRule('height', null, 'numeric', null, 'client');
  124. $mform->setHelpButton('height', array('height', get_string('slideshowheight', 'lesson'), 'lesson'));
  125. $mform->setType('height', PARAM_INT);
  126. $mform->addElement('text', 'bgcolor', get_string('slideshowbgcolor', 'lesson'));
  127. $mform->setDefault('bgcolor', '#FFFFFF');
  128. $mform->addRule('bgcolor', null, 'required', null, 'client');
  129. $mform->setHelpButton('bgcolor', array('bgcolor', get_string('slideshowbgcolor', 'lesson'), 'lesson'));
  130. $mform->setType('bgcolor', PARAM_TEXT);
  131. $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
  132. $mform->setHelpButton('displayleft', array('displayleft', get_string('displayleftmenu', 'lesson'), 'lesson'));
  133. $mform->setDefault('displayleft', 0);
  134. $options = array();
  135. for($i = 100; $i >= 0; $i--) {
  136. $options[$i] = $i.'%';
  137. }
  138. $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
  139. $mform->setDefault('displayleftif', 0);
  140. $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
  141. $mform->setHelpButton('progressbar', array('progressbar', get_string('progressbar', 'lesson'), 'lesson'));
  142. $mform->setDefault('progressbar', 0);
  143. //-------------------------------------------------------------------------------
  144. $mform->addElement('header', '', get_string('accesscontrol', 'lesson'));
  145. $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
  146. $mform->setHelpButton('usepassword', array('usepassword', get_string('usepassword', 'lesson'), 'lesson'));
  147. $mform->setDefault('usepassword', 0);
  148. $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
  149. $mform->setHelpButton('password', array('password', get_string('password', 'lesson'), 'lesson'));
  150. $mform->setDefault('password', '');
  151. $mform->setType('password', PARAM_RAW);
  152. $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
  153. $mform->setDefault('available', 0);
  154. $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
  155. $mform->setDefault('deadline', 0);
  156. //-------------------------------------------------------------------------------
  157. $mform->addElement('header', '', get_string('dependencyon', 'lesson'));
  158. $options = array(0=>get_string('none'));
  159. if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
  160. foreach($lessons as $lesson) {
  161. if ($lesson->id != $this->_instance){
  162. $options[$lesson->id] = format_string($lesson->name, true);
  163. }
  164. }
  165. }
  166. $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
  167. $mform->setHelpButton('dependency', array('dependency', get_string('dependencyon', 'lesson'), 'lesson'));
  168. $mform->setDefault('dependency', 0);
  169. $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
  170. $mform->setDefault('timespent', 0);
  171. $mform->setType('timespent', PARAM_INT);
  172. $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
  173. $mform->setDefault('completed', 0);
  174. $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
  175. $mform->setDefault('gradebetterthan', 0);
  176. $mform->setType('gradebetterthan', PARAM_INT);
  177. //-------------------------------------------------------------------------------
  178. $mform->addElement('header', '', get_string('mediafile', 'lesson'));
  179. $mform->addElement('choosecoursefile', 'mediafile', get_string('mediafile', 'lesson'), array('courseid'=>$COURSE->id));
  180. $mform->setHelpButton('mediafile', array('mediafile', get_string('mediafile', 'lesson'), 'lesson'));
  181. $mform->setDefault('mediafile', '');
  182. $mform->setType('mediafile', PARAM_RAW);
  183. $mform->addElement('selectyesno', 'mediaclose', get_string('mediaclose', 'lesson'));
  184. $mform->setDefault('mediaclose', 0);
  185. $mform->addElement('text', 'mediaheight', get_string('mediaheight', 'lesson'));
  186. $mform->setHelpButton('mediaheight', array('mediaheight', get_string('mediaheight', 'lesson'), 'lesson'));
  187. $mform->setDefault('mediaheight', 100);
  188. $mform->addRule('mediaheight', null, 'required', null, 'client');
  189. $mform->addRule('mediaheight', null, 'numeric', null, 'client');
  190. $mform->setType('mediaheight', PARAM_INT);
  191. $mform->addElement('text', 'mediawidth', get_string('mediawidth', 'lesson'));
  192. $mform->setHelpButton('mediawidth', array('mediawidth', get_string('mediawidth', 'lesson'), 'lesson'));
  193. $mform->setDefault('mediawidth', 650);
  194. $mform->addRule('mediawidth', null, 'required', null, 'client');
  195. $mform->addRule('mediawidth', null, 'numeric', null, 'client');
  196. $mform->setType('mediawidth', PARAM_INT);
  197. //-------------------------------------------------------------------------------
  198. $mform->addElement('header', '', get_string('other', 'lesson'));
  199. // get the modules
  200. if ($mods = get_course_mods($COURSE->id)) {
  201. $modinstances = array();
  202. foreach ($mods as $mod) {
  203. // get the module name and then store it in a new array
  204. if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
  205. if (isset($this->_cm->id) and $this->_cm->id != $mod->id){
  206. $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
  207. }
  208. }
  209. }
  210. asort($modinstances); // sort by module name
  211. $modinstances=array(0=>get_string('none'))+$modinstances;
  212. $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
  213. $mform->setHelpButton('activitylink', array('activitylink', get_string('activitylink', 'lesson'), 'lesson'));
  214. $mform->setDefault('activitylink', 0);
  215. }
  216. $mform->addElement('text', 'maxhighscores', get_string('maxhighscores', 'lesson'));
  217. $mform->setHelpButton('maxhighscores', array('maxhighscores', get_string('maxhighscores', 'lesson'), 'lesson'));
  218. $mform->setDefault('maxhighscores', 10);
  219. $mform->addRule('maxhighscores', null, 'required', null, 'client');
  220. $mform->addRule('maxhighscores', null, 'numeric', null, 'client');
  221. $mform->setType('maxhighscores', PARAM_INT);
  222. $mform->addElement('selectyesno', 'lessondefault', get_string('lessondefault', 'lesson'));
  223. $mform->setHelpButton('lessondefault', array('lessondefault', get_string('lessondefault', 'lesson'), 'lesson'));
  224. $mform->setDefault('lessondefault', 0);
  225. //-------------------------------------------------------------------------------
  226. $features = new stdClass;
  227. $features->groups = false;
  228. $features->groupings = true;
  229. $features->groupmembersonly = true;
  230. $this->standard_coursemodule_elements($features);
  231. //-------------------------------------------------------------------------------
  232. // buttons
  233. $this->add_action_buttons();
  234. }
  235. /**
  236. * Enforce defaults here
  237. *
  238. * @param array $default_values Form defaults
  239. * @return void
  240. **/
  241. function data_preprocessing(&$default_values) {
  242. global $module;
  243. if (isset($default_values['conditions'])) {
  244. $conditions = unserialize($default_values['conditions']);
  245. $default_values['timespent'] = $conditions->timespent;
  246. $default_values['completed'] = $conditions->completed;
  247. $default_values['gradebetterthan'] = $conditions->gradebetterthan;
  248. }
  249. // after this passwords are clear text, MDL-11090
  250. if (isset($default_values['password']) and ($module->version<2008112600)) {
  251. unset($default_values['password']);
  252. }
  253. if (isset($default_values['add']) and $defaults = get_record('lesson_default', 'course', $default_values['course'])) {
  254. foreach ($defaults as $fieldname => $default) {
  255. switch ($fieldname) {
  256. case 'conditions':
  257. $conditions = unserialize($default);
  258. $default_values['timespent'] = $conditions->timespent;
  259. $default_values['completed'] = $conditions->completed;
  260. $default_values['gradebetterthan'] = $conditions->gradebetterthan;
  261. break;
  262. default:
  263. $default_values[$fieldname] = $default;
  264. break;
  265. }
  266. }
  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. return $errors;
  281. }
  282. }
  283. ?>