import.php 4.82 KB
Newer Older
1
<?php
Petr Skoda's avatar
Petr Skoda committed
2

3
require('../../config.php');
Petr Skoda's avatar
Petr Skoda committed
4
require_once($CFG->dirroot.'/mod/book/locallib.php');
5
require_once('import_form.php');
Petr Skoda's avatar
Petr Skoda committed
6

7
$id = required_param('id', PARAM_INT);           // Course Module ID
Petr Skoda's avatar
Petr Skoda committed
8
9
10
11
12

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

13
if (!$cm = get_coursemodule_from_id('book', $id)) {
Petr Skoda's avatar
Petr Skoda committed
14
15
16
17
18
19
20
    error('Course Module ID was incorrect');
}

if (!$course = get_record('course', 'id', $cm->course)) {
    error('Course is misconfigured');
}

21
22
require_login($course, false, $cm);

23
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
24
require_capability('mod/book:import', $context);
Petr Skoda's avatar
Petr Skoda committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

if (!$book = get_record('book', 'id', $cm->instance)) {
    error('Course module is incorrect');
}

//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');

42
43
44
45
$navlinks = array();
$navlinks[] = array('name' => $strimport, 'link' => '', 'type' => 'title');

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

47
48
$mform = new book_import_form(null, $cm);
$mform->set_data(array('id'=>$cm->id));
Petr Skoda's avatar
Petr Skoda committed
49

50
51
52
53
54
55
/// 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
56
    }
57
58

} else if ($data = $mform->get_data(false)) {
Petr Skoda's avatar
Petr Skoda committed
59
    $coursebase = $CFG->dataroot.'/'.$book->course;
60
61
62
63

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

    if ($reference == '') {
Petr Skoda's avatar
Petr Skoda committed
64
65
        $base = $coursebase;
    } else {
66
        $base = $coursebase.'/'.$reference;
Petr Skoda's avatar
Petr Skoda committed
67
68
69
70
71
72
73
74
75
    }

    //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;
76
            if (filetype($path) == 'file' and preg_match($htmlpat, $file)) {
Petr Skoda's avatar
Petr Skoda committed
77
78
79
80
81
                $refs[] = str_replace($coursebase, '', $path);
            }
        }
        asort($refs);
    } else if (is_file($base)) { //import single file
82
        $refs[] = '/'.$reference;
Petr Skoda's avatar
Petr Skoda committed
83
84
85
86
    } else { //what is it???
        error('Incorrect file/directory specified!');
    }

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

Petr Skoda's avatar
Petr Skoda committed
89
    //import files
90
91
    print_box_start('generalbox boxaligncenter centerpara');
    echo '<strong>'.get_string('importing', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
92
93
94
95
96
97
98
99
100
101
102
103
104
    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) {
            $chapter->title = addslashes($chapter->title);
            $chapter->content = addslashes($chapter->content);
            $chapter->importsrc = addslashes($chapter->importsrc);
            $chapter->bookid = $book->id;
            $chapter->pagenum = count_records('book_chapters', 'bookid', $book->id)+1;
            $chapter->timecreated = time();
            $chapter->timemodified = time();
            echo "imsrc:".$chapter->importsrc;
105
            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
106
107
108
109
110
111
112
113
114
115
116
117
                $chapter->subchapter = 1;
            } else {
                $chapter->subchapter = 0;
            }
            if (!$chapter->id = insert_record('book_chapters', $chapter)) {
                error('Could not update your book');
            }
            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 />';
118
    echo '<strong>'.get_string('relinking', 'book').':</strong>';
Petr Skoda's avatar
Petr Skoda committed
119
120
121
122
    echo '<table cellpadding="2" cellspacing="2" border="1">';
    //relink whole book = all chapters
    book_relink($cm->id, $book->id, $course->id);
    echo '</table><br />';
123
124
125
126
    print_box_end();
    print_continue('view.php?id='.$cm->id);
    print_footer($course);
    die;
Petr Skoda's avatar
Petr Skoda committed
127
128
}

129
130
131
132
133
134
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
135
$icon = '<img class="icon" src="pix/chapter.gif" alt="" />&nbsp;';
136
137
138
139
print_heading_with_help($pageheading, 'import', 'book', $icon);

$mform->display();

Petr Skoda's avatar
Petr Skoda committed
140
141
print_footer($course);