Automatic installer.php lang files by installer_builder (20090119)
[moodle.git] / question / import.php
blobf75474146f47b5cc634fc9e0896e1a38ff55dc59
1 <?php // $Id$
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");
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->importquestions = get_string("importquestions", "quiz");
24 list($catid, $catcontext) = explode(',', $pagevars['cat']);
25 if (!$category = get_record("question_categories", "id", $catid)) {
26 print_error('nocategory','quiz');
29 //this page can be called without courseid or cmid in which case
30 //we get the context from the category object.
31 if ($contexts === null) { // need to get the course from the chosen category
32 $contexts = new question_edit_contexts(get_context_instance_by_id($category->contextid));
33 $thiscontext = $contexts->lowest();
34 if ($thiscontext->contextlevel == CONTEXT_COURSE){
35 require_login($thiscontext->instanceid, false);
36 } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
37 list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
38 require_login($cm->course, false, $cm);
40 $contexts->require_one_edit_tab_cap($edittab);
43 // ensure the files area exists for this course
44 make_upload_directory("$COURSE->id");
46 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
47 'defaultcategory'=>$pagevars['cat']));
49 if ($import_form->is_cancelled()){
50 redirect($thispageurl);
52 //==========
53 // PAGE HEADER
54 //==========
56 if ($cm!==null) {
57 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
58 ? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
59 : "";
60 $navlinks = array();
61 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id", 'type' => 'activity');
62 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
63 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
64 $navigation = build_navigation($navlinks);
65 print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule);
67 $currenttab = 'edit';
68 $mode = 'import';
69 ${$cm->modname} = $module;
70 include($CFG->dirroot."/mod/$cm->modname/tabs.php");
71 } else {
72 // Print basic page layout.
73 $navlinks = array();
74 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
75 $navigation = build_navigation($navlinks);
77 print_header_simple($txt->importquestions, '', $navigation);
78 // print tabs
79 $currenttab = 'import';
80 include('tabs.php');
84 // file upload form sumitted
85 if ($form = $import_form->get_data()) {
87 // file checks out ok
88 $fileisgood = false;
90 // work out if this is an uploaded file
91 // or one from the filesarea.
92 if (!empty($form->choosefile)) {
93 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
94 $realfilename = $form->choosefile;
95 if (file_exists($importfile)) {
96 $fileisgood = true;
97 } else {
98 print_error('uploadproblem', 'moodle', $form->choosefile);
100 } else {
101 // must be upload file
102 $realfilename = $import_form->get_importfile_realname();
103 if (!$importfile = $import_form->get_importfile_name()) {
104 print_error('uploadproblem', 'moodle');
105 }else {
106 $fileisgood = true;
110 // process if we are happy file is ok
111 if ($fileisgood) {
113 if (! is_readable("format/$form->format/format.php")) {
114 print_error('formatnotfound','quiz', $form->format);
117 require_once("format.php"); // Parent class
118 require_once("format/$form->format/format.php");
120 $classname = "qformat_$form->format";
121 $qformat = new $classname();
123 // load data into class
124 $qformat->setCategory($category);
125 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
126 $qformat->setCourse($COURSE);
127 $qformat->setFilename($importfile);
128 $qformat->setRealfilename($realfilename);
129 $qformat->setMatchgrades($form->matchgrades);
130 $qformat->setCatfromfile(!empty($form->catfromfile));
131 $qformat->setContextfromfile(!empty($form->contextfromfile));
132 $qformat->setStoponerror($form->stoponerror);
134 // Do anything before that we need to
135 if (! $qformat->importpreprocess()) {
136 print_error('importerror', 'quiz', $thispageurl->out());
139 // Process the uploaded file
140 if (! $qformat->importprocess()) {
141 print_error('importerror', 'quiz', $thispageurl->out());
144 // In case anything needs to be done after
145 if (! $qformat->importpostprocess()) {
146 print_error('importerror', 'quiz', $thispageurl->out());
149 echo "<hr />";
150 print_continue("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
151 print_footer($COURSE);
152 exit;
156 print_heading_with_help($txt->importquestions, "import", "quiz");
158 /// Print upload form
159 $import_form->display();
160 print_footer($COURSE);