Fixed a notice. Tidied up format class call a bit. Added provision
[moodle.git] / question / export.php
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 */
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!");
22     }
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;
31         }
32     }
34     if (! $category = get_record("question_categories", "id", $categoryid)) {
35         $category = get_default_question_category($courseid);
36     }
38     if (! $categorycourse = get_record("course", "id", $category->course)) {
39         error( get_string('nocategory','quiz') );
40     }
42     require_login($course->id, false);
43     
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");
57     
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');
77     }
79     if (!empty($format)) {   /// Filename
81         if (!confirm_sesskey()) {
82             echo( 'Sesskey error' );
83         }
85         if (! is_readable("format/$format/format.php")) {
86             error('Format not known ('.clean_text($form->format).')');
87         }
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");
102         }
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");
107         }
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");
112         }
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";
121         }
122         else {
123           $efile = "{$CFG->wwwroot}/file.php?file=/".$qformat->question_get_export_dir()."/$exportfilename".$file_ext."&forcedownload=1";
124         }
125         echo "</p><center><a href=\"$efile\">$download_str</a></center></p>";
126         echo "</p><center><font size=\"-1\">$downloadextra_str</font></center></p>";
128         print_continue("edit.php?courseid=$course->id");
129         print_footer($course);
130         exit;
131     }
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);
141     }
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 "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />\n";
148     echo "<table cellpadding=\"5\">\n";
150     echo "<tr><td align=\"right\">\n";
151     print_string("category", "quiz");
152     echo ":</td><td>";
153     if (!$showcatmenu) { // category already specified
154         echo question_category_coursename($category);
155         echo " <input type=\"hidden\" name=\"category\" value=\"$category->id\" />";
156     } else { // no category specified, let user choose
157         question_category_select_menu($course->id, true, false, $category->id);
158     }
159     //echo str_replace('&nbsp;', '', $category->name) . " ($categorycourse->shortname)";
160     echo "</td></tr>\n";
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";
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";
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";
180     echo "</table>\n";
181     echo "</form>\n";
182     print_simple_box_end();
184     print_footer($course);
186 ?>