Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * This page handles editing and creation of assign overrides
  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  require_once(dirname(__FILE__) . '/../../config.php');
  27  require_once($CFG->dirroot.'/mod/assign/lib.php');
  28  require_once($CFG->dirroot.'/mod/assign/locallib.php');
  29  require_once($CFG->dirroot.'/mod/assign/override_form.php');
  30  
  31  
  32  $cmid = optional_param('cmid', 0, PARAM_INT);
  33  $overrideid = optional_param('id', 0, PARAM_INT);
  34  $action = optional_param('action', null, PARAM_ALPHA);
  35  $reset = optional_param('reset', false, PARAM_BOOL);
  36  $userid = optional_param('userid', null, PARAM_INT);
  37  $userchange = optional_param('userchange', false, PARAM_BOOL);
  38  
  39  $pagetitle = get_string('editoverride', 'assign');
  40  
  41  $override = null;
  42  if ($overrideid) {
  43  
  44      if (! $override = $DB->get_record('assign_overrides', array('id' => $overrideid))) {
  45          print_error('invalidoverrideid', 'assign');
  46      }
  47  
  48      list($course, $cm) = get_course_and_cm_from_instance($override->assignid, 'assign');
  49  
  50  } else if ($cmid) {
  51      list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
  52  
  53  } else {
  54      print_error('invalidcoursemodule');
  55  }
  56  
  57  $url = new moodle_url('/mod/assign/overrideedit.php');
  58  if ($action) {
  59      $url->param('action', $action);
  60  }
  61  if ($overrideid) {
  62      $url->param('id', $overrideid);
  63  } else {
  64      $url->param('cmid', $cmid);
  65  }
  66  
  67  $PAGE->set_url($url);
  68  
  69  require_login($course, false, $cm);
  70  
  71  $context = context_module::instance($cm->id);
  72  $assign = new assign($context, $cm, $course);
  73  $assigninstance = $assign->get_instance($userid);
  74  $shouldadduserid = $userid && !empty($course->relativedatesmode);
  75  $shouldresetform = optional_param('resetbutton', 0, PARAM_ALPHA) || ($userchange && $action !== 'duplicate');
  76  
  77  // Add or edit an override.
  78  require_capability('mod/assign:manageoverrides', $context);
  79  
  80  if ($overrideid) {
  81      // Editing an override.
  82      $data = clone $override;
  83  
  84      if ($override->groupid) {
  85          if (!groups_group_visible($override->groupid, $course, $cm)) {
  86              print_error('invalidoverrideid', 'assign');
  87          }
  88      } else {
  89          if (!groups_user_groups_visible($course, $override->userid, $cm)) {
  90              print_error('invalidoverrideid', 'assign');
  91          }
  92      }
  93  } else {
  94      // Creating a new override.
  95      $data = new stdClass();
  96  }
  97  
  98  // Merge assign defaults with data.
  99  $keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate');
 100  foreach ($keys as $key) {
 101      if (!isset($data->{$key}) || $reset) {
 102          $data->{$key} = $assigninstance->{$key};
 103      }
 104  }
 105  
 106  // True if group-based override.
 107  $groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
 108  
 109  // If we are duplicating an override, then clear the user/group and override id
 110  // since they will change.
 111  if ($action === 'duplicate') {
 112      $override->id = $data->id = null;
 113      $override->userid = $data->userid = null;
 114      $override->groupid = $data->groupid = null;
 115      $pagetitle = get_string('duplicateoverride', 'assign');
 116  }
 117  
 118  if ($shouldadduserid) {
 119      $data->userid = $userid;
 120  }
 121  
 122  $overridelisturl = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
 123  if (!$groupmode) {
 124      $overridelisturl->param('mode', 'user');
 125  }
 126  
 127  // Setup the form.
 128  $mform = new assign_override_form($url, $cm, $assign, $context, $groupmode, $override, $userid);
 129  $mform->set_data($data);
 130  
 131  if ($mform->is_cancelled()) {
 132      redirect($overridelisturl);
 133  
 134  } else if ($shouldresetform) {
 135      $url->param('reset', true);
 136      if ($shouldadduserid) {
 137          $url->param('userid', $userid);
 138      }
 139      redirect($url);
 140  
 141  } else if (!$userchange && $fromform = $mform->get_data()) {
 142      // Process the data.
 143      $fromform->assignid = $assigninstance->id;
 144  
 145      // Replace unchanged values with null.
 146      foreach ($keys as $key) {
 147          if (($fromform->{$key} == $assigninstance->{$key})) {
 148              $fromform->{$key} = null;
 149          }
 150      }
 151  
 152      // See if we are replacing an existing override.
 153      $userorgroupchanged = false;
 154      if (empty($override->id)) {
 155          $userorgroupchanged = true;
 156      } else if (!empty($fromform->userid)) {
 157          $userorgroupchanged = $fromform->userid !== $override->userid;
 158      } else {
 159          $userorgroupchanged = $fromform->groupid !== $override->groupid;
 160      }
 161  
 162      if ($userorgroupchanged) {
 163          $conditions = array(
 164                  'assignid' => $assigninstance->id,
 165                  'userid' => empty($fromform->userid) ? null : $fromform->userid,
 166                  'groupid' => empty($fromform->groupid) ? null : $fromform->groupid);
 167          if ($oldoverride = $DB->get_record('assign_overrides', $conditions)) {
 168              // There is an old override, so we merge any new settings on top of
 169              // the older override.
 170              foreach ($keys as $key) {
 171                  if (is_null($fromform->{$key})) {
 172                      $fromform->{$key} = $oldoverride->{$key};
 173                  }
 174              }
 175  
 176              $assign->delete_override($oldoverride->id);
 177          }
 178      }
 179  
 180      // Set the common parameters for one of the events we may be triggering.
 181      $params = array(
 182          'context' => $context,
 183          'other' => array(
 184              'assignid' => $assigninstance->id
 185          )
 186      );
 187      if (!empty($override->id)) {
 188          $fromform->id = $override->id;
 189          $DB->update_record('assign_overrides', $fromform);
 190  
 191          // Determine which override updated event to fire.
 192          $params['objectid'] = $override->id;
 193          if (!$groupmode) {
 194              $params['relateduserid'] = $fromform->userid;
 195              $event = \mod_assign\event\user_override_updated::create($params);
 196          } else {
 197              $params['other']['groupid'] = $fromform->groupid;
 198              $event = \mod_assign\event\group_override_updated::create($params);
 199          }
 200  
 201          // Trigger the override updated event.
 202          $event->trigger();
 203      } else {
 204          unset($fromform->id);
 205          $fromform->id = $DB->insert_record('assign_overrides', $fromform);
 206          if ($groupmode) {
 207              $fromform->sortorder = 1;
 208  
 209              $overridecountgroup = $DB->count_records('assign_overrides',
 210                  array('userid' => null, 'assignid' => $assigninstance->id));
 211              $overridecountall = $DB->count_records('assign_overrides', array('assignid' => $assigninstance->id));
 212              if ((!$overridecountgroup) && ($overridecountall)) { // No group overrides and there are user overrides.
 213                  $fromform->sortorder = 1;
 214              } else {
 215                  $fromform->sortorder = $overridecountgroup;
 216  
 217              }
 218  
 219              $DB->update_record('assign_overrides', $fromform);
 220              reorder_group_overrides($assigninstance->id);
 221          }
 222  
 223          // Determine which override created event to fire.
 224          $params['objectid'] = $fromform->id;
 225          if (!$groupmode) {
 226              $params['relateduserid'] = $fromform->userid;
 227              $event = \mod_assign\event\user_override_created::create($params);
 228          } else {
 229              $params['other']['groupid'] = $fromform->groupid;
 230              $event = \mod_assign\event\group_override_created::create($params);
 231          }
 232  
 233          // Trigger the override created event.
 234          $event->trigger();
 235      }
 236  
 237      assign_update_events($assign, $fromform);
 238  
 239      if (!empty($fromform->submitbutton)) {
 240          redirect($overridelisturl);
 241      }
 242  
 243      // The user pressed the 'again' button, so redirect back to this page.
 244      $url->remove_params('cmid');
 245      $url->param('action', 'duplicate');
 246      $url->param('id', $fromform->id);
 247      redirect($url);
 248  
 249  }
 250  
 251  // Print the form.
 252  $PAGE->navbar->add($pagetitle);
 253  $PAGE->set_pagelayout('admin');
 254  $PAGE->set_title($pagetitle);
 255  $PAGE->set_heading($course->fullname);
 256  echo $OUTPUT->header();
 257  echo $OUTPUT->heading(format_string($assigninstance->name, true, array('context' => $context)));
 258  
 259  $mform->display();
 260  
 261  echo $OUTPUT->footer();