Fixed a notice. Tidied up format class call a bit. Added provision
[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
15 $categoryid = optional_param('category',0, PARAM_INT);
16 $courseid = required_param('courseid',PARAM_INT);
5a4bcbc3 17 $format = optional_param('format','', PARAM_FILE );
18 $exportfilename = optional_param('exportfilename','',PARAM_FILE );
aca318e1 19
20 if (! $course = get_record("course", "id", $courseid)) {
21 error("Course does not exist!");
22 }
0c1e3a14 23
971cd508 24 $showcatmenu = false;
0c1e3a14 25 if ($categoryid) { // update category in session variable
26 $SESSION->questioncat = $categoryid;
27 } else { // try to get category from modform
aca318e1 28 $showcatmenu = true; // will ensure that user can choose category
dc454ef4 29 if (isset($SESSION->questioncat)) {
30 $categoryid = $SESSION->questioncat;
aca318e1 31 }
32 }
33
dc1f00de 34 if (! $category = get_record("question_categories", "id", $categoryid)) {
062f1125 35 $category = get_default_question_category($courseid);
aca318e1 36 }
37
38 if (! $categorycourse = get_record("course", "id", $category->course)) {
1e3d6fd8 39 error( get_string('nocategory','quiz') );
aca318e1 40 }
41
42 require_login($course->id, false);
d02eeded 43
44 $context = get_context_instance(CONTEXT_COURSE, $course->id);
45 require_capability('moodle/question:export', $context);
aca318e1 46
47 // ensure the files area exists for this course
48 make_upload_directory( "$course->id" );
49
50 $strexportquestions = get_string("exportquestions", "quiz");
51 $strquestions = get_string("questions", "quiz");
52
53 $strquizzes = get_string('modulenameplural', 'quiz');
54 $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
55
56 $dirname = get_string("exportfilename","quiz");
57
58 /// Header:
59
60 if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) {
d02eeded 61 $strupdatemodule = has_capability('moodle/course:manageactivities', $context)
aca318e1 62 ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz'))
63 : "";
64 print_header_simple($strexportquestions, '',
70c01adb 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>'.
aca318e1 67 ' -> '.$strexportquestions,
68 "", "", true, $strupdatemodule);
69 $currenttab = 'edit';
70 $mode = 'export';
70c01adb 71 include($CFG->dirroot.'/mod/quiz/tabs.php');
aca318e1 72 } else {
2662cf46 73 print_header_simple($strexportquestions, '', $strexportquestions);
567709c4 74 // print tabs
75 $currenttab = 'export';
76 include('tabs.php');
aca318e1 77 }
78
79 if (!empty($format)) { /// Filename
80
81 if (!confirm_sesskey()) {
82 echo( 'Sesskey error' );
83 }
84
85 if (! is_readable("format/$format/format.php")) {
86 error('Format not known ('.clean_text($form->format).')');
87 }
88
89 require("format.php"); // Parent class
90 require("format/$format/format.php");
91
f5565b69 92 $classname = "qformat_$format";
93 $qformat = new $classname();
aca318e1 94
08892d5b 95 $qformat->setCategory( $category );
96 $qformat->setCourse( $course );
97 $qformat->setFilename( $exportfilename );
98
99 if (! $qformat->exportpreprocess()) { // Do anything before that we need to
1e3d6fd8 100 error( get_string('exporterror','quiz'),
101 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 102 }
103
08892d5b 104 if (! $qformat->exportprocess()) { // Process the export data
1e3d6fd8 105 error( get_string('exporterror','quiz'),
106 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 107 }
108
f5565b69 109 if (! $qformat->exportpostprocess()) { // In case anything needs to be done after
1e3d6fd8 110 error( get_string('exporterror','quiz'),
111 "$CFG->wwwroot/question/export.php?courseid={$course->id}&amp;category=$category->id");
aca318e1 112 }
113 echo "<hr />";
114
115 // link to download the finished file
f5565b69 116 $file_ext = $qformat->export_file_extension();
aca318e1 117 $download_str = get_string( 'download', 'quiz' );
118 $downloadextra_str = get_string( 'downloadextra','quiz' );
119 if ($CFG->slasharguments) {
1367cb8d 120 $efile = "{$CFG->wwwroot}/file.php/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."?forcedownload=1";
aca318e1 121 }
122 else {
1367cb8d 123 $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
aca318e1 124 }
aca318e1 125 echo "</p><center><a href=\"$efile\">$download_str</a></center></p>";
126 echo "</p><center><font size=\"-1\">$downloadextra_str</font></center></p>";
127
5d77ace2 128 print_continue("edit.php?courseid=$course->id");
aca318e1 129 print_footer($course);
130 exit;
131 }
132
133 /// Print upload form
134
135 // get valid formats to generate dropdown list
136 $fileformatnames = get_import_export_formats( "export" );
137
138 // get filename
139 if (empty($exportfilename)) {
140 $exportfilename = default_export_filename($course, $category);
141 }
142
aca318e1 143 print_heading_with_help($strexportquestions, "export", "quiz");
144
145 print_simple_box_start("center");
146 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"export.php\">\n";
147 echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />\n";
148 echo "<table cellpadding=\"5\">\n";
149
150 echo "<tr><td align=\"right\">\n";
151 print_string("category", "quiz");
152 echo ":</td><td>";
153 if (!$showcatmenu) { // category already specified
dc1f00de 154 echo question_category_coursename($category);
aca318e1 155 echo " <input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
156 } else { // no category specified, let user choose
b20ea669 157 question_category_select_menu($course->id, true, false, $category->id);
aca318e1 158 }
159 //echo str_replace('&nbsp;', '', $category->name) . " ($categorycourse->shortname)";
160 echo "</td></tr>\n";
161
162 echo "<tr><td align=\"right\">";
163 print_string("fileformat", "quiz");
164 echo ":</td><td>";
165 choose_from_menu($fileformatnames, "format", "gift", "");
166 helpbutton("export", $strexportquestions, "quiz");
167 echo "</td></tr>\n";
168
169 echo "<tr><td align=\"right\">";
170 print_string("exportname", "quiz" );
171 echo ":</td><td>";
172 echo "<input type=\"text\" size=\"40\" name=\"exportfilename\" value=\"$exportfilename\" />";
173 echo "</td></tr>\n";
174
175 echo "<tr><td align=\"center\" colspan=\"2\">";
176 echo " <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />";
177 echo " <input type=\"submit\" name=\"save\" value=\"".get_string("exportquestions","quiz")."\" />";
178 echo "</td></tr>\n";
179
180 echo "</table>\n";
181 echo "</form>\n";
182 print_simple_box_end();
183
184 print_footer($course);
185
186?>