2 // This file is part of Book module for Moodle - http://moodle.org/
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.
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.
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/>.
21 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require(dirname(__FILE__).'/../../config.php');
26 require_once(dirname(__FILE__).'/locallib.php');
27 require_once($CFG->libdir.'/completionlib.php');
29 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
30 $bid = optional_param('b', 0, PARAM_INT); // Book id
31 $chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
32 $edit = optional_param('edit', -1, PARAM_BOOL); // Edit mode
34 // =========================================================================
35 // security checks START - teachers edit; students view
36 // =========================================================================
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);
42 $book = $DB->get_record('book', array('id'=>$bid), '*', MUST_EXIST);
43 $cm = get_coursemodule_from_instance('book', $book->id, 0, false, MUST_EXIST);
44 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
48 require_course_login($course, true, $cm);
50 $context = context_module::instance($cm->id);
51 require_capability('mod/book:read', $context);
53 $allowedit = has_capability('mod/book:edit', $context);
54 $viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
57 if ($edit != -1 and confirm_sesskey()) {
58 $USER->editing = $edit;
60 if (isset($USER->editing)) {
61 $edit = $USER->editing;
71 $chapters = book_preload_chapters($book);
73 if ($allowedit and !$chapters) {
74 redirect('edit.php?cmid='.$cm->id); // No chapters - add new one.
76 // Check chapterid and read chapter data
77 if ($chapterid == '0') { // Go to first chapter if no given.
78 foreach ($chapters as $ch) {
90 if (!$chapterid or !$chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id))) {
91 print_error('errorchapter', 'mod_book', new moodle_url('/course/view.php', array('id'=>$course->id)));
94 // chapter is hidden for students
95 if ($chapter->hidden and !$viewhidden) {
96 print_error('errorchapter', 'mod_book', new moodle_url('/course/view.php', array('id'=>$course->id)));
99 $PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
102 // Unset all page parameters.
107 // Security checks END.
109 add_to_log($course->id, 'book', 'view', 'view.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
111 // Read standard strings.
112 $strbooks = get_string('modulenameplural', 'mod_book');
113 $strbook = get_string('modulename', 'mod_book');
114 $strtoc = get_string('toc', 'mod_book');
117 $PAGE->set_title(format_string($book->name));
118 $PAGE->add_body_class('mod_book');
119 $PAGE->set_heading(format_string($course->fullname));
121 book_add_fake_block($chapters, $chapter, $book, $cm, $edit);
123 // prepare chapter navigation icons
127 foreach ($chapters as $ch) {
128 if (!$edit and $ch->hidden) {
131 if ($last == $chapter->id) {
135 if ($ch->id != $chapter->id) {
143 $chnavigation .= '<a title="'.get_string('navprev', 'book').'" href="view.php?id='.$cm->id.
144 '&chapterid='.$previd.'"><img src="'.$OUTPUT->pix_url('nav_prev', 'mod_book').'" class="bigicon" alt="'.get_string('navprev', 'book').'"/></a>';
146 $chnavigation .= '<img src="'.$OUTPUT->pix_url('nav_prev_dis', 'mod_book').'" class="bigicon" alt="" />';
149 $chnavigation .= '<a title="'.get_string('navnext', 'book').'" href="view.php?id='.$cm->id.
150 '&chapterid='.$nextid.'"><img src="'.$OUTPUT->pix_url('nav_next', 'mod_book').'" class="bigicon" alt="'.get_string('navnext', 'book').'" /></a>';
153 if ($section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
154 $sec = $section->section;
156 if ($course->id == $SITE->id) {
157 $returnurl = "$CFG->wwwroot/";
159 $returnurl = "$CFG->wwwroot/course/view.php?id=$course->id#section-$sec";
161 $chnavigation .= '<a title="'.get_string('navexit', 'book').'" href="'.$returnurl.'"><img src="'.$OUTPUT->pix_url('nav_exit', 'mod_book').
162 '" class="bigicon" alt="'.get_string('navexit', 'book').'" /></a>';
164 // we are cheating a bit here, viewing the last page means user has viewed the whole book
165 $completion = new completion_info($course);
166 $completion->set_module_viewed($cm);
169 // =====================================================
170 // Book display HTML code
171 // =====================================================
173 echo $OUTPUT->header();
176 echo '<div class="navtop">'.$chnavigation.'</div>';
179 echo $OUTPUT->box_start('generalbox book_content');
180 if (!$book->customtitles) {
181 $hidden = $chapter->hidden ? 'dimmed_text' : '';
182 if (!$chapter->subchapter) {
183 $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
184 echo '<p class="book_chapter_title '.$hidden.'">'.$currtitle.'</p>';
186 $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
187 $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
188 echo '<p class="book_chapter_title '.$hidden.'">'.$currtitle.'<br />'.$currsubtitle.'</p>';
191 $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
192 echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
194 echo $OUTPUT->box_end();
197 echo '<div class="navbottom">'.$chnavigation.'</div>';
199 echo $OUTPUT->footer();