Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / question / import.php
bloba763f375cf1e9c268810c1231e18bc84f441fed0
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->importerror = get_string('importerror','quiz');
23 $txt->importquestions = get_string("importquestions", "quiz");
25 list($catid, $catcontext) = explode(',', $pagevars['cat']);
26 if (!$category = get_record("question_categories", "id", $catid)) {
27 print_error('nocategory','quiz');
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);
41 $contexts->require_one_edit_tab_cap($edittab);
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);
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 $navlinks = array();
62 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id", 'type' => 'activity');
63 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
64 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
65 $navigation = build_navigation($navlinks);
66 print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule);
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 $navlinks = array();
75 $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
76 $navigation = build_navigation($navlinks);
78 print_header_simple($txt->importquestions, '', $navigation);
79 // print tabs
80 $currenttab = 'import';
81 include('tabs.php');
85 // file upload form sumitted
86 if ($form = $import_form->get_data()) {
88 // file checks out ok
89 $fileisgood = false;
91 // work out if this is an uploaded file
92 // or one from the filesarea.
93 if (!empty($form->choosefile)) {
94 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$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 if (!$importfile = $import_form->get_importfile_name()) {
103 print_error('uploadproblem', 'moodle');
104 }else {
105 $fileisgood = true;
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);
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->setMatchgrades($form->matchgrades);
128 $qformat->setCatfromfile(!empty($form->catfromfile));
129 $qformat->setContextfromfile(!empty($form->contextfromfile));
130 $qformat->setStoponerror($form->stoponerror);
132 // Do anything before that we need to
133 if (! $qformat->importpreprocess()) {
134 error($txt->importerror, $thispageurl->out());
137 // Process the uploaded file
138 if (! $qformat->importprocess()) {
139 error($txt->importerror, $thispageurl->out());
142 // In case anything needs to be done after
143 if (! $qformat->importpostprocess()) {
144 error($txt->importerror, $thispageurl->out());
147 echo "<hr />";
148 print_continue("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
149 print_footer($COURSE);
150 exit;
154 print_heading_with_help($txt->importquestions, "import", "quiz");
156 /// Print upload form
157 $import_form->display();
158 print_footer($COURSE);