Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
75
    if ($override->groupid) {
76
        if (!groups_group_visible($override->groupid, $course, $cm)) {
77
            throw new \moodle_exception('invalidoverrideid', 'quiz');
78
        }
79
    } else {
80
        if (!groups_user_groups_visible($course, $override->userid, $cm)) {
81
            throw new \moodle_exception('invalidoverrideid', 'quiz');
82
        }
83
    }
84
} else {
85
    // Creating a new override.
86
    $data = new stdClass();
87
}
88
 
89
// Merge quiz defaults with data.
90
$keys = ['timeopen', 'timeclose', 'timelimit', 'attempts', 'password'];
91
foreach ($keys as $key) {
92
    if (!isset($data->{$key}) || $reset) {
93
        $data->{$key} = $quiz->{$key};
94
    }
95
}
96
 
97
// If we are duplicating an override, then clear the user/group and override id
98
// since they will change.
99
if ($action === 'duplicate') {
100
    $override->id = null;
101
    $override->userid = null;
102
    $override->groupid = null;
103
}
104
 
105
// True if group-based override.
106
$groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
107
 
108
$overridelisturl = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]);
109
if (!$groupmode) {
110
    $overridelisturl->param('mode', 'user');
111
}
112
 
113
// Setup the form.
114
$mform = new edit_override_form($url, $cm, $quiz, $context, $groupmode, $override);
115
$mform->set_data($data);
116
 
117
if ($mform->is_cancelled()) {
118
    redirect($overridelisturl);
119
 
120
} else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
121
    $url->param('reset', true);
122
    redirect($url);
123
 
124
} else if ($fromform = $mform->get_data()) {
125
    // Only include id when editing (i.e. action is empty).
126
    if (empty($action) && !empty($overrideid)) {
127
        $fromform->id = $overrideid;
128
    }
129
 
130
    // Process the data.
131
    $id = $manager->save_override((array) $fromform);
132
 
133
    if (!empty($fromform->submitbutton)) {
134
        redirect($overridelisturl);
135
    }
136
 
137
    // The user pressed the 'again' button, so redirect back to this page.
138
    $url->remove_params('cmid');
139
    $url->param('action', 'duplicate');
140
    $url->param('id', $id);
141
    redirect($url);
142
 
143
}
144
 
145
// Print the form.
146
$pagetitle = get_string('editoverride', 'quiz');
147
$PAGE->navbar->add($pagetitle);
148
$PAGE->set_pagelayout('admin');
149
$PAGE->add_body_class('limitedwidth');
150
$PAGE->set_title($pagetitle);
151
$PAGE->set_heading($course->fullname);
152
$PAGE->activityheader->set_attrs([
153
    "title" => format_string($quiz->name, true, ['context' => $context]),
154
    "description" => "",
155
    "hidecompletion" => true
156
]);
157
echo $OUTPUT->header();
158
 
159
$mform->display();
160
 
161
echo $OUTPUT->footer();