8dc26d8dcc145fc4681f698913d50eef1ab01a6a
[moodle.git] / mod / lesson / import.php
1 <?php // $Id$
2 /**
3  * Imports lesson pages
4  *
5  * @version $Id$
6  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7  * @package lesson
8  **/
10     require_once("../../config.php");
11     require_once("lib.php");
12     require_once("locallib.php");
13     require_once($CFG->libdir.'/questionlib.php');
15     $id     = required_param('id', PARAM_INT);         // Course Module ID
16     $pageid = optional_param('pageid', '', PARAM_INT); // Page ID
18     if (! $cm = get_coursemodule_from_id('lesson', $id)) {
19         print_error('invalidcoursemodule');
20     }
22     if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
23         print_error('coursemisconf');
24     }
26     if (! $lesson = $DB->get_record("lesson", array("id" => $cm->instance))) {
27         print_error('invalidcoursemodule');
28     }
31     require_login($course->id, false, $cm);
32     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
33     require_capability('mod/lesson:edit', $context);
35     $strimportquestions = get_string("importquestions", "lesson");
36     $strlessons = get_string("modulenameplural", "lesson");
38     $navigation = build_navigation($strimportquestions, $cm);
39     print_header_simple("$strimportquestions", " $strimportquestions", $navigation);
41     if ($form = data_submitted()) {   /// Filename
43         $form->format = clean_param($form->format, PARAM_SAFEDIR); // For safety
45         if (empty($_FILES['newfile'])) {      // file was just uploaded
46             echo $OUTPUT->notification(get_string("uploadproblem") );
47         }
49         if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
50             echo $OUTPUT->notification(get_string("uploadnofilefound") );
52         } else {  // Valid file is found
54             if (! is_readable("$CFG->dirroot/question/format/$form->format/format.php")) {
55                 print_error('unknowformat','', '', $form->format);
56             }
58             require("format.php");  // Parent class
59             require("$CFG->dirroot/question/format/$form->format/format.php");
61             $classname = "qformat_$form->format";
62             $format = new $classname();
64             if (! $format->importpreprocess()) {             // Do anything before that we need to
65                 print_error('preprocesserror', 'lesson');
66             }
68             if (! $format->importprocess($_FILES['newfile']['tmp_name'], $lesson, $pageid)) {    // Process the uploaded file
69                 print_error('processerror', 'lesson');
70             }
72             if (! $format->importpostprocess()) {                     // In case anything needs to be done after
73                 print_error('postprocesserror', 'lesson');
74             }
76             echo "<hr>";
77             echo $OUTPUT->continue_button("view.php?id=$cm->id");
78             echo $OUTPUT->footer();
79             exit;
80         }
81     }
83     /// Print upload form
85     $fileformatnames = get_import_export_formats('import');
87     print_heading_with_help($strimportquestions, "import", "lesson");
89     echo $OUTPUT->box_start('generalbox boxaligncenter');
90     echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"import.php\">";
91     echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
92     echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />\n";
93     echo "<table cellpadding=\"5\">";
95     echo "<tr><td align=\"right\">";
96     print_string("fileformat", "lesson");
97     echo ":</td><td>";
98     echo $OUTPUT->select(html_select::make($fileformatnames, "format", "gift", false));
99     echo "</td></tr>";
101     echo "<tr><td align=\"right\">";
102     print_string("upload");
103     echo ":</td><td>";
104     echo "<input name=\"newfile\" type=\"file\" size=\"50\" />";
105     echo "</td></tr><tr><td>&nbsp;</td><td>";
106     echo "<input type=\"submit\" name=\"save\" value=\"".get_string("uploadthisfile")."\" />";
107     echo "</td></tr>";
109     echo "</table>";
110     echo "</form>";
111     echo $OUTPUT->box_end();
113     echo $OUTPUT->footer();
115 ?>