view.php 8.9 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 view page
 *
20
21
 * @package    mod_book
 * @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

25
26
27
require(__DIR__.'/../../config.php');
require_once(__DIR__.'/lib.php');
require_once(__DIR__.'/locallib.php');
Petr Skoda's avatar
Petr Skoda committed
28
require_once($CFG->libdir.'/completionlib.php');
Petr Skoda's avatar
Petr Skoda committed
29

Petr Skoda's avatar
Petr Skoda committed
30
31
$id        = optional_param('id', 0, PARAM_INT);        // Course Module ID
$bid       = optional_param('b', 0, PARAM_INT);         // Book id
Petr Skoda's avatar
Petr Skoda committed
32
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
33
$edit      = optional_param('edit', -1, PARAM_BOOL);    // Edit mode
Petr Skoda's avatar
Petr Skoda committed
34
35
36
37

// =========================================================================
// security checks START - teachers edit; students view
// =========================================================================
Petr Skoda's avatar
Petr Skoda committed
38
39
40
41
42
43
if ($id) {
    $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);
} else {
    $book = $DB->get_record('book', array('id'=>$bid), '*', MUST_EXIST);
44
    $cm = get_coursemodule_from_instance('book', $book->id, 0, false, MUST_EXIST);
Petr Skoda's avatar
Petr Skoda committed
45
46
47
    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
    $id = $cm->id;
}
Petr Skoda's avatar
Petr Skoda committed
48

49
50
require_course_login($course, true, $cm);

51
$context = context_module::instance($cm->id);
52
53
require_capability('mod/book:read', $context);

54
55
$allowedit  = has_capability('mod/book:edit', $context);
$viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
56
57

if ($allowedit) {
Petr Skoda's avatar
Petr Skoda committed
58
    if ($edit != -1 and confirm_sesskey()) {
Petr Skoda's avatar
Petr Skoda committed
59
60
61
62
63
64
65
66
67
68
69
70
        $USER->editing = $edit;
    } else {
        if (isset($USER->editing)) {
            $edit = $USER->editing;
        } else {
            $edit = 0;
        }
    }
} else {
    $edit = 0;
}

71
// read chapters
72
$chapters = book_preload_chapters($book);
Petr Skoda's avatar
Petr Skoda committed
73

74
if ($allowedit and !$chapters) {
75
    redirect('edit.php?cmid='.$cm->id); // No chapters - add new one.
Petr Skoda's avatar
Petr Skoda committed
76
}
77
78
// Check chapterid and read chapter data
if ($chapterid == '0') { // Go to first chapter if no given.
79
80
    // Trigger course module viewed event.
    book_view($book, null, false, $course, $cm, $context);
81

82
    foreach ($chapters as $ch) {
Petr Skoda's avatar
Petr Skoda committed
83
        if ($edit) {
Petr Skoda's avatar
Petr Skoda committed
84
85
86
87
88
89
90
91
92
93
            $chapterid = $ch->id;
            break;
        }
        if (!$ch->hidden) {
            $chapterid = $ch->id;
            break;
        }
    }
}

94
$courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
95

96
97
98
99
100
101
102
103
// No content in the book.
if (!$chapterid) {
    $PAGE->set_url('/mod/book/view.php', array('id' => $id));
    notice(get_string('nocontent', 'mod_book'), $courseurl->out(false));
}
// Chapter doesnt exist or it is hidden for students
if ((!$chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id))) or ($chapter->hidden and !$viewhidden)) {
    print_error('errorchapter', 'mod_book', $courseurl);
104
}
105

Petr Skoda's avatar
Petr Skoda committed
106
$PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
Petr Skoda's avatar
Petr Skoda committed
107
108


109
// Unset all page parameters.
Petr Skoda's avatar
Petr Skoda committed
110
unset($id);
Petr Skoda's avatar
Petr Skoda committed
111
unset($bid);
Petr Skoda's avatar
Petr Skoda committed
112
113
unset($chapterid);

114
// Read standard strings.
115
116
117
$strbooks = get_string('modulenameplural', 'mod_book');
$strbook  = get_string('modulename', 'mod_book');
$strtoc   = get_string('toc', 'mod_book');
Petr Skoda's avatar
Petr Skoda committed
118

119
// prepare header
120
121
$pagetitle = $book->name . ": " . $chapter->title;
$PAGE->set_title($pagetitle);
122
$PAGE->set_heading($course->fullname);
123
124
125

book_add_fake_block($chapters, $chapter, $book, $cm, $edit);

126
// prepare chapter navigation icons
Petr Skoda's avatar
Petr Skoda committed
127
$previd = null;
128
$prevtitle = null;
129
$navprevtitle = null;
Petr Skoda's avatar
Petr Skoda committed
130
$nextid = null;
131
$nexttitle = null;
132
$navnexttitle = null;
Petr Skoda's avatar
Petr Skoda committed
133
$last = null;
Petr Skoda's avatar
Petr Skoda committed
134
foreach ($chapters as $ch) {
Petr Skoda's avatar
Petr Skoda committed
135
    if (!$edit and $ch->hidden) {
Petr Skoda's avatar
Petr Skoda committed
136
        continue;
Petr Skoda's avatar
Petr Skoda committed
137
    }
Petr Skoda's avatar
Petr Skoda committed
138
139
    if ($last == $chapter->id) {
        $nextid = $ch->id;
140
        $nexttitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
141
        $navnexttitle = get_string('navnexttitle', 'mod_book', $nexttitle);
Petr Skoda's avatar
Petr Skoda committed
142
        break;
Petr Skoda's avatar
Petr Skoda committed
143
    }
Petr Skoda's avatar
Petr Skoda committed
144
    if ($ch->id != $chapter->id) {
Petr Skoda's avatar
Petr Skoda committed
145
        $previd = $ch->id;
146
        $prevtitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
147
        $navprevtitle = get_string('navprevtitle', 'mod_book', $prevtitle);
Petr Skoda's avatar
Petr Skoda committed
148
    }
Petr Skoda's avatar
Petr Skoda committed
149
    $last = $ch->id;
Petr Skoda's avatar
Petr Skoda committed
150
}
Petr Skoda's avatar
Petr Skoda committed
151

