edit.php 7.83 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

17

18
/**
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 * Page to edit quizzes
 *
 * This page generally has two columns:
 * The right column lists all available questions in a chosen category and
 * allows them to be edited or more to be added. This column is only there if
 * the quiz does not already have student attempts
 * The left column lists all questions that have been added to the current quiz.
 * The lecturer can add questions from the right hand list to the quiz or remove them
 *
 * The script also processes a number of actions:
 * Actions affecting a quiz:
 * up and down  Changes the order of questions and page breaks
 * addquestion  Adds a single question to the quiz
 * add          Adds several selected questions to the quiz
 * addrandom    Adds a certain number of random questions to the quiz
 * repaginate   Re-paginates the quiz
 * delete       Removes a question from the quiz
 * savechanges  Saves the order and grades for questions in the quiz
 *
38
 * @package    mod_quiz
39
40
 * @copyright  1999 onwards Martin Dougiamas and others {@link http://moodle.com}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
42
 */

43

44
45
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
46
require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
47
require_once($CFG->dirroot . '/question/editlib.php');
48

49
50
// These params are only passed from page request to request while we stay on
// this page otherwise they would go in question_edit_setup.
51
$scrollpos = optional_param('scrollpos', '', PARAM_INT);
52

53
list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
54
        question_edit_setup('editq', '/mod/quiz/edit.php', true);
55
56

$defaultcategoryobj = question_make_default_categories($contexts->all());
57
$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
58

59
$quizhasattempts = quiz_has_attempts($quiz->id);
60

61
$PAGE->set_url($thispageurl);
62
63

// Get the course object and related bits.
64
65
66
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
$quizobj = new quiz($quiz, $cm, $course);
$structure = $quizobj->get_structure();
67

68
// You need mod/quiz:manage in addition to question capabilities to access this page.
69
require_capability('mod/quiz:manage', $contexts->lowest());
70

71
72
73
74
75
76
77
78
79
80
81
// Log this visit.
$params = array(
    'courseid' => $course->id,
    'context' => $contexts->lowest(),
    'other' => array(
        'quizid' => $quiz->id
    )
);
$event = \mod_quiz\event\edit_page_viewed::create($params);
$event->trigger();

82
// Process commands ============================================================.
83
84

// Get the list of question ids had their check-boxes ticked.
85
$selectedslots = array();
86
87
88
$params = (array) data_submitted();
foreach ($params as $key => $value) {
    if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
89
        $selectedslots[] = $matches[1];
90
    }
91
92
}

93
94
95
96
$afteractionurl = new moodle_url($thispageurl);
if ($scrollpos) {
    $afteractionurl->param('scrollpos', $scrollpos);
}
97
98

if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
99
    // Re-paginate the quiz.
100
    $structure->check_can_be_edited();
101
    $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
102
    quiz_repaginate_questions($quiz->id, $questionsperpage );
103
    quiz_delete_previews($quiz);
104
    redirect($afteractionurl);
105
}
106
107

if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
108
    // Add a single question to the current quiz.
109
    $structure->check_can_be_edited();
110
    quiz_require_question_use($addquestion);
111
    $addonpage = optional_param('addonpage', 0, PARAM_INT);
112
    quiz_add_quiz_question($addquestion, $quiz, $addonpage);
113
    quiz_delete_previews($quiz);
114
    quiz_update_sumgrades($quiz);
115
    $thispageurl->param('lastchanged', $addquestion);
116
    redirect($afteractionurl);
117
118
}

119
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
120
    $structure->check_can_be_edited();
121
    $addonpage = optional_param('addonpage', 0, PARAM_INT);
122
    // Add selected questions to the current quiz.
123
    $rawdata = (array) data_submitted();
124
    foreach ($rawdata as $key => $value) { // Parse input for question ids.
125
126
        if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
            $key = $matches[1];
127
            quiz_require_question_use($key);
128
            quiz_add_quiz_question($key, $quiz, $addonpage);
129
130
        }
    }
131
    quiz_delete_previews($quiz);
132
    quiz_update_sumgrades($quiz);
133
    redirect($afteractionurl);
134
135
}

136
if ($addsectionatpage = optional_param('addsectionatpage', false, PARAM_INT)) {
137
    // Add a section to the quiz.
138
139
    $structure->check_can_be_edited();
    $structure->add_section_heading($addsectionatpage);
140
141
142
143
    quiz_delete_previews($quiz);
    redirect($afteractionurl);
}

144
if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
145
    // Add random questions to the quiz.
146
    $structure->check_can_be_edited();
147
    $recurse = optional_param('recurse', 0, PARAM_BOOL);
148
    $addonpage = optional_param('addonpage', 0, PARAM_INT);
149
150
151
    $categoryid = required_param('categoryid', PARAM_INT);
    $randomcount = required_param('randomcount', PARAM_INT);
    quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
152

153
    quiz_delete_previews($quiz);
154
    quiz_update_sumgrades($quiz);
155
    redirect($afteractionurl);
156
}
157
158

if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
159

160
    // If rescaling is required save the new maximum.
161
162
    $maxgrade = unformat_float(optional_param('maxgrade', '', PARAM_RAW_TRIMMED), true);
    if (is_float($maxgrade) && $maxgrade >= 0) {
163
        quiz_set_grade($maxgrade, $quiz);
164
165
166
        quiz_update_all_final_grades($quiz);
        quiz_update_grades($quiz, 0, true);
    }
167

168
    redirect($afteractionurl);
169
}
170

171
// Get the question bank view.
172
$questionbank = new mod_quiz\question\bank\custom_view($contexts, $thispageurl, $course, $cm, $quiz);
173
$questionbank->set_quiz_has_attempts($quizhasattempts);
174

175
176
177
178
179
180
// End of process commands =====================================================.

$PAGE->set_pagelayout('incourse');
$PAGE->set_pagetype('mod-quiz-edit');

$output = $PAGE->get_renderer('mod_quiz', 'edit');
181

182
$PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
183
$PAGE->set_heading($course->fullname);
184
185
186
187
$node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
if ($node) {
    $node->make_active();
}
188
echo $OUTPUT->header();
189

190
// Initialise the JavaScript.
191
$quizeditconfig = new stdClass();
192
$quizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
193
$quizeditconfig->dialoglisteners = array();
194
195
196
197
198
$numberoflisteners = $DB->get_field_sql("
    SELECT COALESCE(MAX(page), 1)
      FROM {quiz_slots}
     WHERE quizid = ?", array($quiz->id));

199
200
201
for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
    $quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
}
202

203
$PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
204
$PAGE->requires->js('/question/qengine.js');
205

206
207
// Questions wrapper start.
echo html_writer::start_tag('div', array('class' => 'mod-quiz-edit-content'));
208

209
echo $output->edit_page($quizobj, $structure, $contexts, $thispageurl, $pagevars);
210

211
212
// Questions wrapper end.
echo html_writer::end_tag('div');
213

214
echo $OUTPUT->footer();