MDL-16221
[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
271e6dec 47 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
48 'defaultcategory'=>$pagevars['cat']));
aca318e1 49
271e6dec 50 if ($import_form->is_cancelled()){
51 redirect($thispageurl);
52 }
cbc87100 53 //==========
54 // PAGE HEADER
55 //==========
aca318e1 56
b72ff476 57 if ($cm!==null) {
271e6dec 58 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
59 ? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
aca318e1 60 : "";
3b27b0fe 61 $navlinks = array();
83601351 62 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id", 'type' => 'activity');
0cbddd85 63 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
3b27b0fe 64 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
65 $navigation = build_navigation($navlinks);
b72ff476 66 print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule);
67
aca318e1 68 $currenttab = 'edit';
69 $mode = 'import';
b72ff476 70 ${$cm->modname} = $module;
71 include($CFG->dirroot."/mod/$cm->modname/tabs.php");
aca318e1 72 } else {
b72ff476 73 // Print basic page layout.
3b27b0fe 74 $navlinks = array();
75 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
76 $navigation = build_navigation($navlinks);
271e6dec 77
b72ff476 78 print_header_simple($txt->importquestions, '', $navigation);
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}";
cbc87100 95 if (file_exists($importfile)) {
96 $fileisgood = true;
271e6dec 97 } else {
5a2a5331 98 print_error('uploadproblem', 'moodle', $form->choosefile);
cbc87100 99 }
b72ff476 100 } else {
cbc87100 101 // must be upload file
271e6dec 102 if (!$importfile = $import_form->get_importfile_name()) {
5a2a5331 103 print_error('uploadproblem', 'moodle');
271e6dec 104 }else {
cbc87100 105 $fileisgood = true;
106 }
aca318e1 107 }
aca318e1 108
cbc87100 109 // process if we are happy file is ok
271e6dec 110 if ($fileisgood) {
aca318e1 111
271e6dec 112 if (! is_readable("format/$form->format/format.php")) {
5a2a5331 113 print_error('formatnotfound','quiz', $form->format);
aca318e1 114 }
115
271e6dec 116 require_once("format.php"); // Parent class
117 require_once("format/$form->format/format.php");
aca318e1 118
271e6dec 119 $classname = "qformat_$form->format";
f5565b69 120 $qformat = new $classname();
aca318e1 121
08892d5b 122 // load data into class
271e6dec 123 $qformat->setCategory($category);
124 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
125 $qformat->setCourse($COURSE);
126 $qformat->setFilename($importfile);
127 $qformat->setMatchgrades($form->matchgrades);
128 $qformat->setCatfromfile(!empty($form->catfromfile));
129 $qformat->setContextfromfile(!empty($form->contextfromfile));
130 $qformat->setStoponerror($form->stoponerror);
08892d5b 131
f3701561 132 // Do anything before that we need to
271e6dec 133 if (! $qformat->importpreprocess()) {
fd9f6cd3 134 //TODO: need more detailed error info
135 print_error('cannotimport', '', $thispageurl->out());
aca318e1 136 }
137
f3701561 138 // Process the uploaded file
271e6dec 139 if (! $qformat->importprocess()) {
fd9f6cd3 140 //TODO: need more detailed error info
141 print_error('cannotimport', '', $thispageurl->out());
aca318e1 142 }
143
f3701561 144 // In case anything needs to be done after
145 if (! $qformat->importpostprocess()) {
bd8ee7c1 146 //TODO: need more detailed error info
147 print_error('cannotimport', '', $thispageurl->out());
aca318e1 148 }
149
150 echo "<hr />";
271e6dec 151 print_continue("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
152 print_footer($COURSE);
aca318e1 153 exit;
154 }
155 }
156
ced7916c 157 print_heading_with_help($txt->importquestions, "import", "quiz");
aca318e1 158
271e6dec 159 /// Print upload form
160 $import_form->display();
161 print_footer($COURSE);
aca318e1 162
163?>