question MDL-19820 Updated print_header and build_navigation to OUTPUT and PAGE equiv...
[moodle.git] / question / export.php
1 <?php // $Id$
2 /**
3  * Export quiz questions into the given category
4  *
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
10  */
12     require_once("../config.php");
13     require_once("editlib.php");
14     require_once("export_form.php");
16     list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('export');
19     // get display strings
20     $strexportquestions = get_string('exportquestions', 'quiz');
22     // make sure we are using the user's most recent category choice
23     if (empty($categoryid)) {
24         $categoryid = $pagevars['cat'];
25     }
27     // ensure the files area exists for this course
28     make_upload_directory("$COURSE->id");
29     list($catid, $catcontext) = explode(',', $pagevars['cat']);
30     if (!$category = $DB->get_record("question_categories", array("id" => $catid, 'contextid' => $catcontext))) {
31         print_error('nocategory','quiz');
32     }
34     /// Header
35     $PAGE->set_title($strexportquestions);
36     if ($cm!==null) {
37         $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest())
38             ? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
39             : "";
40         $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/index.php', array('id'=>$COURSE->id)));
41         $PAGE->navbar->add(format_string($module->name), new moodle_url($CFG->wwwroot.'/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
42         $PAGE->navbar->add($strexportquestions);
43         $PAGE->set_button($strupdatemodule);
44         echo $OUTPUT->header();
45         $currenttab = 'edit';
46         $mode = 'export';
47         ${$cm->modname} = $module;
48         include($CFG->dirroot."/mod/$cm->modname/tabs.php");
49     } else {
50         // Print basic page layout.
51         $PAGE->navbar->add($strexportquestions);
52         echo $OUTPUT->header();
53         // print tabs
54         $currenttab = 'export';
55         include('tabs.php');
56     }
58     $exportfilename = default_export_filename($COURSE, $category);
59     $export_form = new question_export_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('export'), 'defaultcategory'=>$pagevars['cat'],
60                                     'defaultfilename'=>$exportfilename));
63     if ($from_form = $export_form->get_data()) {   /// Filename
66         if (! is_readable("format/$from_form->format/format.php")) {
67             print_error('unknowformat', '', '', $from_form->format);
68         }
70         // load parent class for import/export
71         require_once("format.php");
73         // and then the class for the selected format
74         require_once("format/$from_form->format/format.php");
76         $classname = "qformat_$from_form->format";
77         $qformat = new $classname();
78         $qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
79         $qformat->setCategory($category);
80         $qformat->setCourse($COURSE);
82         if (empty($from_form->exportfilename)) {
83             $from_form->exportfilename = default_export_filename($COURSE, $category);
84         }
85         $qformat->setFilename($from_form->exportfilename);
86         $canaccessbackupdata = has_capability('moodle/site:backup', $contexts->lowest());
87         $qformat->set_can_access_backupdata($canaccessbackupdata);
88         $qformat->setCattofile(!empty($from_form->cattofile));
89         $qformat->setContexttofile(!empty($from_form->contexttofile));
91         if (! $qformat->exportpreprocess()) {   // Do anything before that we need to
92             print_error('exporterror', 'question', $thispageurl->out());
93         }
95         if (! $qformat->exportprocess()) {         // Process the export data
96             print_error('exporterror', 'question', $thispageurl->out());
97         }
99         if (! $qformat->exportpostprocess()) {                    // In case anything needs to be done after
100             print_error('exporterror', 'question', $thispageurl->out());
101         }
102         echo "<hr />";
104         // link to download the finished file
105         $file_ext = $qformat->export_file_extension();
106         $filename = $from_form->exportfilename . $file_ext;
107         if ($canaccessbackupdata) {
108             $efile = get_file_url($qformat->question_get_export_dir() . '/' . $filename,
109                     array('forcedownload' => 1));
110             echo '<p><div class="boxaligncenter"><a href="' . $efile . '">' .
111                     get_string('download', 'quiz') . '</a></div></p>';
112             echo '<p><div class="boxaligncenter"><font size="-1">' .
113                     get_string('downloadextra', 'quiz') . '</font></div></p>';
114         } else {
115             $efile = get_file_url($filename, null, 'questionfile');
116             echo '<p><div class="boxaligncenter">' .
117                     get_string('yourfileshoulddownload', 'question', $efile) . '</div></p>';
118             $PAGE->requires->js_function_call('document.location.replace', array($efile))->after_delay(1);
119         }
121         echo $OUTPUT->continue_button('edit.php?' . $thispageurl->get_query_string());
122         echo $OUTPUT->footer();
123         exit;
124     }
126     /// Display export form
127     print_heading_with_help($strexportquestions, 'export', 'quiz');
129     $export_form->display();
131     echo $OUTPUT->footer();
132 ?>