index.php 5.08 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

26
27
die('TODO');

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

32
33
$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
34

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

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

41
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
42
require_capability('mod/book:import', $context);
Petr Skoda's avatar
Petr Skoda committed
43

44
$PAGE->set_url('/mod/book/import.php', array('id'=>$id, 'chapterid'=>$chapterid));
Petr Skoda's avatar
Petr Skoda committed
45

46
47
48
49
50
51
52
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
53

54
55
56
$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
57
58

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

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

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

72
73
74
} else if ($data = $mform->get_data()) {


75

76
77
/*

Petr Skoda's avatar
Petr Skoda committed
78
    $coursebase = $CFG->dataroot.'/'.$book->course;
79
80
81
82

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

    if ($reference == '') {
Petr Skoda's avatar
Petr Skoda committed
83
84
        $base = $coursebase;
    } else {
85
        $base = $coursebase.'/'.$reference;
Petr Skoda's avatar
Petr Skoda committed
86
87
88
89
90
91
92
93
94
    }

    //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;
95
            if (filetype($path) == 'file' and preg_match($htmlpat, $file)) {
Petr Skoda's avatar
Petr Skoda committed
96
97
98
99
100
                $refs[] = str_replace($coursebase, '', $path);
            }
        }
        asort($refs);
    } else if (is_file($base)) { //import single file
101
        $refs[] = '/'.$reference;
Petr Skoda's avatar
Petr Skoda committed
102
103
104
105
    } else { //what is it???
        error('Incorrect file/directory specified!');
    }

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

Petr Skoda's avatar
Petr Skoda committed
108
    //import files
109
110
    print_box_start('generalbox boxaligncenter centerpara');
    echo '<strong>'.get_string('importing', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
111
    echo '<table cellpadding="2" cellspacing="2" border="1">';
112
    book_preload_chapters($book); // fix structure
Petr Skoda's avatar
Petr Skoda committed
113
114
115
    foreach($refs as $ref) {
        $chapter = book_read_chapter($coursebase, $ref);
        if ($chapter) {
Petr Skoda's avatar
Petr Skoda committed
116
            $chapter->bookid       = $book->id;
Petr Skoda's avatar
Petr Skoda committed
117
            $chapter->pagenum      = $DB->count_records('book_chapters', array('bookid'=>$book->id)+1);
Petr Skoda's avatar
Petr Skoda committed
118
            $chapter->timecreated  = time();
Petr Skoda's avatar
Petr Skoda committed
119
120
            $chapter->timemodified = time();
            echo "imsrc:".$chapter->importsrc;
121
            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
122
123
124
125
                $chapter->subchapter = 1;
            } else {
                $chapter->subchapter = 0;
            }
Petr Skoda's avatar
Petr Skoda committed
126
127
            $chapter->id = $DB->insert_record('book_chapters', $chapter);

Petr Skoda's avatar
Petr Skoda committed
128
129
130
131
132
            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 />';
133
    echo '<strong>'.get_string('relinking', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
134
135
136
137
    echo '<table cellpadding="2" cellspacing="2" border="1">';
    //relink whole book = all chapters
    book_relink($cm->id, $book->id, $course->id);
    echo '</table><br />';
138
139
140
141
142
    print_box_end();
    print_continue('view.php?id='.$cm->id);
    print_footer($course);
    die;

143
144
 */
}
145

146
echo $OUTPUT->header();
147
echo $OUTPUT->heading(get_string('importingchapters', 'mod_book'));
148
149
150

$mform->display();

151
echo $OUTPUT->footer();