PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/override_form.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 279 lines | 161 code | 46 blank | 72 comment | 32 complexity | e815027b0ba0ac5b108bb2b8714c4011 MD5 | raw file
  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. * Settings form for overrides in the lesson module.
  18. *
  19. * @package mod_lesson
  20. * @copyright 2015 Jean-Michel Vedrine
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir . '/formslib.php');
  25. require_once($CFG->dirroot . '/mod/lesson/mod_form.php');
  26. /**
  27. * Form for editing settings overrides.
  28. *
  29. * @copyright 2015 Jean-Michel Vedrine
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class lesson_override_form extends moodleform {
  33. /** @var object course module object. */
  34. protected $cm;
  35. /** @var object the lesson settings object. */
  36. protected $lesson;
  37. /** @var context the lesson context. */
  38. protected $context;
  39. /** @var bool editing group override (true) or user override (false). */
  40. protected $groupmode;
  41. /** @var int groupid, if provided. */
  42. protected $groupid;
  43. /** @var int userid, if provided. */
  44. protected $userid;
  45. /**
  46. * Constructor.
  47. * @param moodle_url $submiturl the form action URL.
  48. * @param object $cm course module object.
  49. * @param object $lesson the lesson settings object.
  50. * @param object $context the lesson context.
  51. * @param bool $groupmode editing group override (true) or user override (false).
  52. * @param object $override the override being edited, if it already exists.
  53. */
  54. public function __construct($submiturl, $cm, $lesson, $context, $groupmode, $override) {
  55. $this->cm = $cm;
  56. $this->lesson = $lesson;
  57. $this->context = $context;
  58. $this->groupmode = $groupmode;
  59. $this->groupid = empty($override->groupid) ? 0 : $override->groupid;
  60. $this->userid = empty($override->userid) ? 0 : $override->userid;
  61. parent::__construct($submiturl, null, 'post');
  62. }
  63. /**
  64. * Define this form - called by the parent constructor
  65. */
  66. protected function definition() {
  67. global $CFG, $DB;
  68. $cm = $this->cm;
  69. $mform = $this->_form;
  70. $mform->addElement('header', 'override', get_string('override', 'lesson'));
  71. if ($this->groupmode) {
  72. // Group override.
  73. if ($this->groupid) {
  74. // There is already a groupid, so freeze the selector.
  75. $groupchoices = array();
  76. $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
  77. $mform->addElement('select', 'groupid',
  78. get_string('overridegroup', 'lesson'), $groupchoices);
  79. $mform->freeze('groupid');
  80. } else {
  81. // Prepare the list of groups.
  82. $groups = groups_get_all_groups($cm->course);
  83. if (empty($groups)) {
  84. // Generate an error.
  85. $link = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id));
  86. print_error('groupsnone', 'lesson', $link);
  87. }
  88. $groupchoices = array();
  89. foreach ($groups as $group) {
  90. $groupchoices[$group->id] = $group->name;
  91. }
  92. unset($groups);
  93. if (count($groupchoices) == 0) {
  94. $groupchoices[0] = get_string('none');
  95. }
  96. $mform->addElement('select', 'groupid',
  97. get_string('overridegroup', 'lesson'), $groupchoices);
  98. $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
  99. }
  100. } else {
  101. // User override.
  102. if ($this->userid) {
  103. // There is already a userid, so freeze the selector.
  104. $user = $DB->get_record('user', array('id' => $this->userid));
  105. $userchoices = array();
  106. $userchoices[$this->userid] = fullname($user);
  107. $mform->addElement('select', 'userid',
  108. get_string('overrideuser', 'lesson'), $userchoices);
  109. $mform->freeze('userid');
  110. } else {
  111. // Prepare the list of users.
  112. $users = get_enrolled_users($this->context, '', 0,
  113. 'u.id, u.email, ' . get_all_user_name_fields(true, 'u'));
  114. // Filter users based on any fixed restrictions (groups, profile).
  115. $info = new \core_availability\info_module($cm);
  116. $users = $info->filter_user_list($users);
  117. if (empty($users)) {
  118. // Generate an error.
  119. $link = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id));
  120. print_error('usersnone', 'lesson', $link);
  121. }
  122. $userchoices = array();
  123. $canviewemail = in_array('email', get_extra_user_fields($this->context));
  124. foreach ($users as $id => $user) {
  125. if (empty($invalidusers[$id]) || (!empty($override) &&
  126. $id == $override->userid)) {
  127. if ($canviewemail) {
  128. $userchoices[$id] = fullname($user) . ', ' . $user->email;
  129. } else {
  130. $userchoices[$id] = fullname($user);
  131. }
  132. }
  133. }
  134. unset($users);
  135. if (count($userchoices) == 0) {
  136. $userchoices[0] = get_string('none');
  137. }
  138. $mform->addElement('searchableselector', 'userid',
  139. get_string('overrideuser', 'lesson'), $userchoices);
  140. $mform->addRule('userid', get_string('required'), 'required', null, 'client');
  141. }
  142. }
  143. // Password.
  144. // This field has to be above the date and timelimit fields,
  145. // otherwise browsers will clear it when those fields are changed.
  146. $mform->addElement('passwordunmask', 'password', get_string('usepassword', 'lesson'));
  147. $mform->setType('password', PARAM_TEXT);
  148. $mform->addHelpButton('password', 'usepassword', 'lesson');
  149. $mform->setDefault('password', $this->lesson->password);;
  150. // Open and close dates.
  151. $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional' => true));
  152. $mform->setDefault('available', $this->lesson->available);
  153. $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional' => true));
  154. $mform->setDefault('deadline', $this->lesson->deadline);
  155. // Lesson time limit.
  156. $mform->addElement('duration', 'timelimit',
  157. get_string('timelimit', 'lesson'), array('optional' => true));
  158. if ($this->lesson->timelimit != 0) {
  159. $mform->setDefault('timelimit', 0);
  160. } else {
  161. $mform->setDefault('timelimit', $this->lesson->timelimit);
  162. }
  163. // Try a question again.
  164. $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
  165. $mform->addHelpButton('review', 'displayreview', 'lesson');
  166. $mform->setDefault('review', $this->lesson->review);
  167. // Number of attempts.
  168. $numbers = array();
  169. for ($i = 10; $i > 0; $i--) {
  170. $numbers[$i] = $i;
  171. }
  172. $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
  173. $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
  174. $mform->setDefault('maxattempts', $this->lesson->maxattempts);
  175. // Retake allowed.
  176. $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
  177. $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
  178. $mform->setDefault('retake', $this->lesson->retake);
  179. // Submit buttons.
  180. $mform->addElement('submit', 'resetbutton',
  181. get_string('reverttodefaults', 'lesson'));
  182. $buttonarray = array();
  183. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  184. get_string('save', 'lesson'));
  185. $buttonarray[] = $mform->createElement('submit', 'againbutton',
  186. get_string('saveoverrideandstay', 'lesson'));
  187. $buttonarray[] = $mform->createElement('cancel');
  188. $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
  189. $mform->closeHeaderBefore('buttonbar');
  190. }
  191. /**
  192. * Validate the submitted form data.
  193. *
  194. * @param array $data array of ("fieldname"=>value) of submitted data
  195. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  196. * @return array of "element_name"=>"error_description" if there are errors
  197. */
  198. public function validation($data, $files) {
  199. global $COURSE, $DB;
  200. $errors = parent::validation($data, $files);
  201. $mform =& $this->_form;
  202. $lesson = $this->lesson;
  203. if ($mform->elementExists('userid')) {
  204. if (empty($data['userid'])) {
  205. $errors['userid'] = get_string('required');
  206. }
  207. }
  208. if ($mform->elementExists('groupid')) {
  209. if (empty($data['groupid'])) {
  210. $errors['groupid'] = get_string('required');
  211. }
  212. }
  213. // Ensure that the dates make sense.
  214. if (!empty($data['available']) && !empty($data['deadline'])) {
  215. if ($data['deadline'] < $data['available'] ) {
  216. $errors['deadline'] = get_string('closebeforeopen', 'lesson');
  217. }
  218. }
  219. // Ensure that at least one lesson setting was changed.
  220. $changed = false;
  221. $keys = array('available', 'deadline', 'review', 'timelimit', 'maxattempts', 'retake', 'password');
  222. foreach ($keys as $key) {
  223. if ($data[$key] != $lesson->{$key}) {
  224. $changed = true;
  225. break;
  226. }
  227. }
  228. if (!$changed) {
  229. $errors['available'] = get_string('nooverridedata', 'lesson');
  230. }
  231. return $errors;
  232. }
  233. }