import.php 5.4 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
20
21
22
23
24
// 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
 *
 * @package    mod
 * @subpackage book
 * @copyright  2004-2010 Petr Skoda  {@link http://skodak.org}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
Petr Skoda's avatar
Petr Skoda committed
25

26
require('../../config.php');
Petr Skoda's avatar
Petr Skoda committed
27
require_once($CFG->dirroot.'/mod/book/locallib.php');
28
require_once('import_form.php');
Petr Skoda's avatar
Petr Skoda committed
29

30
$id = required_param('id', PARAM_INT);           // Course Module ID
Petr Skoda's avatar
Petr Skoda committed
31
32
33
34
35

// =========================================================================
// security checks START - only teachers edit
// =========================================================================

Petr Skoda's avatar
Petr Skoda committed
36
37
38
$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
39

40
41
require_login($course, false, $cm);

42
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
43
require_capability('mod/book:import', $context);
Petr Skoda's avatar
Petr Skoda committed
44
45
46
47
48
49
50
51
52
53
54
55
56

//check all variables
unset($id);

// =========================================================================
// security checks END
// =========================================================================

///prepare the page header
$strbook = get_string('modulename', 'book');
$strbooks = get_string('modulenameplural', 'book');
$strimport = get_string('import', 'book');

57
58
59
60
$navlinks = array();
$navlinks[] = array('name' => $strimport, 'link' => '', 'type' => 'title');

$navigation = build_navigation($navlinks, $cm);
Petr Skoda's avatar
Petr Skoda committed
61

62
63
$mform = new book_import_form(null, $cm);
$mform->set_data(array('id'=>$cm->id));
Petr Skoda's avatar
Petr Skoda committed
64

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

} else if ($data = $mform->get_data(false)) {
Petr Skoda's avatar
Petr Skoda committed
74
    $coursebase = $CFG->dataroot.'/'.$book->course;
75
76
77
78

    $reference = book_prepare_link($data->reference);

    if ($reference == '') {
Petr Skoda's avatar
Petr Skoda committed
79
80
        $base = $coursebase;
    } else {
81
        $base = $coursebase.'/'.$reference;
Petr Skoda's avatar
Petr Skoda committed
82
83
84
85
86
87
88
89
90
    }

    //prepare list of html files in $refs
    $refs = array();
    $htmlpat = '/\.html$|\.htm$/i';
    if (is_dir($base)) { //import whole directory
        $basedir = opendir($base);
        while ($file = readdir($basedir)) {
            $path = $base.'/'.$file;
91
            if (filetype($path) == 'file' and preg_match($htmlpat, $file)) {
Petr Skoda's avatar
Petr Skoda committed
92
93
94
95
96
                $refs[] = str_replace($coursebase, '', $path);
            }
        }
        asort($refs);
    } else if (is_file($base)) { //import single file
97
        $refs[] = '/'.$reference;
Petr Skoda's avatar
Petr Skoda committed
98
99
100
101
    } else { //what is it???
        error('Incorrect file/directory specified!');
    }

102
103
    print_header("$course->shortname: $book->name", $course->fullname, $navigation);

Petr Skoda's avatar
Petr Skoda committed
104
    //import files
105
106
    print_box_start('generalbox boxaligncenter centerpara');
    echo '<strong>'.get_string('importing', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
107
108
109
110
111
    echo '<table cellpadding="2" cellspacing="2" border="1">';
    book_check_structure($book->id);
    foreach($refs as $ref) {
        $chapter = book_read_chapter($coursebase, $ref);
        if ($chapter) {
Petr Skoda's avatar
Petr Skoda committed
112
113
114
            $chapter->bookid       = $book->id;
            $chapter->pagenum      = $DB->count_records('book_chapters', array('bookid'=>$book->id)+1;
            $chapter->timecreated  = time();
Petr Skoda's avatar
Petr Skoda committed
115
116
            $chapter->timemodified = time();
            echo "imsrc:".$chapter->importsrc;
117
            if (($data->subchapter) || preg_match('/_sub\.htm/i', $chapter->importsrc)) { //if filename or directory starts with sub_* treat as subdirecotories
Petr Skoda's avatar
Petr Skoda committed
118
119
120
121
                $chapter->subchapter = 1;
            } else {
                $chapter->subchapter = 0;
            }
Petr Skoda's avatar
Petr Skoda committed
122
123
            $chapter->id = $DB->insert_record('book_chapters', $chapter);

Petr Skoda's avatar
Petr Skoda committed
124
125
126
127
128
            add_to_log($course->id, 'course', 'update mod', '../mod/book/view.php?id='.$cm->id, 'book '.$book->id);
            add_to_log($course->id, 'book', 'update', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
        }
    }
    echo '</table><br />';
129
    echo '<strong>'.get_string('relinking', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
130
131
132
133
    echo '<table cellpadding="2" cellspacing="2" border="1">';
    //relink whole book = all chapters
    book_relink($cm->id, $book->id, $course->id);
    echo '</table><br />';
134
135
136
137
    print_box_end();
    print_continue('view.php?id='.$cm->id);
    print_footer($course);
    die;
Petr Skoda's avatar
Petr Skoda committed
138
139
}

140
141
142
143
144
145
print_header("$course->shortname: $book->name", $course->fullname, $navigation);

$strdoimport = get_string('doimport', 'book');
$strchoose = get_string('choose');
$pageheading = get_string('importingchapters', 'book');

Petr Skoda's avatar
Petr Skoda committed
146
$icon = '<img class="icon" src="pix/chapter.gif" alt="" />&nbsp;';
147
148
149
150
print_heading_with_help($pageheading, 'import', 'book', $icon);

$mform->display();

Petr Skoda's avatar
Petr Skoda committed
151
152
print_footer($course);