Automatic installer lang files (20100928)
[moodle.git] / question / import.php
blob7affec34e0d5ff03e4cefa43e3d57f826c58f621
1 <?php
2 /**
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 //this page can be called without courseid or cmid in which case
32 //we get the context from the category object.
33 if ($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);
42 $contexts->require_one_edit_tab_cap($edittab);
45 $PAGE->set_url($thispageurl->out());
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);
53 //==========
54 // PAGE HEADER
55 //==========
56 $PAGE->set_title($txt->importquestions);
57 $PAGE->set_heading($COURSE->fullname);
58 echo $OUTPUT->header();
60 // file upload form sumitted
61 if ($form = $import_form->get_data()) {
63 // file checks out ok
64 $fileisgood = false;
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);
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;
86 // process if we are happy file is ok
87 if ($fileisgood) {
89 if (! is_readable("format/$form->format/format.php")) {
90 print_error('formatnotfound','quiz', $form->format);
93 require_once("format.php"); // Parent class
94 require_once("format/$form->format/format.php");
96 $classname = "qformat_$form->format";
97 $qformat = new $classname();
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);
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());
116 // Process the uploaded file
117 if (! $qformat->importprocess()) {
118 //TODO: need more detailed error info
119 print_error('cannotimport', '', $thispageurl->out());
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());
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;
136 echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
138 /// Print upload form
139 $import_form->display();
140 echo $OUTPUT->footer();