1 |
efrain |
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 quiz overrides
|
|
|
19 |
*
|
|
|
20 |
* @package mod_quiz
|
|
|
21 |
* @copyright 2010 Matt Petro
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use mod_quiz\form\edit_override_form;
|
|
|
26 |
use mod_quiz\quiz_settings;
|
|
|
27 |
|
|
|
28 |
require_once(__DIR__ . '/../../config.php');
|
|
|
29 |
require_once($CFG->dirroot.'/mod/quiz/lib.php');
|
|
|
30 |
require_once($CFG->dirroot.'/mod/quiz/locallib.php');
|
|
|
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;
|
|
|
38 |
if ($overrideid) {
|
|
|
39 |
$override = $DB->get_record('quiz_overrides', ['id' => $overrideid], '*', MUST_EXIST);
|
|
|
40 |
$quizobj = quiz_settings::create($override->quiz);
|
|
|
41 |
} else {
|
|
|
42 |
$quizobj = quiz_settings::create_for_cmid($cmid);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
$quiz = $quizobj->get_quiz();
|
|
|
46 |
$cm = $quizobj->get_cm();
|
|
|
47 |
$course = $quizobj->get_course();
|
|
|
48 |
$context = $quizobj->get_context();
|
|
|
49 |
$manager = $quizobj->get_override_manager();
|
|
|
50 |
|
|
|
51 |
$url = new moodle_url('/mod/quiz/overrideedit.php');
|
|
|
52 |
if ($action) {
|
|
|
53 |
$url->param('action', $action);
|
|
|
54 |
}
|
|
|
55 |
if ($overrideid) {
|
|
|
56 |
$url->param('id', $overrideid);
|
|
|
57 |
} else {
|
|
|
58 |
$url->param('cmid', $cmid);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$PAGE->set_url($url);
|
|
|
62 |
|
|
|
63 |
// Activate the secondary nav tab.
|
|
|
64 |
$PAGE->set_secondary_active_tab("mod_quiz_useroverrides");
|
|
|
65 |
|
|
|
66 |
require_login($course, false, $cm);
|
|
|
67 |
|
|
|
68 |
// Add or edit an override.
|
|
|
69 |
$manager->require_manage_capability();
|
|
|
70 |
|
|
|
71 |
if ($overrideid) {
|
|
|
72 |
// Editing an override.
|
|
|
73 |
$data = clone $override;
|
|
|
74 |
|
11 |
efrain |
75 |
if (!$manager->can_view_override($override, $course, $cm)) {
|
|
|
76 |
throw new \moodle_exception('invalidoverrideid', 'quiz');
|
1 |
efrain |
77 |
}
|
|
|
78 |
} else {
|
|
|
79 |
// Creating a new override.
|
|
|
80 |
$data = new stdClass();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Merge quiz defaults with data.
|
|
|
84 |
$keys = ['timeopen', 'timeclose', 'timelimit', 'attempts', 'password'];
|
|
|
85 |
foreach ($keys as $key) {
|
|
|
86 |
if (!isset($data->{$key}) || $reset) {
|
|
|
87 |
$data->{$key} = $quiz->{$key};
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// If we are duplicating an override, then clear the user/group and override id
|
|
|
92 |
// since they will change.
|
|
|
93 |
if ($action === 'duplicate') {
|
|
|
94 |
$override->id = null;
|
|
|
95 |
$override->userid = null;
|
|
|
96 |
$override->groupid = null;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// True if group-based override.
|
|
|
100 |
$groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
|
|
|
101 |
|
|
|
102 |
$overridelisturl = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]);
|
|
|
103 |
if (!$groupmode) {
|
|
|
104 |
$overridelisturl->param('mode', 'user');
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Setup the form.
|
|
|
108 |
$mform = new edit_override_form($url, $cm, $quiz, $context, $groupmode, $override);
|
|
|
109 |
$mform->set_data($data);
|
|
|
110 |
|
|
|
111 |
if ($mform->is_cancelled()) {
|
|
|
112 |
redirect($overridelisturl);
|
|
|
113 |
|
|
|
114 |
} else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
|
|
|
115 |
$url->param('reset', true);
|
|
|
116 |
redirect($url);
|
|
|
117 |
|
|
|
118 |
} else if ($fromform = $mform->get_data()) {
|
|
|
119 |
// Only include id when editing (i.e. action is empty).
|
|
|
120 |
if (empty($action) && !empty($overrideid)) {
|
|
|
121 |
$fromform->id = $overrideid;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// Process the data.
|
|
|
125 |
$id = $manager->save_override((array) $fromform);
|
|
|
126 |
|
|
|
127 |
if (!empty($fromform->submitbutton)) {
|
|
|
128 |
redirect($overridelisturl);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// The user pressed the 'again' button, so redirect back to this page.
|
|
|
132 |
$url->remove_params('cmid');
|
|
|
133 |
$url->param('action', 'duplicate');
|
|
|
134 |
$url->param('id', $id);
|
|
|
135 |
redirect($url);
|
|
|
136 |
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// Print the form.
|
|
|
140 |
$pagetitle = get_string('editoverride', 'quiz');
|
|
|
141 |
$PAGE->navbar->add($pagetitle);
|
|
|
142 |
$PAGE->set_pagelayout('admin');
|
|
|
143 |
$PAGE->add_body_class('limitedwidth');
|
|
|
144 |
$PAGE->set_title($pagetitle);
|
|
|
145 |
$PAGE->set_heading($course->fullname);
|
|
|
146 |
$PAGE->activityheader->set_attrs([
|
|
|
147 |
"title" => format_string($quiz->name, true, ['context' => $context]),
|
|
|
148 |
"description" => "",
|
|
|
149 |
"hidecompletion" => true
|
|
|
150 |
]);
|
|
|
151 |
echo $OUTPUT->header();
|
|
|
152 |
|
|
|
153 |
$mform->display();
|
|
|
154 |
|
|
|
155 |
echo $OUTPUT->footer();
|