Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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          $mform->addElement('header', 'override', get_string('override', 'assign'));
 102  
 103          $assigngroupmode = groups_get_activity_groupmode($cm);
 104          $accessallgroups = ($assigngroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context);
 105  
 106          if ($this->groupmode) {
 107              // Group override.
 108              if ($this->groupid) {
 109                  // There is already a groupid, so freeze the selector.
 110                  $groupchoices = array();
 111                  $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
 112                  $mform->addElement('select', 'groupid',
 113                          get_string('overridegroup', 'assign'), $groupchoices);
 114                  $mform->freeze('groupid');
 115                  // Add a sortorder element.
 116                  $mform->addElement('hidden', 'sortorder', $this->sortorder);
 117                  $mform->setType('sortorder', PARAM_INT);
 118                  $mform->freeze('sortorder');
 119              } else {
 120                  // Prepare the list of groups.
 121                  // Only include the groups the current can access.
 122                  $groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
 123                  if (empty($groups)) {
 124                      // Generate an error.
 125                      $link = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
 126                      print_error('groupsnone', 'assign', $link);
 127                  }
 128  
 129                  $groupchoices = array();
 130                  foreach ($groups as $group) {
 131                      $groupchoices[$group->id] = $group->name;
 132                  }
 133                  unset($groups);
 134  
 135                  if (count($groupchoices) == 0) {
 136                      $groupchoices[0] = get_string('none');
 137                  }
 138  
 139                  $mform->addElement('select', 'groupid',
 140                          get_string('overridegroup', 'assign'), $groupchoices);
 141                  $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
 142              }
 143          } else {
 144              // User override.
 145              if ($this->userid) {
 146                  // There is already a userid, so freeze the selector.
 147                  $user = $DB->get_record('user', array('id' => $this->userid));
 148                  $userchoices = array();
 149                  $userchoices[$this->userid] = fullname($user);
 150                  $mform->addElement('select', 'userid',
 151                          get_string('overrideuser', 'assign'), $userchoices);
 152                  $mform->freeze('userid');
 153              } else {
 154                  // Prepare the list of users.
 155                  $users = [];
 156                  list($sort) = users_order_by_sql('u');
 157  
 158                  // Get the list of appropriate users, depending on whether and how groups are used.
 159                  if ($accessallgroups) {
 160                      $users = get_enrolled_users($this->context, '', 0,
 161                              'u.id, u.email, ' . get_all_user_name_fields(true, 'u'), $sort);
 162                  } else if ($groups = groups_get_activity_allowed_groups($cm)) {
 163                      $enrolledjoin = get_enrolled_join($this->context, 'u.id');
 164                      $userfields = 'u.id, u.email, ' . get_all_user_name_fields(true, 'u');
 165                      list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
 166                      $params = $enrolledjoin->params + $ingroupparams;
 167                      $sql = "SELECT $userfields
 168                                FROM {user} u
 169                                JOIN {groups_members} gm ON gm.userid = u.id
 170                                     {$enrolledjoin->joins}
 171                               WHERE gm.groupid $ingroupsql
 172                                     AND {$enrolledjoin->wheres}
 173                            ORDER BY $sort";
 174                      $users = $DB->get_records_sql($sql, $params);
 175                  }
 176  
 177                  // Filter users based on any fixed restrictions (groups, profile).
 178                  $info = new \core_availability\info_module($cm);
 179                  $users = $info->filter_user_list($users);
 180  
 181                  if (empty($users)) {
 182                      // Generate an error.
 183                      $link = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
 184                      print_error('usersnone', 'assign', $link);
 185                  }
 186  
 187                  $userchoices = array();
 188                  $canviewemail = in_array('email', get_extra_user_fields($this->context));
 189                  foreach ($users as $id => $user) {
 190                      if (empty($invalidusers[$id]) || (!empty($override) &&
 191                              $id == $override->userid)) {
 192                          if ($canviewemail) {
 193                              $userchoices[$id] = fullname($user) . ', ' . $user->email;
 194                          } else {
 195                              $userchoices[$id] = fullname($user);
 196                          }
 197                      }
 198                  }
 199                  unset($users);
 200  
 201                  if (count($userchoices) == 0) {
 202                      $userchoices[0] = get_string('none');
 203                  }
 204                  $mform->addElement('searchableselector', 'userid',
 205                          get_string('overrideuser', 'assign'), $userchoices);
 206                  $mform->addRule('userid', get_string('required'), 'required', null, 'client');
 207  
 208                  if ($inrelativedatesmode) {
 209                      // If in relative dates mode then add the JS to reload the page when the user
 210                      // selection is changed to ensure that the correct dates are displayed.
 211                      $PAGE->requires->js_call_amd('mod_assign/override_form', 'init', [
 212                          $mform->getAttribute('id'),
 213                          'userid'
 214                      ]);
 215                  }
 216              }
 217  
 218              if ($inrelativedatesmode) {
 219                  if ($userid) {
 220                      $templatecontext = [
 221                          'allowsubmissionsfromdate' => $assigninstance->allowsubmissionsfromdate,
 222                          'duedate' => $assigninstance->duedate,
 223                          'cutoffdate' => $assigninstance->cutoffdate
 224                      ];
 225                      $html = $OUTPUT->render_from_template('mod_assign/override_form_user_defaults', $templatecontext);
 226                  } else {
 227                      $html = get_string('noselection', 'form');
 228                  }
 229  
 230                  $groupelements = [];
 231                  $groupelements[] = $mform->createElement('html', $html);
 232                  $mform->addGroup($groupelements, null, get_string('userassignmentdefaults', 'mod_assign'), null, false);
 233              }
 234          }
 235  
 236          $users = $DB->get_fieldset_select('groups_members', 'userid', 'groupid = ?', array($this->groupid));
 237          array_push($users, $this->userid);
 238          $extensionmax = 0;
 239          foreach ($users as $value) {
 240              $extension = $DB->get_record('assign_user_flags', array('assignment' => $assigninstance->id,
 241                  'userid' => $value));
 242              if ($extension) {
 243                  if ($extensionmax < $extension->extensionduedate) {
 244                      $extensionmax = $extension->extensionduedate;
 245                  }
 246              }
 247          }
 248  
 249          if ($extensionmax) {
 250              $assigninstance->extensionduedate = $extensionmax;
 251          }
 252  
 253          // Open and close dates.
 254          $mform->addElement('date_time_selector', 'allowsubmissionsfromdate',
 255              get_string('allowsubmissionsfromdate', 'assign'), array('optional' => true));
 256          $mform->setDefault('allowsubmissionsfromdate', $assigninstance->allowsubmissionsfromdate);
 257  
 258          $mform->addElement('date_time_selector', 'duedate', get_string('duedate', 'assign'), array('optional' => true));
 259          $mform->setDefault('duedate', $assigninstance->duedate);
 260  
 261          $mform->addElement('date_time_selector', 'cutoffdate', get_string('cutoffdate', 'assign'), array('optional' => true));
 262          $mform->setDefault('cutoffdate', $assigninstance->cutoffdate);
 263  
 264          if (isset($assigninstance->extensionduedate)) {
 265              $mform->addElement('static', 'extensionduedate', get_string('extensionduedate', 'assign'),
 266                  userdate($assigninstance->extensionduedate));
 267          }
 268  
 269          // Submit buttons.
 270          $mform->addElement('submit', 'resetbutton',
 271                  get_string('reverttodefaults', 'assign'));
 272  
 273          $buttonarray = array();
 274          $buttonarray[] = $mform->createElement('submit', 'submitbutton',
 275                  get_string('save', 'assign'));
 276          $buttonarray[] = $mform->createElement('submit', 'againbutton',
 277                  get_string('saveoverrideandstay', 'assign'));
 278          $buttonarray[] = $mform->createElement('cancel');
 279  
 280          $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
 281          $mform->closeHeaderBefore('buttonbar');
 282  
 283      }
 284  
 285      /**
 286       * Validate the submitted form data.
 287       *
 288       * @param array $data array of ("fieldname"=>value) of submitted data
 289       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 290       * @return array of "element_name"=>"error_description" if there are errors
 291       */
 292      public function validation($data, $files) {
 293          $errors = parent::validation($data, $files);
 294  
 295          $mform =& $this->_form;
 296          $userid = $this->selecteduserid ?? $this->userid ?: null;
 297          $assigninstance = $this->assign->get_instance($userid);
 298  
 299          if ($mform->elementExists('userid')) {
 300              if (empty($data['userid'])) {
 301                  $errors['userid'] = get_string('required');
 302              }
 303          }
 304  
 305          if ($mform->elementExists('groupid')) {
 306              if (empty($data['groupid'])) {
 307                  $errors['groupid'] = get_string('required');
 308              }
 309          }
 310  
 311          // Ensure that the dates make sense.
 312          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['cutoffdate'])) {
 313              if ($data['cutoffdate'] < $data['allowsubmissionsfromdate']) {
 314                  $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
 315              }
 316          }
 317  
 318          if (!empty($data['allowsubmissionsfromdate']) && !empty($data['duedate'])) {
 319              if ($data['duedate'] < $data['allowsubmissionsfromdate']) {
 320                  $errors['duedate'] = get_string('duedatevalidation', 'assign');
 321              }
 322          }
 323  
 324          if (!empty($data['cutoffdate']) && !empty($data['duedate'])) {
 325              if ($data['cutoffdate'] < $data['duedate'] ) {
 326                  $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
 327              }
 328          }
 329  
 330          // Ensure that override duedate/allowsubmissionsfromdate are before extension date if exist.
 331          if (!empty($assigninstance->extensionduedate) && !empty($data['duedate'])) {
 332              if ($assigninstance->extensionduedate < $data['duedate']) {
 333                  $errors['duedate'] = get_string('extensionnotafterduedate', 'assign');
 334              }
 335          }
 336          if (!empty($assigninstance->extensionduedate) && !empty($data['allowsubmissionsfromdate'])) {
 337              if ($assigninstance->extensionduedate < $data['allowsubmissionsfromdate']) {
 338                  $errors['allowsubmissionsfromdate'] = get_string('extensionnotafterfromdate', 'assign');
 339              }
 340          }
 341  
 342          // Ensure that at least one assign setting was changed.
 343          $changed = false;
 344          $keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate');
 345          foreach ($keys as $key) {
 346              if ($data[$key] != $assigninstance->{$key}) {
 347                  $changed = true;
 348                  break;
 349              }
 350          }
 351  
 352          if (!$changed) {
 353              $errors['allowsubmissionsfromdate'] = get_string('nooverridedata', 'assign');
 354          }
 355  
 356          return $errors;
 357      }
 358  }