MDL-32709 book: coding style, deprecated and minor fixes
[moodle.git] / mod / book / view.php
1 <?php
2 // This file is part of Book module for Moodle - http://moodle.org/
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/>.
17 /**
18  * Book view page
19  *
20  * @package    mod_book
21  * @copyright  2004-2011 Petr Skoda {@link http://skodak.org}
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
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 // =========================================================================
37 if ($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);
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);
45     $id = $cm->id;
46 }
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);
56 if ($allowedit) {
57     if ($edit != -1 and confirm_sesskey()) {
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 }
70 // read chapters
71 $chapters = book_preload_chapters($book);
73 if ($allowedit and !$chapters) {
74     redirect('edit.php?cmid='.$cm->id); // No chapters - add new one.
75 }
76 // Check chapterid and read chapter data
77 if ($chapterid == '0') { // Go to first chapter if no given.
78     foreach ($chapters as $ch) {
79         if ($edit) {
80             $chapterid = $ch->id;
81             break;
82         }
83         if (!$ch->hidden) {
84             $chapterid = $ch->id;
85             break;
86         }
87     }
88 }
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)));
92 }
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)));
97 }
99 $PAGE->set_url('/mod/book/view.php', array('id'=>$id, 'chapterid'=>$chapterid));
102 // Unset all page parameters.
103 unset($id);
104 unset($bid);
105 unset($chapterid);
107 // Security checks END.
109 add_to_log($course->id, 'book', 'view', 'view.php?id='.$cm->id.'&amp;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');
116 // prepare header
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
124 $previd = null;
125 $nextid = null;
126 $last = null;
127 foreach ($chapters as $ch) {
128     if (!$edit and $ch->hidden) {
129         continue;
130     }
131     if ($last == $chapter->id) {
132         $nextid = $ch->id;
133         break;
134     }
135     if ($ch->id != $chapter->id) {
136         $previd = $ch->id;
137     }
138     $last = $ch->id;
141 $chnavigation = '';
142 if ($previd) {
143     $chnavigation .= '<a title="'.get_string('navprev', 'book').'" href="view.php?id='.$cm->id.
144             '&amp;chapterid='.$previd.'"><img src="'.$OUTPUT->pix_url('nav_prev', 'mod_book').'" class="bigicon" alt="'.get_string('navprev', 'book').'"/></a>';
145 } else {
146     $chnavigation .= '<img src="'.$OUTPUT->pix_url('nav_prev_dis', 'mod_book').'" class="bigicon" alt="" />';
148 if ($nextid) {
149     $chnavigation .= '<a title="'.get_string('navnext', 'book').'" href="view.php?id='.$cm->id.
150             '&amp;chapterid='.$nextid.'"><img src="'.$OUTPUT->pix_url('nav_next', 'mod_book').'" class="bigicon" alt="'.get_string('navnext', 'book').'" /></a>';
151 } else {
152     $sec = '';
153     if ($section = $DB->get_record('course_sections', array('id'=>$cm->section))) {
154         $sec = $section->section;
155     }
156     if ($course->id == $SITE->id) {
157         $returnurl = "$CFG->wwwroot/";
158     } else {
159         $returnurl = "$CFG->wwwroot/course/view.php?id=$course->id#section-$sec";
160     }
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();
175 // upper nav
176 echo '<div class="navtop">'.$chnavigation.'</div>';
178 // chapter itself
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>';
185     } else {
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>';
189     }
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();
196 // lower navigation
197 echo '<div class="navbottom">'.$chnavigation.'</div>';
199 echo $OUTPUT->footer();