weekly on-sync release 3.1dev
[moodle.git] / question / import.php
CommitLineData
aeb15530 1<?php
f7c1dfaf
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle 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// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
16
aca318e1 17/**
f7c1dfaf 18 * Defines the import questions form.
4323d029 19 *
f7c1dfaf
TH
20 * @package moodlecore
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4323d029 24 */
aca318e1 25
f7c1dfaf
TH
26
27require_once(dirname(__FILE__) . '/../config.php');
28require_once($CFG->dirroot . '/question/editlib.php');
29require_once($CFG->dirroot . '/question/import_form.php');
30require_once($CFG->dirroot . '/question/format.php');
a1eb90c0
DC
31
32list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
33 question_edit_setup('import', '/question/import.php', false, false);
34
35// get display strings
36$txt = new stdClass();
5e8a85aa 37$txt->importerror = get_string('importerror', 'question');
5fcefc97 38$txt->importquestions = get_string('importquestions', 'question');
a1eb90c0
DC
39
40list($catid, $catcontext) = explode(',', $pagevars['cat']);
5e8a85aa
TH
41if (!$category = $DB->get_record("question_categories", array('id' => $catid))) {
42 print_error('nocategory', 'question');
a1eb90c0
DC
43}
44
d197ea43 45$categorycontext = context::instance_by_id($category->contextid);
cde2709a 46$category->context = $categorycontext;
a1eb90c0
DC
47//this page can be called without courseid or cmid in which case
48//we get the context from the category object.
49if ($contexts === null) { // need to get the course from the chosen category
cde2709a 50 $contexts = new question_edit_contexts($categorycontext);
a1eb90c0
DC
51 $thiscontext = $contexts->lowest();
52 if ($thiscontext->contextlevel == CONTEXT_COURSE){
53 require_login($thiscontext->instanceid, false);
54 } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
55 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
56 require_login($cm->course, false, $cm);
aca318e1 57 }
a1eb90c0
DC
58 $contexts->require_one_edit_tab_cap($edittab);
59}
60
f19ed874 61$PAGE->set_url($thispageurl);
a1eb90c0
DC
62
63$import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
64 'defaultcategory'=>$pagevars['cat']));
65
66if ($import_form->is_cancelled()){
67 redirect($thispageurl);
68}
69//==========
70// PAGE HEADER
71//==========
72$PAGE->set_title($txt->importquestions);
73$PAGE->set_heading($COURSE->fullname);
74echo $OUTPUT->header();
75
76// file upload form sumitted
77if ($form = $import_form->get_data()) {
78
79 // file checks out ok
80 $fileisgood = false;
81
82 // work out if this is an uploaded file
83 // or one from the filesarea.
cde2709a
DC
84 $realfilename = $import_form->get_new_filename('newfile');
85
b6b50231 86 $importfile = "{$CFG->tempdir}/questionimport/{$realfilename}";
af9b1444 87 make_temp_directory('questionimport');
cde2709a 88 if (!$result = $import_form->save_file('newfile', $importfile, true)) {
f7c1dfaf
TH
89 throw new moodle_exception('uploadproblem');
90 }
91
92 $formatfile = 'format/' . $form->format . '/format.php';
93 if (!is_readable($formatfile)) {
5e8a85aa 94 throw new moodle_exception('formatnotfound', 'question', '', $form->format);
aca318e1 95 }
96
f7c1dfaf
TH
97 require_once($formatfile);
98
99 $classname = 'qformat_' . $form->format;
100 $qformat = new $classname();
101
102 // load data into class
103 $qformat->setCategory($category);
104 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
105 $qformat->setCourse($COURSE);
106 $qformat->setFilename($importfile);
107 $qformat->setRealfilename($realfilename);
108 $qformat->setMatchgrades($form->matchgrades);
109 $qformat->setCatfromfile(!empty($form->catfromfile));
110 $qformat->setContextfromfile(!empty($form->contextfromfile));
111 $qformat->setStoponerror($form->stoponerror);
112
113 // Do anything before that we need to
114 if (!$qformat->importpreprocess()) {
f7c1dfaf 115 print_error('cannotimport', '', $thispageurl->out());
a1eb90c0 116 }
f7c1dfaf
TH
117
118 // Process the uploaded file
119 if (!$qformat->importprocess($category)) {
f7c1dfaf
TH
120 print_error('cannotimport', '', $thispageurl->out());
121 }
122
123 // In case anything needs to be done after
124 if (!$qformat->importpostprocess()) {
f7c1dfaf
TH
125 print_error('cannotimport', '', $thispageurl->out());
126 }
127
128 $params = $thispageurl->params() + array(
129 'category' => $qformat->category->id . ',' . $qformat->category->contextid);
130 echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
131 echo $OUTPUT->footer();
132 exit;
a1eb90c0 133}
aca318e1 134
5fcefc97 135echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
aeb15530 136
a1eb90c0
DC
137/// Print upload form
138$import_form->display();
139echo $OUTPUT->footer();