MDL-58900 mod_assign: fix incorrect overrides ordering
[moodle.git] / mod / assign / overrideedit.php
CommitLineData
d685b959
IT
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
26require_once(dirname(__FILE__) . '/../../config.php');
27require_once($CFG->dirroot.'/mod/assign/lib.php');
28require_once($CFG->dirroot.'/mod/assign/locallib.php');
29require_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
eeaefbb8
KH
37$pagetitle = get_string('editoverride', 'assign');
38
d685b959
IT
39$override = null;
40if ($overrideid) {
41
42 if (! $override = $DB->get_record('assign_overrides', array('id' => $overrideid))) {
43 print_error('invalidoverrideid', 'assign');
44 }
45
f3ec5411 46 list($course, $cm) = get_course_and_cm_from_instance($override->assignid, 'assign');
d685b959
IT
47
48} else if ($cmid) {
49 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
d685b959
IT
50
51} else {
52 print_error('invalidcoursemodule');
53}
d685b959
IT
54
55$url = new moodle_url('/mod/assign/overrideedit.php');
56if ($action) {
57 $url->param('action', $action);
58}
59if ($overrideid) {
60 $url->param('id', $overrideid);
61} else {
62 $url->param('cmid', $cmid);
63}
64
65$PAGE->set_url($url);
66
67require_login($course, false, $cm);
68
69$context = context_module::instance($cm->id);
f3ec5411
JP
70$assign = new assign($context, $cm, $course);
71$assigninstance = $assign->get_instance();
d685b959
IT
72
73// Add or edit an override.
74require_capability('mod/assign:manageoverrides', $context);
75
76if ($overrideid) {
77 // Editing an override.
78 $data = clone $override;
79} else {
80 // Creating a new override.
81 $data = new stdClass();
82}
83
84// Merge assign defaults with data.
85$keys = array('duedate', 'cutoffdate', 'allowsubmissionsfromdate');
86foreach ($keys as $key) {
87 if (!isset($data->{$key}) || $reset) {
f3ec5411 88 $data->{$key} = $assigninstance->{$key};
d685b959
IT
89 }
90}
91
92// True if group-based override.
93$groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
94
95// If we are duplicating an override, then clear the user/group and override id
96// since they will change.
97if ($action === 'duplicate') {
98 $override->id = $data->id = null;
99 $override->userid = $data->userid = null;
100 $override->groupid = $data->groupid = null;
eeaefbb8 101 $pagetitle = get_string('duplicateoverride', 'assign');
d685b959
IT
102}
103
104$overridelisturl = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
105if (!$groupmode) {
106 $overridelisturl->param('mode', 'user');
107}
108
109// Setup the form.
110$mform = new assign_override_form($url, $cm, $assign, $context, $groupmode, $override);
111$mform->set_data($data);
112
113if ($mform->is_cancelled()) {
114 redirect($overridelisturl);
115
116} else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
117 $url->param('reset', true);
118 redirect($url);
119
120} else if ($fromform = $mform->get_data()) {
121 // Process the data.
f3ec5411 122 $fromform->assignid = $assigninstance->id;
d685b959
IT
123
124 // Replace unchanged values with null.
125 foreach ($keys as $key) {
f3ec5411 126 if (($fromform->{$key} == $assigninstance->{$key})) {
d685b959
IT
127 $fromform->{$key} = null;
128 }
129 }
130
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;
137 } else {
138 $userorgroupchanged = $fromform->groupid !== $override->groupid;
139 }
140
141 if ($userorgroupchanged) {
142 $conditions = array(
f3ec5411 143 'assignid' => $assigninstance->id,
d685b959
IT
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};
152 }
153 }
154
155 $assign->delete_override($oldoverride->id);
156 }
157 }
158
159 // Set the common parameters for one of the events we may be triggering.
160 $params = array(
161 'context' => $context,
162 'other' => array(
f3ec5411 163 'assignid' => $assigninstance->id
d685b959
IT
164 )
165 );
166 if (!empty($override->id)) {
167 $fromform->id = $override->id;
168 $DB->update_record('assign_overrides', $fromform);
169
170 // Determine which override updated event to fire.
171 $params['objectid'] = $override->id;
172 if (!$groupmode) {
173 $params['relateduserid'] = $fromform->userid;
174 $event = \mod_assign\event\user_override_updated::create($params);
175 } else {
176 $params['other']['groupid'] = $fromform->groupid;
177 $event = \mod_assign\event\group_override_updated::create($params);
178 }
179
180 // Trigger the override updated event.
181 $event->trigger();
182 } else {
183 unset($fromform->id);
184 $fromform->id = $DB->insert_record('assign_overrides', $fromform);
185 if ($groupmode) {
dc0deaf0 186 $fromform->sortorder = 1;
d685b959
IT
187
188 $overridecountgroup = $DB->count_records('assign_overrides',
f3ec5411
JP
189 array('userid' => null, 'assignid' => $assigninstance->id));
190 $overridecountall = $DB->count_records('assign_overrides', array('assignid' => $assigninstance->id));
d685b959
IT
191 if ((!$overridecountgroup) && ($overridecountall)) { // No group overrides and there are user overrides.
192 $fromform->sortorder = 1;
193 } else {
194 $fromform->sortorder = $overridecountgroup;
195
196 }
197
198 $DB->update_record('assign_overrides', $fromform);
dc0deaf0 199 reorder_group_overrides($assigninstance->id);
d685b959
IT
200 }
201
202 // Determine which override created event to fire.
203 $params['objectid'] = $fromform->id;
204 if (!$groupmode) {
205 $params['relateduserid'] = $fromform->userid;
206 $event = \mod_assign\event\user_override_created::create($params);
207 } else {
208 $params['other']['groupid'] = $fromform->groupid;
209 $event = \mod_assign\event\group_override_created::create($params);
210 }
211
212 // Trigger the override created event.
213 $event->trigger();
214 }
215
216 assign_update_events($assign, $fromform);
217
218 if (!empty($fromform->submitbutton)) {
219 redirect($overridelisturl);
220 }
221
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);
226 redirect($url);
227
228}
229
230// Print the form.
d685b959
IT
231$PAGE->navbar->add($pagetitle);
232$PAGE->set_pagelayout('admin');
233$PAGE->set_title($pagetitle);
234$PAGE->set_heading($course->fullname);
235echo $OUTPUT->header();
f3ec5411 236echo $OUTPUT->heading(format_string($assigninstance->name, true, array('context' => $context)));
d685b959
IT
237
238$mform->display();
239
240echo $OUTPUT->footer();