MDL-20636 Reveiw all throw statements.
[moodle.git] / question / export.php
CommitLineData
aeb15530 1<?php
d3603157
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
aca318e1 18/**
d3603157 19 * Script for importing questions into the question bank.
4323d029 20 *
d3603157
TH
21 * @package moodlecore
22 * @subpackage questionbank
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4323d029 25 */
aca318e1 26
aca318e1 27
d3603157
TH
28require_once(dirname(__FILE__) . '/../config.php');
29require_once($CFG->dirroot . '/question/editlib.php');
30require_once($CFG->dirroot . '/question/export_form.php');
56ed242b 31
d3603157 32$PAGE->set_pagelayout('standard');
d187f660 33
d3603157
TH
34list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
35 question_edit_setup('export', '/question/export.php');
0c1e3a14 36
d3603157
TH
37// get display strings
38$strexportquestions = get_string('exportquestions', 'question');
aca318e1 39
d3603157
TH
40// make sure we are using the user's most recent category choice
41if (empty($categoryid)) {
42 $categoryid = $pagevars['cat'];
43}
44
45list($catid, $catcontext) = explode(',', $pagevars['cat']);
46if (!$category = $DB->get_record("question_categories", array("id" => $catid, 'contextid' => $catcontext))) {
47 print_error('nocategory','quiz');
48}
49
50/// Header
51$PAGE->set_url($thispageurl->out());
52$PAGE->set_title($strexportquestions);
53$PAGE->set_heading($COURSE->fullname);
54echo $OUTPUT->header();
55
56$export_form = new question_export_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('export'), 'defaultcategory'=>$pagevars['cat']));
22afe6d6 57
d3603157
TH
58
59if ($from_form = $export_form->get_data()) {
60 $thiscontext = $contexts->lowest();
61 if (!is_readable("format/$from_form->format/format.php")) {
62 print_error('unknowformat', '', '', $from_form->format);
63 }
64 $withcategories = 'nocategories';
65 if (!empty($from_form->cattofile)) {
66 $withcategories = 'withcategories';
aca318e1 67 }
d3603157
TH
68 $withcontexts = 'nocontexts';
69 if (!empty($from_form->contexttofile)) {
70 $withcontexts = 'withcontexts';
71 }
72
73 $classname = 'qformat_' . $from_form->format;
74 $qformat = new $classname();
75 $filename = question_default_export_filename($COURSE, $category) .
76 $qformat->export_file_extension();
77 $export_url = question_make_export_url($thiscontext->id, $category->id,
78 $from_form->format, $withcategories, $withcontexts, $filename);
aca318e1 79
d3603157
TH
80 echo $OUTPUT->box_start();
81 echo get_string('yourfileshoulddownload', 'question', $export_url->out());
82 echo $OUTPUT->box_end();
aca318e1 83
d3603157 84 $PAGE->requires->js_function_call('document.location.replace', array($export_url->out()), false, 1);
271e6dec 85
d3603157 86 echo $OUTPUT->continue_button(new moodle_url('edit.php', $thispageurl->params()));
9b59580b 87 echo $OUTPUT->footer();
d3603157
TH
88 exit;
89}
90
91/// Display export form
92echo $OUTPUT->heading_with_help($strexportquestions, 'exportquestions', 'question');
93
94$export_form->display();
95
96echo $OUTPUT->footer();