-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathquestionxmledit.php
More file actions
162 lines (148 loc) · 5.92 KB
/
questionxmledit.php
File metadata and controls
162 lines (148 loc) · 5.92 KB
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
<?php
// This file is part of Stack - http://stack.maths.ed.ac.uk/
//
// Stack 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.
//
// Stack 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 Stack. If not, see <http://www.gnu.org/licenses/>.
/**
* This script lets the user edit the question XML directly and attempt
* to import the XML as a new version.
*
* @package qtype_stack
* @copyright 2025 Universiy of Edinburgh
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once(__DIR__ . '/locallib.php');
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->dirroot . '/question/format/xml/format.php');
require_once(__DIR__ . '/questionxmlform.php');
use core_question\local\bank\question_edit_contexts;
use qformat_xml;
require_login();
// Get the parameters from the URL.
$questionid = required_param('id', PARAM_INT);
[$qversion, $questionid] = get_latest_question_version($questionid);
$questiondata = question_bank::load_question_data($questionid);
if (!$questiondata) {
throw new stack_exception('questiondoesnotexist');
}
$question = question_bank::load_question($questionid);
// Process any other URL parameters, and do require_login.
[$context, $seed, $urlparams] = qtype_stack_setup_question_test_page($question);
// Check permissions.
question_require_capability_on($questiondata, 'edit');
$editparams = $urlparams;
unset($editparams['questionid']);
unset($editparams['seed']);
$editparams['id'] = $question->id;
$questionediturl = new moodle_url('/question/bank/editquestion/question.php', $editparams);
$questioneditlatesturl = new moodle_url('/question/type/stack/questioneditlatest.php', $editparams);
$PAGE->set_url('/question/type/stack/questionxmledit.php', $editparams);
$title = stack_string('editxmltitle');
$PAGE->set_title($title);
$mform = new qtype_stack_question_xml_form(
$PAGE->url,
['submitlabel' => stack_string('editxmlbutton'), 'xmlstring' => '', 'numberrows' => 5]
);
$qformat = new qformat_xml();
$contexts = new question_edit_contexts($context);
$qformat->setCattofile(false);
$qformat->setContexttofile(false);
$qformat->setContextfromfile(false);
$qformat->setStoponerror(true);
$qformat->setCourse($COURSE);
$errors = '';
$notices = '';
$warnings = '';
$xmlstring = '';
if ($mform->is_cancelled()) {
unset($urlparams['testcase']);
$qtype = new qtype_stack();
redirect($qtype->get_question_test_url($question));
} else if ($fromform = $mform->get_data()) {
$importfile = make_request_directory() . "/importq.xml";
file_put_contents($importfile, $fromform->questionxml);
try {
$result = \qbank_importasversion\importer::import_file($qformat, $question, $importfile);
$errors = $result->error ?? '';
$notices = $result->notice ?? '';
} catch (Exception $e) {
$errors = $e->getMessage();
}
// The import process spits out the question description somewhere. Clean output to remove.
ob_clean();
// Refresh data with newly saved question.
[$qversion, $questionid] = get_latest_question_version($questionid);
$question = question_bank::load_question($questionid);
$warnings = implode(' ', $question->validate_warnings(true));
$questiondata = question_bank::load_question_data($questionid);
}
if (!empty($errors)) {
// We've tried to save the question but failed. Show POSTed XML.
$xmlstring = $fromform->questionxml;
} else {
$qformat->setQuestions([$questiondata]);
if (!$qformat->exportpreprocess()) {
$notices .= stack_string('xmldisplayerror');
} else {
if (!$xmlstring = $qformat->exportprocess(true)) {
$xmlstring = '';
$notices .= stack_string('xmldisplayerror');
}
}
}
echo $OUTPUT->header();
$links = [];
$qtype = new qtype_stack();
$qtestlink = $qtype->get_question_test_url($question);
$links[] = html_writer::link($qtestlink, '<i class="fa fa-wrench"></i> '
. stack_string('runquestiontests'), ['class' => 'nav-link']);
$qpreviewlink = qbank_previewquestion\helper::question_preview_url($questionid, null, null, null, null, $context);
$links[] = html_writer::link(
$qpreviewlink,
'<i class="fa fa-plus-circle"></i> ' . stack_string('questionpreview'),
['class' => 'nav-link']
);
$links[] = html_writer::link(
$questioneditlatesturl,
stack_string('editquestioninthequestionbank'),
['class' => 'nav-link']
);
$links[] = html_writer::link(
$PAGE->url,
stack_string('reloadsavedXML'),
['class' => 'nav-link']
);
echo html_writer::tag('nav', implode(' ', $links), ['class' => 'nav']);
echo $OUTPUT->heading($title);
echo $OUTPUT->heading($question->name, 3);
echo html_writer::tag('p', stack_string('version') . ' ' . $qversion);
$fout = '';
if ($errors) {
$errors .= ' ' . stack_string('notsaved');
$fout .= html_writer::tag('div', $errors, ['class' => 'alert alert-danger']);
} else if ($notices) {
$fout .= html_writer::tag('div', $notices, ['class' => 'alert alert-warning']);
$fout .= html_writer::tag('p', $warnings);
}
echo html_writer::tag('div', $fout);
$xmlstringlen = max(substr_count($xmlstring, "\n") + 3, 8);
// Redo form with the correct textarea size and display.
$mform = new qtype_stack_question_xml_form(
$PAGE->url,
['submitlabel' => stack_string('editxmlbutton'), 'xmlstring' => '', 'numberrows' => $xmlstringlen]
);
$mform->setConstants(['questionxml' => $xmlstring]);
$mform->display();
echo html_writer::tag('p', stack_string('editxmlintro'));
echo $OUTPUT->footer();