Added function for matching/checking question grades on quiz import.
[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');
cbc87100 14 require_once($CFG->dirroot.'/lib/uploadlib.php');
aca318e1 15
cbc87100 16 // get parameters
17 $params = new stdClass;
18 $params->choosefile = optional_param('choosefile','',PARAM_PATH);
aca318e1 19 $categoryid = optional_param('category', 0, PARAM_INT);
20 $courseid = optional_param('course', 0, PARAM_INT);
5a4bcbc3 21 $format = optional_param('format','',PARAM_FILE);
aca318e1 22
cbc87100 23 // get display strings
24 $txt = new stdClass();
25 $txt->category = get_string('category','quiz');
26 $txt->choosefile = get_string('choosefile','quiz');
ced7916c 27 $txt->editingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
cbc87100 28 $txt->file = get_string('file');
29 $txt->fileformat = get_string('fileformat','quiz');
ced7916c 30 $txt->importerror = get_string('importerror','quiz');
cbc87100 31 $txt->importfilearea = get_string('importfilearea','quiz');
32 $txt->importfileupload = get_string('importfileupload','quiz');
ced7916c 33 $txt->importquestions = get_string("importquestions", "quiz");
34 $txt->modulename = get_string('modulename','quiz');
35 $txt->modulenameplural = get_string('modulenameplural','quiz');
36 $txt->nocategory = get_string('nocategory','quiz');
37 $txt->onlyteachersimport = get_string('onlyteachersimport','quiz');
38 $txt->questions = get_string("questions", "quiz");
39 $txt->quizzes = get_string('modulenameplural', 'quiz');
cbc87100 40 $txt->upload = get_string('upload');
41 $txt->uploadproblem = get_string('uploadproblem');
42 $txt->uploadthisfile = get_string('uploadthisfile');
43
aca318e1 44 if (!$categoryid) { // try to get category from modform
45 $showcatmenu = true; // will ensure that user can choose category
46 if (isset($SESSION->modform)) {
47 $categoryid = $SESSION->modform->category;
48 }
49 }
50
dc1f00de 51 if (! $category = get_record("question_categories", "id", $categoryid)) {
aca318e1 52 // if no valid category was given, use the default category
53 if ($courseid) {
062f1125 54 $category = get_default_question_category($courseid);
aca318e1 55 } else {
ced7916c 56 error( $txt->nocategory );
aca318e1 57 }
58 }
59
60 if (!$courseid) { // need to get the course from the chosen category
61 $courseid = $category->course;
62 }
63
64 if (! $course = get_record("course", "id", $courseid)) {
65 error("Invalid course!");
66 }
67
68 require_login($course->id, false);
69
70 if (!isteacheredit($course->id)) {
ced7916c 71 error( $txt->onlyteachersimport );
aca318e1 72 }
73
74 // ensure the files area exists for this course
75 make_upload_directory( "$course->id" );
76
aca318e1 77
cbc87100 78 //==========
79 // PAGE HEADER
80 //==========
aca318e1 81
82 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
83 $strupdatemodule = isteacheredit($course->id)
ced7916c 84 ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename)
aca318e1 85 : "";
ced7916c 86 print_header_simple($txt->importquestions, '',
87 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".$txt->modulenameplural.'</a>'.
70c01adb 88 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
ced7916c 89 ' -> '.$txt->importquestions,
aca318e1 90 "", "", true, $strupdatemodule);
91 $currenttab = 'edit';
92 $mode = 'import';
70c01adb 93 include($CFG->dirroot.'/mod/quiz/tabs.php');
aca318e1 94 } else {
ced7916c 95 print_header_simple($txt->importquestions, '', $txt->importquestions);
567709c4 96 // print tabs
97 $currenttab = 'import';
98 include('tabs.php');
aca318e1 99 }
100
101
cbc87100 102 // file upload form sumitted
103 if (!empty($format) and confirm_sesskey() ) {
aca318e1 104
cbc87100 105 // file checks out ok
106 $fileisgood = false;
aca318e1 107
cbc87100 108 // work out if this is an uploaded file
109 // or one from the filesarea.
110 if (!empty($params->choosefile)) {
111 $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}";
112 if (file_exists($importfile)) {
113 $fileisgood = true;
114 }
115 else {
116 notify($txt->uploadproblem);
117 }
118 }
119 else {
120 // must be upload file
121 if (empty($_FILES['newfile'])) {
122 notify( $txt->uploadproblem );
123 }
124 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
125 notify( $txt->uploadproblem );
126 }
127 else {
128 $importfile = $_FILES['newfile']['tmp_name'];
129 $fileisgood = true;
130 }
aca318e1 131 }
aca318e1 132
cbc87100 133 // process if we are happy file is ok
134 if ($fileisgood) {
aca318e1 135
136 if (! is_readable("format/$format/format.php")) {
1e3d6fd8 137 error( get_string('formatnotfound','quiz', $format) );
aca318e1 138 }
139
140 require("format.php"); // Parent class
141 require("format/$format/format.php");
142
f5565b69 143 $classname = "qformat_$format";
144 $qformat = new $classname();
aca318e1 145
f5565b69 146 if (! $qformat->importpreprocess($category,$course)) { // Do anything before that we need to
ced7916c 147 error( $txt->importerror ,
1e3d6fd8 148 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 149 }
150
cbc87100 151 if (! $qformat->importprocess($importfile) ) { // Process the uploaded file
ced7916c 152 error( $txt->importerror ,
1e3d6fd8 153 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 154 }
155
f5565b69 156 if (! $qformat->importpostprocess()) { // In case anything needs to be done after
ced7916c 157 error( $txt->importerror ,
1e3d6fd8 158 "$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 159 }
160
161 echo "<hr />";
c4c06b67 162 print_continue("edit.php?courseid=$course->id");
aca318e1 163 print_footer($course);
164 exit;
165 }
166 }
167
168 /// Print upload form
169
170 // get list of available import formats
171 $fileformatnames = get_import_export_formats( 'import' );
172
ced7916c 173 print_heading_with_help($txt->importquestions, "import", "quiz");
aca318e1 174
175 /// Get all the existing categories now
176 if (isadmin()) { // the admin can import into all categories
dc1f00de 177 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
aca318e1 178 error("Could not find any question categories!"); // Something is really wrong
179 }
180 } else { // select only the categories to which the teacher has write access
181 $sql = "SELECT c.*
dc1f00de 182 FROM {$CFG->prefix}question_categories AS c,
aca318e1 183 {$CFG->prefix}user_teachers AS t
184 WHERE t.userid = '$USER->id'
185 AND t.course = c.course
186 AND (c.course = '$course->id'
187 OR (c.publish = '1' AND t.editall = '1'))
188 ORDER BY c.parent ASC, c.sortorder ASC, c.name ASC";
189 if (!$categories = get_records_sql($sql)) {
190 error("Could not find any question categories!");
191 }
192 }
193 $categories = add_indented_names($categories);
194 foreach ($categories as $key => $cat) {
195 if ($catcourse = get_record("course", "id", $cat->course)) {
196 if ($cat->publish && $cat->course != $course->id) {
197 $cat->indentedname .= " ($catcourse->shortname)";
198 }
199 $catmenu[$cat->id] = $cat->indentedname;
200 }
201 }
cbc87100 202
203 //==========
204 // DISPLAY
205 //==========
206
207 ?>
208
209 <form name="form" enctype="multipart/form-data" method="post" action="import.php">
210 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
211 <?php print_simple_box_start("center"); ?>
212 <table cellpadding="5">
213 <tr>
214 <td align="right"><?php echo $txt->category; ?>:</td>
215 <td><?php choose_from_menu($catmenu, "category", $category->id, ""); ?></td>
216 </tr>
217
218 <tr>
219 <td align="right"><?php echo $txt->fileformat; ?>:</td>
220 <td><?php choose_from_menu($fileformatnames, "format", "gift", "");
ced7916c 221 helpbutton("import", $txt->importquestions, "quiz"); ?></td>
cbc87100 222 </tr>
223 </table>
224 <?php
225 print_simple_box_end();
226
227 print_simple_box_start('center'); ?>
228 <?php echo $txt->importfileupload; ?>
229 <table cellpadding="5">
230 <tr>
231 <td align="right"><?php echo $txt->upload; ?>:</td>
232 <td><?php upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes,0,false); ?></td>
233 </tr>
234
235 <tr>
236 <td>&nbsp;</td>
237 <td><input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
238 </tr>
239 </table>
240 <?php
241 print_simple_box_end();
242
243 print_simple_box_start('center'); ?>
244 <?php echo $txt->importfilearea; ?>
245 <table cellpadding="5">
246 <tr>
247 <td align="right"><?php echo $txt->file; ?>:</td>
248 <td><input type="text" name="choosefile" size="50" /></td>
249 </tr>
250
251 <tr>
252 <td>&nbsp;</td>
253 <td><?php button_to_popup_window ("/files/index.php?id={$course->id}&amp;choose=form.choosefile",
254 "coursefiles", $txt->choosefile, 500, 750, $txt->choosefile); ?>
255 <input type="submit" name="save" value="<?php echo $txt->uploadthisfile; ?>" /></td>
256 </tr>
257 </table>
258 <?php
259 print_simple_box_end(); ?>
260 </form>
261
262 <?php
aca318e1 263 print_footer($course);
264
265?>