3 * Import quiz questions into the given category
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
12 require_once("../config.php");
13 require_once("editlib.php" );
14 require_once($CFG->libdir
. '/uploadlib.php');
15 require_once($CFG->libdir
. '/questionlib.php');
18 $params = new stdClass
;
19 $params->choosefile
= optional_param('choosefile','',PARAM_PATH
);
20 $categoryid = optional_param('category', 0, PARAM_INT
);
21 $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL
);
22 $courseid = optional_param('course', 0, PARAM_INT
);
23 $format = optional_param('format','',PARAM_FILE
);
24 $params->matchgrades
= optional_param('matchgrades','',PARAM_ALPHA
);
26 // get display strings
27 $txt = new stdClass();
28 $txt->category
= get_string('category','quiz');
29 $txt->choosefile
= get_string('choosefile','quiz');
30 $txt->editingquiz
= get_string(isset($SESSION->modform
->instance
) ?
"editingquiz" : "editquestions", "quiz");
31 $txt->file
= get_string('file');
32 $txt->fileformat
= get_string('fileformat','quiz');
33 $txt->fromfile
= get_string('fromfile','quiz');
34 $txt->importcategory
= get_string('importcategory','quiz');
35 $txt->importerror
= get_string('importerror','quiz');
36 $txt->importfilearea
= get_string('importfilearea','quiz');
37 $txt->importfileupload
= get_string('importfileupload','quiz');
38 $txt->importfromthisfile
= get_string('importfromthisfile','quiz');
39 $txt->importquestions
= get_string("importquestions", "quiz");
40 $txt->matchgrades
= get_string('matchgrades','quiz');
41 $txt->matchgradeserror
= get_string('matchgradeserror','quiz');
42 $txt->matchgradesnearest
= get_string('matchgradesnearest','quiz');
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');
49 $txt->upload
= get_string('upload');
50 $txt->uploadproblem
= get_string('uploadproblem');
51 $txt->uploadthisfile
= get_string('uploadthisfile');
54 $matchgrades = array();
55 $matchgrades['error'] = $txt->matchgradeserror
;
56 $matchgrades['nearest'] = $txt->matchgradesnearest
;
58 if ($categoryid) { // update category in session variable
59 $SESSION->questioncat
= $categoryid;
60 } else { // try to get category from modform
61 $showcatmenu = true; // will ensure that user can choose category
62 if (isset($SESSION->questioncat
)) {
63 $categoryid = $SESSION->questioncat
;
67 if (! $category = get_record("question_categories", "id", $categoryid)) {
68 // if no valid category was given, use the default category
70 $category = get_default_question_category($courseid);
72 error( $txt->nocategory
);
76 if (!$courseid) { // need to get the course from the chosen category
77 $courseid = $category->course
;
80 if (! $course = get_record("course", "id", $courseid)) {
81 error("Invalid course!");
84 require_login($course->id
, false);
86 require_capability('moodle/question:import', get_context_instance(CONTEXT_COURSE
, $course->id
));
88 // ensure the files area exists for this course
89 make_upload_directory( "$course->id" );
96 if (isset($SESSION->modform
->instance
) and $quiz = get_record('quiz', 'id', $SESSION->modform
->instance
)) {
97 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE
, $course->id
))
98 ?
update_module_button($SESSION->modform
->cmid
, $course->id
, $txt->modulename
)
100 print_header_simple($txt->importquestions
, '',
101 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".$txt->modulenameplural
.'</a>'.
102 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name
).'</a>'.
103 ' -> '.$txt->importquestions
,
104 "", "", true, $strupdatemodule);
105 $currenttab = 'edit';
107 include($CFG->dirroot
.'/mod/quiz/tabs.php');
109 print_header_simple($txt->importquestions
, '', $txt->importquestions
);
111 $currenttab = 'import';
116 // file upload form sumitted
117 if (!empty($format) and confirm_sesskey() ) {
119 // file checks out ok
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)) {
130 notify($txt->uploadproblem
);
134 // must be upload file
135 if (empty($_FILES['newfile'])) {
136 notify( $txt->uploadproblem
);
138 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
139 notify( $txt->uploadproblem
);
142 $importfile = $_FILES['newfile']['tmp_name'];
147 // process if we are happy file is ok
150 if (! is_readable("format/$format/format.php")) {
151 error( get_string('formatnotfound','quiz', $format) );
154 require("format.php"); // Parent class
155 require("format/$format/format.php");
157 $classname = "qformat_$format";
158 $qformat = new $classname();
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 );
167 if (! $qformat->importpreprocess()) { // Do anything before that we need to
168 error( $txt->importerror
,
169 "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
172 if (! $qformat->importprocess() ) { // Process the uploaded file
173 error( $txt->importerror
,
174 "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
177 if (! $qformat->importpostprocess()) { // In case anything needs to be done after
178 error( $txt->importerror
,
179 "$CFG->wwwroot/question/import.php?courseid={$course->id}&category=$category->id");
183 print_continue("edit.php?courseid=$course->id");
184 print_footer($course);
189 /// Print upload form
191 // get list of available import formats
192 $fileformatnames = get_import_export_formats( 'import' );
194 print_heading_with_help($txt->importquestions
, "import", "quiz");
196 /// Get all the existing categories now
197 if (has_capability('moodle/question:manage', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) { // the admin can import into all categories
198 if (!$categories = get_records_select("question_categories", "course = '{$course->id}' OR publish = '1'", "parent, sortorder, name ASC")) {
199 error("Could not find any question categories!"); // Something is really wrong
201 } else { // select only the categories to which the teacher has write access
202 $cats = get_records('question_categories');
203 $categories = array();
204 foreach ($cats as $cat) {
205 if (has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE
, $cat->course
))) {
206 $categories[] = $cat;
209 if (empty($categories)) {
210 error("Could not find any question categories!");
213 $categories = add_indented_names($categories);
214 foreach ($categories as $key => $cat) {
215 if ($catcourse = get_record("course", "id", $cat->course
)) {
216 if ($cat->publish
&& $cat->course
!= $course->id
) {
217 $cat->indentedname
.= " ($catcourse->shortname)";
219 $catmenu[$cat->id
] = $cat->indentedname
;
229 <form id
="form" enctype
="multipart/form-data" method
="post" action
="import.php">
230 <fieldset
class="invisiblefieldset">
231 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey(); ?>" />
232 <?php
print_simple_box_start("center"); ?
>
233 <table cellpadding
="5">
235 <td align
="right"><?php
echo $txt->category
; ?
>:</td
>
236 <td
><?php
choose_from_menu($catmenu, "category", $category->id
, ""); ?
>
237 <?php
echo $txt->fromfile
; ?
>
238 <input name
="catfromfile" type
="checkbox" />
239 <?php
helpbutton('importcategory', $txt->importcategory
, 'quiz'); ?
></td
>
243 <td align
="right"><?php
echo $txt->fileformat
; ?
>:</td
>
244 <td
><?php
choose_from_menu($fileformatnames, "format", "gift", "");
245 helpbutton("import", $txt->importquestions
, "quiz"); ?
></td
>
248 <td align
="right"><?php
echo $txt->matchgrades
; ?
></td
>
249 <td
><?php
choose_from_menu($matchgrades,'matchgrades',$txt->matchgradeserror
,'' );
250 helpbutton('matchgrades', $txt->matchgrades
, 'quiz'); ?
></td
>
254 print_simple_box_end();
256 print_simple_box_start('center'); ?
>
257 <?php
echo $txt->importfileupload
; ?
>
258 <table cellpadding
="5">
260 <td align
="right"><?php
echo $txt->upload
; ?
>:</td
>
261 <td
><?php
upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes
,0,false); ?
></td
>
266 <td
><input type
="submit" name
="save" value
="<?php echo $txt->uploadthisfile; ?>" /></td
>
270 print_simple_box_end();
272 print_simple_box_start('center'); ?
>
273 <?php
echo $txt->importfilearea
; ?
>
274 <table cellpadding
="5">
276 <td align
="right"><?php
echo $txt->file
; ?
>:</td
>
277 <td
><input type
="text" name
="choosefile" size
="50" /></td
>
282 <td
><?php
button_to_popup_window ("/files/index.php?id={$course->id}&choose=form.choosefile",
283 "coursefiles", $txt->choosefile
, 500, 750, $txt->choosefile
); ?
>
284 <input type
="submit" name
="save" value
="<?php echo $txt->importfromthisfile; ?>" /></td
>
288 print_simple_box_end(); ?
>
293 print_footer($course);