Subtree merged git://github.com/skodak/moodle-mod_book.git (master branch) into mod...
[moodle.git] / mod / book / tool / print / index.php
CommitLineData
8d623d14 1<?php
2c1e98e6
PS
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/>.
16
17/**
18 * Book printing
19 *
7927cab1
PS
20 * @package booktool
21 * @subpackage print
e2790e4b 22 * @copyright 2004-2011 Petr Skoda {@link http://skodak.org}
2c1e98e6
PS
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
02039379 25
7927cab1 26require(dirname(__FILE__).'/../../../../config.php');
b962f030 27require_once(dirname(__FILE__).'/locallib.php');
02039379
PS
28
29$id = required_param('id', PARAM_INT); // Course Module ID
30$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
31
32// =========================================================================
33// security checks START - teachers and students view
34// =========================================================================
02039379 35
2c1e98e6
PS
36$cm = get_coursemodule_from_id('book', $id, 0, false, MUST_EXIST);
37$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
38$book = $DB->get_record('book', array('id'=>$cm->instance), '*', MUST_EXIST);
02039379 39
dab784a3 40require_course_login($course, true, $cm);
c6d2eda2 41
c6d2eda2
PS
42$context = get_context_instance(CONTEXT_MODULE, $cm->id);
43require_capability('mod/book:read', $context);
7927cab1 44require_capability('booktool/print:print', $context);
c6d2eda2 45
02039379
PS
46//check all variables
47if ($chapterid) {
210ed611 48 //single chapter printing - only visible!
ee952cbb 49 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id), '*', MUST_EXIST);
02039379
PS
50} else {
51 //complete book
52 $chapter = false;
53}
56e7577e
PS
54
55$PAGE->set_url('/mod/book/print.php', array('id'=>$id, 'chapterid'=>$chapterid));
56
02039379
PS
57unset($id);
58unset($chapterid);
59// =========================================================================
60// security checks END
61// =========================================================================
62
ee952cbb
PS
63/// read chapters
64$chapters = book_preload_chapters($book);
65
1cc316b7
PS
66$strbooks = get_string('modulenameplural', 'mod_book');
67$strbook = get_string('modulename', 'mod_book');
68$strtop = get_string('top', 'mod_book');
02039379
PS
69
70@header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
71@header('Pragma: no-cache');
c6d2eda2 72@header('Expires: ');
02039379 73@header('Accept-Ranges: none');
dab784a3 74@header('Content-type: text/html; charset=utf-8');
02039379 75
02039379 76if ($chapter) {
02039379 77
ee952cbb
PS
78 if ($chapter->hidden) {
79 require_capability('mod/book:viewhiddenchapters', $context);
80 }
02039379 81
7927cab1 82 add_to_log($course->id, 'book', 'print', 'tool/print/index.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
02039379
PS
83
84 /// page header
85 ?>
86 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
87 <html>
88 <head>
ad1e18e2 89 <title><?php echo format_string($book->name, true, array('context'=>$context)) ?></title>
dab784a3 90 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ad1e18e2 91 <meta name="description" content="<?php echo s(format_string($book->name, true, array('context'=>$context))) ?>" />
bd094524 92 <link rel="stylesheet" type="text/css" href="print.css" />
02039379
PS
93 </head>
94 <body>
95 <a name="top"></a>
96 <div class="chapter">
661b9ea2 97 <?php
02039379 98
ee952cbb 99
02039379 100 if (!$book->customtitles) {
ee952cbb
PS
101 if (!$chapter->subchapter) {
102 $currtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
103 echo '<p class="book_chapter_title">'.$currtitle.'</p>';
02039379 104 } else {
ee952cbb
PS
105 $currtitle = book_get_chapter_title($chapters[$chapter->id]->parent, $chapters, $book, $context);
106 $currsubtitle = book_get_chapter_title($chapter->id, $chapters, $book, $context);
02039379
PS
107 echo '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
108 }
109 }
ee952cbb 110
f4e1849e
PS
111 $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
112 echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
02039379
PS
113 echo '</div>';
114 echo '</body> </html>';
115
116} else {
7927cab1 117 add_to_log($course->id, 'book', 'print', 'tool/print/index.php?id='.$cm->id, $book->id, $cm->id);
ee952cbb 118 $allchapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
02039379
PS
119
120 /// page header
121 ?>
122 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
123 <html>
124 <head>
ad1e18e2
PS
125 <title><?php echo format_string($book->name, true, array('context'=>$context)) ?></title>
126 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
127 <meta name="description" content="<?php echo s(format_string($book->name, true, array('noclean'=>true, 'context'=>$context))) ?>" />
bd094524 128 <link rel="stylesheet" type="text/css" href="print.css" />
02039379
PS
129 </head>
130 <body>
131 <a name="top"></a>
ad1e18e2
PS
132 <p class="book_title"><?php echo format_string($book->name, true, array('context'=>$context)) ?></p>
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>
7927cab1 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) {
166 echo '<p class="book_chapter_title">'.$titles[$ch->id].'</p>';
02039379 167 }
ee952cbb 168 $content = str_replace($link1, '#ch', $chapter->content);
f4e1849e
PS
169 $content = str_replace($link2, '#top', $content);
170 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $ch->id);
ee952cbb 171 echo format_text($content, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
f4e1849e
PS
172 echo '</div>';
173 //echo '<a href="#toc">'.$strtop.'</a>';
02039379
PS
174 }
175 echo '</body> </html>';
176}
177