2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This page handles editing and creation of lesson overrides
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
);
40 if (! $override = $DB->get_record('lesson_overrides', array('id' => $overrideid))) {
41 print_error('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');
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
));
53 print_error('invalidcoursemodule');
55 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
57 $url = new moodle_url('/mod/lesson/overrideedit.php');
59 $url->param('action', $action);
62 $url->param('id', $overrideid);
64 $url->param('cmid', $cmid);
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);
77 // Editing an override.
78 $data = clone $override;
80 // Creating a new override.
81 $data = new stdClass();
84 // Merge lesson defaults with data.
85 $keys = array('available', 'deadline', 'review', 'timelimit', 'maxattempts', 'retake', 'password');
86 foreach ($keys as $key) {
87 if (!isset($data->{$key}) ||
$reset) {
88 $data->{$key} = $lesson->{$key};
92 // True if group-based override.
93 $groupmode = !empty($data->groupid
) ||
($action === 'addgroup' && empty($overrideid));
95 // If we are duplicating an override, then clear the user/group and override id
96 // since they will change.
97 if ($action === 'duplicate') {
98 $override->id
= $data->id
= null;
99 $override->userid
= $data->userid
= null;
100 $override->groupid
= $data->groupid
= null;
103 $overridelisturl = new moodle_url('/mod/lesson/overrides.php', array('cmid' => $cm->id
));
105 $overridelisturl->param('mode', 'user');
109 $mform = new lesson_override_form($url, $cm, $lesson, $context, $groupmode, $override);
110 $mform->set_data($data);
112 if ($mform->is_cancelled()) {
113 redirect($overridelisturl);
115 } else if (optional_param('resetbutton', 0, PARAM_ALPHA
)) {
116 $url->param('reset', true);
119 } else if ($fromform = $mform->get_data()) {
121 $fromform->lessonid
= $lesson->id
;
123 // Replace unchanged values with null.
124 foreach ($keys as $key) {
125 if ($fromform->{$key} == $lesson->{$key}) {
126 $fromform->{$key} = null;
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
;
137 $userorgroupchanged = $fromform->groupid
!== $override->groupid
;
140 if ($userorgroupchanged) {
142 'lessonid' => $lesson->id
,
143 'userid' => empty($fromform->userid
) ?
null : $fromform->userid
,
144 'groupid' => empty($fromform->groupid
) ?
null : $fromform->groupid
);
145 if ($oldoverride = $DB->get_record('lesson_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};
154 $lesson->delete_override($oldoverride->id
);
158 // Set the common parameters for one of the events we may be triggering.
160 'context' => $context,
162 'lessonid' => $lesson->id
165 if (!empty($override->id
)) {
166 $fromform->id
= $override->id
;
167 $DB->update_record('lesson_overrides', $fromform);
169 // Determine which override updated event to fire.
170 $params['objectid'] = $override->id
;
172 $params['relateduserid'] = $fromform->userid
;
173 $event = \mod_lesson\event\user_override_updated
::create($params);
175 $params['other']['groupid'] = $fromform->groupid
;
176 $event = \mod_lesson\event\group_override_updated
::create($params);
179 // Trigger the override updated event.
182 unset($fromform->id
);
183 $fromform->id
= $DB->insert_record('lesson_overrides', $fromform);
185 // Determine which override created event to fire.
186 $params['objectid'] = $fromform->id
;
188 $params['relateduserid'] = $fromform->userid
;
189 $event = \mod_lesson\event\user_override_created
::create($params);
191 $params['other']['groupid'] = $fromform->groupid
;
192 $event = \mod_lesson\event\group_override_created
::create($params);
195 // Trigger the override created event.
200 // Priorities may have shifted, so we need to update all of the calendar events for group overrides.
201 lesson_update_events($lesson);
203 // User override. We only need to update the calendar event for this user override.
204 lesson_update_events($lesson, $fromform);
208 if (!empty($fromform->submitbutton
)) {
209 redirect($overridelisturl);
212 // The user pressed the 'again' button, so redirect back to this page.
213 $url->remove_params('cmid');
214 $url->param('action', 'duplicate');
215 $url->param('id', $fromform->id
);
221 $pagetitle = get_string('editoverride', 'lesson');
222 $PAGE->navbar
->add($pagetitle);
223 $PAGE->set_pagelayout('admin');
224 $PAGE->set_title($pagetitle);
225 $PAGE->set_heading($course->fullname
);
226 echo $OUTPUT->header();
227 echo $OUTPUT->heading(format_string($lesson->name
, true, array('context' => $context)));
231 echo $OUTPUT->footer();