rss MDL-25076 made forum rss feeds that are displaying discussions instead of posts...
[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
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
a1eb90c0
DC
45$PAGE->set_url($thispageurl->out());
46
47$import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
48 'defaultcategory'=>$pagevars['cat']));
49
50if ($import_form->is_cancelled()){
51 redirect($thispageurl);
52}
53//==========
54// PAGE HEADER
55//==========
56$PAGE->set_title($txt->importquestions);
57$PAGE->set_heading($COURSE->fullname);
58echo $OUTPUT->header();
59
60// file upload form sumitted
61if ($form = $import_form->get_data()) {
62
63 // file checks out ok
64 $fileisgood = false;
65
66 // work out if this is an uploaded file
67 // or one from the filesarea.
68 if (!empty($form->choosefile)) {
69 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
70 $realfilename = $form->choosefile;
71 if (file_exists($importfile)) {
72 $fileisgood = true;
73 } else {
74 print_error('uploadproblem', 'moodle', $form->choosefile);
75 }
76 } else {
77 $realfilename = $import_form->get_new_filename('newfile');
78 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$realfilename}";
79 if (!$result = $import_form->save_file('newfile', $importfile, true)) {
80 print_error('uploadproblem', 'moodle');
81 }else {
82 $fileisgood = true;
271e6dec 83 }
aca318e1 84 }
85
a1eb90c0
DC
86 // process if we are happy file is ok
87 if ($fileisgood) {
aca318e1 88
a1eb90c0
DC
89 if (! is_readable("format/$form->format/format.php")) {
90 print_error('formatnotfound','quiz', $form->format);
91 }
aca318e1 92
a1eb90c0
DC
93 require_once("format.php"); // Parent class
94 require_once("format/$form->format/format.php");
95
96 $classname = "qformat_$form->format";
97 $qformat = new $classname();
98
99 // load data into class
100 $qformat->setCategory($category);
101 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
102 $qformat->setCourse($COURSE);
103 $qformat->setFilename($importfile);
104 $qformat->setRealfilename($realfilename);
105 $qformat->setMatchgrades($form->matchgrades);
106 $qformat->setCatfromfile(!empty($form->catfromfile));
107 $qformat->setContextfromfile(!empty($form->contextfromfile));
108 $qformat->setStoponerror($form->stoponerror);
109
110 // Do anything before that we need to
111 if (! $qformat->importpreprocess()) {
112 //TODO: need more detailed error info
113 print_error('cannotimport', '', $thispageurl->out());
aca318e1 114 }
aca318e1 115
a1eb90c0
DC
116 // Process the uploaded file
117 if (! $qformat->importprocess()) {
118 //TODO: need more detailed error info
119 print_error('cannotimport', '', $thispageurl->out());
aca318e1 120 }
aca318e1 121
a1eb90c0
DC
122 // In case anything needs to be done after
123 if (! $qformat->importpostprocess()) {
124 //TODO: need more detailed error info
125 print_error('cannotimport', '', $thispageurl->out());
126 }
aca318e1 127
a1eb90c0
DC
128 echo '<hr />';
129 $params = $thispageurl->params() + array('category'=>"{$qformat->category->id},{$qformat->category->contextid}");
130 echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
131 echo $OUTPUT->footer();
132 exit;
133 }
134}
aca318e1 135
5fcefc97 136echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
aeb15530 137
a1eb90c0
DC
138/// Print upload form
139$import_form->display();
140echo $OUTPUT->footer();