MDL-37961 webservice: PARAM_BOOL with PARAM_DEFAULT accepts boolean default
[moodle.git] / mod / quiz / overrideedit.php
blobb298780312e18ed04866e96e0e51b2afc817119d
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 quiz overrides
20 * @package mod
21 * @subpackage quiz
22 * @copyright 2010 Matt Petro
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(__FILE__) . '/../../config.php');
28 require_once($CFG->dirroot.'/mod/quiz/lib.php');
29 require_once($CFG->dirroot.'/mod/quiz/locallib.php');
30 require_once($CFG->dirroot.'/mod/quiz/override_form.php');
33 $cmid = optional_param('cmid', 0, PARAM_INT);
34 $overrideid = optional_param('id', 0, PARAM_INT);
35 $action = optional_param('action', null, PARAM_ALPHA);
36 $reset = optional_param('reset', false, PARAM_BOOL);
38 $override = null;
39 if ($overrideid) {
41 if (! $override = $DB->get_record('quiz_overrides', array('id' => $overrideid))) {
42 print_error('invalidoverrideid', 'quiz');
44 if (! $quiz = $DB->get_record('quiz', array('id' => $override->quiz))) {
45 print_error('invalidcoursemodule');
47 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $quiz->course)) {
48 print_error('invalidcoursemodule');
50 } else if ($cmid) {
52 if (! $cm = get_coursemodule_from_id('quiz', $cmid)) {
53 print_error('invalidcoursemodule');
55 if (! $quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
56 print_error('invalidcoursemodule');
58 } else {
59 print_error('invalidcoursemodule');
61 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
63 $url = new moodle_url('/mod/quiz/overrideedit.php');
64 if ($action) {
65 $url->param('action', $action);
67 if ($overrideid) {
68 $url->param('id', $overrideid);
69 } else {
70 $url->param('cmid', $cmid);
73 $PAGE->set_url($url);
75 require_login($course, false, $cm);
77 $context = context_module::instance($cm->id);
79 // Add or edit an override.
80 require_capability('mod/quiz:manageoverrides', $context);
82 if ($overrideid) {
83 // Editing an override.
84 $data = clone $override;
85 } else {
86 // Creating a new override.
87 $data = new stdClass();
90 // Merge quiz defaults with data.
91 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
92 foreach ($keys as $key) {
93 if (!isset($data->{$key}) || $reset) {
94 $data->{$key} = $quiz->{$key};
98 // If we are duplicating an override, then clear the user/group and override id
99 // since they will change.
100 if ($action === 'duplicate') {
101 $override->id = null;
102 $override->userid = null;
103 $override->groupid = null;
106 // True if group-based override.
107 $groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
109 $overridelisturl = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
110 if (!$groupmode) {
111 $overridelisturl->param('mode', 'user');
114 // Setup the form.
115 $mform = new quiz_override_form($url, $cm, $quiz, $context, $groupmode, $override);
116 $mform->set_data($data);
118 if ($mform->is_cancelled()) {
119 redirect($overridelisturl);
121 } else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
122 $url->param('reset', true);
123 redirect($url);
125 } else if ($fromform = $mform->get_data()) {
126 // Process the data.
127 $fromform->quiz = $quiz->id;
129 // Replace unchanged values with null.
130 foreach ($keys as $key) {
131 if ($fromform->{$key} == $quiz->{$key}) {
132 $fromform->{$key} = null;
136 // See if we are replacing an existing override.
137 $userorgroupchanged = false;
138 if (empty($override->id)) {
139 $userorgroupchanged = true;
140 } else if (!empty($fromform->userid)) {
141 $userorgroupchanged = $fromform->userid !== $override->userid;
142 } else {
143 $userorgroupchanged = $fromform->groupid !== $override->groupid;
146 if ($userorgroupchanged) {
147 $conditions = array(
148 'quiz' => $quiz->id,
149 'userid' => empty($fromform->userid)? null : $fromform->userid,
150 'groupid' => empty($fromform->groupid)? null : $fromform->groupid);
151 if ($oldoverride = $DB->get_record('quiz_overrides', $conditions)) {
152 // There is an old override, so we merge any new settings on top of
153 // the older override.
154 foreach ($keys as $key) {
155 if (is_null($fromform->{$key})) {
156 $fromform->{$key} = $oldoverride->{$key};
159 // Delete the old override.
160 $DB->delete_records('quiz_overrides', array('id' => $oldoverride->id));
164 if (!empty($override->id)) {
165 $fromform->id = $override->id;
166 $DB->update_record('quiz_overrides', $fromform);
167 } else {
168 unset($fromform->id);
169 $fromform->id = $DB->insert_record('quiz_overrides', $fromform);
172 quiz_update_open_attempts(array('quizid'=>$quiz->id));
173 quiz_update_events($quiz, $fromform);
175 add_to_log($cm->course, 'quiz', 'edit override',
176 "overrideedit.php?id=$fromform->id", $quiz->id, $cm->id);
178 if (!empty($fromform->submitbutton)) {
179 redirect($overridelisturl);
182 // The user pressed the 'again' button, so redirect back to this page.
183 $url->remove_params('cmid');
184 $url->param('action', 'duplicate');
185 $url->param('id', $fromform->id);
186 redirect($url);
190 // Print the form.
191 $pagetitle = get_string('editoverride', 'quiz');
192 $PAGE->navbar->add($pagetitle);
193 $PAGE->set_pagelayout('admin');
194 $PAGE->set_title($pagetitle);
195 $PAGE->set_heading($course->fullname);
196 echo $OUTPUT->header();
197 echo $OUTPUT->heading($pagetitle);
199 $mform->display();
201 echo $OUTPUT->footer();