-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
404 lines (346 loc) · 16.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
// This file is part of mod_offlinequiz for 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/>.
/**
* Page to edit offlinequizzes
*
* 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 offlinequiz does not already have student attempts
* The left column lists all questions that have been added to the current offlinequiz.
* The lecturer can add questions from the right hand list to the offlinequiz or remove them
*
* The script also processes a number of actions:
* Actions affecting a offlinequiz:
* up and down Changes the order of questions and page breaks
* addquestion Adds a single question to the offlinequiz
* add Adds several selected questions to the offlinequiz
* addrandom Adds a certain number of random questions to the offlinequiz
* repaginate Re-paginates the offlinequiz
* delete Removes a question from the offlinequiz
* savechanges Saves the order and grades for questions in the offlinequiz
*
*
* @package mod
* @subpackage offlinequiz
* @author Juergen Zimmer <[email protected]>
* @copyright 2015 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @since Moodle 2.8+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
require_once($CFG->dirroot . '/mod/offlinequiz/offlinequiz.class.php');
require_once($CFG->dirroot . '/mod/offlinequiz/addrandomform.php');
require_once($CFG->dirroot . '/question/category_class.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/statistics/lib.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);
// Patch problem with nested forms and category parameter, otherwise question_edit_setup has problems.
if (array_key_exists('savechanges', $_POST) && $_POST['savechanges']) {
unset($_POST['category']);
}
if (array_key_exists('offlinequizdeleteselected', $_POST) && $_POST['offlinequizdeleteselected']) {
unset($_POST['category']);
}
list($thispageurl, $contexts, $cmid, $cm, $offlinequiz, $pagevars) =
question_edit_setup('editq', '/mod/offlinequiz/edit.php', true);
$course = $DB->get_record('course', array('id' => $offlinequiz->course), '*', MUST_EXIST);
require_login($course, false, $cm);
// You need mod/offlinequiz:manage in addition to question capabilities to access this page.
require_capability('mod/offlinequiz:manage', $contexts->lowest());
$defaultcategoryobj = question_make_default_categories($contexts->all());
$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
// See if we do bulk grade editing.
$offlinequizgradetool = optional_param('gradetool', -1, PARAM_BOOL);
if ($offlinequizgradetool > -1) {
$thispageurl->param('gradetool', $offlinequizgradetool);
set_user_preference('offlinequiz_gradetab', $offlinequizgradetool);
} else {
$offlinequizgradetool = get_user_preferences('offlinequiz_gradetab', 0);
}
// Determine groupid.
$groupnumber = optional_param('groupnumber', 1, PARAM_INT);
if ($groupnumber === -1 and !empty($SESSION->question_pagevars['groupnumber'])) {
$groupnumber = $SESSION->question_pagevars['groupnumber'];
}
if ($groupnumber === -1) {
$groupnumber = 1;
}
$offlinequiz->groupnumber = $groupnumber;
$thispageurl->param('groupnumber', $offlinequiz->groupnumber);
// Load the offlinequiz group and set the groupid in the offlinequiz object.
if ($offlinequizgroup = offlinequiz_get_group($offlinequiz, $groupnumber)) {
$offlinequiz->groupid = $offlinequizgroup->id;
$groupquestions = offlinequiz_get_group_question_ids($offlinequiz);
$offlinequiz->questions = $groupquestions;
} else {
print_error('invalidgroupnumber', 'offlinequiz');
}
$offlinequiz->sumgrades = $offlinequizgroup->sumgrades;
$offlinequizhasattempts = offlinequiz_has_scanned_pages($offlinequiz->id);
$docscreated = $offlinequiz->docscreated;
$recordupdateanddocscreated = null;
$PAGE->set_url($thispageurl);
// Update version references before get_structure().
if ($newquestionid = optional_param('lastchanged', null, PARAM_INT)) {
$sql = "SELECT DISTINCT ogq.questionid AS oldquestionid,
ogq.maxmark AS maxmark,
(SELECT COUNT(*)
FROM {question_answers} qa
WHERE qa.question = ogq.questionid) AS answercount
FROM {offlinequiz_group_questions} ogq
JOIN {question_versions} qv1 ON qv1.questionid = ogq.questionid
JOIN {question_versions} qv2 ON qv1.questionbankentryid = qv2.questionbankentryid AND qv1.version <> qv2.version
WHERE ogq.offlinequizid = :offlinequizid
AND qv2.questionid = :newquestionid";
$questionupdates = $DB->get_records_sql($sql, ['offlinequizid' => $offlinequiz->id, 'newquestionid' => $newquestionid]);
$newanswercount = $DB->count_records('question_answers', ['question' => $newquestionid]);
foreach ($questionupdates as $questionupdate) {
if (!$docscreated || $questionupdate->answercount == $newanswercount) {
offlinequiz_update_question_instance($offlinequiz,
$questionupdate->oldquestionid,
$questionupdate->maxmark,
$newquestionid);
offlinequiz_update_all_attempt_sumgrades($offlinequiz);
offlinequiz_update_grades($offlinequiz, 0, true);
offlinequiz_delete_statistics_caches($offlinequiz->id);
}
}
}
// Get the course object and related bits.
$offlinequizobj = new offlinequiz($offlinequiz, $cm, $course);
$structure = $offlinequizobj->get_structure();
if ($warning = optional_param('warning', '', PARAM_TEXT)) {
$structure->add_warning(urldecode($warning));
}
if ($recordupdateanddocscreated) {
$structure->add_warning($recordupdateanddocscreated);
}
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
// Log this visit.
$params = array(
'courseid' => $course->id,
'context' => $contexts->lowest(),
'other' => array(
'offlinequizid' => $offlinequiz->id,
)
);
$event = \mod_offlinequiz\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);
}
// Get the list of question ids had their check-boxes ticked.
$selectedquestionids = array();
$params = (array) data_submitted();
foreach ($params as $key => $value) {
if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
$selectedquestionids[] = $matches[1];
}
}
if (optional_param('offlinequizdeleteselected', false, PARAM_BOOL) &&
!empty($selectedquestionids) && confirm_sesskey()) {
offlinequiz_remove_questionlist($offlinequiz, $selectedquestionids);
offlinequiz_delete_template_usages($offlinequiz);
$offlinequiz->sumgrades = offlinequiz_update_sumgrades($offlinequiz);
redirect($afteractionurl);
}
if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
// Re-paginate the offlinequiz.
$structure->check_can_be_edited();
$questionsperpage = optional_param('questionsperpage', $offlinequiz->questionsperpage, PARAM_INT);
offlinequiz_repaginate_questions($offlinequiz->id, $offlinequiz->groupid, $questionsperpage);
offlinequiz_delete_template_usages($offlinequiz);
redirect($afteractionurl);
}
if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
// Add a single question to the current offlinequiz.
$structure->check_can_be_edited();
offlinequiz_require_question_use($addquestion);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
// If the question is already in another group, take the maxmark of that.
if ($maxmarks = $DB->get_fieldset_select(
'offlinequiz_group_questions',
'maxmark',
'offlinequizid = :offlinequizid AND questionid = :questionid',
array('offlinequizid' => $offlinequiz->id, 'questionid' => $addquestion)
)) {
offlinequiz_add_offlinequiz_question($addquestion, $offlinequiz, $addonpage, $maxmarks[0]);
} else {
offlinequiz_add_offlinequiz_question($addquestion, $offlinequiz, $addonpage);
}
offlinequiz_delete_template_usages($offlinequiz);
offlinequiz_update_sumgrades($offlinequiz);
$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 offlinequiz.
$rawdata = (array) data_submitted();
foreach ($rawdata as $key => $value) { // Parse input for question ids.
if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
$key = $matches[1];
offlinequiz_require_question_use($key);
// If the question is already in another group, take the maxmark of that.
if ($maxmarks = $DB->get_fieldset_select(
'offlinequiz_group_questions',
'maxmark',
'offlinequizid = :offlinequizid AND questionid = :questionid',
array('offlinequizid' => $offlinequiz->id, 'questionid' => $key)
)) {
offlinequiz_add_offlinequiz_question($key, $offlinequiz, $addonpage, $maxmarks[0]);
} else {
offlinequiz_add_offlinequiz_question($key, $offlinequiz, $addonpage);
}
}
}
offlinequiz_delete_template_usages($offlinequiz);
offlinequiz_update_sumgrades($offlinequiz);
redirect($afteractionurl);
}
if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
// Add random questions to the offlinequiz.
$structure->check_can_be_edited();
$recurse = optional_param('recurse', 0, PARAM_BOOL);
$preventsamequestion = optional_param('preventsamequestion', 0, PARAM_BOOL);
$addonpage = optional_param('addonpage', 0, PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
$randomcount = required_param('randomcount', PARAM_INT);
offlinequiz_add_random_questions($offlinequiz, $addonpage, $categoryid, $randomcount, $recurse, $preventsamequestion);
offlinequiz_delete_template_usages($offlinequiz);
offlinequiz_update_sumgrades($offlinequiz);
redirect($afteractionurl);
}
if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
// Parameter to copy selected questions to another group.
$copyselectedtogroup = optional_param('copyselectedtogrouptop', 0, PARAM_INT);
if ($copyselectedtogroup) {
if (($selectedquestionids) && ($newgroup = offlinequiz_get_group($offlinequiz, $copyselectedtogroup))) {
$fromofflinegroup = optional_param('fromofflinegroup', 0, PARAM_INT);
offlinequiz_add_questionlist_to_group($selectedquestionids, $offlinequiz, $newgroup, $fromofflinegroup);
offlinequiz_update_sumgrades($offlinequiz, $newgroup->id);
// Delete the templates, just to be sure.
offlinequiz_delete_template_usages($offlinequiz);
}
redirect($afteractionurl);
}
// If rescaling is required save the new maximum.
$maxgrade = str_replace(',', '.', optional_param('maxgrade', -1, PARAM_RAW));
if (!is_numeric($maxgrade)) {
$afteractionurl->param('warning', urlencode(get_string('maxgradewarning', 'offlinequiz')));
} else {
$maxgrade = unformat_float($maxgrade);
if ($maxgrade >= 0) {
offlinequiz_set_grade($maxgrade, $offlinequiz);
offlinequiz_update_grades($offlinequiz, 0, true);
}
}
redirect($afteractionurl);
}
$savegrades = optional_param('savegrades', '', PARAM_ALPHA);
if ($savegrades == 'bulksavegrades' && confirm_sesskey()) {
$rawdata = (array) data_submitted();
foreach ($rawdata as $key => $value) {
if (preg_match('!^g([0-9]+)$!', $key, $matches)) {
if (is_numeric(str_replace(',', '.', $value))) {
// Parse input for question -> grades.
$questionid = $matches[1];
offlinequiz_update_question_instance($offlinequiz, $questionid, unformat_float($value));
} else {
$bulkgradewarning = true;
}
}
}
// Redmine 983: Upgrade sumgrades for all offlinequiz groups.
if ($groups = $DB->get_records(
'offlinequiz_groups',
array('offlinequizid' => $offlinequiz->id),
'groupnumber',
'*',
0,
$offlinequiz->numgroups
)) {
foreach ($groups as $group) {
$sumgrade = offlinequiz_update_sumgrades($offlinequiz, $group->id);
}
}
offlinequiz_update_all_attempt_sumgrades($offlinequiz);
offlinequiz_update_grades($offlinequiz, 0, true);
redirect($afteractionurl);
}
// Get the question bank view.
$questionbank = new mod_offlinequiz\question\bank\custom_view($contexts, $thispageurl, $course, $cm, $offlinequiz);
$questionbank->set_offlinequiz_has_scanned_pages($docscreated);
// End of process commands =====================================================.
$PAGE->set_pagelayout('incourse');
$PAGE->set_pagetype('mod-offlinequiz-edit');
$PAGE->force_settings_menu(true);
$output = $PAGE->get_renderer('mod_offlinequiz', 'edit');
$PAGE->set_title(get_string('editingofflinequizx', 'offlinequiz', format_string($offlinequiz->name)));
$PAGE->set_heading($course->fullname);
$node = $PAGE->settingsnav->find('mod_offlinequiz_edit', navigation_node::TYPE_SETTING);
if ($node) {
$node->make_active();
}
echo $OUTPUT->header();
// Initialise the JavaScript.
$offlinequizeditconfig = new stdClass();
$offlinequizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
$offlinequizeditconfig->dialoglisteners = array();
$numberoflisteners = $DB->get_field_sql("
SELECT COALESCE(MAX(page), 1)
FROM {offlinequiz_group_questions}
WHERE offlinequizid = ?
AND offlinegroupid = ?", array($offlinequiz->id, $offlinequiz->groupid));
for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
$offlinequizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
}
$PAGE->requires->data_for_js('offlinequiz_edit_config', $offlinequizeditconfig);
$PAGE->requires->js('/question/qengine.js');
// Questions wrapper start.
if ($offlinequizgradetool) {
echo html_writer::start_tag('div', array('class' => 'mod-offlinequiz-edit-content edit_grades'));
} else {
echo html_writer::start_tag('div', array('class' => 'mod-offlinequiz-edit-content'));
}
$letterstr = 'ABCDEFGHIJKL';
$groupletters = array();
for ($i = 1; $i <= $offlinequiz->numgroups; $i++) {
$groupletters[$i] = $letterstr[$i - 1];
}
if ($offlinequizgradetool) {
$output->edit_grades_page($offlinequizobj, $structure, $contexts, $thispageurl, $pagevars, $groupletters);
} else {
$output->edit_page($offlinequizobj, $structure, $contexts, $thispageurl, $pagevars, $groupletters);
}
// Questions wrapper end.
echo html_writer::end_tag('div');
echo $OUTPUT->footer();