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