weekly release 3.7dev
[moodle.git] / mod / book / tool / print / index.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 printing
19 *
3542c4f4
EL
20 * @package booktool_print
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
1fcf0ca8
RS
25require(__DIR__.'/../../../../config.php');
26require_once(__DIR__.'/locallib.php');
02039379
PS
27
28$id = required_param('id', PARAM_INT); // Course Module ID
29$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
30
31// =========================================================================
32// security checks START - teachers and students view
33// =========================================================================
02039379 34
2c1e98e6 35$cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
74df2951 36$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
2c1e98e6 37$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
02039379 38
dab784a3 39require_course_login($course, true, $cm);
c6d2eda2 40
1fc9e895 41$context = context_module::instance($cm->id);
c6d2eda2 42require_capability('mod/book:read', $context);
7927cab1 43require_capability('booktool/print:print', $context);
c6d2eda2 44
3542c4f4 45// Check all variables.
02039379 46if ($chapterid) {
3542c4f4 47 // Single chapter printing - only visible!
ee952cbb 48 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
02039379 49} else {
3542c4f4 50 // Complete book.
02039379
PS
51 $chapter = false;
52}
56e7577e
PS
53
54$PAGE->set_url('/mod/book/print.php', array('id'=>$id, 'chapterid'=>$chapterid));
55
15b3c4e0
JC
56// Use "embedded" instead of "print" because Bootstrapbase shows top
57// header bar and navbar even on print style - which is inconsistent
58// with extant behaviour.
59$PAGE->set_pagelayout("embedded");
60
02039379
PS
61unset($id);
62unset($chapterid);
02039379 63
3542c4f4
EL
64// Security checks END.
65
66// read chapters
ee952cbb
PS
67$chapters = book_preload_chapters($book);
68
1cc316b7
PS
69$strbooks = get_string('modulenameplural', 'mod_book');
70$strbook = get_string('modulename', 'mod_book');
71$strtop = get_string('top', 'mod_book');
02039379 72
15b3c4e0
JC
73// Page header.
74$strtitle = format_string($book->name, true, array('context'=>$context));
75$PAGE->set_title($strtitle);
76$PAGE->set_heading($strtitle);
d8389cd6 77$PAGE->requires->css('/mod/book/tool/print/print.css');
15b3c4e0
JC
78
79// Begin page output.
80echo $OUTPUT->header();
02039379 81
02039379 82if ($chapter) {
02039379 83
ee952cbb
PS
84 if ($chapter->hidden) {
85 require_capability('mod/book:viewhiddenchapters', $context);
86 }
ef2a9ece 87 \booktool_print\event\chapter_printed::create_from_chapter($book, $context, $chapter)->trigger();
02039379 88
5595391e
JPG
89 // Print dialog link.
90 $printtext = get_string('printchapter', 'booktool_print');
15b3c4e0
JC
91 $printicon = $OUTPUT->pix_icon('chapter', $printtext, 'booktool_print', array('class' => 'icon'));
92 $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
5595391e
JPG
93 echo html_writer::link('#', $printicon.$printtext, $printlinkatt);
94 ?>
02039379 95 <a name="top"></a>
d8a6d49c
RW
96 <?php
97 echo $OUTPUT->heading(format_string($book->name, true, array('context'=>$context)), 1);
98 ?>
02039379 99 <div class="chapter">
661b9ea2 100 <?php
02039379 101 if (!$book->customtitles) {
ee952cbb
PS
102 if (!$chapter->subchapter) {
103 $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
d8a6d49c 104 echo $OUTPUT->heading($currtitle);
02039379 105 } else {
ee952cbb
PS
106 $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
107 $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
d8a6d49c
RW
108 echo $OUTPUT->heading($currtitle);
109 echo $OUTPUT->heading($currsubtitle, 3);
02039379
PS
110 }
111 }
ee952cbb 112
f4e1849e
PS
113 $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
114 echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
02039379 115 echo '</div>';
02039379
PS
116
117} else {
ef2a9ece 118 \booktool_print\event\book_printed::create_from_book($book, $context)->trigger();
7f6e3eef 119
ee952cbb 120 $allchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
34570f86 121 $book->intro = file_rewrite_pluginfile_urls($book->intro, 'pluginfile.php', $context->id, 'mod_book', 'intro', null);
02039379 122
5595391e
JPG
123 // Print dialog link.
124 $printtext = get_string('printbook', 'booktool_print');
15b3c4e0
JC
125 $printicon = $OUTPUT->pix_icon('book', $printtext, 'booktool_print', array('class' => 'icon'));
126 $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'hidden-print');
5595391e
JPG
127 echo html_writer::link('#', $printicon.$printtext, $printlinkatt);
128 ?>
02039379 129 <a name="top"></a>
d8a6d49c
RW
130 <?php
131 echo $OUTPUT->heading(format_string($book->name, true, array('context'=>$context)), 1);
132 ?>
ad1e18e2 133 <p class="book_summary"><?php echo format_text($book->intro, $book->introformat, array('noclean'=>true, 'context'=>$context)) ?></p>
02039379
PS
134 <div class="book_info"><table>
135 <tr>
661b9ea2 136 <td><?php echo get_string('site') ?>:</td>
ad1e18e2 137 <td><a href="<?php echo $CFG->wwwroot ?>"><?php echo format_string($SITE->fullname, true, array('context'=>$context)) ?></a></td>
02039379 138 </tr><tr>
661b9ea2 139 <td><?php echo get_string('course') ?>:</td>
ad1e18e2 140 <td><?php echo format_string($course->fullname, true, array('context'=>$context)) ?></td>
02039379 141 </tr><tr>
1cc316b7 142 <td><?php echo get_string('modulename', 'mod_book') ?>:</td>
ad1e18e2 143 <td><?php echo format_string($book->name, true, array('context'=>$context)) ?></td>
02039379 144 </tr><tr>
7927cab1 145 <td><?php echo get_string('printedby', 'booktool_print') ?>:</td>
ad1e18e2 146 <td><?php echo fullname($USER, true) ?></td>
02039379 147 </tr><tr>
079be74d 148 <td><?php echo get_string('printdate', 'booktool_print') ?>:</td>
661b9ea2 149 <td><?php echo userdate(time()) ?></td>
02039379
PS
150 </tr>
151 </table></div>
152
661b9ea2 153 <?php
b962f030 154 list($toc, $titles) = booktool_print_get_toc($chapters, $book, $cm);
02039379
PS
155 echo $toc;
156 // chapters
157 $link1 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id.'&chapterid=';
158 $link2 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id;
159 foreach ($chapters as $ch) {
ee952cbb
PS
160 $chapter = $allchapters[$ch->id];
161 if ($chapter->hidden) {
162 continue;
163 }
f4e1849e
PS
164 echo '<div class="book_chapter"><a name="ch'.$ch->id.'"></a>';
165 if (!$book->customtitles) {
6f51394b 166 if (!$chapter->subchapter) {
d8a6d49c 167 echo $OUTPUT->heading($titles[$ch->id]);
6f51394b 168 } else {
d8a6d49c 169 echo $OUTPUT->heading($titles[$ch->id], 3);
6f51394b 170 }
02039379 171 }
ee952cbb 172 $content = str_replace($link1, '#ch', $chapter->content);
f4e1849e
PS
173 $content = str_replace($link2, '#top', $content);
174 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $ch->id);
ee952cbb 175 echo format_text($content, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
f4e1849e 176 echo '</div>';
3542c4f4 177 // echo '<a href="#toc">'.$strtop.'</a>';
02039379 178 }
02039379
PS
179}
180
15b3c4e0
JC
181// Finish page output.
182echo $OUTPUT->footer();