MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / view.php
CommitLineData
8d623d14 1<?php
b15ef0b0 2// This file is part of Moodle - http://moodle.org/
2c1e98e6
PS
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Book view page
19 *
3542c4f4
EL
20 * @package mod_book
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
02039379 24
74979715
PS
25require(dirname(__FILE__).'/../../config.php');
26require_once(dirname(__FILE__).'/locallib.php');
2890e4c1 27require_once($CFG->libdir.'/completionlib.php');
02039379 28
e6bf43cd
PS
29$id = optional_param('id', 0, PARAM_INT); // Course Module ID
30$bid = optional_param('b', 0, PARAM_INT); // Book id
02039379 31$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
c6d2eda2 32$edit = optional_param('edit', -1, PARAM_BOOL); // Edit mode
02039379
PS
33
34// =========================================================================
35// security checks START - teachers edit; students view
36// =========================================================================
e6bf43cd
PS
37if ($id) {
38 $cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
39 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
40 $book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
41} else {
42 $book = $DB->get_record('book', array('id'=>$bid), '*', MUST_EXIST);
c60d3ab3 43 $cm = get_coursemodule_from_instance('book', $book->id, 0, false, MUST_EXIST);
e6bf43cd
PS
44 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
45 $id = $cm->id;
46}
02039379 47
dab784a3
PS
48require_course_login($course, true, $cm);
49
1fc9e895 50$context = context_module::instance($cm->id);
c6d2eda2
PS
51require_capability('mod/book:read', $context);
52
ad1c11bd
PS
53$allowedit = has_capability('mod/book:edit', $context);
54$viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
dab784a3
PS
55
56if ($allowedit) {
9d2fe179 57 if ($edit != -1 and confirm_sesskey()) {
02039379
PS
58 $USER->editing = $edit;
59 } else {
60 if (isset($USER->editing)) {
61 $edit = $USER->editing;
62 } else {
63 $edit = 0;
64 }
65 }
66} else {
67 $edit = 0;
68}
69
3542c4f4 70// read chapters
b1a9b01e 71$chapters = book_preload_chapters($book);
02039379 72
ad1c11bd 73if ($allowedit and !$chapters) {
3542c4f4 74 redirect('edit.php?cmid='.$cm->id); // No chapters - add new one.
02039379 75}
3542c4f4
EL
76// Check chapterid and read chapter data
77if ($chapterid == '0') { // Go to first chapter if no given.
ef2a9ece 78 \mod_book\event\course_module_viewed::create_from_book($book, $context)->trigger();
7f6e3eef 79
29656fe2 80 foreach ($chapters as $ch) {
c5832d6b 81 if ($edit) {
02039379
PS
82 $chapterid = $ch->id;
83 break;
84 }
85 if (!$ch->hidden) {
86 $chapterid = $ch->id;
87 break;
88 }
89 }
90}
91
5219cc87 92$courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
ad1c11bd 93
5219cc87
AA
94// No content in the book.
95if (!$chapterid) {
96 $PAGE->set_url('/mod/book/view.php', array('id' => $id));
97 notice(get_string('nocontent', 'mod_book'), $courseurl->out(false));
98}
99// Chapter doesnt exist or it is hidden for students
100if ((!$chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id))) or ($chapter->hidden and !$viewhidden)) {
101 print_error('errorchapter', 'mod_book', $courseurl);
ad1c11bd 102}
758f7dde 103
f4e1849e 104$PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
02039379 105
02039379 106
3542c4f4 107// Unset all page parameters.
02039379 108unset($id);
e6bf43cd 109unset($bid);
02039379
PS
110unset($chapterid);
111
3542c4f4 112// Security checks END.
02039379 113
ef2a9ece 114\mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter)->trigger();
02039379 115
3542c4f4 116// Read standard strings.
1cc316b7
PS
117$strbooks = get_string('modulenameplural', 'mod_book');
118$strbook = get_string('modulename', 'mod_book');
119$strtoc = get_string('toc', 'mod_book');
02039379 120
3542c4f4 121// prepare header
93ec5539
AA
122$pagetitle = $book->name . ": " . $chapter->title;
123$PAGE->set_title($pagetitle);
5b7e511e 124$PAGE->set_heading($course->fullname);
ad1c11bd
PS
125
126book_add_fake_block($chapters, $chapter, $book, $cm, $edit);
127
3542c4f4 128// prepare chapter navigation icons
02039379 129$previd = null;
f4de8461 130$prevtitle = null;
02039379 131$nextid = null;
f4de8461 132$nexttitle = null;
86f6bb26 133$last = null;
02039379 134foreach ($chapters as $ch) {
c5832d6b 135 if (!$edit and $ch->hidden) {
86f6bb26 136 continue;
02039379 137 }
86f6bb26
PS
138 if ($last == $chapter->id) {
139 $nextid = $ch->id;
f4de8461 140 $nexttitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
86f6bb26 141 break;
02039379 142 }
86f6bb26 143 if ($ch->id != $chapter->id) {
02039379 144 $previd = $ch->id;
f4de8461 145 $prevtitle = book_get_chapter_title($ch->id, $chapters, $book, $context);
02039379 146 }
86f6bb26 147 $last = $ch->id;
02039379 148}
86f6bb26 149
bdb66a46 150
f4de8461
DB
151if ($book->navstyle) {
152 $navprevicon = right_to_left() ? 'nav_next' : 'nav_prev';
153 $navnexticon = right_to_left() ? 'nav_prev' : 'nav_next';
154 $navprevdisicon = right_to_left() ? 'nav_next_dis' : 'nav_prev_dis';
155
156 $chnavigation = '';
157 if ($previd) {
158 $navprev = get_string('navprev', 'book');
159 if ($book->navstyle == 1) {
160 $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
161 $cm->id . '&amp;chapterid=' . $previd . '">' .
162 '<img src="' . $OUTPUT->pix_url($navprevicon, 'mod_book') . '" class="icon" alt="' . $navprev . '"/></a>';
163 } else {
164 $chnavigation .= '<a title="' . $navprev . '" class="bookprev" href="view.php?id=' .
165 $cm->id . '&amp;chapterid=' . $previd . '">' .
166 '<span class="chaptername"><span class="arrow">' . $OUTPUT->larrow() . '&nbsp;</span></span>' .
167 $navprev . ':&nbsp;<span class="chaptername">' . $prevtitle . '</span></a>';
168 }
169 } else {
170 if ($book->navstyle == 1) {
171 $chnavigation .= '<img src="' . $OUTPUT->pix_url($navprevdisicon, 'mod_book') . '" class="icon" alt="" />';
172 }
173 }
174 if ($nextid) {
175 $navnext = get_string('navnext', 'book');
176 if ($book->navstyle == 1) {
177 $chnavigation .= '<a title="' . $navnext . '" class="booknext" href="view.php?id=' .
178 $cm->id . '&amp;chapterid='.$nextid.'">' .
179 '<img src="' . $OUTPUT->pix_url($navnexticon, 'mod_book').'" class="icon" alt="' . $navnext . '" /></a>';
180 } else {
181 $chnavigation .= ' <a title="' . $navnext . '" class="booknext" href="view.php?id=' .
182 $cm->id . '&amp;chapterid='.$nextid.'">' .
183 $navnext . ':<span class="chaptername">&nbsp;' . $nexttitle.
184 '<span class="arrow">&nbsp;' . $OUTPUT->rarrow() . '</span></span></a>';
185 }
186 } else {
187 $navexit = get_string('navexit', 'book');
188 $sec = $DB->get_field('course_sections', 'section', array('id' => $cm->section));
189 $returnurl = course_get_url($course, $sec);
190 if ($book->navstyle == 1) {
191 $chnavigation .= '<a title="' . $navexit . '" class="bookexit" href="'.$returnurl.'">' .
192 '<img src="' . $OUTPUT->pix_url('nav_exit', 'mod_book') . '" class="icon" alt="' . $navexit . '" /></a>';
193 } else {
194 $chnavigation .= ' <a title="' . $navexit . '" class="bookexit" href="'.$returnurl.'">' .
195 '<span class="chaptername">' . $navexit . '&nbsp;' . $OUTPUT->uarrow() . '</span></a>';
196 }
197
198 // We cheat a bit here in assuming that viewing the last page means the user viewed the whole book.
199 $completion = new completion_info($course);
200 $completion->set_module_viewed($cm);
201 }
02039379
PS
202}
203
02039379
PS
204// =====================================================
205// Book display HTML code
206// =====================================================
207
337b43a7 208echo $OUTPUT->header();
d8a6d49c 209echo $OUTPUT->heading($book->name);
337b43a7 210
f4de8461
DB
211$navclasses = book_get_nav_classes();
212
213if ($book->navstyle) {
214 // Upper navigation.
215 echo '<div class="navtop clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
216}
b52247a5 217
f4de8461 218// The chapter itself.
d8a6d49c
RW
219$hidden = $chapter->hidden ? ' dimmed_text' : null;
220echo $OUTPUT->box_start('generalbox book_content' . $hidden);
221
b52247a5 222if (!$book->customtitles) {
b1a9b01e
PS
223 if (!$chapter->subchapter) {
224 $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
d8a6d49c 225 echo $OUTPUT->heading($currtitle, 3);
b1a9b01e
PS
226 } else {
227 $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
228 $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
d8a6d49c
RW
229 echo $OUTPUT->heading($currtitle, 3);
230 echo $OUTPUT->heading($currsubtitle, 4);
b1a9b01e 231 }
b52247a5
PS
232}
233$chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
1bd6a2d0 234echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'overflowdiv'=>true, 'context'=>$context));
02039379 235
b52247a5
PS
236echo $OUTPUT->box_end();
237
f4de8461
DB
238if ($book->navstyle) {
239 // Lower navigation.
240 echo '<div class="navbottom clearfix ' . $navclasses[$book->navstyle] . '">' . $chnavigation . '</div>';
241}
f4e1849e 242
f19d9517 243echo $OUTPUT->footer();