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