Moving import/export to new question area.
[moodle.git] / question / import.php
CommitLineData
aca318e1 1<?php // $Id$
2/**
3* Import quiz questions into the given category
4*
5* @version $Id$
6* @author Martin Dougiamas, Howard Miller, and many others.
7* {@link http://moodle.org}
8* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9* @package quiz
10*/
11
12 require_once("../config.php");
13 require_once("locallib.php"); // TODO: this should not need locallib.php
14 require_once($CFG->libdir.'/questionlib.php');
15
16 $categoryid = optional_param('category', 0, PARAM_INT);
17 $courseid = optional_param('course', 0, PARAM_INT);
18 $format = optional_param('format','',PARAM_CLEANFILE);
19
20 if (!$categoryid) { // try to get category from modform
21 $showcatmenu = true; // will ensure that user can choose category
22 if (isset($SESSION->modform)) {
23 $categoryid = $SESSION->modform->category;
24 }
25 }
26
27 if (! $category = get_record("quiz_categories", "id", $categoryid)) {
28 // if no valid category was given, use the default category
29 if ($courseid) {
30 $category = quiz_get_default_category($courseid);
31 } else {
32 error("No category specified");
33 }
34 }
35
36 if (!$courseid) { // need to get the course from the chosen category
37 $courseid = $category->course;
38 }
39
40 if (! $course = get_record("course", "id", $courseid)) {
41 error("Invalid course!");
42 }
43
44 require_login($course->id, false);
45
46 if (!isteacheredit($course->id)) {
47 error("Only editing teachers can import quiz questions!");
48 }
49
50 // ensure the files area exists for this course
51 make_upload_directory( "$course->id" );
52
53 $strimportquestions = get_string("importquestions", "quiz");
54 $strquestions = get_string("questions", "quiz");
55
56 $strquizzes = get_string('modulenameplural', 'quiz');
57 $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
58
59
60 /// Header:
61
62 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
63 $strupdatemodule = isteacheredit($course->id)
64 ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
65 : "";
66 print_header_simple($strimportquestions, '',
67 "<a href=\"index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
68 " -> <a href=\"view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
69 ' -> '.$strimportquestions,
70 "", "", true, $strupdatemodule);
71 $currenttab = 'edit';
72 $mode = 'import';
73 include('tabs.php');
74 } else {
75 print_header_simple($strimportquestions, '',
76 "<a href=\"index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
77 '-> <a href="edit.php">'.get_string('editquestions', 'quiz').'</a>'.
78 ' -> '.$strimportquestions);
79 }
80
81
82 if (!empty($format)) { /// Filename
83
84 if (!confirm_sesskey()) {
85 error( 'sesskey error' );
86 }
87
88 if (empty($_FILES['newfile'])) { // file was just uploaded
89 notify(get_string("uploadproblem") );
90 }
91
92 if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
93 notify(get_string("uploadnofilefound") );
94
95 } else { // Valid file is found
96
97 if (! is_readable("format/$format/format.php")) {
98 error("Format not known ($format)");
99 }
100
101 require("format.php"); // Parent class
102 require("format/$format/format.php");
103
104 $classname = "quiz_format_$format";
105 $quiz_format = new $classname();
106
107 if (! $quiz_format->importpreprocess($category,$course)) { // Do anything before that we need to
108 error("Error occurred during pre-processing!",
109 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
110 }
111
112 if (! $quiz_format->importprocess($_FILES['newfile']['tmp_name'])) { // Process the uploaded file
113 error("Error occurred during processing!",
114 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
115 }
116
117 if (! $quiz_format->importpostprocess()) { // In case anything needs to be done after
118 error("Error occurred during post-processing!",
119 "$CFG->wwwroot/mod/quiz/import.php?category=$category->id");
120 }
121
122 echo "<hr />";
123 print_continue("edit.php");
124 print_footer($course);
125 exit;
126 }
127 }
128
129 /// Print upload form
130
131 // get list of available import formats
132 $fileformatnames = get_import_export_formats( 'import' );
133
134 print_heading_with_help($strimportquestions, "import", "quiz");
135
136 /// Get all the existing categories now
137 if (isadmin()) { // the admin can import into all categories
138 if (!$categories = get_records_select("quiz_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
139 error("Could not find any question categories!"); // Something is really wrong
140 }
141 } else { // select only the categories to which the teacher has write access
142 $sql = "SELECT c.*
143 FROM {$CFG->prefix}quiz_categories AS c,
144 {$CFG->prefix}user_teachers AS t
145 WHERE t.userid = '$USER->id'
146 AND t.course = c.course
147 AND (c.course = '$course->id'
148 OR (c.publish = '1' AND t.editall = '1'))
149 ORDER BY c.parent ASC, c.sortorder ASC, c.name ASC";
150 if (!$categories = get_records_sql($sql)) {
151 error("Could not find any question categories!");
152 }
153 }
154 $categories = add_indented_names($categories);
155 foreach ($categories as $key => $cat) {
156 if ($catcourse = get_record("course", "id", $cat->course)) {
157 if ($cat->publish && $cat->course != $course->id) {
158 $cat->indentedname .= " ($catcourse->shortname)";
159 }
160 $catmenu[$cat->id] = $cat->indentedname;
161 }
162 }
163
164 print_simple_box_start("center");
165 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"import.php\">\n";
166 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />\n";
167 echo "<table cellpadding=\"5\">\n";
168
169 echo "<tr><td align=\"right\">";
170 print_string("category", "quiz");
171 echo ":</td><td>";
172 if (!showcatmenu) { // category already specified
173 echo quiz_get_category_coursename($category);
174 echo " <input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
175 } else { // no category specified, let user choose
176 choose_from_menu($catmenu, "category", $category->id, "");
177 }
178 echo "</tr>\n";
179
180 echo "<tr><td align=\"right\">";
181 print_string("fileformat", "quiz");
182 echo ":</td><td>";
183 choose_from_menu($fileformatnames, "format", "gift", "");
184 helpbutton("import", $strimportquestions, "quiz");
185 echo "</tr>\n";
186
187 echo "<tr><td align=\"right\">";
188 print_string("upload");
189 echo ":</td><td>";
190 require_once($CFG->dirroot.'/lib/uploadlib.php');
191 upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false);
192 echo "</tr><tr><td>&nbsp;</td><td>";
193 echo " <input type=\"submit\" name=\"save\" value=\"".get_string("uploadthisfile")."\" />";
194 echo "</td></tr>\n";
195
196 echo "</table>\n";
197 echo "</form>\n";
198 print_simple_box_end();
199
200 print_footer($course);
201
202?>