forked from jhoopes/moodle-mod_activequiz
-
Notifications
You must be signed in to change notification settings - Fork 6
/
edit.php
executable file
·213 lines (189 loc) · 6.47 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
209
210
211
212
213
<?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/>.
/**
* Edit page for editing questions on the quiz
*
* @package mod_jazzquiz
* @author Sebastian S. Gundersen <sebastsg@stud.ntnu.no>
* @copyright 2014 University of Wisconsin - Madison
* @copyright 2018 NTNU
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_jazzquiz;
require_once('../../config.php');
require_once($CFG->dirroot . '/mod/jazzquiz/lib.php');
require_once($CFG->dirroot . '/mod/jazzquiz/locallib.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_login();
/**
* Check if this JazzQuiz has an open session.
* @param int $jazzquizid
* @return bool
* @throws \dml_exception
*/
function jazzquiz_session_open($jazzquizid) {
global $DB;
$sessions = $DB->get_records('jazzquiz_sessions', [
'jazzquizid' => $jazzquizid,
'sessionopen' => 1
]);
return count($sessions) > 0;
}
/**
* Gets the question bank view based on the options passed in at the page setup.
* @param \core_question\local\bank\question_edit_contexts $contexts
* @param jazzquiz $jazzquiz
* @param \moodle_url $url
* @param array $pagevars
* @return string
* @throws \coding_exception
*/
function get_qbank_view(
\core_question\local\bank\question_edit_contexts $contexts,
jazzquiz $jazzquiz, \moodle_url $url, array $pagevars) {
$qperpage = optional_param('qperpage', 10, PARAM_INT);
$qpage = optional_param('qpage', 0, PARAM_INT);
$newpagevars = [
'qpage' => $qpage,
'qperpage' => $qperpage,
'cat' => $pagevars['cat'],
'recurse' => $pagevars['recurse'],
'showhidden' => $pagevars['showhidden'],
'qbshowtext' => $pagevars['qbshowtext']
];
// Capture question bank display in buffer to have the renderer render output.
ob_start();
$questionbank = new bank\jazzquiz_question_bank_view($contexts, $url, $jazzquiz->course, $jazzquiz->cm);
$questionbank->display($newpagevars, 'editq');
return ob_get_clean();
}
/**
* Echos the list of questions using the renderer for jazzquiz.
* @param \core_question\local\bank\question_edit_contexts $contexts
* @param jazzquiz $jazzquiz
* @param \moodle_url $url
* @param array $pagevars
* @throws \coding_exception
* @throws \moodle_exception
*/
function list_questions(
\core_question\local\bank\question_edit_contexts $contexts,
jazzquiz $jazzquiz, \moodle_url $url, array $pagevars) {
$qbankview = get_qbank_view($contexts, $jazzquiz, $url, $pagevars);
$jazzquiz->renderer->list_questions($jazzquiz, $jazzquiz->questions, $qbankview, $url);
}
/**
* @param jazzquiz $jazzquiz
* @throws \coding_exception
*/
function jazzquiz_edit_order(jazzquiz $jazzquiz) {
$order = required_param('order', PARAM_RAW);
$order = json_decode($order);
$jazzquiz->set_question_order($order);
}
/**
* @param jazzquiz $jazzquiz
* @param \moodle_url $url
* @throws \coding_exception
* @throws \moodle_exception
*/
function jazzquiz_edit_add_question(jazzquiz $jazzquiz, \moodle_url $url) {
$questionids = required_param('questionids', PARAM_TEXT);
$questionids = explode(',', $questionids);
foreach ($questionids as $questionid) {
$jazzquiz->add_question($questionid);
}
// Ensure there is no action or questionid in the base url.
$url->remove_params('action', 'questionids');
redirect($url, null, 0);
}
/**
* @param jazzquiz $jazzquiz
* @throws \coding_exception
*/
function jazzquiz_edit_edit_question(jazzquiz $jazzquiz) {
$questionid = required_param('questionid', PARAM_INT);
$jazzquiz->edit_question($questionid);
}
/**
* @param jazzquiz $jazzquiz
* @param \core_question\local\bank\question_edit_contexts $contexts
* @param \moodle_url $url
* @param $pagevars
* @throws \coding_exception
* @throws \moodle_exception
*/
function jazzquiz_edit_qlist(
jazzquiz $jazzquiz, \core_question\local\bank\question_edit_contexts $contexts,
\moodle_url $url, array $pagevars) {
$jazzquiz->renderer->header($jazzquiz, 'edit');
list_questions($contexts, $jazzquiz, $url, $pagevars);
$jazzquiz->renderer->footer();
}
/**
* View edit page.
*/
function jazzquiz_edit() {
global $PAGE, $COURSE;
$action = optional_param('action', 'listquestions', PARAM_ALPHA);
// Inconsistency in question_edit_setup.
if (isset($_GET['id'])) {
$_GET['cmid'] = $_GET['id'];
}
if (isset($_POST['id'])) {
$_POST['cmid'] = $_POST['id'];
}
list(
$url,
$contexts,
$cmid,
$cm,
$module, // JazzQuiz database record.
$pagevars) = question_edit_setup('editq', '/mod/jazzquiz/edit.php', true);
$jazzquiz = new jazzquiz($cmid);
$renderer = $jazzquiz->renderer;
$modulename = get_string('modulename', 'jazzquiz');
$quizname = format_string($jazzquiz->data->name, true);
$PAGE->set_url($url);
$PAGE->set_title(strip_tags($jazzquiz->course->shortname . ': ' . $modulename . ': ' . $quizname));
$PAGE->set_heading($jazzquiz->course->fullname);
if (jazzquiz_session_open($jazzquiz->data->id)) {
// Can't edit during a session.
$renderer->header($jazzquiz, 'edit');
$renderer->session_is_open_error();
$renderer->footer();
return;
}
// Process moving, deleting and unhiding questions...
$questionbank = new \core_question\local\bank\view($contexts, $url, $COURSE, $cm);
switch ($action) {
case 'order':
jazzquiz_edit_order($jazzquiz);
break;
case 'addquestion':
jazzquiz_edit_add_question($jazzquiz, $url);
break;
case 'editquestion':
jazzquiz_edit_edit_question($jazzquiz);
break;
case 'listquestions':
jazzquiz_edit_qlist($jazzquiz, $contexts, $url, $pagevars);
break;
default:
break;
}
}
jazzquiz_edit();