MDL-45144 delete duplicated legacy log entries in mod_book
[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
7927cab1 25require(dirname(__FILE__).'/../../../../config.php');
b962f030 26require_once(dirname(__FILE__).'/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
02039379
PS
56unset($id);
57unset($chapterid);
02039379 58
3542c4f4
EL
59// Security checks END.
60
61// read chapters
ee952cbb
PS
62$chapters = book_preload_chapters($book);
63
1cc316b7
PS
64$strbooks = get_string('modulenameplural', 'mod_book');
65$strbook = get_string('modulename', 'mod_book');
66$strtop = get_string('top', 'mod_book');
02039379
PS
67
68@header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
69@header('Pragma: no-cache');
c6d2eda2 70@header('Expires: ');
02039379 71@header('Accept-Ranges: none');
dab784a3 72@header('Content-type: text/html; charset=utf-8');
02039379 73
02039379 74if ($chapter) {
02039379 75
ee952cbb
PS
76 if ($chapter->hidden) {
77 require_capability('mod/book:viewhiddenchapters', $context);
78 }
02039379 79
7f6e3eef
FM
80 $params = array(
81 'context' => $context,
82 'objectid' => $chapter->id
83 );
84 $event = \booktool_print\event\chapter_printed::create($params);
85 $event->add_record_snapshot('book_chapters', $chapter);
86 $event->trigger();
02039379 87
3542c4f4 88 // page header
02039379 89 ?>
5595391e 90 <!DOCTYPE HTML>
02039379
PS
91 <html>
92 <head>
ad1e18e2 93 <title><?php echo format_string($book->name, true, array('context'=>$context)) ?></title>
dab784a3 94 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ad1e18e2 95 <meta name="description" content="<?php echo s(format_string($book->name, true, array('context'=>$context))) ?>" />
bd094524 96 <link rel="stylesheet" type="text/css" href="print.css" />
02039379
PS
97 </head>
98 <body>
5595391e
JPG
99 <?php
100 // Print dialog link.
101 $printtext = get_string('printchapter', 'booktool_print');
102 $printicon = $OUTPUT->pix_icon('chapter', $printtext, 'booktool_print', array('class' => 'book_print_icon'));
103 $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'book_no_print');
104 echo html_writer::link('#', $printicon.$printtext, $printlinkatt);
105 ?>
02039379 106 <a name="top"></a>
d8a6d49c
RW
107 <?php
108 echo $OUTPUT->heading(format_string($book->name, true, array('context'=>$context)), 1);
109 ?>
02039379 110 <div class="chapter">
661b9ea2 111 <?php
02039379 112
ee952cbb 113
02039379 114 if (!$book->customtitles) {
ee952cbb
PS
115 if (!$chapter->subchapter) {
116 $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
d8a6d49c 117 echo $OUTPUT->heading($currtitle);
02039379 118 } else {
ee952cbb
PS
119 $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
120 $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
d8a6d49c
RW
121 echo $OUTPUT->heading($currtitle);
122 echo $OUTPUT->heading($currsubtitle, 3);
02039379
PS
123 }
124 }
ee952cbb 125
f4e1849e
PS
126 $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
127 echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
02039379
PS
128 echo '</div>';
129 echo '</body> </html>';
130
131} else {
7f6e3eef
FM
132 $params = array(
133 'context' => $context,
134 'objectid' => $book->id
135 );
136 $event = \booktool_print\event\book_printed::create($params);
137 $event->add_record_snapshot('book', $book);
138 $event->trigger();
139
ee952cbb 140 $allchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
34570f86 141 $book->intro = file_rewrite_pluginfile_urls($book->intro, 'pluginfile.php', $context->id, 'mod_book', 'intro', null);
02039379 142
3542c4f4 143 // page header
02039379 144 ?>
5595391e 145 <!DOCTYPE HTML>
02039379
PS
146 <html>
147 <head>
ad1e18e2
PS
148 <title><?php echo format_string($book->name, true, array('context'=>$context)) ?></title>
149 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
150 <meta name="description" content="<?php echo s(format_string($book->name, true, array('noclean'=>true, 'context'=>$context))) ?>" />
bd094524 151 <link rel="stylesheet" type="text/css" href="print.css" />
02039379
PS
152 </head>
153 <body>
5595391e
JPG
154 <?php
155 // Print dialog link.
156 $printtext = get_string('printbook', 'booktool_print');
157 $printicon = $OUTPUT->pix_icon('book', $printtext, 'booktool_print', array('class' => 'book_print_icon'));
158 $printlinkatt = array('onclick' => 'window.print();return false;', 'class' => 'book_no_print');
159 echo html_writer::link('#', $printicon.$printtext, $printlinkatt);
160 ?>
02039379 161 <a name="top"></a>
d8a6d49c
RW
162 <?php
163 echo $OUTPUT->heading(format_string($book->name, true, array('context'=>$context)), 1);
164 ?>
ad1e18e2 165 <p class="book_summary"><?php echo format_text($book->intro, $book->introformat, array('noclean'=>true, 'context'=>$context)) ?></p>
02039379
PS
166 <div class="book_info"><table>
167 <tr>
661b9ea2 168 <td><?php echo get_string('site') ?>:</td>
ad1e18e2 169 <td><a href="<?php echo $CFG->wwwroot ?>"><?php echo format_string($SITE->fullname, true, array('context'=>$context)) ?></a></td>
02039379 170 </tr><tr>
661b9ea2 171 <td><?php echo get_string('course') ?>:</td>
ad1e18e2 172 <td><?php echo format_string($course->fullname, true, array('context'=>$context)) ?></td>
02039379 173 </tr><tr>
1cc316b7 174 <td><?php echo get_string('modulename', 'mod_book') ?>:</td>
ad1e18e2 175 <td><?php echo format_string($book->name, true, array('context'=>$context)) ?></td>
02039379 176 </tr><tr>
7927cab1 177 <td><?php echo get_string('printedby', 'booktool_print') ?>:</td>
ad1e18e2 178 <td><?php echo fullname($USER, true) ?></td>
02039379 179 </tr><tr>
079be74d 180 <td><?php echo get_string('printdate', 'booktool_print') ?>:</td>
661b9ea2 181 <td><?php echo userdate(time()) ?></td>
02039379
PS
182 </tr>
183 </table></div>
184
661b9ea2 185 <?php
b962f030 186 list($toc, $titles) = booktool_print_get_toc($chapters, $book, $cm);
02039379
PS
187 echo $toc;
188 // chapters
189 $link1 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id.'&chapterid=';
190 $link2 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id;
191 foreach ($chapters as $ch) {
ee952cbb
PS
192 $chapter = $allchapters[$ch->id];
193 if ($chapter->hidden) {
194 continue;
195 }
f4e1849e
PS
196 echo '<div class="book_chapter"><a name="ch'.$ch->id.'"></a>';
197 if (!$book->customtitles) {
6f51394b 198 if (!$chapter->subchapter) {
d8a6d49c 199 echo $OUTPUT->heading($titles[$ch->id]);
6f51394b 200 } else {
d8a6d49c 201 echo $OUTPUT->heading($titles[$ch->id], 3);
6f51394b 202 }
02039379 203 }
ee952cbb 204 $content = str_replace($link1, '#ch', $chapter->content);
f4e1849e
PS
205 $content = str_replace($link2, '#top', $content);
206 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $ch->id);
ee952cbb 207 echo format_text($content, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
f4e1849e 208 echo '</div>';
3542c4f4 209 // echo '<a href="#toc">'.$strtop.'</a>';
02039379
PS
210 }
211 echo '</body> </html>';
212}
213