9e1a65f0aba030a3a19118903e9c45d7fcbee4cd
[moodle.git] / question / export.php
1 <?php
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_url($thispageurl->out());
36     $PAGE->set_title($strexportquestions);
37     if ($cm!==null) {
38         $PAGE->navbar->add($strexportquestions);
39         echo $OUTPUT->header();
40         $currenttab = 'edit';
41         $mode = 'export';
42         ${$cm->modname} = $module;
43         include($CFG->dirroot."/mod/$cm->modname/tabs.php");
44     } else {
45         // Print basic page layout.
46         $PAGE->navbar->add($strexportquestions);
47         echo $OUTPUT->header();
48         // print tabs
49         $currenttab = 'export';
50         include('tabs.php');
51     }
53     $exportfilename = default_export_filename($COURSE, $category);
54     $export_form = new question_export_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('export'), 'defaultcategory'=>$pagevars['cat'],
55                                     'defaultfilename'=>$exportfilename));
58     if ($from_form = $export_form->get_data()) {   /// Filename
61         if (! is_readable("format/$from_form->format/format.php")) {
62             print_error('unknowformat', '', '', $from_form->format);
63         }
65         // load parent class for import/export
66         require_once("format.php");
68         // and then the class for the selected format
69         require_once("format/$from_form->format/format.php");
71         $classname = "qformat_$from_form->format";
72         $qformat = new $classname();
73         $qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
74         $qformat->setCategory($category);
75         $qformat->setCourse($COURSE);
77         if (empty($from_form->exportfilename)) {
78             $from_form->exportfilename = default_export_filename($COURSE, $category);
79         }
80         $qformat->setFilename($from_form->exportfilename);
81         $canaccessbackupdata = has_capability('moodle/site:backup', $contexts->lowest());
82         $qformat->set_can_access_backupdata($canaccessbackupdata);
83         $qformat->setCattofile(!empty($from_form->cattofile));
84         $qformat->setContexttofile(!empty($from_form->contexttofile));
86         if (! $qformat->exportpreprocess()) {   // Do anything before that we need to
87             print_error('exporterror', 'question', $thispageurl->out());
88         }
90         if (! $qformat->exportprocess()) {         // Process the export data
91             print_error('exporterror', 'question', $thispageurl->out());
92         }
94         if (! $qformat->exportpostprocess()) {                    // In case anything needs to be done after
95             print_error('exporterror', 'question', $thispageurl->out());
96         }
97         echo "<hr />";
99         // link to download the finished file
100         $file_ext = $qformat->export_file_extension();
101         $filename = $from_form->exportfilename . $file_ext;
102         if ($canaccessbackupdata) {
103             $efile = get_file_url($qformat->question_get_export_dir() . '/' . $filename,
104                     array('forcedownload' => 1));
105             echo '<p><div class="boxaligncenter"><a href="' . $efile . '">' .
106                     get_string('download', 'quiz') . '</a></div></p>';
107             echo '<p><div class="boxaligncenter"><font size="-1">' .
108                     get_string('downloadextra', 'quiz') . '</font></div></p>';
109         } else {
110             $efile = get_file_url($filename, null, 'questionfile');
111             echo '<p><div class="boxaligncenter">' .
112                     get_string('yourfileshoulddownload', 'question', $efile) . '</div></p>';
113             $PAGE->requires->js_function_call('document.location.replace', array($efile))->after_delay(1);
114         }
116         echo $OUTPUT->continue_button(new moodle_url('edit.php', $thispageurl->params()));
117         echo $OUTPUT->footer();
118         exit;
119     }
121     /// Display export form
122     echo $OUTPUT->heading_with_help($strexportquestions, 'export', 'quiz');
124     $export_form->display();
126     echo $OUTPUT->footer();