refactor chapter title proccessing
[moodle.git] / print.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 *
20 * @package mod
21 * @subpackage book
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
PS
25
26require_once('../../config.php');
e355240d 27require_once($CFG->dirroot.'/mod/book/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);
44require_capability('mod/book:print', $context);
45
02039379
PS
46//check all variables
47if ($chapterid) {
210ed611 48 //single chapter printing - only visible!
f4e1849e 49 $chapter = $DB->get_record('book_chapters', array('id'=>$chapterid, 'bookid'=>$book->id, 'hidden'=>0), '*', 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
1cc316b7
PS
63$strbooks = get_string('modulenameplural', 'mod_book');
64$strbook = get_string('modulename', 'mod_book');
65$strtop = get_string('top', 'mod_book');
02039379
PS
66
67@header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
68@header('Pragma: no-cache');
c6d2eda2 69@header('Expires: ');
02039379 70@header('Accept-Ranges: none');
dab784a3 71@header('Content-type: text/html; charset=utf-8');
02039379 72
02039379
PS
73if ($chapter) {
74 add_to_log($course->id, 'book', 'print', 'print.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
75
f4e1849e 76 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id, 'hidden'=>0), 'pagenum, title');
02039379 77
4171f20d 78 list($toc, $currtitle, $currsubtitle, $titles) = book_get_toc($chapters, $chapter, $book, $cm, 0, 0);
02039379
PS
79
80 /// page header
81 ?>
82 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
83 <html>
84 <head>
ad1e18e2 85 <title><?php echo format_string($book->name, true, array('context'=>$context)) ?></title>
dab784a3 86 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ad1e18e2 87 <meta name="description" content="<?php echo s(format_string($book->name, true, array('context'=>$context))) ?>" />
02039379
PS
88 <link rel="stylesheet" type="text/css" href="book_print.css" />
89 </head>
90 <body>
91 <a name="top"></a>
92 <div class="chapter">
661b9ea2 93 <?php
02039379
PS
94
95 if (!$book->customtitles) {
96 if ($currsubtitle == '&nbsp;') {
97 echo '<p class="book_chapter_title">'.$currtitle.'<p>';
98 } else {
99 echo '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
100 }
101 }
f4e1849e
PS
102 $chaptertext = file_rewrite_pluginfile_urls($chapter->content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $chapter->id);
103 echo format_text($chaptertext, $chapter->contentformat, array('noclean'=>true, 'context'=>$context));
02039379
PS
104 echo '</div>';
105 echo '</body> </html>';
106
107} else {
108 add_to_log($course->id, 'book', 'print', 'print.php?id='.$cm->id, $book->id, $cm->id);
9d2fe179 109 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum');
02039379
PS
110
111 /// page header
112 ?>
113 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
114 <html>
115 <head>
ad1e18e2
PS
116 <title><?php echo format_string($book->name, true, array('context'=>$context)) ?></title>
117 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
118 <meta name="description" content="<?php echo s(format_string($book->name, true, array('noclean'=>true, 'context'=>$context))) ?>" />
02039379
PS
119 <link rel="stylesheet" type="text/css" href="book_print.css" />
120 </head>
121 <body>
122 <a name="top"></a>
ad1e18e2
PS
123 <p class="book_title"><?php echo format_string($book->name, true, array('context'=>$context)) ?></p>
124 <p class="book_summary"><?php echo format_text($book->intro, $book->introformat, array('noclean'=>true, 'context'=>$context)) ?></p>
02039379
PS
125 <div class="book_info"><table>
126 <tr>
661b9ea2 127 <td><?php echo get_string('site') ?>:</td>
ad1e18e2 128 <td><a href="<?php echo $CFG->wwwroot ?>"><?php echo format_string($SITE->fullname, true, array('context'=>$context)) ?></a></td>
02039379 129 </tr><tr>
661b9ea2 130 <td><?php echo get_string('course') ?>:</td>
ad1e18e2 131 <td><?php echo format_string($course->fullname, true, array('context'=>$context)) ?></td>
02039379 132 </tr><tr>
1cc316b7 133 <td><?php echo get_string('modulename', 'mod_book') ?>:</td>
ad1e18e2 134 <td><?php echo format_string($book->name, true, array('context'=>$context)) ?></td>
02039379 135 </tr><tr>
1cc316b7 136 <td><?php echo get_string('printedby', 'mod_book') ?>:</td>
ad1e18e2 137 <td><?php echo fullname($USER, true) ?></td>
02039379 138 </tr><tr>
1cc316b7 139 <td><?php echo get_string('printdate','mod_book') ?>:</td>
661b9ea2 140 <td><?php echo userdate(time()) ?></td>
02039379
PS
141 </tr>
142 </table></div>
143
661b9ea2 144 <?php
4171f20d 145 list($toc, $currenttitle, $currsubtitle, $titles) = book_get_toc($chapters, $chapter, $book, $cm, 0, 1);
02039379
PS
146 echo $toc;
147 // chapters
148 $link1 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id.'&chapterid=';
149 $link2 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id;
150 foreach ($chapters as $ch) {
f4e1849e
PS
151 echo '<div class="book_chapter"><a name="ch'.$ch->id.'"></a>';
152 if (!$book->customtitles) {
153 echo '<p class="book_chapter_title">'.$titles[$ch->id].'</p>';
02039379 154 }
f4e1849e
PS
155 $content = str_replace($link1, '#ch', $ch->content);
156 $content = str_replace($link2, '#top', $content);
157 $content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $context->id, 'mod_book', 'chapter', $ch->id);
158 echo format_text($content, $ch->contentformat, array('noclean'=>true, 'context'=>$context));
159 echo '</div>';
160 //echo '<a href="#toc">'.$strtop.'</a>';
02039379
PS
161 }
162 echo '</body> </html>';
163}
164