lesson MDL-19812 Updated print_header and build_navigation to OUTPUT and PAGE equivalents
[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     $PAGE->navbar->add($strimportquestions);
39     $PAGE->set_title($strimportquestions);
40     $PAGE->set_heading($strimportquestions);
41     echo $OUTPUT->header();
43     if ($form = data_submitted()) {   /// Filename
45         $form->format = clean_param($form->format, PARAM_SAFEDIR); // For safety
47         if (empty($_FILES['newfile'])) {      // file was just uploaded
48             echo $OUTPUT->notification(get_string("uploadproblem") );
49         }
51         if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
52             echo $OUTPUT->notification(get_string("uploadnofilefound") );
54         } else {  // Valid file is found
56             if (! is_readable("$CFG->dirroot/question/format/$form->format/format.php")) {
57                 print_error('unknowformat','', '', $form->format);
58             }
60             require("format.php");  // Parent class
61             require("$CFG->dirroot/question/format/$form->format/format.php");
63             $classname = "qformat_$form->format";
64             $format = new $classname();
66             if (! $format->importpreprocess()) {             // Do anything before that we need to
67                 print_error('preprocesserror', 'lesson');
68             }
70             if (! $format->importprocess($_FILES['newfile']['tmp_name'], $lesson, $pageid)) {    // Process the uploaded file
71                 print_error('processerror', 'lesson');
72             }
74             if (! $format->importpostprocess()) {                     // In case anything needs to be done after
75                 print_error('postprocesserror', 'lesson');
76             }
78             echo "<hr>";
79             echo $OUTPUT->continue_button("view.php?id=$cm->id");
80             echo $OUTPUT->footer();
81             exit;
82         }
83     }
85     /// Print upload form
87     $fileformatnames = get_import_export_formats('import');
89     print_heading_with_help($strimportquestions, "import", "lesson");
91     echo $OUTPUT->box_start('generalbox boxaligncenter');
92     echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"import.php\">";
93     echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
94     echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />\n";
95     echo "<table cellpadding=\"5\">";
97     echo "<tr><td align=\"right\">";
98     print_string("fileformat", "lesson");
99     echo ":</td><td>";
100     echo $OUTPUT->select(html_select::make($fileformatnames, "format", "gift", false));
101     echo "</td></tr>";
103     echo "<tr><td align=\"right\">";
104     print_string("upload");
105     echo ":</td><td>";
106     echo "<input name=\"newfile\" type=\"file\" size=\"50\" />";
107     echo "</td></tr><tr><td>&nbsp;</td><td>";
108     echo "<input type=\"submit\" name=\"save\" value=\"".get_string("uploadthisfile")."\" />";
109     echo "</td></tr>";
111     echo "</table>";
112     echo "</form>";
113     echo $OUTPUT->box_end();
115     echo $OUTPUT->footer();
117 ?>