2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This page handles editing and creation of assign overrides
21 * @copyright 2016 Ilya Tregubov
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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');
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
);
37 $pagetitle = get_string('editoverride', 'assign');
42 if (! $override = $DB->get_record('assign_overrides', array('id' => $overrideid))) {
43 print_error('invalidoverrideid', 'assign');
46 list($course, $cm) = get_course_and_cm_from_instance($override->assignid
, 'assign');
49 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
52 print_error('invalidcoursemodule');
55 $url = new moodle_url('/mod/assign/overrideedit.php');
57 $url->param('action', $action);
60 $url->param('id', $overrideid);
62 $url->param('cmid', $cmid);
67 require_login($course, false, $cm);
69 $context = context_module
::instance($cm->id
);
70 $assign = new assign($context, $cm, $course);
71 $assigninstance = $assign->get_instance();
73 // Add or edit an override.
74 require_capability('mod/assign:manageoverrides', $context);
77 // Editing an override.
78 $data = clone $override;
80 // Creating a new override.
81 $data = new stdClass();
84 // Merge assign defaults with data.
85 $keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate');
86 foreach ($keys as $key) {
87 if (!isset($data->{$key}) ||
$reset) {
88 $data->{$key} = $assigninstance->{$key};
92 // True if group-based override.
93 $groupmode = !empty($data->groupid
) ||
($action === 'addgroup' && empty($overrideid));
95 // If we are duplicating an override, then clear the user/group and override id
96 // since they will change.
97 if ($action === 'duplicate') {
98 $override->id
= $data->id
= null;
99 $override->userid
= $data->userid
= null;
100 $override->groupid
= $data->groupid
= null;
101 $pagetitle = get_string('duplicateoverride', 'assign');
104 $overridelisturl = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id
));
106 $overridelisturl->param('mode', 'user');
110 $mform = new assign_override_form($url, $cm, $assign, $context, $groupmode, $override);
111 $mform->set_data($data);
113 if ($mform->is_cancelled()) {
114 redirect($overridelisturl);
116 } else if (optional_param('resetbutton', 0, PARAM_ALPHA
)) {
117 $url->param('reset', true);
120 } else if ($fromform = $mform->get_data()) {
122 $fromform->assignid
= $assigninstance->id
;
124 // Replace unchanged values with null.
125 foreach ($keys as $key) {
126 if (($fromform->{$key} == $assigninstance->{$key})) {
127 $fromform->{$key} = null;
131 // See if we are replacing an existing override.
132 $userorgroupchanged = false;
133 if (empty($override->id
)) {
134 $userorgroupchanged = true;
135 } else if (!empty($fromform->userid
)) {
136 $userorgroupchanged = $fromform->userid
!== $override->userid
;
138 $userorgroupchanged = $fromform->groupid
!== $override->groupid
;
141 if ($userorgroupchanged) {
143 'assignid' => $assigninstance->id
,
144 'userid' => empty($fromform->userid
) ?
null : $fromform->userid
,
145 'groupid' => empty($fromform->groupid
) ?
null : $fromform->groupid
);
146 if ($oldoverride = $DB->get_record('assign_overrides', $conditions)) {
147 // There is an old override, so we merge any new settings on top of
148 // the older override.
149 foreach ($keys as $key) {
150 if (is_null($fromform->{$key})) {
151 $fromform->{$key} = $oldoverride->{$key};
155 $assign->delete_override($oldoverride->id
);
159 // Set the common parameters for one of the events we may be triggering.
161 'context' => $context,
163 'assignid' => $assigninstance->id
166 if (!empty($override->id
)) {
167 $fromform->id
= $override->id
;
168 $DB->update_record('assign_overrides', $fromform);
170 // Determine which override updated event to fire.
171 $params['objectid'] = $override->id
;
173 $params['relateduserid'] = $fromform->userid
;
174 $event = \mod_assign\event\user_override_updated
::create($params);
176 $params['other']['groupid'] = $fromform->groupid
;
177 $event = \mod_assign\event\group_override_updated
::create($params);
180 // Trigger the override updated event.
183 unset($fromform->id
);
184 $fromform->id
= $DB->insert_record('assign_overrides', $fromform);
186 $fromform->sortorder
= 1;
188 $overridecountgroup = $DB->count_records('assign_overrides',
189 array('userid' => null, 'assignid' => $assigninstance->id
));
190 $overridecountall = $DB->count_records('assign_overrides', array('assignid' => $assigninstance->id
));
191 if ((!$overridecountgroup) && ($overridecountall)) { // No group overrides and there are user overrides.
192 $fromform->sortorder
= 1;
194 $fromform->sortorder
= $overridecountgroup;
198 $DB->update_record('assign_overrides', $fromform);
199 reorder_group_overrides($assigninstance->id
);
202 // Determine which override created event to fire.
203 $params['objectid'] = $fromform->id
;
205 $params['relateduserid'] = $fromform->userid
;
206 $event = \mod_assign\event\user_override_created
::create($params);
208 $params['other']['groupid'] = $fromform->groupid
;
209 $event = \mod_assign\event\group_override_created
::create($params);
212 // Trigger the override created event.
216 assign_update_events($assign, $fromform);
218 if (!empty($fromform->submitbutton
)) {
219 redirect($overridelisturl);
222 // The user pressed the 'again' button, so redirect back to this page.
223 $url->remove_params('cmid');
224 $url->param('action', 'duplicate');
225 $url->param('id', $fromform->id
);
231 $PAGE->navbar
->add($pagetitle);
232 $PAGE->set_pagelayout('admin');
233 $PAGE->set_title($pagetitle);
234 $PAGE->set_heading($course->fullname
);
235 echo $OUTPUT->header();
236 echo $OUTPUT->heading(format_string($assigninstance->name
, true, array('context' => $context)));
240 echo $OUTPUT->footer();