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