index.php 3.26 KB
Newer Older
1
<?php
Petr Skoda's avatar
Petr Skoda committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// This file is part of Book module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Book import
 *
20
21
 * @package    booktool
 * @subpackage importhtml
22
 * @copyright  2004-2011 Petr Skoda  {@link http://skodak.org}
Petr Skoda's avatar
Petr Skoda committed
23
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Petr Skoda's avatar
Petr Skoda committed
25

Petr Skoda's avatar
Petr Skoda committed
26
27
28
require(dirname(__FILE__).'/../../../../config.php');
require_once(dirname(__FILE__).'/locallib.php');
require_once(dirname(__FILE__).'/import_form.php');
Petr Skoda's avatar
Petr Skoda committed
29

30
31
$id        = required_param('id', PARAM_INT);           // Course Module ID
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
Petr Skoda's avatar
Petr Skoda committed
32

Petr Skoda's avatar
Petr Skoda committed
33
34
35
$cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
Petr Skoda's avatar
Petr Skoda committed
36

37
38
require_login($course, false, $cm);

39
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
Petr Skoda's avatar
Petr Skoda committed
40
require_capability('booktool/importhtml:import', $context);
Petr Skoda's avatar
Petr Skoda committed
41

Petr Skoda's avatar
Petr Skoda committed
42
$PAGE->set_url('/mod/book/tool/importhtml/index.php', array('id'=>$id, 'chapterid'=>$chapterid));
Petr Skoda's avatar
Petr Skoda committed
43

44
45
46
47
48
49
50
if ($chapterid) {
    if (!$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
        $chapterid = 0;
    }
} else {
    $chapter = false;
}
Petr Skoda's avatar
Petr Skoda committed
51

52
53
54
$PAGE->set_title(format_string($book->name));
$PAGE->add_body_class('mod_book');
$PAGE->set_heading(format_string($course->fullname));
Petr Skoda's avatar
Petr Skoda committed
55
56

///prepare the page header
57
58
$strbook = get_string('modulename', 'mod_book');
$strbooks = get_string('modulenameplural', 'mod_book');
Petr Skoda's avatar
Petr Skoda committed
59

Petr Skoda's avatar
Petr Skoda committed
60
$mform = new booktool_importhtml_form(null, array('id'=>$id, 'chapterid'=>$chapterid));
Petr Skoda's avatar
Petr Skoda committed
61

62
63
/// If data submitted, then process and store.
if ($mform->is_cancelled()) {
64
    if (empty($chapter->id)) {
65
66
67
        redirect("view.php?id=$cm->id");
    } else {
        redirect("view.php?id=$cm->id&chapterid=$chapter->id");
Petr Skoda's avatar
Petr Skoda committed
68
    }
69

70
} else if ($data = $mform->get_data()) {
Petr Skoda's avatar
Petr Skoda committed
71
72
73
74
75
76
77
78
    echo $OUTPUT->header();
    echo $OUTPUT->heading(get_string('importingchapters', 'booktool_importhtml'));

    // this is a bloody hack - children do not try this at home!
    $fs = get_file_storage();
    $draftid = file_get_submitted_draft_itemid('importfile');
    if (!$files = $fs->get_area_files(get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false)) {
        redirect($PAGE->url);
Petr Skoda's avatar
Petr Skoda committed
79
    }
Petr Skoda's avatar
Petr Skoda committed
80
81
    $file = reset($files);
    toolbook_importhtml_import_chapters($file, $data->type, $book, $context);
Petr Skoda's avatar
Petr Skoda committed
82

Petr Skoda's avatar
Petr Skoda committed
83
84
    echo $OUTPUT->continue_button(new moodle_url('/mod/book/view.php', array('id'=>$id)));
    echo $OUTPUT->footer();
85
    die;
86
}
87

88
echo $OUTPUT->header();
Petr Skoda's avatar
Petr Skoda committed
89
echo $OUTPUT->heading(get_string('import', 'booktool_importhtml'));
90
91
92

$mform->display();

93
echo $OUTPUT->footer();