MDL-9123:
[moodle-linuxchix.git] / question / export.php
blobfe8401032a9358110ac0cd9c6651bf93b5056fa7
1 <?php // $Id$
2 /**
3 * Export 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" );
15 // get parameters
16 $categoryid = optional_param('category',0, PARAM_INT);
17 $cattofile = optional_param('cattofile',0, PARAM_BOOL);
18 $courseid = required_param('courseid',PARAM_INT);
19 $exportfilename = optional_param('exportfilename','',PARAM_FILE );
20 $format = optional_param('format','', PARAM_FILE );
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');
35 $txt->tofile = get_string('tofile','quiz');
38 if (!$course = get_record("course", "id", $courseid)) {
39 error("Course does not exist!");
42 if ($categoryid) { // update category in session variable
43 $SESSION->questioncat = $categoryid;
44 } else { // try to get category from modform
45 if (isset($SESSION->questioncat)) {
46 $categoryid = $SESSION->questioncat;
50 if (!$category = get_record("question_categories", "id", $categoryid)) {
51 $category = get_default_question_category($courseid);
54 if (!$categorycourse = get_record("course", "id", $category->course)) {
55 print_error('nocategory','quiz');
58 require_login($course->id, false);
60 // check role capability
61 $context = get_context_instance(CONTEXT_COURSE, $course->id);
62 require_capability('moodle/question:export', $context);
64 // ensure the files area exists for this course
65 make_upload_directory( "$course->id" );
67 // check category is valid
68 if (!empty($categoryid)) {
69 $validcats = question_category_options( $course->id, true, false );
70 if (!array_key_exists( $categoryid, $validcats)) {
71 print_error( "Category id ($categoryid) is not permitted." );
75 /// Header
76 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
77 $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
78 ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename )
79 : "";
80 print_header_simple($txt->exportquestions, '',
81 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">$txt->modulenameplural</a>".
82 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
83 ' -> '.$txt->exportquestions,
84 "", "", true, $strupdatemodule);
85 $currenttab = 'edit';
86 $mode = 'export';
87 include($CFG->dirroot.'/mod/quiz/tabs.php');
88 } else {
89 print_header_simple($txt->exportquestions, '', $txt->exportquestions);
90 // print tabs
91 $currenttab = 'export';
92 include('tabs.php');
95 if (!empty($format)) { /// Filename
97 if (!confirm_sesskey()) {
98 print_error( 'sesskey' );
101 if (! is_readable("format/$format/format.php")) {
102 error( "Format not known ($format)" ); }
104 // load parent class for import/export
105 require("format.php");
107 // and then the class for the selected format
108 require("format/$format/format.php");
110 $classname = "qformat_$format";
111 $qformat = new $classname();
113 $qformat->setCategory( $category );
114 $qformat->setCourse( $course );
115 $qformat->setFilename( $exportfilename );
116 $qformat->setCattofile( $cattofile );
118 if (! $qformat->exportpreprocess()) { // Do anything before that we need to
119 error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
122 if (! $qformat->exportprocess()) { // Process the export data
123 error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
126 if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
127 error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
129 echo "<hr />";
131 // link to download the finished file
132 $file_ext = $qformat->export_file_extension();
133 if ($CFG->slasharguments) {
134 $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
136 else {
137 $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
139 echo "<p><div class=\"boxaligncenter\"><a href=\"$efile\">$txt->download</a></div></p>";
140 echo "<p><div class=\"boxaligncenter\"><font size=\"-1\">$txt->downloadextra</font></div></p>";
142 print_continue("edit.php?courseid=$course->id");
143 print_footer($course);
144 exit;
147 /// Display upload form
149 // get valid formats to generate dropdown list
150 $fileformatnames = get_import_export_formats( 'export' );
152 // get filename
153 if (empty($exportfilename)) {
154 $exportfilename = default_export_filename($course, $category);
157 print_heading_with_help($txt->exportquestions, 'export', 'quiz');
158 print_simple_box_start('center');
161 <form enctype="multipart/form-data" method="post" action="export.php">
162 <fieldset class="invisiblefieldset" style="display: block;">
163 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
164 <input type="hidden" name="courseid" value="<?php echo $course->id; ?>" />
166 <table cellpadding="5">
167 <tr>
168 <td align="right"><?php echo $txt->category; ?>:</td>
169 <td>
170 <?php
171 question_category_select_menu($course->id, true, false, $category->id);
172 echo $txt->tofile; ?>
173 <input name="cattofile" type="checkbox" />
174 <?php helpbutton('exportcategory', $txt->exportcategory, 'quiz'); ?>
175 </td>
176 </tr>
177 <tr>
178 <td align="right"><?php echo $txt->fileformat; ?>:</td>
179 <td>
180 <?php choose_from_menu($fileformatnames, 'format', 'gift', '');
181 helpbutton('export', $txt->exportquestions, 'quiz'); ?>
182 </td>
183 </tr>
184 <tr>
185 <td align="right"><?php echo $txt->exportname; ?>:</td>
186 <td>
187 <input type="text" size="40" name="exportfilename" value="<?php echo $exportfilename; ?>" />
188 </td>
189 </tr>
190 <tr>
191 <td align="center" >
192 <input type="submit" name="save" value="<?php echo $txt->exportquestions; ?>" />
193 </td>
194 <td>&nbsp;</td>
195 </tr>
196 </table>
197 </fieldset>
198 </form>
199 <?php
201 print_simple_box_end();
202 print_footer($course);