conversion to 2.0
[moodle.git] / print.php
CommitLineData
8d623d14 1<?php
02039379
PS
2
3require_once('../../config.php');
e355240d 4require_once($CFG->dirroot.'/mod/book/locallib.php');
02039379
PS
5
6$id = required_param('id', PARAM_INT); // Course Module ID
7$chapterid = optional_param('chapterid', 0, PARAM_INT); // Chapter ID
8
9// =========================================================================
10// security checks START - teachers and students view
11// =========================================================================
dab784a3 12if (!$cm = get_coursemodule_from_id('book', $id)) {
02039379
PS
13 error('Course Module ID was incorrect');
14}
15
16if (!$course = get_record('course', 'id', $cm->course)) {
17 error('Course is misconfigured');
18}
19
dab784a3 20require_course_login($course, true, $cm);
c6d2eda2 21
02039379
PS
22if (!$book = get_record('book', 'id', $cm->instance)) {
23 error('Course module is incorrect');
24}
25
26if ($book->disableprinting) {
27 error('Printing is disabled');
28}
29
c6d2eda2
PS
30$context = get_context_instance(CONTEXT_MODULE, $cm->id);
31require_capability('mod/book:read', $context);
32require_capability('mod/book:print', $context);
33
02039379
PS
34//check all variables
35if ($chapterid) {
36 //single chapter printing
37 if (!$chapter = get_record('book_chapters', 'id', $chapterid)) {
38 error('Incorrect chapter ID');
39 }
40 if ($chapter->bookid != $book->id) {//chapter id not in this book!!!!
41 error('Chapter not in this book!');
42 }
43 if ($chapter->hidden) {
44 error('Only visible chapters can be printed');
45 }
46} else {
47 //complete book
48 $chapter = false;
49}
50unset($id);
51unset($chapterid);
52// =========================================================================
53// security checks END
54// =========================================================================
55
56$strbooks = get_string('modulenameplural', 'book');
57$strbook = get_string('modulename', 'book');
dab784a3 58$strtop = get_string('top', 'book');
02039379
PS
59
60@header('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
61@header('Pragma: no-cache');
c6d2eda2 62@header('Expires: ');
02039379 63@header('Accept-Ranges: none');
dab784a3 64@header('Content-type: text/html; charset=utf-8');
02039379 65
dab784a3 66$formatoptions = new object();
02039379
PS
67$formatoptions->noclean = true;
68
69if ($chapter) {
70 add_to_log($course->id, 'book', 'print', 'print.php?id='.$cm->id.'&chapterid='.$chapter->id, $book->id, $cm->id);
71
72 $chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum, title');
73
74 $print = 0;
75 $edit = 0;
76 require('toc.php');
77
78 /// page header
79 ?>
80 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
81 <html>
82 <head>
dab784a3
PS
83 <title><?PHP echo format_string($book->name) ?></title>
84 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
85 <meta name="description" content="<?PHP echo s(format_string($book->name)) ?>" />
02039379
PS
86 <link rel="stylesheet" type="text/css" href="book_print.css" />
87 </head>
88 <body>
89 <a name="top"></a>
90 <div class="chapter">
91 <?PHP
92
93 if (!$book->customtitles) {
94 if ($currsubtitle == '&nbsp;') {
95 echo '<p class="book_chapter_title">'.$currtitle.'<p>';
96 } else {
97 echo '<p class="book_chapter_title">'.$currtitle.'<br />'.$currsubtitle.'</p>';
98 }
99 }
100 echo format_text($chapter->content, FORMAT_HTML, $formatoptions, $course->id);
101 echo '</div>';
102 echo '</body> </html>';
103
104} else {
105 add_to_log($course->id, 'book', 'print', 'print.php?id='.$cm->id, $book->id, $cm->id);
106 $site = get_record('course','id',1);
107 $chapters = get_records('book_chapters', 'bookid', $book->id, 'pagenum');
108
109 /// page header
110 ?>
111 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
112 <html>
113 <head>
dab784a3 114 <title><?PHP echo format_string(name) ?></title>
02039379 115 <meta http-equiv="Content-Type" content="text/html; charset=<?PHP echo $encoding ?>" />
dab784a3 116 <meta name="description" content="<?PHP echo s(format_string($book->name)) ?>" />
02039379
PS
117 <link rel="stylesheet" type="text/css" href="book_print.css" />
118 </head>
119 <body>
120 <a name="top"></a>
dab784a3
PS
121 <p class="book_title"><?PHP echo format_string($book->name) ?></p>
122 <p class="book_summary"><?PHP echo format_string($book->summary) ?></p>
02039379
PS
123 <div class="book_info"><table>
124 <tr>
125 <td><?PHP echo get_string('site') ?>:</td>
dab784a3 126 <td><a href="<?PHP echo $CFG->wwwroot ?>"><?PHP echo format_string($site->fullname) ?></a></td>
02039379
PS
127 </tr><tr>
128 <td><?PHP echo get_string('course') ?>:</td>
dab784a3 129 <td><?PHP echo format_string($course->fullname) ?></td>
02039379
PS
130 </tr><tr>
131 <td><?PHP echo get_string('modulename', 'book') ?>:</td>
dab784a3 132 <td><?PHP echo format_string($book->name) ?></td>
02039379
PS
133 </tr><tr>
134 <td><?PHP echo get_string('printedby', 'book') ?>:</td>
dab784a3 135 <td><?PHP echo format_string(fullname($USER, true)) ?></td>
02039379
PS
136 </tr><tr>
137 <td><?PHP echo get_string('printdate','book') ?>:</td>
138 <td><?PHP echo userdate(time()) ?></td>
139 </tr>
140 </table></div>
141
142 <?PHP
143 $print = 1;
144 require('toc.php');
145 echo $toc;
146 // chapters
147 $link1 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id.'&chapterid=';
148 $link2 = $CFG->wwwroot.'/mod/book/view.php?id='.$course->id;
149 foreach ($chapters as $ch) {
150 if (!$ch->hidden) {
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>';
154 }
155 $content = str_replace($link1, '#ch', $ch->content);
156 $content = str_replace($link2, '#top', $content);
157 echo format_text($content, FORMAT_HTML, $formatoptions, $course->id);
158 echo '</div>';
159 //echo '<a href="#toc">'.$strtop.'</a>';
160
161 }
162 }
163 echo '</body> </html>';
164}
165