MDL-23479 backup - chat revised & old code deleted (but non-existing restore logs)
[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');
22$txt->importquestions = get_string("importquestions", "quiz");
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
31//this page can be called without courseid or cmid in which case
32//we get the context from the category object.
33if ($contexts === null) { // need to get the course from the chosen category
34 $contexts = new question_edit_contexts(get_context_instance_by_id($category->contextid));
35 $thiscontext = $contexts->lowest();
36 if ($thiscontext->contextlevel == CONTEXT_COURSE){
37 require_login($thiscontext->instanceid, false);
38 } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
39 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
40 require_login($cm->course, false, $cm);
aca318e1 41 }
a1eb90c0
DC
42 $contexts->require_one_edit_tab_cap($edittab);
43}
44
45// ensure the files area exists for this course
46make_upload_directory("$COURSE->id");
47
48$PAGE->set_url($thispageurl->out());
49
50$import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
51 'defaultcategory'=>$pagevars['cat']));
52
53if ($import_form->is_cancelled()){
54 redirect($thispageurl);
55}
56//==========
57// PAGE HEADER
58//==========
59$PAGE->set_title($txt->importquestions);
60$PAGE->set_heading($COURSE->fullname);
61echo $OUTPUT->header();
62
63// file upload form sumitted
64if ($form = $import_form->get_data()) {
65
66 // file checks out ok
67 $fileisgood = false;
68
69 // work out if this is an uploaded file
70 // or one from the filesarea.
71 if (!empty($form->choosefile)) {
72 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
73 $realfilename = $form->choosefile;
74 if (file_exists($importfile)) {
75 $fileisgood = true;
76 } else {
77 print_error('uploadproblem', 'moodle', $form->choosefile);
78 }
79 } else {
80 $realfilename = $import_form->get_new_filename('newfile');
81 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$realfilename}";
82 if (!$result = $import_form->save_file('newfile', $importfile, true)) {
83 print_error('uploadproblem', 'moodle');
84 }else {
85 $fileisgood = true;
271e6dec 86 }
aca318e1 87 }
88
a1eb90c0
DC
89 // process if we are happy file is ok
90 if ($fileisgood) {
aca318e1 91
a1eb90c0
DC
92 if (! is_readable("format/$form->format/format.php")) {
93 print_error('formatnotfound','quiz', $form->format);
94 }
aca318e1 95
a1eb90c0
DC
96 require_once("format.php"); // Parent class
97 require_once("format/$form->format/format.php");
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()) {
115 //TODO: need more detailed error info
116 print_error('cannotimport', '', $thispageurl->out());
aca318e1 117 }
aca318e1 118
a1eb90c0
DC
119 // Process the uploaded file
120 if (! $qformat->importprocess()) {
121 //TODO: need more detailed error info
122 print_error('cannotimport', '', $thispageurl->out());
aca318e1 123 }
aca318e1 124
a1eb90c0
DC
125 // In case anything needs to be done after
126 if (! $qformat->importpostprocess()) {
127 //TODO: need more detailed error info
128 print_error('cannotimport', '', $thispageurl->out());
129 }
aca318e1 130
a1eb90c0
DC
131 echo '<hr />';
132 $params = $thispageurl->params() + array('category'=>"{$qformat->category->id},{$qformat->category->contextid}");
133 echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
134 echo $OUTPUT->footer();
135 exit;
136 }
137}
aca318e1 138
a1eb90c0 139echo $OUTPUT->heading_with_help($txt->importquestions, 'import', 'quiz');
aeb15530 140
a1eb90c0
DC
141/// Print upload form
142$import_form->display();
143echo $OUTPUT->footer();