Community block MDL-19314 add community block access control (two new capability...
[moodle.git] / question / import.php
CommitLineData
aeb15530 1<?php
aca318e1 2/**
4323d029 3 * Import quiz questions into the given category
4 *
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
10 */
aca318e1 11
12 require_once("../config.php");
56ed242b 13 require_once("editlib.php"); // Includes lib/questionlib.php
3ed998f3 14 require_once($CFG->libdir . '/uploadlib.php');
271e6dec 15 require_once("import_form.php");
aca318e1 16
453b28d8
TH
17 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
18 question_edit_setup('import', '/question/import.php', false, false);
7d43178b 19
271e6dec 20 // get display strings
cbc87100 21 $txt = new stdClass();
ced7916c 22 $txt->importerror = get_string('importerror','quiz');
ced7916c 23 $txt->importquestions = get_string("importquestions", "quiz");
aca318e1 24
271e6dec 25 list($catid, $catcontext) = explode(',', $pagevars['cat']);
f34488b2 26 if (!$category = $DB->get_record("question_categories", array("id" => $catid))) {
7bc26c8f 27 print_error('nocategory','quiz');
aca318e1 28 }
29
56ed242b
SH
30 $PAGE->set_pagelayout('standard');
31
271e6dec 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);
42 }
43 $contexts->require_one_edit_tab_cap($edittab);
aca318e1 44 }
45
aca318e1 46 // ensure the files area exists for this course
271e6dec 47 make_upload_directory("$COURSE->id");
aca318e1 48
5c0daafc 49 $PAGE->set_url($thispageurl->out());
50
271e6dec 51 $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
52 'defaultcategory'=>$pagevars['cat']));
aca318e1 53
271e6dec 54 if ($import_form->is_cancelled()){
55 redirect($thispageurl);
56 }
cbc87100 57 //==========
58 // PAGE HEADER
59 //==========
56ed242b 60 $PAGE->set_title($txt->importquestions);
0f56fde2 61 $PAGE->set_heading($COURSE->fullname);
56ed242b 62 echo $OUTPUT->header();
aca318e1 63
cbc87100 64 // file upload form sumitted
271e6dec 65 if ($form = $import_form->get_data()) {
aca318e1 66
cbc87100 67 // file checks out ok
68 $fileisgood = false;
aca318e1 69
271e6dec 70 // work out if this is an uploaded file
cbc87100 71 // or one from the filesarea.
271e6dec 72 if (!empty($form->choosefile)) {
73 $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
73b7b195 74 $realfilename = $form->choosefile;
cbc87100 75 if (file_exists($importfile)) {
76 $fileisgood = true;
271e6dec 77 } else {
5a2a5331 78 print_error('uploadproblem', 'moodle', $form->choosefile);
cbc87100 79 }
b72ff476 80 } else {
cbc87100 81 // must be upload file
73b7b195 82 $realfilename = $import_form->get_importfile_realname();
271e6dec 83 if (!$importfile = $import_form->get_importfile_name()) {
5a2a5331 84 print_error('uploadproblem', 'moodle');
271e6dec 85 }else {
cbc87100 86 $fileisgood = true;
87 }
aca318e1 88 }
aca318e1 89
cbc87100 90 // process if we are happy file is ok
271e6dec 91 if ($fileisgood) {
aca318e1 92
271e6dec 93 if (! is_readable("format/$form->format/format.php")) {
5a2a5331 94 print_error('formatnotfound','quiz', $form->format);
aca318e1 95 }
96
271e6dec 97 require_once("format.php"); // Parent class
98 require_once("format/$form->format/format.php");
aca318e1 99
271e6dec 100 $classname = "qformat_$form->format";
f5565b69 101 $qformat = new $classname();
aca318e1 102
08892d5b 103 // load data into class
271e6dec 104 $qformat->setCategory($category);
105 $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
106 $qformat->setCourse($COURSE);
107 $qformat->setFilename($importfile);
73b7b195 108 $qformat->setRealfilename($realfilename);
271e6dec 109 $qformat->setMatchgrades($form->matchgrades);
110 $qformat->setCatfromfile(!empty($form->catfromfile));
111 $qformat->setContextfromfile(!empty($form->contextfromfile));
112 $qformat->setStoponerror($form->stoponerror);
08892d5b 113
f3701561 114 // Do anything before that we need to
271e6dec 115 if (! $qformat->importpreprocess()) {
fd9f6cd3 116 //TODO: need more detailed error info
117 print_error('cannotimport', '', $thispageurl->out());
aca318e1 118 }
119
f3701561 120 // Process the uploaded file
271e6dec 121 if (! $qformat->importprocess()) {
fd9f6cd3 122 //TODO: need more detailed error info
123 print_error('cannotimport', '', $thispageurl->out());
aca318e1 124 }
125
f3701561 126 // In case anything needs to be done after
127 if (! $qformat->importpostprocess()) {
bd8ee7c1 128 //TODO: need more detailed error info
129 print_error('cannotimport', '', $thispageurl->out());
aca318e1 130 }
131
132 echo "<hr />";
8afba50b
PS
133 $params = $thispageurl->params() + array('category'=>"{$qformat->category->id},{$qformat->category->contextid}");
134 echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
9b59580b 135 echo $OUTPUT->footer();
aca318e1 136 exit;
137 }
138 }
139
4bcc5118 140 echo $OUTPUT->heading_with_help($txt->importquestions, 'import', 'quiz');
aca318e1 141
271e6dec 142 /// Print upload form
143 $import_form->display();
9b59580b 144 echo $OUTPUT->footer();
aca318e1 145
aeb15530 146