add support for page types
[moodle.git] / locallib.php
CommitLineData
e355240d
PS
1<?php
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 module local lib functions
19 *
20 * @package mod
21 * @subpackage book
ad1c11bd 22 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
e355240d
PS
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die;
27
f6ca19ae
PS
28require_once($CFG->dirroot.'/mod/book/lib.php');
29require_once($CFG->libdir.'/filelib.php');
30
45573d5a
PS
31define('BOOK_NUM_NONE', '0');
32define('BOOK_NUM_NUMBERS', '1');
33define('BOOK_NUM_BULLETS', '2');
34define('BOOK_NUM_INDENTED', '3');
35
2c1e98e6 36/**
ad1c11bd
PS
37 * Preload book chapters and fix toc structure if necessary.
38 *
39 * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, hidden'
40 * and extra 'parent, number, subchapters, prev, next'.
41 * Please note the content/text of chapters is not included.
42 *
b962f030 43 * @param stdClass $book
ad1c11bd 44 * @return array of id=>chapter
2c1e98e6 45 */
b1a9b01e 46function book_preload_chapters($book) {
2c1e98e6 47 global $DB;
b1a9b01e 48 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter, title, hidden');
ad1c11bd
PS
49 if (!$chapters) {
50 return array();
51 }
52
53 $prev = null;
54 $prevsub = null;
2c1e98e6 55
ad1c11bd
PS
56 $first = true;
57 $hidesub = true;
58 $parent = null;
59 $pagenum = 0; // chapter sort
60 $i = 0; // main chapter num
61 $j = 0; // subchapter num
62 foreach($chapters as $id=>$ch) {
63 $oldch = clone($ch);
64 $pagenum++;
65 $ch->pagenum = $pagenum;
66 if ($first) {
67 // book can not start with a subchapter
68 $ch->subchapter = 0;
69 $first = false;
70 }
71 if (!$ch->subchapter) {
72 $ch->prev = $prev;
73 $ch->next = null;
74 if ($prev) {
75 $chapters[$prev]->next = $ch->id;
76 }
77 if ($ch->hidden) {
b1a9b01e
PS
78 if ($book->numbering == BOOK_NUM_NUMBERS) {
79 $ch->number = 'x';
80 } else {
81 $ch->number = null;
82 }
ad1c11bd
PS
83 } else {
84 $i++;
85 $ch->number = $i;
86 }
87 $j = 0;
88 $prevsub = null;
89 $hidesub = $ch->hidden;
90 $parent = $ch->id;
91 $ch->parent = null;
92 $ch->subchpaters = array();
93 } else {
94 $ch->prev = $prevsub;
95 $ch->next = null;
96 if ($prevsub) {
97 $chapters[$prevsub]->next = $ch->id;
e355240d 98 }
ad1c11bd
PS
99 $ch->parent = $parent;
100 $ch->subchpaters = null;
101 $chapters[$parent]->subchapters[$ch->id] = $ch->id;
102 if ($hidesub) {
103 // all subchapters in hidden chapter must be hidden too
bc953600 104 $ch->hidden = 1;
e355240d 105 }
ad1c11bd 106 if ($ch->hidden) {
b1a9b01e
PS
107 if ($book->numbering == BOOK_NUM_NUMBERS) {
108 $ch->number = 'x';
109 } else {
110 $ch->number = null;
111 }
ad1c11bd
PS
112 } else {
113 $j++;
114 $ch->number = $j;
23b02c4f 115 }
e355240d 116 }
ad1c11bd
PS
117 if ($oldch->subchapter != $ch->subchapter or $oldch->pagenum != $ch->pagenum or $oldch->hidden != $ch->hidden) {
118 // update only if something changed
119 $DB->update_record('book_chapters', $ch);
120 }
121 $chapters[$id] = $ch;
e355240d 122 }
ad1c11bd
PS
123
124 return $chapters;
e355240d
PS
125}
126
b1a9b01e
PS
127function book_get_chapter_title($chid, $chapters, $book, $context) {
128 $ch = $chapters[$chid];
129 $title = trim(format_string($ch->title, true, array('context'=>$context)));
130 $numbers = array();
131 if ($book->numbering == BOOK_NUM_NUMBERS) {
132 if ($ch->parent and $chapters[$ch->parent]->number) {
133 $numbers[] = $chapters[$ch->parent]->number;
134 }
135 if ($ch->number) {
136 $numbers[] = $ch->number;
137 }
138 }
139
140 if ($numbers) {
141 $title = implode('.', $numbers).' '.$title;
142 }
143
144 return $title;
145}
146
f6ca19ae
PS
147/**
148 * General logging to table
149 * @param string $str1
150 * @param string $str2
151 * @param int $level
152 * @return void
153 */
e355240d
PS
154function book_log($str1, $str2, $level = 0) {
155 switch ($level) {
156 case 1:
157 echo '<tr><td><span class="dimmed_text">'.$str1.'</span></td><td><span class="dimmed_text">'.$str2.'</span></td></tr>';
158 break;
159 case 2:
160 echo '<tr><td><span style="color: rgb(255, 0, 0);">'.$str1.'</span></td><td><span style="color: rgb(255, 0, 0);">'.$str2.'</span></td></tr>';
161 break;
162 default:
163 echo '<tr><td>'.$str1.'</class></td><td>'.$str2.'</td></tr>';
164 break;
165 }
166}
167
ad1c11bd
PS
168function book_add_fake_block($chapters, $chapter, $book, $cm, $edit) {
169 global $OUTPUT, $PAGE;
170
8cb50b2a 171 $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
ad1c11bd
PS
172
173 if ($edit) {
8cb50b2a 174 $toc .= '<div class="book_faq">';
ad1c11bd
PS
175 $toc .= $OUTPUT->help_icon('faq', 'mod_book', get_string('faq', 'mod_book'));
176 $toc .= '</div>';
177 }
178
179 $bc = new block_contents();
180 $bc->title = get_string('toc', 'mod_book');
181 $bc->attributes['class'] = 'block';
182 $bc->content = $toc;
183
184 $regions = $PAGE->blocks->get_regions();
185 $firstregion = reset($regions);
186 $PAGE->blocks->add_fake_block($bc, $firstregion);
187}
188
ee952cbb
PS
189/**
190 * Generate toc structure
191 *
192 * @param array $chapters
193 * @param stdClass $chapter
194 * @param stdClass $book
195 * @param stdClass $cm
196 * @param bool $edit
ee952cbb
PS
197 * @return string
198 */
199function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
200 global $USER, $OUTPUT;
201
202 $toc = ''; //representation of toc (HTML)
203 $nch = 0; //chapter number
204 $ns = 0; //subchapter number
205 $first = 1;
206
207 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
208
209 switch ($book->numbering) {
210 case BOOK_NUM_NONE:
211 $toc .= '<div class="book_toc_none">';
212 break;
213 case BOOK_NUM_NUMBERS:
214 $toc .= '<div class="book_toc_numbered">';
215 break;
216 case BOOK_NUM_BULLETS:
217 $toc .= '<div class="book_toc_bullets">';
218 break;
219 case BOOK_NUM_INDENTED:
220 $toc .= '<div class="book_toc_indented">';
221 break;
222 }
223
224
225 if ($edit) { ///teacher's TOC
f6ca19ae
PS
226 $toc .= '<ul>';
227 $i = 0;
228 foreach($chapters as $ch) {
229 $i++;
dc888a31 230 $title = trim(format_string($ch->title, true, array('context'=>$context)));
f6ca19ae
PS
231 if (!$ch->subchapter) {
232 $toc .= ($first) ? '<li>' : '</ul></li><li>';
233 if (!$ch->hidden) {
234 $nch++;
235 $ns = 0;
236 if ($book->numbering == BOOK_NUM_NUMBERS) {
237 $title = "$nch $title";
238 }
239 } else {
240 if ($book->numbering == BOOK_NUM_NUMBERS) {
241 $title = "x $title";
242 }
243 $title = '<span class="dimmed_text">'.$title.'</span>';
244 }
f6ca19ae
PS
245 } else {
246 $toc .= ($first) ? '<li><ul><li>' : '<li>';
247 if (!$ch->hidden) {
248 $ns++;
249 if ($book->numbering == BOOK_NUM_NUMBERS) {
250 $title = "$nch.$ns $title";
251 }
252 } else {
253 if ($book->numbering == BOOK_NUM_NUMBERS) {
254 $title = "x.x $title";
255 }
256 $title = '<span class="dimmed_text">'.$title.'</span>';
257 }
258 }
259
260 if ($ch->id == $chapter->id) {
261 $toc .= '<strong>'.$title.'</strong>';
f6ca19ae
PS
262 } else {
263 $toc .= '<a title="'.s($title).'" href="view.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'">'.$title.'</a>';
264 }
265 $toc .= '&nbsp;&nbsp;';
266 if ($i != 1) {
267 $toc .= ' <a title="'.get_string('up').'" href="move.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'&amp;up=1&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/up').'" class="iconsmall" alt="'.get_string('up').'" /></a>';
268 }
269 if ($i != count($chapters)) {
270 $toc .= ' <a title="'.get_string('down').'" href="move.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'&amp;up=0&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/down').'" class="iconsmall" alt="'.get_string('down').'" /></a>';
271 }
272 $toc .= ' <a title="'.get_string('edit').'" href="edit.php?cmid='.$cm->id.'&amp;id='.$ch->id.'"><img src="'.$OUTPUT->pix_url('t/edit').'" class="iconsmall" alt="'.get_string('edit').'" /></a>';
273 $toc .= ' <a title="'.get_string('delete').'" href="delete.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/delete').'" class="iconsmall" alt="'.get_string('delete').'" /></a>';
274 if ($ch->hidden) {
275 $toc .= ' <a title="'.get_string('show').'" href="show.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/show').'" class="iconsmall" alt="'.get_string('show').'" /></a>';
276 } else {
277 $toc .= ' <a title="'.get_string('hide').'" href="show.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'&amp;sesskey='.$USER->sesskey.'"><img src="'.$OUTPUT->pix_url('t/hide').'" class="iconsmall" alt="'.get_string('hide').'" /></a>';
278 }
4cd51b08 279 $toc .= ' <a title="'.get_string('addafter', 'mod_book').'" href="edit.php?cmid='.$cm->id.'&amp;pagenum='.$ch->pagenum.'&amp;subchapter='.$ch->subchapter.'"><img src="'.$OUTPUT->pix_url('add', 'mod_book').'" class="iconsmall" alt="'.get_string('addafter', 'mod_book').'" /></a>';
f6ca19ae
PS
280
281 $toc .= (!$ch->subchapter) ? '<ul>' : '</li>';
282 $first = 0;
283 }
284 $toc .= '</ul></li></ul>';
285 } else { //normal students view
286 $toc .= '<ul>';
287 foreach($chapters as $ch) {
dc888a31 288 $title = trim(format_string($ch->title, true, array('context'=>$context)));
f6ca19ae
PS
289 if (!$ch->hidden) {
290 if (!$ch->subchapter) {
291 $nch++;
292 $ns = 0;
293 $toc .= ($first) ? '<li>' : '</ul></li><li>';
294 if ($book->numbering == BOOK_NUM_NUMBERS) {
295 $title = "$nch $title";
296 }
f6ca19ae
PS
297 } else {
298 $ns++;
299 $toc .= ($first) ? '<li><ul><li>' : '<li>';
300 if ($book->numbering == BOOK_NUM_NUMBERS) {
301 $title = "$nch.$ns $title";
302 }
303 }
304 if ($ch->id == $chapter->id) {
305 $toc .= '<strong>'.$title.'</strong>';
f6ca19ae
PS
306 } else {
307 $toc .= '<a title="'.s($title).'" href="view.php?id='.$cm->id.'&amp;chapterid='.$ch->id.'">'.$title.'</a>';
308 }
309 $toc .= (!$ch->subchapter) ? '<ul>' : '</li>';
310 $first = 0;
311 }
312 }
313 $toc .= '</ul></li></ul>';
314 }
315
316 $toc .= '</div>';
317
318 $toc = str_replace('<ul></ul>', '', $toc); //cleanup of invalid structures
319
ee952cbb 320 return $toc;
f6ca19ae 321}