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