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