question MDL-19820 Updated print_header and build_navigation to OUTPUT and PAGE equiv...
[moodle.git] / question / import.php
1 <?php // $Id$
2 /**
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  */
12     require_once("../config.php");
13     require_once("editlib.php");
14     require_once($CFG->libdir . '/uploadlib.php');
15     require_once($CFG->libdir . '/questionlib.php');
16     require_once("import_form.php");
18     list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('import', false, false);
20    // get display strings
21     $txt = new stdClass();
22     $txt->importerror = get_string('importerror','quiz');
23     $txt->importquestions = get_string("importquestions", "quiz");
25     list($catid, $catcontext) = explode(',', $pagevars['cat']);
26     if (!$category = $DB->get_record("question_categories", array("id" => $catid))) {
27         print_error('nocategory','quiz');
28     }
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);
42     }
44     // ensure the files area exists for this course
45     make_upload_directory("$COURSE->id");
47     $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
48                                                         'defaultcategory'=>$pagevars['cat']));
50     if ($import_form->is_cancelled()){
51         redirect($thispageurl);
52     }
53     //==========
54     // PAGE HEADER
55     //==========
57     if ($cm!==null) {
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))
60             : "";
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();
68         $currenttab = 'edit';
69         $mode = 'import';
70         ${$cm->modname} = $module;
71         include($CFG->dirroot."/mod/$cm->modname/tabs.php");
72     } else {
73         // Print basic page layout.
74         $PAGE->navbar->add($txt->importquestions);
75         $PAGE->set_title($txt->importquestions);
76         echo $OUTPUT->header();
77         // print tabs
78         $currenttab = 'import';
79         include('tabs.php');
80     }
83     // file upload form sumitted
84     if ($form = $import_form->get_data()) {
86         // file checks out ok
87         $fileisgood = false;
89         // work out if this is an uploaded file
90         // or one from the filesarea.
91         if (!empty($form->choosefile)) {
92             $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
93             $realfilename = $form->choosefile;
94             if (file_exists($importfile)) {
95                 $fileisgood = true;
96             } else {
97                 print_error('uploadproblem', 'moodle', $form->choosefile);
98             }
99         } else {
100             // must be upload file
101             $realfilename = $import_form->get_importfile_realname();
102             if (!$importfile = $import_form->get_importfile_name()) {
103                 print_error('uploadproblem', 'moodle');
104             }else {
105                 $fileisgood = true;
106             }
107         }
109         // process if we are happy file is ok
110         if ($fileisgood) {
112             if (! is_readable("format/$form->format/format.php")) {
113                 print_error('formatnotfound','quiz', $form->format);
114             }
116             require_once("format.php");  // Parent class
117             require_once("format/$form->format/format.php");
119             $classname = "qformat_$form->format";
120             $qformat = new $classname();
122             // load data into class
123             $qformat->setCategory($category);
124             $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
125             $qformat->setCourse($COURSE);
126             $qformat->setFilename($importfile);
127             $qformat->setRealfilename($realfilename);
128             $qformat->setMatchgrades($form->matchgrades);
129             $qformat->setCatfromfile(!empty($form->catfromfile));
130             $qformat->setContextfromfile(!empty($form->contextfromfile));
131             $qformat->setStoponerror($form->stoponerror);
133             // Do anything before that we need to
134             if (! $qformat->importpreprocess()) {
135                 //TODO: need more detailed error info
136                 print_error('cannotimport', '', $thispageurl->out());
137             }
139             // Process the uploaded file
140             if (! $qformat->importprocess()) {
141                 //TODO: need more detailed error info
142                 print_error('cannotimport', '', $thispageurl->out());
143             }
145             // In case anything needs to be done after
146             if (! $qformat->importpostprocess()) {
147                 //TODO: need more detailed error info
148                 print_error('cannotimport', '', $thispageurl->out());
149             }
151             echo "<hr />";
152             echo $OUTPUT->continue_button("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
153             echo $OUTPUT->footer();
154             exit;
155         }
156     }
158     print_heading_with_help($txt->importquestions, "import", "quiz");
160     /// Print upload form
161     $import_form->display();
162     echo $OUTPUT->footer();
164 ?>