1 |
efrain |
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 |
throw new \moodle_exception('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 |
throw new \moodle_exception('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 |
throw new \moodle_exception('invalidoverrideid', 'assign');
|
|
|
87 |
}
|
|
|
88 |
} else {
|
|
|
89 |
if (!groups_user_groups_visible($course, $override->userid, $cm)) {
|
|
|
90 |
throw new \moodle_exception('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', 'timelimit');
|
|
|
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 (!isset($fromform->{$key}) || $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 |
$cachekey = $groupmode ? "{$fromform->assignid}_g_{$fromform->groupid}" : "{$fromform->assignid}_u_{$fromform->userid}";
|
|
|
191 |
cache::make('mod_assign', 'overrides')->delete($cachekey);
|
|
|
192 |
|
|
|
193 |
// Determine which override updated event to fire.
|
|
|
194 |
$params['objectid'] = $override->id;
|
|
|
195 |
if (!$groupmode) {
|
|
|
196 |
$params['relateduserid'] = $fromform->userid;
|
|
|
197 |
$event = \mod_assign\event\user_override_updated::create($params);
|
|
|
198 |
} else {
|
|
|
199 |
$params['other']['groupid'] = $fromform->groupid;
|
|
|
200 |
$event = \mod_assign\event\group_override_updated::create($params);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
// Trigger the override updated event.
|
|
|
204 |
$event->trigger();
|
|
|
205 |
} else {
|
|
|
206 |
unset($fromform->id);
|
|
|
207 |
$fromform->id = $DB->insert_record('assign_overrides', $fromform);
|
|
|
208 |
if ($groupmode) {
|
|
|
209 |
$fromform->sortorder = 1;
|
|
|
210 |
|
|
|
211 |
$overridecountgroup = $DB->count_records('assign_overrides',
|
|
|
212 |
array('userid' => null, 'assignid' => $assigninstance->id));
|
|
|
213 |
$overridecountall = $DB->count_records('assign_overrides', array('assignid' => $assigninstance->id));
|
|
|
214 |
if ((!$overridecountgroup) && ($overridecountall)) { // No group overrides and there are user overrides.
|
|
|
215 |
$fromform->sortorder = 1;
|
|
|
216 |
} else {
|
|
|
217 |
$fromform->sortorder = $overridecountgroup;
|
|
|
218 |
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$DB->update_record('assign_overrides', $fromform);
|
|
|
222 |
reorder_group_overrides($assigninstance->id);
|
|
|
223 |
}
|
|
|
224 |
$cachekey = $groupmode ? "{$fromform->assignid}_g_{$fromform->groupid}" : "{$fromform->assignid}_u_{$fromform->userid}";
|
|
|
225 |
cache::make('mod_assign', 'overrides')->delete($cachekey);
|
|
|
226 |
|
|
|
227 |
// Determine which override created event to fire.
|
|
|
228 |
$params['objectid'] = $fromform->id;
|
|
|
229 |
if (!$groupmode) {
|
|
|
230 |
$params['relateduserid'] = $fromform->userid;
|
|
|
231 |
$event = \mod_assign\event\user_override_created::create($params);
|
|
|
232 |
} else {
|
|
|
233 |
$params['other']['groupid'] = $fromform->groupid;
|
|
|
234 |
$event = \mod_assign\event\group_override_created::create($params);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
// Trigger the override created event.
|
|
|
238 |
$event->trigger();
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
assign_update_events($assign, $fromform);
|
|
|
242 |
|
|
|
243 |
if (!empty($fromform->submitbutton)) {
|
|
|
244 |
redirect($overridelisturl);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// The user pressed the 'again' button, so redirect back to this page.
|
|
|
248 |
$url->remove_params('cmid');
|
|
|
249 |
$url->param('action', 'duplicate');
|
|
|
250 |
$url->param('id', $fromform->id);
|
|
|
251 |
redirect($url);
|
|
|
252 |
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
// Print the form.
|
|
|
256 |
$PAGE->navbar->add($pagetitle);
|
|
|
257 |
$PAGE->set_pagelayout('admin');
|
|
|
258 |
$PAGE->add_body_class('limitedwidth');
|
|
|
259 |
$PAGE->set_title($pagetitle);
|
|
|
260 |
$PAGE->set_heading($course->fullname);
|
|
|
261 |
$PAGE->set_secondary_active_tab('mod_assign_useroverrides');
|
|
|
262 |
$activityheader = $PAGE->activityheader;
|
|
|
263 |
$activityheader->set_attrs([
|
|
|
264 |
'description' => '',
|
|
|
265 |
'hidecompletion' => true,
|
|
|
266 |
'title' => $activityheader->is_title_allowed() ? format_string($assigninstance->name, true, ['context' => $context]) : ""
|
|
|
267 |
]);
|
|
|
268 |
echo $OUTPUT->header();
|
|
|
269 |
|
|
|
270 |
$mform->display();
|
|
|
271 |
|
|
|
272 |
echo $OUTPUT->footer();
|