merging MOODLE_19_QUESTIONS with HEAD
[moodle.git] / question / export.php
CommitLineData
aca318e1 1<?php // $Id$
2/**
4323d029 3 * Export quiz questions into the given category
4 *
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
10 */
aca318e1 11
12 require_once("../config.php");
4fbfd971 13 require_once( "editlib.php" );
aca318e1 14
b72ff476 15 list($thispageurl, $courseid, $cmid, $cm, $module, $pagevars) = question_edit_setup();
16
f1abd39f 17 $cattofile = optional_param('cattofile',0, PARAM_BOOL);
b72ff476 18
5a4bcbc3 19 $exportfilename = optional_param('exportfilename','',PARAM_FILE );
f1abd39f 20 $format = optional_param('format','', PARAM_FILE );
46d79818 21 $categoryid = optional_param('category',0,PARAM_INT);
d187f660 22
f1abd39f 23 // get display strings
24 $txt = new object;
25 $txt->category = get_string('category','quiz');
26 $txt->download = get_string('download','quiz');
27 $txt->downloadextra = get_string('downloadextra','quiz');
28 $txt->exporterror = get_string('exporterror','quiz');
29 $txt->exportname = get_string('exportname','quiz');
30 $txt->exportquestions = get_string('exportquestions', 'quiz');
31 $txt->fileformat = get_string('fileformat','quiz');
32 $txt->exportcategory = get_string('exportcategory','quiz');
33 $txt->modulename = get_string('modulename','quiz');
34 $txt->modulenameplural = get_string('modulenameplural','quiz');
f1abd39f 35 $txt->tofile = get_string('tofile','quiz');
36
aca318e1 37
d187f660 38 if (!$course = get_record("course", "id", $courseid)) {
aca318e1 39 error("Course does not exist!");
40 }
0c1e3a14 41
46d79818 42 // make sure we are using the user's most recent category choice
43 if (empty($categoryid)) {
44 $categoryid = $pagevars['cat'];
45 }
aca318e1 46
46d79818 47 if (!$category = get_record("question_categories", "id", $categoryid)) {
d187f660 48 $category = get_default_question_category($courseid);
49 }
50
51 if (!$categorycourse = get_record("course", "id", $category->course)) {
52 print_error('nocategory','quiz');
aca318e1 53 }
54
d187f660 55
f1abd39f 56 // check role capability
d02eeded 57 $context = get_context_instance(CONTEXT_COURSE, $course->id);
58 require_capability('moodle/question:export', $context);
aca318e1 59
60 // ensure the files area exists for this course
61 make_upload_directory( "$course->id" );
62
22afe6d6 63 // check category is valid
46d79818 64 $validcats = question_category_options( $course->id, true, false );
65 if (!array_key_exists( $categoryid, $validcats)) {
66 print_error( 'invalidcategory','quiz' );
22afe6d6 67 }
68
f1abd39f 69 /// Header
b72ff476 70 if ($cm!==null) {
71 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))
72 ? update_module_button($cm->id, $course->id, get_string('modulename', $cm->modname))
aca318e1 73 : "";
3b27b0fe 74 $navlinks = array();
75 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$course->id", 'type' => 'activity');
76 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title');
77 $navlinks[] = array('name' => $txt->exportquestions, 'link' => '', 'type' => 'title');
78 $navigation = build_navigation($navlinks);
b72ff476 79 print_header_simple($txt->exportquestions, '', $navigation, "", "", true, $strupdatemodule);
80
aca318e1 81 $currenttab = 'edit';
82 $mode = 'export';
b72ff476 83 ${$cm->modname} = $module;
84 include($CFG->dirroot."/mod/$cm->modname/tabs.php");
aca318e1 85 } else {
b72ff476 86 // Print basic page layout.
3b27b0fe 87 $navlinks = array();
88 $navlinks[] = array('name' => $txt->exportquestions, 'link' => '', 'type' => 'title');
89 $navigation = build_navigation($navlinks);
b72ff476 90
91 print_header_simple($txt->exportquestions, '', $navigation);
567709c4 92 // print tabs
93 $currenttab = 'export';
94 include('tabs.php');
aca318e1 95 }
96
97 if (!empty($format)) { /// Filename
98
99 if (!confirm_sesskey()) {
f1abd39f 100 print_error( 'sesskey' );
aca318e1 101 }
102
103 if (! is_readable("format/$format/format.php")) {
f1abd39f 104 error( "Format not known ($format)" ); }
aca318e1 105
f1abd39f 106 // load parent class for import/export
d187f660 107 require("format.php");
108
f1abd39f 109 // and then the class for the selected format
aca318e1 110 require("format/$format/format.php");
111
f5565b69 112 $classname = "qformat_$format";
113 $qformat = new $classname();
aca318e1 114
08892d5b 115 $qformat->setCategory( $category );
116 $qformat->setCourse( $course );
117 $qformat->setFilename( $exportfilename );
f1abd39f 118 $qformat->setCattofile( $cattofile );
08892d5b 119
120 if (! $qformat->exportpreprocess()) { // Do anything before that we need to
7bc26c8f 121 error( $txt->exporterror, $thispageurl->out());
aca318e1 122 }
123
08892d5b 124 if (! $qformat->exportprocess()) { // Process the export data
7bc26c8f 125 error( $txt->exporterror, $thispageurl->out());
aca318e1 126 }
127
f5565b69 128 if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
7bc26c8f 129 error( $txt->exporterror, $thispageurl->out());
aca318e1 130 }
131 echo "<hr />";
132
133 // link to download the finished file
f5565b69 134 $file_ext = $qformat->export_file_extension();
aca318e1 135 if ($CFG->slasharguments) {
1367cb8d 136 $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
aca318e1 137 }
138 else {
1367cb8d 139 $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
aca318e1 140 }
c37385bb 141 echo "<p><div class=\"boxaligncenter\"><a href=\"$efile\">$txt->download</a></div></p>";
142 echo "<p><div class=\"boxaligncenter\"><font size=\"-1\">$txt->downloadextra</font></div></p>";
aca318e1 143
b72ff476 144 print_continue("edit.php?".$thispageurl->get_query_string());
aca318e1 145 print_footer($course);
146 exit;
147 }
148
f1abd39f 149 /// Display upload form
aca318e1 150
151 // get valid formats to generate dropdown list
f1abd39f 152 $fileformatnames = get_import_export_formats( 'export' );
aca318e1 153
154 // get filename
155 if (empty($exportfilename)) {
156 $exportfilename = default_export_filename($course, $category);
157 }
158
f1abd39f 159 print_heading_with_help($txt->exportquestions, 'export', 'quiz');
d187f660 160 print_simple_box_start('center');
161?>
aca318e1 162
f1abd39f 163 <form enctype="multipart/form-data" method="post" action="export.php">
d187f660 164 <fieldset class="invisiblefieldset" style="display: block;">
165 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
b72ff476 166 <?php echo $thispageurl->hidden_params_out(array(), 3); ?>
f1abd39f 167 <table cellpadding="5">
168 <tr>
169 <td align="right"><?php echo $txt->category; ?>:</td>
170 <td>
d187f660 171 <?php
172 question_category_select_menu($course->id, true, false, $category->id);
173 echo $txt->tofile; ?>
174 <input name="cattofile" type="checkbox" />
175 <?php helpbutton('exportcategory', $txt->exportcategory, 'quiz'); ?>
176 </td>
177 </tr>
178 <tr>
179 <td align="right"><?php echo $txt->fileformat; ?>:</td>
180 <td>
181 <?php choose_from_menu($fileformatnames, 'format', 'gift', '');
182 helpbutton('export', $txt->exportquestions, 'quiz'); ?>
183 </td>
184 </tr>
185 <tr>
186 <td align="right"><?php echo $txt->exportname; ?>:</td>
187 <td>
188 <input type="text" size="40" name="exportfilename" value="<?php echo $exportfilename; ?>" />
189 </td>
190 </tr>
191 <tr>
192 <td align="center" >
193 <input type="submit" name="save" value="<?php echo $txt->exportquestions; ?>" />
194 </td>
195 <td>&nbsp;</td>
196 </tr>
197 </table>
198 </fieldset>
199 </form>
f1abd39f 200 <?php
d187f660 201
aca318e1 202 print_simple_box_end();
aca318e1 203 print_footer($course);
f1abd39f 204?>
aca318e1 205