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