MDL-63738 question bank: a link to download a single question
[moodle.git] / question / exportone.php
CommitLineData
616442a2
TH
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.
13//
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/>.
16
17/**
18 * Script to download the export of a single question.
19 *
20 * @copyright 2015 the Open University
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24require_once(__DIR__.'/../config.php');
25
26require_once($CFG->libdir . '/questionlib.php');
27require_once($CFG->dirroot . '/question/format/xml/format.php');
28
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()];
34
35if ($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');
46}
47require_sesskey();
48
49// Load the necessary data.
50$contexts = new question_edit_contexts($thiscontext);
51$questiondata = question_bank::load_question_data($questionid);
52
53// Check permissions.
54question_require_capability_on($questiondata, 'view');
55
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');
61
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);
71
72// Do the export.
73if (!$qformat->exportpreprocess()) {
74 send_file_not_found();
75}
76if (!$content = $qformat->exportprocess(true)) {
77 send_file_not_found();
78}
79send_file($content, $filename, 0, 0, true, true, $qformat->mime_type());