$CFG->tabselectedtofront is out. Part of MDL-8746
[moodle.git] / question / export.php
CommitLineData
aca318e1 1<?php // $Id$
2/**
3* Export 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" );
aca318e1 14
f1abd39f 15 // get parameters
aca318e1 16 $categoryid = optional_param('category',0, PARAM_INT);
f1abd39f 17 $cattofile = optional_param('cattofile',0, PARAM_BOOL);
aca318e1 18 $courseid = required_param('courseid',PARAM_INT);
5a4bcbc3 19 $exportfilename = optional_param('exportfilename','',PARAM_FILE );
f1abd39f 20 $format = optional_param('format','', PARAM_FILE );
21
22
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->nocategory = get_string('nocategory','quiz');
36 $txt->tofile = get_string('tofile','quiz');
37
aca318e1 38
39 if (! $course = get_record("course", "id", $courseid)) {
40 error("Course does not exist!");
41 }
0c1e3a14 42
971cd508 43 $showcatmenu = false;
0c1e3a14 44 if ($categoryid) { // update category in session variable
45 $SESSION->questioncat = $categoryid;
46 } else { // try to get category from modform
aca318e1 47 $showcatmenu = true; // will ensure that user can choose category
dc454ef4 48 if (isset($SESSION->questioncat)) {
49 $categoryid = $SESSION->questioncat;
aca318e1 50 }
51 }
52
b030a630 53 $category = get_default_question_category($courseid);
aca318e1 54
55 if (! $categorycourse = get_record("course", "id", $category->course)) {
f1abd39f 56 error( $txt->nocategory );
aca318e1 57 }
58
59 require_login($course->id, false);
d02eeded 60
f1abd39f 61 // check role capability
d02eeded 62 $context = get_context_instance(CONTEXT_COURSE, $course->id);
63 require_capability('moodle/question:export', $context);
aca318e1 64
65 // ensure the files area exists for this course
66 make_upload_directory( "$course->id" );
67
f1abd39f 68 /// Header
aca318e1 69 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
d02eeded 70 $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
f1abd39f 71 ? update_module_button($SESSION->modform->cmid, $course->id, $txt->modulename )
aca318e1 72 : "";
f1abd39f 73 print_header_simple($txt->exportquestions, '',
74 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">$txt->modulenameplural</a>".
75 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
76 ' -> '.$txt->exportquestions,
77 "", "", true, $strupdatemodule);
aca318e1 78 $currenttab = 'edit';
79 $mode = 'export';
70c01adb 80 include($CFG->dirroot.'/mod/quiz/tabs.php');
aca318e1 81 } else {
f1abd39f 82 print_header_simple($txt->exportquestions, '', $txt->exportquestions);
567709c4 83 // print tabs
84 $currenttab = 'export';
85 include('tabs.php');
aca318e1 86 }
87
88 if (!empty($format)) { /// Filename
89
90 if (!confirm_sesskey()) {
f1abd39f 91 print_error( 'sesskey' );
aca318e1 92 }
93
94 if (! is_readable("format/$format/format.php")) {
f1abd39f 95 error( "Format not known ($format)" ); }
aca318e1 96
f1abd39f 97 // load parent class for import/export
98 require("format.php");
99
100 // and then the class for the selected format
aca318e1 101 require("format/$format/format.php");
102
f5565b69 103 $classname = "qformat_$format";
104 $qformat = new $classname();
aca318e1 105
08892d5b 106 $qformat->setCategory( $category );
107 $qformat->setCourse( $course );
108 $qformat->setFilename( $exportfilename );
f1abd39f 109 $qformat->setCattofile( $cattofile );
08892d5b 110
111 if (! $qformat->exportpreprocess()) { // Do anything before that we need to
f1abd39f 112 error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 113 }
114
08892d5b 115 if (! $qformat->exportprocess()) { // Process the export data
f1abd39f 116 error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 117 }
118
f5565b69 119 if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
f1abd39f 120 error( $txt->exporterror, "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 121 }
122 echo "<hr />";
123
124 // link to download the finished file
f5565b69 125 $file_ext = $qformat->export_file_extension();
aca318e1 126 if ($CFG->slasharguments) {
1367cb8d 127 $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
aca318e1 128 }
129 else {
1367cb8d 130 $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
aca318e1 131 }
c37385bb 132 echo "<p><div class=\"boxaligncenter\"><a href=\"$efile\">$txt->download</a></div></p>";
133 echo "<p><div class=\"boxaligncenter\"><font size=\"-1\">$txt->downloadextra</font></div></p>";
aca318e1 134
5d77ace2 135 print_continue("edit.php?courseid=$course->id");
aca318e1 136 print_footer($course);
137 exit;
138 }
139
f1abd39f 140 /// Display upload form
aca318e1 141
142 // get valid formats to generate dropdown list
f1abd39f 143 $fileformatnames = get_import_export_formats( 'export' );
aca318e1 144
145 // get filename
146 if (empty($exportfilename)) {
147 $exportfilename = default_export_filename($course, $category);
148 }
149
f1abd39f 150 print_heading_with_help($txt->exportquestions, 'export', 'quiz');
c37385bb 151 print_simple_box_start('center');
f1abd39f 152?>
aca318e1 153
f1abd39f 154 <form enctype="multipart/form-data" method="post" action="export.php">
c37385bb 155 <fieldset class="invisiblefieldset">
f1abd39f 156 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
157 <input type="hidden" name="courseid" value="<?php echo $course->id; ?>" />
158
159 <table cellpadding="5">
160 <tr>
161 <td align="right"><?php echo $txt->category; ?>:</td>
162 <td>
163 <?php
164 if (!$showcatmenu) { // category already specified
165 echo '<strong>'.question_category_coursename($category).'</strong>&nbsp;&nbsp;'; ?>
166 <input type="hidden" name="category" value="<?php echo $category->id ?>" />
167 <?php
168 } else { // no category specified, let user choose
169 question_category_select_menu($course->id, true, false, $category->id);
170 }
171 echo $txt->tofile; ?>
172 <input name="cattofile" type="checkbox" />
173 <?php helpbutton('exportcategory', $txt->exportcategory, 'quiz'); ?>
174 </td>
175 </tr>
176 <tr>
177 <td align="right"><?php echo $txt->fileformat; ?>:</td>
178 <td>
179 <?php choose_from_menu($fileformatnames, 'format', 'gift', '');
180 helpbutton('export', $txt->exportquestions, 'quiz'); ?>
181 </td>
182 </tr>
183 <tr>
184 <td align="right"><?php echo $txt->exportname; ?>:</td>
185 <td>
186 <input type="text" size="40" name="exportfilename" value="<?php echo $exportfilename; ?>" />
187 </td>
188 </tr>
189 <tr>
dacc89ea 190 <td align="center" >
f1abd39f 191 <input type="submit" name="save" value="<?php echo $txt->exportquestions; ?>" />
192 </td>
dacc89ea 193 <td>&nbsp;</td>
f1abd39f 194 </tr>
195 </table>
c37385bb 196 </fieldset>
f1abd39f 197 </form>
198 <?php
c37385bb 199
aca318e1 200 print_simple_box_end();
aca318e1 201 print_footer($course);
f1abd39f 202?>
aca318e1 203