152
$islastchapter = false;
153
154
155
156
157
158
159
160
161
if ($book->navstyle) {
    $navprevicon = right_to_left() ? 'nav_next' : 'nav_prev';
    $navnexticon = right_to_left() ? 'nav_prev' : 'nav_next';
    $navprevdisicon = right_to_left() ? 'nav_next_dis' : 'nav_prev_dis';

    $chnavigation = '';
    if ($previd) {
        $navprev = get_string('navprev', 'book');
        if ($book->navstyle == 1) {
162
            $chnavigation .= '<a title="' . $navprevtitle . '" class="bookprev" href="view.php?id=' .
163
                $cm->id . '&amp;chapterid=' . $previd .  '">' .
164
                $OUTPUT->pix_icon($navprevicon, $navprevtitle, 'mod_book') . '</a>';
165
166
167
168
169
170
171
172
        } else {
            $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
                $cm->id . '&amp;chapterid=' . $previd . '">' .
                '<span class="chaptername"><span class="arrow">' . $OUTPUT->larrow() . '&nbsp;</span></span>' .
                $navprev . ':&nbsp;<span class="chaptername">' . $prevtitle . '</span></a>';
        }
    } else {
        if ($book->navstyle == 1) {
173
            $chnavigation .= $OUTPUT->pix_icon($navprevdisicon, '', 'mod_book');
174
175
176
177
178
        }
    }
    if ($nextid) {
        $navnext = get_string('navnext', 'book');
        if ($book->navstyle == 1) {
179
            $chnavigation .= '<a title="' . $navnexttitle . '" class="booknext" href="view.php?id=' .
180
                $cm->id . '&amp;chapterid='.$nextid.'">' .
181
                $OUTPUT->pix_icon($navnexticon, $navnexttitle, 'mod_book') . '</a>';
182
183
184
185
186
187
188
189
190
191
192
193
        } else {
            $chnavigation .= ' <a title="' . $navnext . '" class="booknext" href="view.php?id=' .
                $cm->id . '&amp;chapterid='.$nextid.'">' .
                $navnext . ':<span class="chaptername">&nbsp;' . $nexttitle.
                '<span class="arrow">&nbsp;' . $OUTPUT->rarrow() . '</span></span></a>';
        }
    } else {
        $navexit = get_string('navexit', 'book');
        $sec = $DB->get_field('course_sections', 'section', array('id' => $cm->section));
        $returnurl = course_get_url($course, $sec);
        if ($book->navstyle == 1) {
            $chnavigation .= '<a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
194
                $OUTPUT->pix_icon('nav_exit', $navexit, 'mod_book') . '</a>';
195
196
197
198
199
        } else {
            $chnavigation .= ' <a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
                '<span class="chaptername">' . $navexit . '&nbsp;' . $OUTPUT->uarrow() . '</span></a>';
        }

200
        $islastchapter = true;
201
    }
Petr Skoda's avatar
Petr Skoda committed
202
203
}

204
205
book_view($book, $chapter, $islastchapter, $course, $cm, $context);

Petr Skoda's avatar
Petr Skoda committed
206
207
208
209
// =====================================================
// Book display HTML code
// =====================================================

210
echo $OUTPUT->header();
211
echo $OUTPUT->heading(format_string($book->name));
212

213
214
215
216
217
218
$navclasses = book_get_nav_classes();

if ($book->navstyle) {
    // Upper navigation.
    echo '<div class="navtop clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
}
Petr Skoda's avatar
Petr Skoda committed
219

220
// The chapter itself.
221
222
223
$hidden = $chapter->hidden ? ' dimmed_text' : null;
echo $OUTPUT->box_start('generalbox book_content' . $hidden);

Petr Skoda's avatar
Petr Skoda committed
224
if (!$book->customtitles) {
225
226
    if (!$chapter->subchapter) {
        $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
227
        echo $OUTPUT->heading($currtitle, 3);
228
229
230
    } else {
        $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
        $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
231
232
        echo $OUTPUT->heading($currtitle, 3);
        echo $OUTPUT->heading($currsubtitle, 4);
233
    }
Petr Skoda's avatar
Petr Skoda committed
234
235
}
$chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
236
echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'overflowdiv'=>true, 'context'=>$context));
Petr Skoda's avatar
Petr Skoda committed
237

Petr Skoda's avatar
Petr Skoda committed
238
239
echo $OUTPUT->box_end();

240
241
242
243
if ($book->navstyle) {
    // Lower navigation.
    echo '<div class="navbottom clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
}
Petr Skoda's avatar
Petr Skoda committed
244

Petr Skoda's avatar
Petr Skoda committed
245
echo $OUTPUT->footer();