MDL-9506 Number of important changes, restored unit test stability, on to next changes...
[moodle-pu.git] / mod / lesson / import.php
blob98fdfe0a8268af9f76d27e8bb6dd6e305ee079dc
1 <?php // $Id$
2 /**
3 * Imports lesson pages
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 error("Course Module ID was incorrect");
22 if (! $course = get_record("course", "id", $cm->course)) {
23 error("Course is misconfigured");
26 if (! $lesson = get_record("lesson", "id", $cm->instance)) {
27 error("Course module is incorrect");
31 require_login($course->id, false);
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 $crumbs[] = array('name' => $strlesson, 'link' => "index.php?id=$course->id", 'type' => 'activity');
39 $crumbs[] = array('name' => format_string($lesson->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
40 $crumbs[] = array('name' => $strimportquestions, 'link' => '', 'type' => 'title');
42 $navigation = build_navigation($crumbs);
44 print_header_simple("$strimportquestions", " $strimportquestions", $navigation);
46 if ($form = data_submitted()) { /// Filename
48 $form->format = clean_param($form->format, PARAM_SAFEDIR); // For safety
50 if (empty($_FILES['newfile'])) { // file was just uploaded
51 notify(get_string("uploadproblem") );
54 if ((!is_uploaded_file($_FILES['newfile']['tmp_name']) or $_FILES['newfile']['size'] == 0)) {
55 notify(get_string("uploadnofilefound") );
57 } else { // Valid file is found
59 if (! is_readable("$CFG->dirroot/question/format/$form->format/format.php")) {
60 error("Format not known ($form->format)");
63 require("format.php"); // Parent class
64 require("$CFG->dirroot/question/format/$form->format/format.php");
66 $classname = "qformat_$form->format";
67 $format = new $classname();
69 if (! $format->importpreprocess()) { // Do anything before that we need to
70 error("Error occurred during pre-processing!");
73 if (! $format->importprocess($_FILES['newfile']['tmp_name'], $lesson, $pageid)) { // Process the uploaded file
74 error("Error occurred during processing!");
77 if (! $format->importpostprocess()) { // In case anything needs to be done after
78 error("Error occurred during post-processing!");
81 echo "<hr>";
82 print_continue("view.php?id=$cm->id");
83 print_footer($course);
84 exit;
88 /// Print upload form
90 $fileformatnames = get_import_export_formats('import');
92 print_heading_with_help($strimportquestions, "import", "lesson");
94 print_simple_box_start("center");
95 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"import.php\">";
96 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
97 echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />\n";
98 echo "<table cellpadding=\"5\">";
100 echo "<tr><td align=\"right\">";
101 print_string("fileformat", "lesson");
102 echo ":</td><td>";
103 choose_from_menu($fileformatnames, "format", "gift", "");
104 echo "</td></tr>";
106 echo "<tr><td align=\"right\">";
107 print_string("upload");
108 echo ":</td><td>";
109 echo "<input name=\"newfile\" type=\"file\" size=\"50\" />";
110 echo "</td></tr><tr><td>&nbsp;</td><td>";
111 echo "<input type=\"submit\" name=\"save\" value=\"".get_string("uploadthisfile")."\" />";
112 echo "</td></tr>";
114 echo "</table>";
115 echo "</form>";
116 print_simple_box_end();
118 print_footer($course);