Differences Between: [Versions 402 and 403]
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 namespace mod_quiz\form; 18 19 use cm_info; 20 use context; 21 use context_module; 22 use mod_quiz_mod_form; 23 use moodle_url; 24 use moodleform; 25 use stdClass; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 require_once($CFG->libdir . '/formslib.php'); 30 require_once($CFG->dirroot . '/mod/quiz/mod_form.php'); 31 32 /** 33 * Form for editing quiz settings overrides. 34 * 35 * @package mod_quiz 36 * @copyright 2010 Matt Petro 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class edit_override_form extends moodleform { 40 41 /** @var cm_info course module object. */ 42 protected $cm; 43 44 /** @var stdClass the quiz settings object. */ 45 protected $quiz; 46 47 /** @var context_module the quiz context. */ 48 protected $context; 49 50 /** @var bool editing group override (true) or user override (false). */ 51 protected $groupmode; 52 53 /** @var int groupid, if provided. */ 54 protected $groupid; 55 56 /** @var int userid, if provided. */ 57 protected $userid; 58 59 /** 60 * Constructor. 61 * 62 * @param moodle_url $submiturl the form action URL. 63 * @param cm_info|stdClass $cm course module object. 64 * @param stdClass $quiz the quiz settings object. 65 * @param context_module $context the quiz context. 66 * @param bool $groupmode editing group override (true) or user override (false). 67 * @param stdClass|null $override the override being edited, if it already exists. 68 */ 69 public function __construct(moodle_url $submiturl, 70 cm_info|stdClass $cm, stdClass $quiz, context_module $context, 71 bool $groupmode, ?stdClass $override) { 72 73 // Normalise our course module instance. 74 if (!($cm instanceof cm_info)) { 75 $course = get_course($quiz->course); 76 $cm = get_fast_modinfo($course)->get_cm($cm->id); 77 } 78 79 $this->cm = $cm; 80 $this->quiz = $quiz; 81 $this->context = $context; 82 $this->groupmode = $groupmode; 83 $this->groupid = empty($override->groupid) ? 0 : $override->groupid; 84 $this->userid = empty($override->userid) ? 0 : $override->userid; 85 86 parent::__construct($submiturl); 87 } 88 89 protected function definition() { 90 global $DB; 91 92 $cm = $this->cm; 93 $mform = $this->_form; 94 95 $mform->addElement('header', 'override', get_string('override', 'quiz')); 96 97 $quizgroupmode = groups_get_activity_groupmode($cm); 98 $accessallgroups = ($quizgroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context); 99 100 if ($this->groupmode) { 101 // Group override. 102 if ($this->groupid) { 103 // There is already a groupid, so freeze the selector. 104 $groupchoices = [ 105 $this->groupid => format_string(groups_get_group_name($this->groupid), true, ['context' => $this->context]), 106 ]; 107 $mform->addElement('select', 'groupid', 108 get_string('overridegroup', 'quiz'), $groupchoices); 109 $mform->freeze('groupid'); 110 } else { 111 // Prepare the list of groups. 112 // Only include the groups the current can access. 113 $groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm); 114 if (empty($groups)) { 115 // Generate an error. 116 $link = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]); 117 throw new \moodle_exception('groupsnone', 'quiz', $link); 118 } 119 120 $groupchoices = []; 121 foreach ($groups as $group) { 122 if ($group->visibility != GROUPS_VISIBILITY_NONE) { 123 $groupchoices[$group->id] = format_string($group->name, true, ['context' => $this->context]); 124 } 125 } 126 unset($groups); 127 128 if (count($groupchoices) == 0) { 129 $groupchoices[0] = get_string('none'); 130 } 131 132 $mform->addElement('select', 'groupid', 133 get_string('overridegroup', 'quiz'), $groupchoices); 134 $mform->addRule('groupid', get_string('required'), 'required', null, 'client'); 135 } 136 } else { 137 // User override. 138 $userfieldsapi = \core_user\fields::for_identity($this->context)->with_userpic()->with_name(); 139 $extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]); 140 if ($this->userid) { 141 // There is already a userid, so freeze the selector. 142 $user = $DB->get_record('user', ['id' => $this->userid]); 143 profile_load_custom_fields($user); 144 $userchoices = []; 145 $userchoices[$this->userid] = self::display_user_name($user, $extrauserfields); 146 $mform->addElement('select', 'userid', 147 get_string('overrideuser', 'quiz'), $userchoices); 148 $mform->freeze('userid'); 149 } else { 150 // Prepare the list of users. 151 $groupids = 0; 152 if (!$accessallgroups) { 153 $groups = groups_get_activity_allowed_groups($cm); 154 $groupids = array_keys($groups); 155 } 156 $enrolledjoin = get_enrolled_with_capabilities_join( 157 $this->context, '', 'mod/quiz:attempt', $groupids, true); 158 $userfieldsql = $userfieldsapi->get_sql('u', true, '', '', false); 159 list($sort, $sortparams) = users_order_by_sql('u', null, 160 $this->context, $userfieldsql->mappings); 161 162 $users = $DB->get_records_sql(" 163 SELECT DISTINCT $userfieldsql->selects 164 FROM {user} u 165 $enrolledjoin->joins 166 $userfieldsql->joins 167 LEFT JOIN {quiz_overrides} existingoverride ON 168 existingoverride.userid = u.id AND existingoverride.quiz = :quizid 169 WHERE existingoverride.id IS NULL 170 AND $enrolledjoin->wheres 171 ORDER BY $sort 172 ", array_merge(['quizid' => $this->quiz->id], $userfieldsql->params, $enrolledjoin->params, $sortparams)); 173 174 // Filter users based on any fixed restrictions (groups, profile). 175 $info = new \core_availability\info_module($cm); 176 $users = $info->filter_user_list($users); 177 178 if (empty($users)) { 179 // Generate an error. 180 $link = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]); 181 throw new \moodle_exception('usersnone', 'quiz', $link); 182 } 183 184 $userchoices = []; 185 foreach ($users as $id => $user) { 186 $userchoices[$id] = self::display_user_name($user, $extrauserfields); 187 } 188 unset($users); 189 190 $mform->addElement('searchableselector', 'userid', 191 get_string('overrideuser', 'quiz'), $userchoices); 192 $mform->addRule('userid', get_string('required'), 'required', null, 'client'); 193 } 194 } 195 196 // Password. 197 // This field has to be above the date and timelimit fields, 198 // otherwise browsers will clear it when those fields are changed. 199 $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz')); 200 $mform->setType('password', PARAM_TEXT); 201 $mform->addHelpButton('password', 'requirepassword', 'quiz'); 202 $mform->setDefault('password', $this->quiz->password); 203 204 // Open and close dates. 205 $mform->addElement('date_time_selector', 'timeopen', 206 get_string('quizopen', 'quiz'), mod_quiz_mod_form::$datefieldoptions); 207 $mform->setDefault('timeopen', $this->quiz->timeopen); 208 209 $mform->addElement('date_time_selector', 'timeclose', 210 get_string('quizclose', 'quiz'), mod_quiz_mod_form::$datefieldoptions); 211 $mform->setDefault('timeclose', $this->quiz->timeclose); 212 213 // Time limit. 214 $mform->addElement('duration', 'timelimit', 215 get_string('timelimit', 'quiz'), ['optional' => true]); 216 $mform->addHelpButton('timelimit', 'timelimit', 'quiz'); 217 $mform->setDefault('timelimit', $this->quiz->timelimit); 218 219 // Number of attempts. 220 $attemptoptions = ['0' => get_string('unlimited')]; 221 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) { 222 $attemptoptions[$i] = $i; 223 } 224 $mform->addElement('select', 'attempts', 225 get_string('attemptsallowed', 'quiz'), $attemptoptions); 226 $mform->addHelpButton('attempts', 'attempts', 'quiz'); 227 $mform->setDefault('attempts', $this->quiz->attempts); 228 229 // Submit buttons. 230 $mform->addElement('submit', 'resetbutton', 231 get_string('reverttodefaults', 'quiz')); 232 233 $buttonarray = []; 234 $buttonarray[] = $mform->createElement('submit', 'submitbutton', 235 get_string('save', 'quiz')); 236 $buttonarray[] = $mform->createElement('submit', 'againbutton', 237 get_string('saveoverrideandstay', 'quiz')); 238 $buttonarray[] = $mform->createElement('cancel'); 239 240 $mform->addGroup($buttonarray, 'buttonbar', '', [' '], false); 241 $mform->closeHeaderBefore('buttonbar'); 242 } 243 244 /** 245 * Get a user's name and identity ready to display. 246 * 247 * @param stdClass $user a user object. 248 * @param array $extrauserfields (identity fields in user table only from the user_fields API) 249 * @return string User's name, with extra info, for display. 250 */ 251 public static function display_user_name(stdClass $user, array $extrauserfields): string { 252 $username = fullname($user); 253 $namefields = []; 254 foreach ($extrauserfields as $field) { 255 if (isset($user->$field) && $user->$field !== '') { 256 $namefields[] = s($user->$field); 257 } else if (strpos($field, 'profile_field_') === 0) { 258 $field = substr($field, 14); 259 if (isset($user->profile[$field]) && $user->profile[$field] !== '') { 260 $namefields[] = s($user->profile[$field]); 261 } 262 } 263 } 264 if ($namefields) { 265 $username .= ' (' . implode(', ', $namefields) . ')'; 266 } 267 return $username; 268 } 269 270 public function validation($data, $files): array { 271 $errors = parent::validation($data, $files); 272 273 $mform =& $this->_form; 274 $quiz = $this->quiz; 275 276 if ($mform->elementExists('userid')) { 277 if (empty($data['userid'])) { 278 $errors['userid'] = get_string('required'); 279 } 280 } 281 282 if ($mform->elementExists('groupid')) { 283 if (empty($data['groupid'])) { 284 $errors['groupid'] = get_string('required'); 285 } 286 } 287 288 // Ensure that the dates make sense. 289 if (!empty($data['timeopen']) && !empty($data['timeclose'])) { 290 if ($data['timeclose'] < $data['timeopen'] ) { 291 $errors['timeclose'] = get_string('closebeforeopen', 'quiz'); 292 } 293 } 294 295 // Ensure that at least one quiz setting was changed. 296 $changed = false; 297 $keys = ['timeopen', 'timeclose', 'timelimit', 'attempts', 'password']; 298 foreach ($keys as $key) { 299 if ($data[$key] != $quiz->{$key}) { 300 $changed = true; 301 break; 302 } 303 } 304 if (!$changed) { 305 $errors['timeopen'] = get_string('nooverridedata', 'quiz'); 306 } 307 308 return $errors; 309 } 310 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body