MDL-20636 Reveiw all throw statements.
[moodle.git] / question / import.php
CommitLineData
aeb15530 1<?php
f7c1dfaf
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/**
f7c1dfaf 19 * Defines the import questions form.
4323d029 20 *
f7c1dfaf
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
f7c1dfaf
TH
27
28require_once(dirname(__FILE__) . '/../config.php');
29require_once($CFG->dirroot . '/question/editlib.php');
30require_once($CFG->dirroot . '/question/import_form.php');
31require_once($CFG->dirroot . '/question/format.php');
a1eb90c0
DC
32
33list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
34 question_edit_setup('import', '/question/import.php', false, false);
35
36// get display strings
37$txt = new stdClass();
38$txt->importerror = get_string('importerror','quiz');
5fcefc97 39$txt->importquestions = get_string('importquestions', 'question');
a1eb90c0
DC
40
41list($catid, $catcontext) = explode(',', $pagevars['cat']);
42if (!$category = $DB->get_record("question_categories", array("id" => $catid))) {
43 print_error('nocategory','quiz');
44}
45
46$PAGE->set_pagelayout('standard');
47
cde2709a
DC
48$categorycontext = get_context_instance_by_id($category->contextid);
49$category->context = $categorycontext;
a1eb90c0
DC
50//this page can be called without courseid or cmid in which case
51//we get the context from the category object.
52if ($contexts === null) { // need to get the course from the chosen category
cde2709a 53 $contexts = new question_edit_contexts($categorycontext);
a1eb90c0
DC
54 $thiscontext = $contexts->lowest();
55 if ($thiscontext->contextlevel == CONTEXT_COURSE){
56 require_login($thiscontext->instanceid, false);
57 } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
58 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
59 require_login($cm->course, false, $cm);
aca318e1 60 }
a1eb90c0
DC
61 $contexts->require_one_edit_tab_cap($edittab);
62}
63
a1eb90c0
DC
64$PAGE->set_url($thispageurl->out());
65
66$import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
67 'defaultcategory'=>$pagevars['cat']));
68
69if ($import_form->is_cancelled()){
70 redirect($thispageurl);
71}
72//==========
73// PAGE HEADER
74//==========
75$PAGE->set_title($txt->importquestions);
76$PAGE->set_heading($COURSE->fullname);
77echo $OUTPUT->header();
78
79// file upload form sumitted
80if ($form = $import_form->get_data()) {
81
82 // file checks out ok
83 $fileisgood = false;
84
85 // work out if this is an uploaded file
86 // or one from the filesarea.
cde2709a
DC
87 $realfilename = $import_form->get_new_filename('newfile');
88
89 $importfile = "{$CFG->dataroot}/temp/questionimport/{$realfilename}";
90 make_upload_directory('temp/questionimport');
91 if (!$result = $import_form->save_file('newfile', $importfile, true)) {
f7c1dfaf
TH
92 throw new moodle_exception('uploadproblem');
93 }
94
95 $formatfile = 'format/' . $form->format . '/format.php';
96 if (!is_readable($formatfile)) {
97 throw new moodle_exception('formatnotfound', 'quiz', '', $form->format);
aca318e1 98 }
99
f7c1dfaf
TH
100 require_once($formatfile);
101
102 $classname = 'qformat_' . $form->format;
103 $qformat = new $classname();
104
105 // load data into class
106 $qformat->setCategory($category);
107 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
108 $qformat->setCourse($COURSE);
109 $qformat->setFilename($importfile);
110 $qformat->setRealfilename($realfilename);
111 $qformat->setMatchgrades($form->matchgrades);
112 $qformat->setCatfromfile(!empty($form->catfromfile));
113 $qformat->setContextfromfile(!empty($form->contextfromfile));
114 $qformat->setStoponerror($form->stoponerror);
115
116 // Do anything before that we need to
117 if (!$qformat->importpreprocess()) {
118 //TODO: need more detailed error info
119 print_error('cannotimport', '', $thispageurl->out());
a1eb90c0 120 }
f7c1dfaf
TH
121
122 // Process the uploaded file
123 if (!$qformat->importprocess($category)) {
124 //TODO: need more detailed error info
125 print_error('cannotimport', '', $thispageurl->out());
126 }
127
128 // In case anything needs to be done after
129 if (!$qformat->importpostprocess()) {
130 //TODO: need more detailed error info
131 print_error('cannotimport', '', $thispageurl->out());
132 }
133
134 $params = $thispageurl->params() + array(
135 'category' => $qformat->category->id . ',' . $qformat->category->contextid);
136 echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
137 echo $OUTPUT->footer();
138 exit;
a1eb90c0 139}
aca318e1 140
5fcefc97 141echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
aeb15530 142
a1eb90c0
DC
143/// Print upload form
144$import_form->display();
145echo $OUTPUT->footer();