forked from azanov/branchedquiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
208 lines (174 loc) · 7.73 KB
/
edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
// 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/>.
/**
* @package mod_branchedquiz
* @copyright 2017 onwards Dominik Wittenberg, Paul Youssef, Pavel Azanov, Alessandro Noli, Robin Voigt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/mod/branchedquiz/locallib.php');
require_once($CFG->dirroot . '/mod/branchedquiz/classes/output/bqedit_renderer.php');
require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_once($CFG->dirroot . '/question/category_class.php');
// These params are only passed from page request to request while we stay on
// this page otherwise they would go in question_edit_setup.
$scrollpos = optional_param('scrollpos', '', PARAM_INT);
list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) = question_edit_setup('editq', '/mod/branchedquiz/edit.php', true);
$defaultcategoryobj = question_make_default_categories($contexts->all());
$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
$quizhasattempts = quiz_has_attempts($quiz->id);
$PAGE->set_url($thispageurl);
// Get the course object and related bits.
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
$quizobj = new quiz($quiz, $cm, $course);
$structure = $quizobj->get_structure();
// You need mod/quiz:manage in addition to question capabilities to access this page.
require_capability('mod/quiz:manage', $contexts->lowest());
// 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();
// Process commands ============================================================.
// Get the list of question ids had their check-boxes ticked.
$selectedslots = array();
$params = (array) data_submitted();
foreach ($params as $key => $value) {
if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
$selectedslots[] = $matches[1];
}
}
$afteractionurl = new moodle_url($thispageurl);
if ($scrollpos) {
$afteractionurl->param('scrollpos', $scrollpos);
}
if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
// Re-paginate the quiz.
$structure->check_can_be_edited();
$questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
quiz_repaginate_questions($quiz->id, $questionsperpage );
branchedquiz_delete_previews($quiz);
redirect($afteractionurl);
}
if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
// Add a single question to the current quiz.
$structure->check_can_be_edited();
quiz_require_question_use($addquestion);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
branchedquiz_add_quiz_question($addquestion, $quiz, $addonpage);
quiz_repaginate_questions($quiz->id, 1);
branchedquiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
$thispageurl->param('lastchanged', $addquestion);
redirect($afteractionurl);
}
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
$structure->check_can_be_edited();
$addonpage = optional_param('addonpage', 0, PARAM_INT);
// Add selected questions to the current quiz.
$rawdata = (array) data_submitted();
$y = 30;
foreach ($rawdata as $key => $value) { // Parse input for question ids.
if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
$key = $matches[1];
quiz_require_question_use($key);
branchedquiz_add_quiz_question($key, $quiz, $addonpage);
}
}
quiz_repaginate_questions($quiz->id, 1);
branchedquiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
redirect($afteractionurl);
}
if ($addsectionatpage = optional_param('addsectionatpage', false, PARAM_INT)) {
// Add a section to the quiz.
$structure->check_can_be_edited();
$structure->add_section_heading($addsectionatpage);
branchedquiz_delete_previews($quiz);
redirect($afteractionurl);
}
if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
// Add random questions to the quiz.
$structure->check_can_be_edited();
$recurse = optional_param('recurse', 0, PARAM_BOOL);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
$randomcount = required_param('randomcount', PARAM_INT);
quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
branchedquiz_delete_previews($quiz);
quiz_update_sumgrades($quiz);
redirect($afteractionurl);
}
if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
// If rescaling is required save the new maximum.
$maxgrade = unformat_float(optional_param('maxgrade', -1, PARAM_RAW));
if ($maxgrade >= 0) {
branchedquiz_set_grade($maxgrade, $quiz);
quiz_update_all_final_grades($quiz);
branchedquiz_update_grades($quiz, 0, true);
}
redirect($afteractionurl);
}
// Get the question bank view.
$questionbank = new mod_branchedquiz\question\bank\custom_view($contexts, $thispageurl, $course, $cm, $quiz);
$questionbank->set_quiz_has_attempts($quizhasattempts);
$questionbank->process_actions($thispageurl, $cm);
// End of process commands =====================================================.
$PAGE->set_pagelayout('incourse');
$PAGE->set_pagetype('mod-quiz-edit');
$output = $PAGE->get_renderer('mod_branchedquiz', 'bqedit');
$PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
$PAGE->set_heading($course->fullname);
$node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
if ($node) {
$node->make_active();
}
echo $OUTPUT->header();
// Initialise the JavaScript.
$quizeditconfig = new stdClass();
$quizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
$quizeditconfig->dialoglisteners = array();
$numberoflisteners = $DB->get_field_sql("
SELECT COALESCE(MAX(page), 1)
FROM {quiz_slots}
WHERE quizid = ?", array($quiz->id));
for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
$quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
}
$PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
$PAGE->requires->js('/question/qengine.js');
// Questions wrapper start.
echo html_writer::start_tag('div', array('class' => 'mod-quiz-edit-content mod-branchedquiz-edit-content'));
echo $output->edit_page($quizobj, $structure, $contexts, $thispageurl, $pagevars);
echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>';
$edges = branchedquiz_get_edges($quiz);
echo '<script>';
echo 'var branchedquiz_edges = ';
echo json_encode($edges);
echo ';';
echo '</script>';
$PAGE->requires->js('/mod/branchedquiz/lib/jsplumb.js');
$PAGE->requires->js('/mod/branchedquiz/edit.js');
// Questions wrapper end.
echo html_writer::end_tag('div');
echo $OUTPUT->footer();