view.php 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

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

Petr Skoda's avatar
Petr Skoda committed
29
30
$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
31
$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
32
$edit      = optional_param('edit', -1, PARAM_BOOL);    // Edit mode
Petr Skoda's avatar
Petr Skoda committed
33
34
35
36

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

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

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

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

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

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

73
if ($allowedit and !$chapters) {
74
    redirect('edit.php?cmid='.$cm->id); // No chapters - add new one.
Petr Skoda's avatar
Petr Skoda committed
75
}
76
77
// Check chapterid and read chapter data
if ($chapterid == '0') { // Go to first chapter if no given.
78
    \mod_book\event\course_module_viewed::create_from_book($book, $context)->trigger();
79

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

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

94
95
96
97
98
99
100
101
// 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);
102
}
103

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


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

112
// Security checks END.
Petr Skoda's avatar
Petr Skoda committed
113

114
\mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter)->trigger();
Petr Skoda's avatar
Petr Skoda committed
115

116
// Read standard strings.
117
118
119
$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
120

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

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

128
// prepare chapter navigation icons
Petr Skoda's avatar
Petr Skoda committed
129
$previd = null;
130
$prevtitle = null;
Petr Skoda's avatar
Petr Skoda committed
131
$nextid = null;
132
$nexttitle = 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);
Petr Skoda's avatar
Petr Skoda committed
141
        break;
Petr Skoda's avatar
Petr Skoda committed
142
    }
Petr Skoda's avatar
Petr Skoda committed
143
    if ($ch->id != $chapter->id) {
Petr Skoda's avatar
Petr Skoda committed
144
        $previd = $ch->id;
145
        $prevtitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
Petr Skoda's avatar
Petr Skoda committed
146
    }
Petr Skoda's avatar
Petr Skoda committed
147
    $last = $ch->id;
Petr Skoda's avatar
Petr Skoda committed
148
}
Petr Skoda's avatar
Petr Skoda committed
149

150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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) {
            $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
                $cm->id . '&amp;chapterid=' . $previd .  '">' .
                '<img src="' . $OUTPUT->pix_url($navprevicon, 'mod_book') . '" class="icon" alt="' . $navprev . '"/></a>';
        } 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) {
            $chnavigation .= '<img src="' . $OUTPUT->pix_url($navprevdisicon, 'mod_book') . '" class="icon" alt="" />';
        }
    }
    if ($nextid) {
        $navnext = get_string('navnext', 'book');
        if ($book->navstyle == 1) {
            $chnavigation .= '<a title="' . $navnext . '" class="booknext" href="view.php?id=' .
                $cm->id . '&amp;chapterid='.$nextid.'">' .
                '<img src="' . $OUTPUT->pix_url($navnexticon, 'mod_book').'" class="icon" alt="' . $navnext . '" /></a>';
        } 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.'">' .
                '<img src="' . $OUTPUT->pix_url('nav_exit', 'mod_book') . '" class="icon" alt="' . $navexit . '" /></a>';
        } else {
            $chnavigation .= ' <a title="' . $navexit . '" class="bookexit"  href="'.$returnurl.'">' .
                '<span class="chaptername">' . $navexit . '&nbsp;' . $OUTPUT->uarrow() . '</span></a>';
        }

        // We cheat a bit here in assuming that viewing the last page means the user viewed the whole book.
        $completion = new completion_info($course);
        $completion->set_module_viewed($cm);
    }
Petr Skoda's avatar
Petr Skoda committed
202
203
204
205
206
207
}

// =====================================================
// Book display HTML code
// =====================================================

208
echo $OUTPUT->header();
209
echo $OUTPUT->heading($book->name);
210

211
212
213
214
215
216
$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
217

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

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

Petr Skoda's avatar
Petr Skoda committed
236
237
echo $OUTPUT->box_end();

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

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