MDL-40735 mod_lesson: fixed up a header and stripped out hardcoded formatting
[moodle.git] / mod / lesson / import.php
CommitLineData
87f83794 1<?php
2
0a4abb73
SH
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.
14//
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/>.
17
5491947a 18/**
19 * Imports lesson pages
20 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
0a4abb73 23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
cc3dbaaa 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5491947a 25 **/
394c97c8 26
0a4abb73
SH
27require_once("../../config.php");
28require_once($CFG->libdir.'/questionlib.php');
29require_once($CFG->dirroot.'/mod/lesson/locallib.php');
30require_once($CFG->dirroot.'/mod/lesson/import_form.php');
31require_once($CFG->dirroot.'/mod/lesson/format.php'); // Parent class
394c97c8 32
0a4abb73
SH
33$id = required_param('id', PARAM_INT); // Course Module ID
34$pageid = optional_param('pageid', '', PARAM_INT); // Page ID
87f83794 35
a6855934 36$PAGE->set_url('/mod/lesson/import.php', array('id'=>$id, 'pageid'=>$pageid));
394c97c8 37
0e35ba6f 38$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
74df2951 39$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
b3daa926
PS
40$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
41
0a4abb73 42require_login($course, false, $cm);
5918e371 43$context = context_module::instance($cm->id);
0a4abb73 44require_capability('mod/lesson:edit', $context);
394c97c8 45
0a4abb73
SH
46$strimportquestions = get_string("importquestions", "lesson");
47$strlessons = get_string("modulenameplural", "lesson");
394c97c8 48
0a4abb73 49$manager = lesson_page_type_manager::get($lesson);
394c97c8 50
0a4abb73
SH
51$data = new stdClass;
52$data->id = $PAGE->cm->id;
53$data->pageid = $pageid;
394c97c8 54
0a4abb73
SH
55$mform = new lesson_import_form(null, array('formats'=>lesson_get_import_export_formats('import')));
56$mform->set_data($data);
394c97c8 57
2458610a 58 $PAGE->navbar->add($strimportquestions);
59 $PAGE->set_title($strimportquestions);
974beea1 60 $PAGE->set_heading(format_string($course->fullname));
2458610a 61 echo $OUTPUT->header();
974beea1
AD
62 echo $OUTPUT->heading(format_string($lesson->name), 2);
63 echo $OUTPUT->heading_with_help($strimportquestions, 'importquestions', 'lesson', '', '', 3);
1e448f63 64
0a4abb73 65if ($data = $mform->get_data()) {
ec81373f 66
0a4abb73 67 require_sesskey();
394c97c8 68
b89bf55f
RT
69 $realfilename = $mform->get_new_filename('questionfile');
70 //TODO: Leave all imported questions in Questionimport for now.
b6b50231 71 $importfile = "{$CFG->tempdir}/questionimport/{$realfilename}";
af9b1444 72 make_temp_directory('questionimport');
b89bf55f
RT
73 if (!$result = $mform->save_file('questionfile', $importfile, true)) {
74 throw new moodle_exception('uploadproblem');
75 }
394c97c8 76
0a4abb73
SH
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);
394c97c8 81 }
0a4abb73
SH
82 require_once($formatclassfile);
83 $format = new $formatclass();
394c97c8 84
0a4abb73
SH
85 // Do anything before that we need to
86 if (! $format->importpreprocess()) {
86f93345 87 print_error('preprocesserror', 'lesson');
394c97c8 88 }
89
0a4abb73
SH
90 // Process the uploaded file
91 if (! $format->importprocess($importfile, $lesson, $pageid)) {
86f93345 92 print_error('processerror', 'lesson');
394c97c8 93 }
94
0a4abb73
SH
95 // In case anything needs to be done after
96 if (! $format->importpostprocess()) {
86f93345 97 print_error('postprocesserror', 'lesson');
394c97c8 98 }
99
5e7856af 100 echo "<hr>";
0a4abb73 101 echo $OUTPUT->continue_button('view.php?id='.$PAGE->cm->id);
394c97c8 102
0a4abb73 103} else {
394c97c8 104
0a4abb73
SH
105 // Print upload form
106 $mform->display();
107}
86342d63 108
0e35ba6f 109echo $OUTPUT->footer();