Merge branch 'MDL-79186-401' of https://github.com/paulholden/moodle into MOODLE_401_...
[moodle.git] / mod / lesson / overrideedit.php
blobfc8ae373c6e8297cd045373de30f48dba1ba734c
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.
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/>.
17 /**
18 * This page handles editing and creation of lesson overrides
20 * @package mod_lesson
21 * @copyright 2015 Jean-Michel Vedrine
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once(__DIR__ . '/../../config.php');
27 require_once($CFG->dirroot.'/mod/lesson/lib.php');
28 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
29 require_once($CFG->dirroot.'/mod/lesson/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 $override = null;
38 if ($overrideid) {
40 if (! $override = $DB->get_record('lesson_overrides', array('id' => $overrideid))) {
41 throw new \moodle_exception('invalidoverrideid', 'lesson');
44 $lesson = new lesson($DB->get_record('lesson', array('id' => $override->lessonid), '*', MUST_EXIST));
46 list($course, $cm) = get_course_and_cm_from_instance($lesson, 'lesson');
48 } else if ($cmid) {
49 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'lesson');
50 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
52 } else {
53 throw new \moodle_exception('invalidcoursemodule');
55 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
57 $url = new moodle_url('/mod/lesson/overrideedit.php');
58 if ($action) {
59 $url->param('action', $action);
61 if ($overrideid) {
62 $url->param('id', $overrideid);
63 } else {
64 $url->param('cmid', $cmid);
67 $PAGE->set_url($url);
69 require_login($course, false, $cm);
71 $context = context_module::instance($cm->id);
73 // Add or edit an override.
74 require_capability('mod/lesson:manageoverrides', $context);
76 if ($overrideid) {
77 // Editing an override.
78 $data = clone $override;
80 if ($override->groupid) {
81 if (!groups_group_visible($override->groupid, $course, $cm)) {
82 throw new \moodle_exception('invalidoverrideid', 'lesson');
84 } else {
85 if (!groups_user_groups_visible($course, $override->userid, $cm)) {
86 throw new \moodle_exception('invalidoverrideid', 'lesson');
89 } else {
90 // Creating a new override.
91 $data = new stdClass();
94 // Merge lesson defaults with data.
95 $keys = array('available', 'deadline', 'review', 'timelimit', 'maxattempts', 'retake', 'password');
96 foreach ($keys as $key) {
97 if (!isset($data->{$key}) || $reset) {
98 $data->{$key} = $lesson->{$key};
102 // True if group-based override.
103 $groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
105 // If we are duplicating an override, then clear the user/group and override id
106 // since they will change.
107 if ($action === 'duplicate') {
108 $override->id = $data->id = null;
109 $override->userid = $data->userid = null;
110 $override->groupid = $data->groupid = null;
113 $overridelisturl = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id));
114 if (!$groupmode) {
115 $overridelisturl->param('mode', 'user');
118 // Setup the form.
119 $mform = new lesson_override_form($url, $cm, $lesson, $context, $groupmode, $override);
120 $mform->set_data($data);
122 if ($mform->is_cancelled()) {
123 redirect($overridelisturl);
125 } else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
126 $url->param('reset', true);
127 redirect($url);
129 } else if ($fromform = $mform->get_data()) {
130 // Process the data.
131 $fromform->lessonid = $lesson->id;
133 // Replace unchanged values with null.
134 foreach ($keys as $key) {
135 if ($fromform->{$key} == $lesson->{$key}) {
136 $fromform->{$key} = null;
140 // See if we are replacing an existing override.
141 $userorgroupchanged = false;
142 if (empty($override->id)) {
143 $userorgroupchanged = true;
144 } else if (!empty($fromform->userid)) {
145 $userorgroupchanged = $fromform->userid !== $override->userid;
146 } else {
147 $userorgroupchanged = $fromform->groupid !== $override->groupid;
150 if ($userorgroupchanged) {
151 $conditions = array(
152 'lessonid' => $lesson->id,
153 'userid' => empty($fromform->userid) ? null : $fromform->userid,
154 'groupid' => empty($fromform->groupid) ? null : $fromform->groupid);
155 if ($oldoverride = $DB->get_record('lesson_overrides', $conditions)) {
156 // There is an old override, so we merge any new settings on top of
157 // the older override.
158 foreach ($keys as $key) {
159 if (is_null($fromform->{$key})) {
160 $fromform->{$key} = $oldoverride->{$key};
164 $lesson->delete_override($oldoverride->id);
168 // Set the common parameters for one of the events we may be triggering.
169 $params = array(
170 'context' => $context,
171 'other' => array(
172 'lessonid' => $lesson->id
175 if (!empty($override->id)) {
176 $fromform->id = $override->id;
177 $DB->update_record('lesson_overrides', $fromform);
178 $cachekey = $groupmode ? "{$fromform->lessonid}_g_{$fromform->groupid}" : "{$fromform->lessonid}_u_{$fromform->userid}";
179 cache::make('mod_lesson', 'overrides')->delete($cachekey);
181 // Determine which override updated event to fire.
182 $params['objectid'] = $override->id;
183 if (!$groupmode) {
184 $params['relateduserid'] = $fromform->userid;
185 $event = \mod_lesson\event\user_override_updated::create($params);
186 } else {
187 $params['other']['groupid'] = $fromform->groupid;
188 $event = \mod_lesson\event\group_override_updated::create($params);
191 // Trigger the override updated event.
192 $event->trigger();
193 } else {
194 unset($fromform->id);
195 $fromform->id = $DB->insert_record('lesson_overrides', $fromform);
196 $cachekey = $groupmode ? "{$fromform->lessonid}_g_{$fromform->groupid}" : "{$fromform->lessonid}_u_{$fromform->userid}";
197 cache::make('mod_lesson', 'overrides')->delete($cachekey);
199 // Determine which override created event to fire.
200 $params['objectid'] = $fromform->id;
201 if (!$groupmode) {
202 $params['relateduserid'] = $fromform->userid;
203 $event = \mod_lesson\event\user_override_created::create($params);
204 } else {
205 $params['other']['groupid'] = $fromform->groupid;
206 $event = \mod_lesson\event\group_override_created::create($params);
209 // Trigger the override created event.
210 $event->trigger();
213 if ($groupmode) {
214 // Priorities may have shifted, so we need to update all of the calendar events for group overrides.
215 lesson_update_events($lesson);
216 } else {
217 // User override. We only need to update the calendar event for this user override.
218 lesson_update_events($lesson, $fromform);
222 if (!empty($fromform->submitbutton)) {
223 redirect($overridelisturl);
226 // The user pressed the 'again' button, so redirect back to this page.
227 $url->remove_params('cmid');
228 $url->param('action', 'duplicate');
229 $url->param('id', $fromform->id);
230 redirect($url);
234 // Print the form.
235 $pagetitle = get_string('editoverride', 'lesson');
236 $PAGE->navbar->add($pagetitle);
237 $PAGE->set_pagelayout('admin');
238 $PAGE->set_secondary_active_tab('mod_lesson_useroverrides');
239 $PAGE->add_body_class('limitedwidth');
240 $PAGE->set_title($pagetitle);
241 $PAGE->set_heading($course->fullname);
242 echo $OUTPUT->header();
243 echo $OUTPUT->heading(format_string($lesson->name, true, array('context' => $context)));
245 $mform->display();
247 echo $OUTPUT->footer();