MDL-36963 Add unit tests for directory operations in mdeploy.php
[moodle.git] / mod / lesson / import.php
blob1a9e93c673faa2fbf81303779cd65fcaff04f7bd
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Imports lesson pages
21 * @package mod
22 * @subpackage lesson
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 **/
27 require_once("../../config.php");
28 require_once($CFG->libdir.'/questionlib.php');
29 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
30 require_once($CFG->dirroot.'/mod/lesson/import_form.php');
31 require_once($CFG->dirroot.'/mod/lesson/format.php'); // Parent class
33 $id = required_param('id', PARAM_INT); // Course Module ID
34 $pageid = optional_param('pageid', '', PARAM_INT); // Page ID
36 $PAGE->set_url('/mod/lesson/import.php', array('id'=>$id, 'pageid'=>$pageid));
38 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);;
39 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
40 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
42 require_login($course, false, $cm);
43 $context = context_module::instance($cm->id);
44 require_capability('mod/lesson:edit', $context);
46 $strimportquestions = get_string("importquestions", "lesson");
47 $strlessons = get_string("modulenameplural", "lesson");
49 $manager = lesson_page_type_manager::get($lesson);
51 $data = new stdClass;
52 $data->id = $PAGE->cm->id;
53 $data->pageid = $pageid;
55 $mform = new lesson_import_form(null, array('formats'=>lesson_get_import_export_formats('import')));
56 $mform->set_data($data);
58 $PAGE->navbar->add($strimportquestions);
59 $PAGE->set_title($strimportquestions);
60 $PAGE->set_heading($strimportquestions);
61 echo $OUTPUT->header();
63 echo $OUTPUT->heading_with_help($strimportquestions, 'importquestions', 'lesson' );
65 if ($data = $mform->get_data()) {
67 require_sesskey();
69 $realfilename = $mform->get_new_filename('questionfile');
70 //TODO: Leave all imported questions in Questionimport for now.
71 $importfile = "{$CFG->tempdir}/questionimport/{$realfilename}";
72 make_temp_directory('questionimport');
73 if (!$result = $mform->save_file('questionfile', $importfile, true)) {
74 throw new moodle_exception('uploadproblem');
77 $formatclass = 'qformat_'.$data->format;
78 $formatclassfile = $CFG->dirroot.'/question/format/'.$data->format.'/format.php';
79 if (!is_readable($formatclassfile)) {
80 print_error('unknowformat','', '', $data->format);
82 require_once($formatclassfile);
83 $format = new $formatclass();
85 // Do anything before that we need to
86 if (! $format->importpreprocess()) {
87 print_error('preprocesserror', 'lesson');
90 // Process the uploaded file
91 if (! $format->importprocess($importfile, $lesson, $pageid)) {
92 print_error('processerror', 'lesson');
95 // In case anything needs to be done after
96 if (! $format->importpostprocess()) {
97 print_error('postprocesserror', 'lesson');
100 echo "<hr>";
101 echo $OUTPUT->continue_button('view.php?id='.$PAGE->cm->id);
103 } else {
105 // Print upload form
106 $mform->display();
109 echo $OUTPUT->footer();