Merge branch 'MDL-69510-310' of https://github.com/snake/moodle into MOODLE_310_STABLE
[moodle.git] / question / exportone.php
blobc2de2bc2ce1a1a63811c64147f27bfa756297174
1 <?php
2 // This file is part of Stack - http://stack.maths.ed.ac.uk/
3 //
4 // Stack is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Stack is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Stack. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Script to download the export of a single question.
20 * @copyright 2015 the Open University
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once(__DIR__.'/../config.php');
26 require_once($CFG->libdir . '/questionlib.php');
27 require_once($CFG->dirroot . '/question/format/xml/format.php');
29 // Get the parameters from the URL.
30 $questionid = required_param('id', PARAM_INT);
31 $cmid = optional_param('cmid', 0, PARAM_INT);
32 $courseid = optional_param('courseid', 0, PARAM_INT);
33 $urlparams = ['id' => $questionid, 'sesskey' => sesskey()];
35 if ($cmid) {
36 $cm = get_coursemodule_from_id(null, $cmid);
37 require_login($cm->course, false, $cm);
38 $thiscontext = context_module::instance($cmid);
39 $urlparams['cmid'] = $cmid;
40 } else if ($courseid) {
41 require_login($courseid, false);
42 $thiscontext = context_course::instance($courseid);
43 $urlparams['courseid'] = $courseid;
44 } else {
45 print_error('missingcourseorcmid', 'question');
47 require_sesskey();
49 // Load the necessary data.
50 $contexts = new question_edit_contexts($thiscontext);
51 $questiondata = question_bank::load_question_data($questionid);
53 // Check permissions.
54 question_require_capability_on($questiondata, 'view');
56 // Initialise $PAGE. Nothing is output, so this does not really matter. Just avoids notices.
57 $nexturl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams);
58 $PAGE->set_url('/question/exportone.php', $urlparams);
59 $PAGE->set_heading($COURSE->fullname);
60 $PAGE->set_pagelayout('admin');
62 // Set up the export format.
63 $qformat = new qformat_xml();
64 $filename = question_default_export_filename($COURSE, $questiondata) .
65 $qformat->export_file_extension();
66 $qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
67 $qformat->setCourse($COURSE);
68 $qformat->setQuestions([$questiondata]);
69 $qformat->setCattofile(false);
70 $qformat->setContexttofile(false);
72 // Do the export.
73 if (!$qformat->exportpreprocess()) {
74 send_file_not_found();
76 if (!$content = $qformat->exportprocess(true)) {
77 send_file_not_found();
79 send_file($content, $filename, 0, 0, true, true, $qformat->mime_type());