3 * Import quiz questions into the given category
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
12 require_once("../config.php");
13 require_once("editlib.php"); // Includes lib/questionlib.php
14 require_once("import_form.php");
16 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
17 question_edit_setup('import', '/question/import.php', false, false);
19 // get display strings
20 $txt = new stdClass();
21 $txt->importerror
= get_string('importerror','quiz');
22 $txt->importquestions
= get_string('importquestions', 'question');
24 list($catid, $catcontext) = explode(',', $pagevars['cat']);
25 if (!$category = $DB->get_record("question_categories", array("id" => $catid))) {
26 print_error('nocategory','quiz');
29 $PAGE->set_pagelayout('standard');
31 $categorycontext = get_context_instance_by_id($category->contextid
);
32 $category->context
= $categorycontext;
33 //this page can be called without courseid or cmid in which case
34 //we get the context from the category object.
35 if ($contexts === null) { // need to get the course from the chosen category
36 $contexts = new question_edit_contexts($categorycontext);
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);
44 $contexts->require_one_edit_tab_cap($edittab);
47 $PAGE->set_url($thispageurl->out());
49 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
50 'defaultcategory'=>$pagevars['cat']));
52 if ($import_form->is_cancelled()){
53 redirect($thispageurl);
58 $PAGE->set_title($txt->importquestions
);
59 $PAGE->set_heading($COURSE->fullname
);
60 echo $OUTPUT->header();
62 // file upload form sumitted
63 if ($form = $import_form->get_data()) {
68 // work out if this is an uploaded file
69 // or one from the filesarea.
70 $realfilename = $import_form->get_new_filename('newfile');
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');
80 // process if we are happy file is ok
83 if (! is_readable("format/$form->format/format.php")) {
84 print_error('formatnotfound','quiz', $form->format
);
87 require_once("format.php"); // Parent class
88 require_once("format/$form->format/format.php");
90 $classname = "qformat_$form->format";
91 $qformat = new $classname();
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
);
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());
110 // Process the uploaded file
111 if (! $qformat->importprocess($category)) {
112 //TODO: need more detailed error info
113 print_error('cannotimport', '', $thispageurl->out());
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());
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();
130 echo $OUTPUT->heading_with_help($txt->importquestions
, 'importquestions', 'question');
132 /// Print upload form
133 $import_form->display();
134 echo $OUTPUT->footer();