quiz MDL-19813 Upgraded deprecated calls and called set_url with pre-generated var
[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))
1d58b567 59 ? $OUTPUT->update_module_button($cm->id, $cm->modname)
aca318e1 60 : "";
d412354c 61 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/index.php', array('id'=>$COURSE->id)));
62 $PAGE->navbar->add(format_string($module->name), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
63 $PAGE->navbar->add($txt->importquestions);
64 $PAGE->set_title($txt->importquestions);
65 $PAGE->set_button($strupdatemodule);
66 echo $OUTPUT->header();
b72ff476 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.
d412354c 74 $PAGE->navbar->add($txt->importquestions);
75 $PAGE->set_title($txt->importquestions);
76 echo $OUTPUT->header();
567709c4 77 // print tabs
78 $currenttab = 'import';
79 include('tabs.php');
aca318e1 80 }
81
82
cbc87100 83 // file upload form sumitted
271e6dec 84 if ($form = $import_form->get_data()) {
aca318e1 85
cbc87100 86 // file checks out ok
87 $fileisgood = false;
aca318e1 88
271e6dec 89 // work out if this is an uploaded file
cbc87100 90 // or one from the filesarea.
271e6dec 91 if (!empty($form->choosefile)) {
92 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
73b7b195 93 $realfilename = $form->choosefile;
cbc87100 94 if (file_exists($importfile)) {
95 $fileisgood = true;
271e6dec 96 } else {
5a2a5331 97 print_error('uploadproblem', 'moodle', $form->choosefile);
cbc87100 98 }
b72ff476 99 } else {
cbc87100 100 // must be upload file
73b7b195 101 $realfilename = $import_form->get_importfile_realname();
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);
73b7b195 127 $qformat->setRealfilename($realfilename);
271e6dec 128 $qformat->setMatchgrades($form->matchgrades);
129 $qformat->setCatfromfile(!empty($form->catfromfile));
130 $qformat->setContextfromfile(!empty($form->contextfromfile));
131 $qformat->setStoponerror($form->stoponerror);
08892d5b 132
f3701561 133 // Do anything before that we need to
271e6dec 134 if (! $qformat->importpreprocess()) {
fd9f6cd3 135 //TODO: need more detailed error info
136 print_error('cannotimport', '', $thispageurl->out());
aca318e1 137 }
138
f3701561 139 // Process the uploaded file
271e6dec 140 if (! $qformat->importprocess()) {
fd9f6cd3 141 //TODO: need more detailed error info
142 print_error('cannotimport', '', $thispageurl->out());
aca318e1 143 }
144
f3701561 145 // In case anything needs to be done after
146 if (! $qformat->importpostprocess()) {
bd8ee7c1 147 //TODO: need more detailed error info
148 print_error('cannotimport', '', $thispageurl->out());
aca318e1 149 }
150
151 echo "<hr />";
fef8f84e 152 echo $OUTPUT->continue_button("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
9b59580b 153 echo $OUTPUT->footer();
aca318e1 154 exit;
155 }
156 }
157
1d58b567 158 $helpicon = new moodle_help_icon();
159 $helpicon->text = $txt->importquestions;
160 $helpicon->page = 'import';
161 $helpicon->module = 'quiz';
162 echo $OUTPUT->heading_with_help($helpicon);
aca318e1 163
271e6dec 164 /// Print upload form
165 $import_form->display();
9b59580b 166 echo $OUTPUT->footer();
aca318e1 167
168?>