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