3 * Import quiz questions into the given category
5 * @author Martin Dougiamas, Howard Miller, and many others.
6 * {@link http://moodle.org}
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package questionbank
9 * @subpackage importexport
12 require_once("../config.php");
13 require_once("editlib.php" );
14 require_once($CFG->libdir
. '/uploadlib.php');
15 require_once($CFG->libdir
. '/questionlib.php');
17 list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup(false, false);
20 $params = new stdClass
;
21 $params->choosefile
= optional_param('choosefile','',PARAM_PATH
);
22 $catfromfile = optional_param('catfromfile', 0, PARAM_BOOL
);
23 $format = optional_param('format','',PARAM_FILE
);
24 $params->matchgrades
= optional_param('matchgrades','',PARAM_ALPHA
);
25 $params->stoponerror
= optional_param('stoponerror', 0, PARAM_BOOL
);
26 $params->category
= optional_param( 'category', 0, PARAM_INT
);
28 // get display strings
29 $txt = new stdClass();
30 $txt->category
= get_string('category','quiz');
31 $txt->choosefile
= get_string('choosefile','quiz');
32 $txt->file
= get_string('file');
33 $txt->fileformat
= get_string('fileformat','quiz');
34 $txt->fromfile
= get_string('fromfile','quiz');
35 $txt->importcategory
= get_string('importcategory','quiz');
36 $txt->importerror
= get_string('importerror','quiz');
37 $txt->importfilearea
= get_string('importfilearea','quiz');
38 $txt->importfileupload
= get_string('importfileupload','quiz');
39 $txt->importfromthisfile
= get_string('importfromthisfile','quiz');
40 $txt->importquestions
= get_string("importquestions", "quiz");
41 $txt->matchgrades
= get_string('matchgrades','quiz');
42 $txt->matchgradeserror
= get_string('matchgradeserror','quiz');
43 $txt->matchgradesnearest
= get_string('matchgradesnearest','quiz');
44 $txt->modulename
= get_string('modulename','quiz');
45 $txt->modulenameplural
= get_string('modulenameplural','quiz');
46 $txt->onlyteachersimport
= get_string('onlyteachersimport','quiz');
47 $txt->questions
= get_string("questions", "quiz");
48 $txt->quizzes
= get_string('modulenameplural', 'quiz');
49 $txt->stoponerror
= get_string('stoponerror', 'quiz');
50 $txt->upload
= get_string('upload');
51 $txt->uploadproblem
= get_string('uploadproblem');
52 $txt->uploadthisfile
= get_string('uploadthisfile');
55 $matchgrades = array();
56 $matchgrades['error'] = $txt->matchgradeserror
;
57 $matchgrades['nearest'] = $txt->matchgradesnearest
;
59 // not sure where $pagevars['cat'] comes from, but it doesn't respect
60 // the user's choice on the form - so this bodge
61 if (empty($params->category
)) {
62 $params->category
= $pagevars['cat'];
65 if (!$category = get_record("question_categories", "id", $params->category
)) {
66 // if no valid category was given, use the default category
67 print_error('nocategory','quiz');
70 // check category is valid (against THIS courseid, before we change it)
71 $validcats = question_category_options( $cmid, false, true );
72 if (!array_key_exists( $params->category
, $validcats )) {
73 print_error( 'invalidcategory', 'quiz' );
76 $localcourseid = $cmid;
77 $courseid = $category->course
;
79 if (!$course = get_record("course", "id", $courseid)) {
80 error("Invalid course!");
83 require_login($course->id
, false);
85 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
86 require_capability('moodle/question:import', $context);
88 // ensure the files area exists for this course
89 make_upload_directory( "$course->id" );
97 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE
, $course->id
))
98 ?
update_module_button($cm->id
, $course->id
, get_string('modulename', $cm->modname
))
101 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname
), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity');
102 $navlinks[] = array('name' => format_string($module->name
), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title');
103 $navlinks[] = array('name' => $txt->importquestions
, 'link' => '', 'type' => 'title');
104 $navigation = build_navigation($navlinks);
105 print_header_simple($txt->importquestions
, '', $navigation, "", "", true, $strupdatemodule);
107 $currenttab = 'edit';
109 $
{$cm->modname
} = $module;
110 include($CFG->dirroot
."/mod/$cm->modname/tabs.php");
112 // Print basic page layout.
114 $navlinks[] = array('name' => $txt->importquestions
, 'link' => '', 'type' => 'title');
115 $navigation = build_navigation($navlinks);
117 print_header_simple($txt->importquestions
, '', $navigation);
119 $currenttab = 'import';
124 // file upload form sumitted
125 if (!empty($format) and confirm_sesskey() ) {
127 // file checks out ok
130 // work out if this is an uploaded file
131 // or one from the filesarea.
132 if (!empty($params->choosefile
)) {
133 $importfile = "{$CFG->dataroot}/{$course->id}/{$params->choosefile}";
134 if (file_exists($importfile)) {
138 notify($txt->uploadproblem
);
141 // must be upload file
142 if (empty($_FILES['newfile'])) {
143 notify( $txt->uploadproblem
);
145 else if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
146 notify( $txt->uploadproblem
);
149 $importfile = $_FILES['newfile']['tmp_name'];
154 // process if we are happy file is ok
157 if (! is_readable("format/$format/format.php")) {
158 error( get_string('formatnotfound','quiz', $format) );
161 require("format.php"); // Parent class
162 require("format/$format/format.php");
164 $classname = "qformat_$format";
165 $qformat = new $classname();
167 // load data into class
168 $qformat->setCategory( $category );
169 $qformat->setCourse( $course );
170 $qformat->setFilename( $importfile );
171 $qformat->setMatchgrades( $params->matchgrades
);
172 $qformat->setCatfromfile( $catfromfile );
173 $qformat->setStoponerror( $params->stoponerror
);
175 // Do anything before that we need to
176 if (! $qformat->importpreprocess()) {
177 error( $txt->importerror
, $thispageurl->out(false, array('category'=>$category->id
)));
180 // Process the uploaded file
181 if (! $qformat->importprocess() ) {
182 error( $txt->importerror
, $thispageurl->out(false, array('category'=>$category->id
)));
185 // In case anything needs to be done after
186 if (! $qformat->importpostprocess()) {
187 error( $txt->importerror
, $thispageurl->out(false, array('category'=>$category->id
)));
191 print_continue("edit.php?".$thispageurl->get_query_string());
192 print_footer($course);
197 /// Print upload form
199 // get list of available import formats
200 $fileformatnames = get_import_export_formats( 'import' );
202 print_heading_with_help($txt->importquestions
, "import", "quiz");
204 /// Get all the existing categories now
205 $catmenu = question_category_options($course->id
, false, true);
213 <form id
="form" enctype
="multipart/form-data" method
="post" action
="import.php">
214 <fieldset
class="invisiblefieldset" style
="display: block;">
215 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey(); ?>" />
216 <input type
="hidden" name
="courseid" value
="<?php echo $localcourseid ?>" />
217 <?php
echo $thispageurl->hidden_params_out(array(), 3); ?
>
218 <?php
print_simple_box_start("center"); ?
>
219 <table cellpadding
="5">
221 <td align
="right"><?php
echo $txt->category
; ?
>:</td
>
222 <td
><?php
choose_from_menu($catmenu, "category", $category->id
, ""); ?
>
223 <?php
echo $txt->fromfile
; ?
>
224 <input name
="catfromfile" type
="checkbox" />
225 <?php
helpbutton('importcategory', $txt->importcategory
, 'quiz'); ?
></td
>
229 <td align
="right"><?php
echo $txt->fileformat
; ?
>:</td
>
230 <td
><?php
choose_from_menu($fileformatnames, 'format', 'gift', '');
231 helpbutton("import", $txt->importquestions
, 'quiz'); ?
></td
>
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
>
239 <td align
="right"><?php
echo $txt->stoponerror
; ?
></td
>
240 <td
><input name
="stoponerror" type
="checkbox" checked
="checked" />
241 <?php
helpbutton('stoponerror', $txt->stoponerror
, 'quiz'); ?
></td
>
245 print_simple_box_end();
247 print_simple_box_start('center'); ?
>
248 <?php
echo $txt->importfileupload
; ?
>
249 <table cellpadding
="5">
251 <td align
="right"><?php
echo $txt->upload
; ?
>:</td
>
252 <td
><?php
upload_print_form_fragment(1,array('newfile'),null,false,null,$course->maxbytes
,0,false); ?
></td
>
257 <td
><input type
="submit" name
="save" value
="<?php echo $txt->uploadthisfile; ?>" /></td
>
261 print_simple_box_end();
263 print_simple_box_start('center'); ?
>
264 <?php
echo $txt->importfilearea
; ?
>
265 <table cellpadding
="5">
267 <td align
="right"><?php
echo $txt->file
; ?
>:</td
>
268 <td
><input type
="text" name
="choosefile" size
="50" /></td
>
273 <td
><?php
button_to_popup_window ("/files/index.php?id={$course->id}&choose=form.choosefile",
274 "coursefiles", $txt->choosefile
, 500, 750, $txt->choosefile
); ?
>
275 <input type
="submit" name
="save" value
="<?php echo $txt->importfromthisfile; ?>" /></td
>
279 print_simple_box_end(); ?
>
284 print_footer($course);