Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / question / export.php
blobe53c088a901f28d9052dae2e2ec22e2156a353f9
1 <?php // $Id$
2 /**
3 * Export quiz questions into the given category
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
12 require_once("../config.php");
13 require_once( "editlib.php" );
15 $categoryid = optional_param('category',0, PARAM_INT);
16 $courseid = required_param('courseid',PARAM_INT);
17 $format = optional_param('format','', PARAM_FILE );
18 $exportfilename = optional_param('exportfilename','',PARAM_FILE );
20 if (! $course = get_record("course", "id", $courseid)) {
21 error("Course does not exist!");
24 $showcatmenu = false;
25 if ($categoryid) { // update category in session variable
26 $SESSION->questioncat = $categoryid;
27 } else { // try to get category from modform
28 $showcatmenu = true; // will ensure that user can choose category
29 if (isset($SESSION->questioncat)) {
30 $categoryid = $SESSION->questioncat;
34 if (! $category = get_record("question_categories", "id", $categoryid)) {
35 $category = get_default_question_category($courseid);
38 if (! $categorycourse = get_record("course", "id", $category->course)) {
39 error( get_string('nocategory','quiz') );
42 require_login($course->id, false);
44 $context = get_context_instance(CONTEXT_COURSE, $course->id);
45 require_capability('moodle/question:export', $context);
47 // ensure the files area exists for this course
48 make_upload_directory( "$course->id" );
50 $strexportquestions = get_string("exportquestions", "quiz");
51 $strquestions = get_string("questions", "quiz");
53 $strquizzes = get_string('modulenameplural', 'quiz');
54 $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
56 $dirname = get_string("exportfilename","quiz");
58 /// Header:
60 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
61 $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
62 ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
63 : "";
64 print_header_simple($strexportquestions, '',
65 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">".get_string('modulenameplural', 'quiz').'</a>'.
66 " -> <a href=\"$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id\">".format_string($quiz->name).'</a>'.
67 ' -> '.$strexportquestions,
68 "", "", true, $strupdatemodule);
69 $currenttab = 'edit';
70 $mode = 'export';
71 include($CFG->dirroot.'/mod/quiz/tabs.php');
72 } else {
73 print_header_simple($strexportquestions, '', $strexportquestions);
74 // print tabs
75 $currenttab = 'export';
76 include('tabs.php');
79 if (!empty($format)) { /// Filename
81 if (!confirm_sesskey()) {
82 echo( 'Sesskey error' );
85 if (! is_readable("format/$format/format.php")) {
86 error('Format not known ('.clean_text($form->format).')');
89 require("format.php"); // Parent class
90 require("format/$format/format.php");
92 $classname = "qformat_$format";
93 $qformat = new $classname();
95 $qformat->setCategory( $category );
96 $qformat->setCourse( $course );
97 $qformat->setFilename( $exportfilename );
99 if (! $qformat->exportpreprocess()) { // Do anything before that we need to
100 error( get_string('exporterror','quiz'),
101 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
104 if (! $qformat->exportprocess()) { // Process the export data
105 error( get_string('exporterror','quiz'),
106 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
109 if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
110 error( get_string('exporterror','quiz'),
111 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
113 echo "<hr />";
115 // link to download the finished file
116 $file_ext = $qformat->export_file_extension();
117 $download_str = get_string( 'download', 'quiz' );
118 $downloadextra_str = get_string( 'downloadextra','quiz' );
119 if ($CFG->slasharguments) {
120 $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
122 else {
123 $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
125 echo "<p><div class=\"boxaligncenter\"><a href=\"$efile\">$download_str</a></div></p>";
126 echo "<p><div class=\"boxaligncenter\"><font size=\"-1\">$downloadextra_str</font></div></p>";
128 print_continue("edit.php?courseid=$course->id");
129 print_footer($course);
130 exit;
133 /// Print upload form
135 // get valid formats to generate dropdown list
136 $fileformatnames = get_import_export_formats( "export" );
138 // get filename
139 if (empty($exportfilename)) {
140 $exportfilename = default_export_filename($course, $category);
143 print_heading_with_help($strexportquestions, "export", "quiz");
145 print_simple_box_start("center");
146 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"export.php\">\n";
147 echo '<fieldset class="invisiblefieldset">';
148 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />\n";
149 echo "<table cellpadding=\"5\">\n";
151 echo "<tr><td align=\"right\">\n";
152 print_string("category", "quiz");
153 echo ":</td><td>";
154 if (!$showcatmenu) { // category already specified
155 echo question_category_coursename($category);
156 echo " <input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
157 } else { // no category specified, let user choose
158 question_category_select_menu($course->id, true, false, $category->id);
160 //echo str_replace('&nbsp;', '', $category->name) . " ($categorycourse->shortname)";
161 echo "</td></tr>\n";
163 echo "<tr><td align=\"right\">";
164 print_string("fileformat", "quiz");
165 echo ":</td><td>";
166 choose_from_menu($fileformatnames, "format", "gift", "");
167 helpbutton("export", $strexportquestions, "quiz");
168 echo "</td></tr>\n";
170 echo "<tr><td align=\"right\">";
171 print_string("exportname", "quiz" );
172 echo ":</td><td>";
173 echo "<input type=\"text\" size=\"40\" name=\"exportfilename\" value=\"$exportfilename\" />";
174 echo "</td></tr>\n";
176 echo "<tr><td align=\"center\" colspan=\"2\">";
177 echo " <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
178 echo " <input type=\"submit\" name=\"save\" value=\"".get_string("exportquestions","quiz")."\" />";
179 echo "</td></tr>\n";
181 echo "</table>\n";
182 echo '</fieldset>';
183 echo "</form>\n";
184 print_simple_box_end();
186 print_footer($course);