MDL-20601 fixed fatal SQL upgrade error, missing {} around table names
[moodle.git] / question / import.php
CommitLineData
aca318e1 1<?php // $Id$
2/**
4323d029 3 * Import quiz questions into the given category
4 *
5 * @author Martin Dougiamas, Howard Miller, and many others.
6 * {@link http://moodle.org}
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package questionbank
9 * @subpackage importexport
10 */
aca318e1 11
12 require_once("../config.php");
271e6dec 13 require_once("editlib.php");
3ed998f3 14 require_once($CFG->libdir . '/uploadlib.php');
15 require_once($CFG->libdir . '/questionlib.php');
271e6dec 16 require_once("import_form.php");
aca318e1 17
271e6dec 18 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('import', false, false);
7d43178b 19
271e6dec 20 // get display strings
cbc87100 21 $txt = new stdClass();
ced7916c 22 $txt->importerror = get_string('importerror','quiz');
ced7916c 23 $txt->importquestions = get_string("importquestions", "quiz");
aca318e1 24
271e6dec 25 list($catid, $catcontext) = explode(',', $pagevars['cat']);
f34488b2 26 if (!$category = $DB->get_record("question_categories", array("id" => $catid))) {
7bc26c8f 27 print_error('nocategory','quiz');
aca318e1 28 }
29
271e6dec 30 //this page can be called without courseid or cmid in which case
31 //we get the context from the category object.
32 if ($contexts === null) { // need to get the course from the chosen category
33 $contexts = new question_edit_contexts(get_context_instance_by_id($category->contextid));
34 $thiscontext = $contexts->lowest();
35 if ($thiscontext->contextlevel == CONTEXT_COURSE){
36 require_login($thiscontext->instanceid, false);
37 } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
38 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
39 require_login($cm->course, false, $cm);
40 }
41 $contexts->require_one_edit_tab_cap($edittab);
aca318e1 42 }
43
aca318e1 44 // ensure the files area exists for this course
271e6dec 45 make_upload_directory("$COURSE->id");
aca318e1 46
5c0daafc 47 $PAGE->set_url($thispageurl->out());
48
271e6dec 49 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
50 'defaultcategory'=>$pagevars['cat']));
aca318e1 51
271e6dec 52 if ($import_form->is_cancelled()){
53 redirect($thispageurl);
54 }
cbc87100 55 //==========
56 // PAGE HEADER
57 //==========
aca318e1 58
b72ff476 59 if ($cm!==null) {
271e6dec 60 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
1d58b567 61 ? $OUTPUT->update_module_button($cm->id, $cm->modname)
aca318e1 62 : "";
d412354c 63 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/index.php', array('id'=>$COURSE->id)));
64 $PAGE->navbar->add(format_string($module->name), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
65 $PAGE->navbar->add($txt->importquestions);
66 $PAGE->set_title($txt->importquestions);
67 $PAGE->set_button($strupdatemodule);
68 echo $OUTPUT->header();
b72ff476 69
aca318e1 70 $currenttab = 'edit';
71 $mode = 'import';
b72ff476 72 ${$cm->modname} = $module;
73 include($CFG->dirroot."/mod/$cm->modname/tabs.php");
aca318e1 74 } else {
b72ff476 75 // Print basic page layout.
d412354c 76 $PAGE->navbar->add($txt->importquestions);
77 $PAGE->set_title($txt->importquestions);
78 echo $OUTPUT->header();
567709c4 79 // print tabs
80 $currenttab = 'import';
81 include('tabs.php');
aca318e1 82 }
83
84
cbc87100 85 // file upload form sumitted
271e6dec 86 if ($form = $import_form->get_data()) {
aca318e1 87
cbc87100 88 // file checks out ok
89 $fileisgood = false;
aca318e1 90
271e6dec 91 // work out if this is an uploaded file
cbc87100 92 // or one from the filesarea.
271e6dec 93 if (!empty($form->choosefile)) {
94 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
73b7b195 95 $realfilename = $form->choosefile;
cbc87100 96 if (file_exists($importfile)) {
97 $fileisgood = true;
271e6dec 98 } else {
5a2a5331 99 print_error('uploadproblem', 'moodle', $form->choosefile);
cbc87100 100 }
b72ff476 101 } else {
cbc87100 102 // must be upload file
73b7b195 103 $realfilename = $import_form->get_importfile_realname();
271e6dec 104 if (!$importfile = $import_form->get_importfile_name()) {
5a2a5331 105 print_error('uploadproblem', 'moodle');
271e6dec 106 }else {
cbc87100 107 $fileisgood = true;
108 }
aca318e1 109 }
aca318e1 110
cbc87100 111 // process if we are happy file is ok
271e6dec 112 if ($fileisgood) {
aca318e1 113
271e6dec 114 if (! is_readable("format/$form->format/format.php")) {
5a2a5331 115 print_error('formatnotfound','quiz', $form->format);
aca318e1 116 }
117
271e6dec 118 require_once("format.php"); // Parent class
119 require_once("format/$form->format/format.php");
aca318e1 120
271e6dec 121 $classname = "qformat_$form->format";
f5565b69 122 $qformat = new $classname();
aca318e1 123
08892d5b 124 // load data into class
271e6dec 125 $qformat->setCategory($category);
126 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
127 $qformat->setCourse($COURSE);
128 $qformat->setFilename($importfile);
73b7b195 129 $qformat->setRealfilename($realfilename);
271e6dec 130 $qformat->setMatchgrades($form->matchgrades);
131 $qformat->setCatfromfile(!empty($form->catfromfile));
132 $qformat->setContextfromfile(!empty($form->contextfromfile));
133 $qformat->setStoponerror($form->stoponerror);
08892d5b 134
f3701561 135 // Do anything before that we need to
271e6dec 136 if (! $qformat->importpreprocess()) {
fd9f6cd3 137 //TODO: need more detailed error info
138 print_error('cannotimport', '', $thispageurl->out());
aca318e1 139 }
140
f3701561 141 // Process the uploaded file
271e6dec 142 if (! $qformat->importprocess()) {
fd9f6cd3 143 //TODO: need more detailed error info
144 print_error('cannotimport', '', $thispageurl->out());
aca318e1 145 }
146
f3701561 147 // In case anything needs to be done after
148 if (! $qformat->importpostprocess()) {
bd8ee7c1 149 //TODO: need more detailed error info
150 print_error('cannotimport', '', $thispageurl->out());
aca318e1 151 }
152
153 echo "<hr />";
fef8f84e 154 echo $OUTPUT->continue_button("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
9b59580b 155 echo $OUTPUT->footer();
aca318e1 156 exit;
157 }
158 }
159
1d58b567 160 $helpicon = new moodle_help_icon();
161 $helpicon->text = $txt->importquestions;
162 $helpicon->page = 'import';
163 $helpicon->module = 'quiz';
164 echo $OUTPUT->heading_with_help($helpicon);
aca318e1 165
271e6dec 166 /// Print upload form
167 $import_form->display();
9b59580b 168 echo $OUTPUT->footer();
aca318e1 169
170?>