See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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 /** 18 * Settings form for overrides in the assign module. 19 * 20 * @package mod_assign 21 * @copyright 2016 Ilya Tregubov 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->libdir . '/formslib.php'); 29 require_once($CFG->dirroot . '/mod/assign/mod_form.php'); 30 31 32 /** 33 * Form for editing settings overrides. 34 * 35 * @copyright 2016 Ilya Tregubov 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class assign_override_form extends moodleform { 39 40 /** @var object course module object. */ 41 protected $cm; 42 43 /** @var object the assign settings object. */ 44 protected $assign; 45 46 /** @var context the assign context. */ 47 protected $context; 48 49 /** @var bool editing group override (true) or user override (false). */ 50 protected $groupmode; 51 52 /** @var int groupid, if provided. */ 53 protected $groupid; 54 55 /** @var int userid, if provided. */ 56 protected $userid; 57 58 /** @var int sortorder, if provided. */ 59 protected $sortorder; 60 61 /** @var int selecteduserid, if provided. */ 62 protected $selecteduserid; 63 64 /** 65 * Constructor. 66 * @param moodle_url $submiturl the form action URL. 67 * @param object $cm course module object. 68 * @param object $assign the assign settings object. 69 * @param object $context the assign context. 70 * @param bool $groupmode editing group override (true) or user override (false). 71 * @param object $override the override being edited, if it already exists. 72 * @param int $selecteduserid the user selected in the form, if any. 73 */ 74 public function __construct($submiturl, $cm, $assign, $context, $groupmode, $override, $selecteduserid = null) { 75 76 $this->cm = $cm; 77 $this->assign = $assign; 78 $this->context = $context; 79 $this->groupmode = $groupmode; 80 $this->groupid = empty($override->groupid) ? 0 : $override->groupid; 81 $this->userid = empty($override->userid) ? 0 : $override->userid; 82 $this->sortorder = empty($override->sortorder) ? null : $override->sortorder; 83 $this->selecteduserid = $selecteduserid; 84 85 parent::__construct($submiturl, null, 'post'); 86 87 } 88 89 /** 90 * Define this form - called by the parent constructor 91 */ 92 protected function definition() { 93 global $DB, $OUTPUT, $PAGE; 94 95 $cm = $this->cm; 96 $mform = $this->_form; 97 $userid = $this->selecteduserid ?? $this->userid ?: null; 98 $assigninstance = $this->assign->get_instance($userid); 99 $inrelativedatesmode = !empty($this->assign->get_course()->relativedatesmode); 100 101 $assigngroupmode = groups_get_activity_groupmode($cm); 102 $accessallgroups = ($assigngroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context); 103 104 if ($this->groupmode) { 105 $mform->addElement('header', 'override', get_string('groupoverrides', 'assign')); 106 // Group override. 107 if ($this->groupid) { 108 // There is already a groupid, so freeze the selector. 109 $groupchoices = [ 110 $this->groupid => format_string(groups_get_group_name($this->groupid), true, [ 111 'context' => $this->context, 112 ]), 113 ]; 114 $mform->addElement('select', 'groupid', 115 get_string('overridegroup', 'assign'), $groupchoices); 116 $mform->freeze('groupid'); 117 // Add a sortorder element. 118 $mform->addElement('hidden', 'sortorder', $this->sortorder); 119 $mform->setType('sortorder', PARAM_INT); 120 $mform->freeze('sortorder'); 121 } else { 122 // Prepare the list of groups. 123 // Only include the groups the current can access. 124 $groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm); 125 if (empty($groups)) { 126 // Generate an error. 127 $link = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id)); 128 throw new \moodle_exception('groupsnone', 'assign', $link); 129 } 130 131 $groupchoices = array(); 132 foreach ($groups as $group) { 133 $groupchoices[$group->id] = format_string($group->name, true, [ 134 'context' => $this->context, 135 ]); 136 } 137 unset($groups); 138 139 if (count($groupchoices) == 0) { 140 $groupchoices[0] = get_string('none'); 141 } 142 143 $mform->addElement('select', 'groupid', 144 get_string('overridegroup', 'assign'), $groupchoices); 145 $mform->addRule('groupid', get_string('required'), 'required', null, 'client'); 146 } 147 } else { 148 $mform->addElement('header', 'override', get_string('useroverrides', 'assign')); 149 // User override. 150 if ($this->userid) { 151 // There is already a userid, so freeze the selector. 152 $user = $DB->get_record('user', array('id' => $this->userid)); 153 $userchoices = array(); 154 $userchoices[$this->userid] = fullname($user); 155 $mform->addElement('select', 'userid', 156 get_string('overrideuser', 'assign'), $userchoices); 157 $mform->freeze('userid'); 158 } else { 159 // Prepare the list of users. 160 $users = []; 161 list($sort) = users_order_by_sql('u'); 162 163 // Get the list of appropriate users, depending on whether and how groups are used. 164 $userfieldsapi = \core_user\fields::for_name(); 165 if ($accessallgroups) { 166 $users = get_enrolled_users($this->context, '', 0, 167 'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects, $sort); 168 } else if ($groups = groups_get_activity_allowed_groups($cm)) { 169 $enrolledjoin = get_enrolled_join($this->context, 'u.id'); 170 $userfields = 'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects; 171 list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED); 172 $params = $enrolledjoin->params + $ingroupparams; 173 $sql = "SELECT $userfields 174 FROM {user} u 175 JOIN {groups_members} gm ON gm.userid = u.id 176 {$enrolledjoin->joins} 177 WHERE gm.groupid $ingroupsql 178 AND {$enrolledjoin->wheres} 179 ORDER BY $sort"; 180 $users = $DB->get_records_sql($sql, $params); 181 } 182 183 // Filter users based on any fixed restrictions (groups, profile). 184 $info = new \core_availability\info_module($cm); 185 $users = $info->filter_user_list($users); 186 187 if (empty($users)) { 188 // Generate an error. 189 $link = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id)); 190 throw new \moodle_exception('usersnone', 'assign', $link); 191 } 192 193 $userchoices = array(); 194 // TODO Does not support custom user profile fields (MDL-70456). 195 $canviewemail = in_array('email', \core_user\fields::get_identity_fields($this->context, false)); 196 foreach ($users as $id => $user) { 197 if (empty($invalidusers[$id]) || (!empty($override) && 198 $id == $override->userid)) { 199 if ($canviewemail) { 200 $userchoices[$id] = fullname($user) . ', ' . $user->email; 201 } else { 202 $userchoices[$id] = fullname($user); 203 } 204 } 205 } 206 unset($users); 207 208 if (count($userchoices) == 0) { 209 $userchoices[0] = get_string('none'); 210 } 211 $mform->addElement('searchableselector', 'userid', 212 get_string('overrideuser', 'assign'), $userchoices); 213 $mform->addRule('userid', get_string('required'), 'required', null, 'client'); 214 215 if ($inrelativedatesmode) { 216 // If in relative dates mode then add the JS to reload the page when the user 217 // selection is changed to ensure that the correct dates are displayed. 218 $PAGE->requires->js_call_amd('mod_assign/override_form', 'init', [ 219 $mform->getAttribute('id'), 220 'userid' 221 ]); 222 } 223 } 224 225 if ($inrelativedatesmode) { 226 if ($userid) { 227 $templatecontext = [ 228 'allowsubmissionsfromdate' => $assigninstance->allowsubmissionsfromdate, 229 'duedate' => $assigninstance->duedate, 230 'cutoffdate' => $assigninstance->cutoffdate 231 ]; 232 $html = $OUTPUT->render_from_template('mod_assign/override_form_user_defaults', $templatecontext); 233 } else { 234 $html = get_string('noselection', 'form'); 235 } 236 237 $groupelements = []; 238 $groupelements[] = $mform->createElement('html', $html); 239 $mform->addGroup($groupelements, null, get_string('userassignmentdefaults', 'mod_assign'), null, false); 240 } 241 } 242 243 $users = $DB->get_fieldset_select('groups_members', 'userid', 'groupid = ?', array($this->groupid)); 244 array_push($users, $this->userid); 245 $extensionmax = 0; 246 foreach ($users as $value) { 247 $extension = $DB->get_record('assign_user_flags', array('assignment' => $assigninstance->id, 248 'userid' => $value)); 249 if ($extension) { 250 if ($extensionmax < $extension->extensionduedate) { 251 $extensionmax = $extension->extensionduedate; 252 } 253 } 254 } 255 256 if ($extensionmax) { 257 $assigninstance->extensionduedate = $extensionmax; 258 } 259 260 // Open and close dates. 261 $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', 262 get_string('allowsubmissionsfromdate', 'assign'), array('optional' => true)); 263 $mform->setDefault('allowsubmissionsfromdate', $assigninstance->allowsubmissionsfromdate); 264 265 $mform->addElement('date_time_selector', 'duedate', get_string('duedate', 'assign'), array('optional' => true)); 266 $mform->setDefault('duedate', $assigninstance->duedate); 267 268 $mform->addElement('date_time_selector', 'cutoffdate', get_string('cutoffdate', 'assign'), array('optional' => true)); 269 $mform->setDefault('cutoffdate', $assigninstance->cutoffdate); 270 271 if (isset($assigninstance->extensionduedate)) { 272 $mform->addElement('static', 'extensionduedate', get_string('extensionduedate', 'assign'), 273 userdate($assigninstance->extensionduedate)); 274 } 275 276 // Time limit. 277 $mform->addElement('duration', 'timelimit', 278 get_string('timelimit', 'assign'), array('optional' => true)); 279 $mform->setDefault('timelimit', $assigninstance->timelimit); 280 281 // Submit buttons. 282 $mform->addElement('submit', 'resetbutton', 283 get_string('reverttodefaults', 'assign')); 284 285 $buttonarray = array(); 286 $buttonarray[] = $mform->createElement('submit', 'submitbutton', 287 get_string('save', 'assign')); 288 $buttonarray[] = $mform->createElement('submit', 'againbutton', 289 get_string('saveoverrideandstay', 'assign')); 290 $buttonarray[] = $mform->createElement('cancel'); 291 292 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false); 293 $mform->closeHeaderBefore('buttonbar'); 294 295 } 296 297 /** 298 * Validate the submitted form data. 299 * 300 * @param array $data array of ("fieldname"=>value) of submitted data 301 * @param array $files array of uploaded files "element_name"=>tmp_file_path 302 * @return array of "element_name"=>"error_description" if there are errors 303 */ 304 public function validation($data, $files) { 305 $errors = parent::validation($data, $files); 306 307 $mform =& $this->_form; 308 $userid = $this->selecteduserid ?? $this->userid ?: null; 309 $assigninstance = $this->assign->get_instance($userid); 310 311 if ($mform->elementExists('userid')) { 312 if (empty($data['userid'])) { 313 $errors['userid'] = get_string('required'); 314 } 315 } 316 317 if ($mform->elementExists('groupid')) { 318 if (empty($data['groupid'])) { 319 $errors['groupid'] = get_string('required'); 320 } 321 } 322 323 // Ensure that the dates make sense. 324 if (!empty($data['allowsubmissionsfromdate']) && !empty($data['cutoffdate'])) { 325 if ($data['cutoffdate'] < $data['allowsubmissionsfromdate']) { 326 $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign'); 327 } 328 } 329 330 if (!empty($data['allowsubmissionsfromdate']) && !empty($data['duedate'])) { 331 if ($data['duedate'] <= $data['allowsubmissionsfromdate']) { 332 $errors['duedate'] = get_string('duedateaftersubmissionvalidation', 'assign'); 333 } 334 } 335 336 if (!empty($data['cutoffdate']) && !empty($data['duedate'])) { 337 if ($data['cutoffdate'] < $data['duedate'] ) { 338 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign'); 339 } 340 } 341 342 // Ensure that override duedate/allowsubmissionsfromdate are before extension date if exist. 343 if (!empty($assigninstance->extensionduedate) && !empty($data['duedate'])) { 344 if ($assigninstance->extensionduedate < $data['duedate']) { 345 $errors['duedate'] = get_string('extensionnotafterduedate', 'assign'); 346 } 347 } 348 if (!empty($assigninstance->extensionduedate) && !empty($data['allowsubmissionsfromdate'])) { 349 if ($assigninstance->extensionduedate < $data['allowsubmissionsfromdate']) { 350 $errors['allowsubmissionsfromdate'] = get_string('extensionnotafterfromdate', 'assign'); 351 } 352 } 353 354 // Ensure that at least one assign setting was changed. 355 $changed = false; 356 $keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate', 'timelimit'); 357 foreach ($keys as $key) { 358 if ($data[$key] != $assigninstance->{$key}) { 359 $changed = true; 360 break; 361 } 362 } 363 364 if (!$changed) { 365 $errors['allowsubmissionsfromdate'] = get_string('nooverridedata', 'assign'); 366 } 367 368 return $errors; 369 } 370 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body