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($CFG->libdir
. '/uploadlib.php');
15 require_once("import_form.php");
17 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
18 question_edit_setup('import', '/question/import.php', 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');
30 $PAGE->set_pagelayout('standard');
32 //this page can be called without courseid or cmid in which case
33 //we get the context from the category object.
34 if ($contexts === null) { // need to get the course from the chosen category
35 $contexts = new question_edit_contexts(get_context_instance_by_id($category->contextid
));
36 $thiscontext = $contexts->lowest();
37 if ($thiscontext->contextlevel
== CONTEXT_COURSE
){
38 require_login($thiscontext->instanceid
, false);
39 } elseif ($thiscontext->contextlevel
== CONTEXT_MODULE
){
40 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid
);
41 require_login($cm->course
, false, $cm);
43 $contexts->require_one_edit_tab_cap($edittab);
46 // ensure the files area exists for this course
47 make_upload_directory("$COURSE->id");
49 $PAGE->set_url($thispageurl->out());
51 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
52 'defaultcategory'=>$pagevars['cat']));
54 if ($import_form->is_cancelled()){
55 redirect($thispageurl);
60 $PAGE->set_title($txt->importquestions
);
61 $PAGE->set_heading($COURSE->fullname
);
62 echo $OUTPUT->header();
64 // file upload form sumitted
65 if ($form = $import_form->get_data()) {
70 // work out if this is an uploaded file
71 // or one from the filesarea.
72 if (!empty($form->choosefile
)) {
73 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
74 $realfilename = $form->choosefile
;
75 if (file_exists($importfile)) {
78 print_error('uploadproblem', 'moodle', $form->choosefile
);
81 $realfilename = $import_form->get_new_filename('newfile');
82 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$realfilename}";
83 if (!$result = $import_form->save_file('newfile', $importfile, true)) {
84 print_error('uploadproblem', 'moodle');
90 // process if we are happy file is ok
93 if (! is_readable("format/$form->format/format.php")) {
94 print_error('formatnotfound','quiz', $form->format
);
97 require_once("format.php"); // Parent class
98 require_once("format/$form->format/format.php");
100 $classname = "qformat_$form->format";
101 $qformat = new $classname();
103 // load data into class
104 $qformat->setCategory($category);
105 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
106 $qformat->setCourse($COURSE);
107 $qformat->setFilename($importfile);
108 $qformat->setRealfilename($realfilename);
109 $qformat->setMatchgrades($form->matchgrades
);
110 $qformat->setCatfromfile(!empty($form->catfromfile
));
111 $qformat->setContextfromfile(!empty($form->contextfromfile
));
112 $qformat->setStoponerror($form->stoponerror
);
114 // Do anything before that we need to
115 if (! $qformat->importpreprocess()) {
116 //TODO: need more detailed error info
117 print_error('cannotimport', '', $thispageurl->out());
120 // Process the uploaded file
121 if (! $qformat->importprocess()) {
122 //TODO: need more detailed error info
123 print_error('cannotimport', '', $thispageurl->out());
126 // In case anything needs to be done after
127 if (! $qformat->importpostprocess()) {
128 //TODO: need more detailed error info
129 print_error('cannotimport', '', $thispageurl->out());
133 $params = $thispageurl->params() +
array('category'=>"{$qformat->category->id},{$qformat->category->contextid}");
134 echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
135 echo $OUTPUT->footer();
140 echo $OUTPUT->heading_with_help($txt->importquestions
, 'import', 'quiz');
142 /// Print upload form
143 $import_form->display();
144 echo $OUTPUT->footer();