import.php 5.04 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
// 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
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
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
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);
40
require_capability('mod/book:import', $context);
Petr Skoda's avatar
Petr Skoda committed
41

42
$PAGE->set_url('/mod/book/import.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
57
58
59

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

60
$mform = new book_import_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
71
72
73
74
75
} else if ($data = $mform->get_data()) {


    die('TODO');
/*

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

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

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

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

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

Petr Skoda's avatar
Petr Skoda committed
106
    //import files
107
108
    print_box_start('generalbox boxaligncenter centerpara');
    echo '<strong>'.get_string('importing', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
109
110
111
112
113
    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
114
            $chapter->bookid       = $book->id;
Petr Skoda's avatar
Petr Skoda committed
115
            $chapter->pagenum      = $DB->count_records('book_chapters', array('bookid'=>$book->id)+1);
Petr Skoda's avatar
Petr Skoda committed
116
            $chapter->timecreated  = time();
Petr Skoda's avatar
Petr Skoda committed
117
118
            $chapter->timemodified = time();
            echo "imsrc:".$chapter->importsrc;
119
            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
120
121
122
123
                $chapter->subchapter = 1;
            } else {
                $chapter->subchapter = 0;
            }
Petr Skoda's avatar
Petr Skoda committed
124
125
            $chapter->id = $DB->insert_record('book_chapters', $chapter);

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

141
142
 */
}
143

144
145
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('importingchapters', 'book'));
146
147
148

$mform->display();

149
echo $OUTPUT->footer();