PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/quiz/override_form.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 266 lines | 164 code | 41 blank | 61 comment | 32 complexity | dc10e411a6cee3debfbf93586b6c6e7e 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 quiz module.
  18. *
  19. * @package mod
  20. * @subpackage quiz
  21. * @copyright 2010 Matt Petro
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir . '/formslib.php');
  26. /**
  27. * Form for editing settings overrides.
  28. *
  29. * @copyright 2010 Matt Petro
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class quiz_override_form extends moodleform {
  33. /** @var object course module object. */
  34. protected $cm;
  35. /** @var object the quiz settings object. */
  36. protected $quiz;
  37. /** @var context the quiz 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 course module object.
  49. * @param object the quiz settings object.
  50. * @param context the quiz context.
  51. * @param bool 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, $quiz, $context, $groupmode, $override) {
  55. $this->cm = $cm;
  56. $this->quiz = $quiz;
  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. protected function definition() {
  64. global $CFG, $DB;
  65. $cm = $this->cm;
  66. $mform = $this->_form;
  67. $mform->addElement('header', 'override', get_string('override', 'quiz'));
  68. if ($this->groupmode) {
  69. // Group override.
  70. if ($this->groupid) {
  71. // There is already a groupid, so freeze the selector.
  72. $groupchoices = array();
  73. $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
  74. $mform->addElement('select', 'groupid',
  75. get_string('overridegroup', 'quiz'), $groupchoices);
  76. $mform->freeze('groupid');
  77. } else {
  78. // Prepare the list of groups.
  79. $groups = groups_get_all_groups($cm->course);
  80. if (empty($groups)) {
  81. // Generate an error.
  82. $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
  83. print_error('groupsnone', 'quiz', $link);
  84. }
  85. $groupchoices = array();
  86. foreach ($groups as $group) {
  87. $groupchoices[$group->id] = $group->name;
  88. }
  89. unset($groups);
  90. if (count($groupchoices) == 0) {
  91. $groupchoices[0] = get_string('none');
  92. }
  93. $mform->addElement('select', 'groupid',
  94. get_string('overridegroup', 'quiz'), $groupchoices);
  95. $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
  96. }
  97. } else {
  98. // User override.
  99. if ($this->userid) {
  100. // There is already a userid, so freeze the selector.
  101. $user = $DB->get_record('user', array('id'=>$this->userid));
  102. $userchoices = array();
  103. $userchoices[$this->userid] = fullname($user);
  104. $mform->addElement('select', 'userid',
  105. get_string('overrideuser', 'quiz'), $userchoices);
  106. $mform->freeze('userid');
  107. } else {
  108. // Prepare the list of users.
  109. $users = array();
  110. list($sort, $sortparams) = users_order_by_sql('u');
  111. if (!empty($sortparams)) {
  112. throw new coding_exception('users_order_by_sql returned some query parameters. ' .
  113. 'This is unexpected, and a problem because there is no way to pass these ' .
  114. 'parameters to get_users_by_capability. See MDL-34657.');
  115. }
  116. if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
  117. // Only users from the grouping.
  118. $groups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
  119. if (!empty($groups)) {
  120. $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
  121. 'u.id, u.firstname, u.lastname, u.email',
  122. $sort, '', '', array_keys($groups),
  123. '', false, true);
  124. }
  125. } else {
  126. $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
  127. 'u.id, u.firstname, u.lastname, u.email' ,
  128. $sort, '', '', '', '', false, true);
  129. }
  130. if (empty($users)) {
  131. // Generate an error.
  132. $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
  133. print_error('usersnone', 'quiz', $link);
  134. }
  135. $userchoices = array();
  136. foreach ($users as $id => $user) {
  137. if (empty($invalidusers[$id]) || (!empty($override) &&
  138. $id == $override->userid)) {
  139. $userchoices[$id] = fullname($user) . ', ' . $user->email;
  140. }
  141. }
  142. unset($users);
  143. if (count($userchoices) == 0) {
  144. $userchoices[0] = get_string('none');
  145. }
  146. $mform->addElement('searchableselector', 'userid',
  147. get_string('overrideuser', 'quiz'), $userchoices);
  148. $mform->addRule('userid', get_string('required'), 'required', null, 'client');
  149. }
  150. }
  151. // Password.
  152. // This field has to be above the date and timelimit fields,
  153. // otherwise browsers will clear it when those fields are changed.
  154. $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
  155. $mform->setType('password', PARAM_TEXT);
  156. $mform->addHelpButton('password', 'requirepassword', 'quiz');
  157. $mform->setDefault('password', $this->quiz->password);
  158. // Open and close dates.
  159. $mform->addElement('date_time_selector', 'timeopen',
  160. get_string('quizopen', 'quiz'), array('optional' => true));
  161. $mform->setDefault('timeopen', $this->quiz->timeopen);
  162. $mform->addElement('date_time_selector', 'timeclose',
  163. get_string('quizclose', 'quiz'), array('optional' => true));
  164. $mform->setDefault('timeclose', $this->quiz->timeclose);
  165. // Time limit.
  166. $mform->addElement('duration', 'timelimit',
  167. get_string('timelimit', 'quiz'), array('optional' => true));
  168. $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
  169. $mform->setDefault('timelimit', $this->quiz->timelimit);
  170. // Number of attempts.
  171. $attemptoptions = array('0' => get_string('unlimited'));
  172. for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
  173. $attemptoptions[$i] = $i;
  174. }
  175. $mform->addElement('select', 'attempts',
  176. get_string('attemptsallowed', 'quiz'), $attemptoptions);
  177. $mform->setDefault('attempts', $this->quiz->attempts);
  178. // Submit buttons.
  179. $mform->addElement('submit', 'resetbutton',
  180. get_string('reverttodefaults', 'quiz'));
  181. $buttonarray = array();
  182. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  183. get_string('save', 'quiz'));
  184. $buttonarray[] = $mform->createElement('submit', 'againbutton',
  185. get_string('saveoverrideandstay', 'quiz'));
  186. $buttonarray[] = $mform->createElement('cancel');
  187. $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
  188. $mform->closeHeaderBefore('buttonbar');
  189. }
  190. public function validation($data, $files) {
  191. global $COURSE, $DB;
  192. $errors = parent::validation($data, $files);
  193. $mform =& $this->_form;
  194. $quiz = $this->quiz;
  195. if ($mform->elementExists('userid')) {
  196. if (empty($data['userid'])) {
  197. $errors['userid'] = get_string('required');
  198. }
  199. }
  200. if ($mform->elementExists('groupid')) {
  201. if (empty($data['groupid'])) {
  202. $errors['groupid'] = get_string('required');
  203. }
  204. }
  205. // Ensure that the dates make sense.
  206. if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
  207. if ($data['timeclose'] < $data['timeopen'] ) {
  208. $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
  209. }
  210. }
  211. // Ensure that at least one quiz setting was changed.
  212. $changed = false;
  213. $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
  214. foreach ($keys as $key) {
  215. if ($data[$key] != $quiz->{$key}) {
  216. $changed = true;
  217. break;
  218. }
  219. }
  220. if (!$changed) {
  221. $errors['timeopen'] = get_string('nooverridedata', 'quiz');
  222. }
  223. return $errors;
  224. }
  225. }