index.php 3.14 KB
Newer Older
1
<?php
2
// This file is part of Moodle - http://moodle.org/
Petr Skoda's avatar
Petr Skoda committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// 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_importhtml
 * @copyright  2004-2011 Petr Skoda {@link http://skodak.org}
Petr Skoda's avatar
Petr Skoda committed
22
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Petr Skoda's avatar
Petr Skoda committed
24

Petr Skoda's avatar
Petr Skoda committed
25
26
27
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
28

29
30
$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
31

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

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

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

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

43
44
45
46
47
48
49
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
50

51
52
$PAGE->set_title($book->name);
$PAGE->set_heading($course->fullname);
Petr Skoda's avatar
Petr Skoda committed
53

54
// Prepare the page header.
55
56
$strbook = get_string('modulename', 'mod_book');
$strbooks = get_string('modulenameplural', 'mod_book');
Petr Skoda's avatar
Petr Skoda committed
57

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

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

68
} else if ($data = $mform->get_data()) {
Petr Skoda's avatar
Petr Skoda committed
69
70
71
72
73
74
    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');
75
    if (!$files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $draftid, 'id DESC', false)) {
Petr Skoda's avatar
Petr Skoda committed
76
        redirect($PAGE->url);
Petr Skoda's avatar
Petr Skoda committed
77
    }
Petr Skoda's avatar
Petr Skoda committed
78
79
    $file = reset($files);
    toolbook_importhtml_import_chapters($file, $data->type, $book, $context);
Petr Skoda's avatar
Petr Skoda committed
80

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

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

$mform->display();

91
echo $OUTPUT->footer();