weekly release 2.9dev
[moodle.git] / mod / book / locallib.php
CommitLineData
e355240d 1<?php
b15ef0b0 2// This file is part of Moodle - http://moodle.org/
e355240d
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 module local lib functions
19 *
3542c4f4
EL
20 * @package mod_book
21 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
e355240d
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die;
26
29656fe2 27require_once(dirname(__FILE__).'/lib.php');
f6ca19ae
PS
28require_once($CFG->libdir.'/filelib.php');
29
2cd972c1
SH
30/**
31 * The following defines are used to define how the chapters and subchapters of a book should be displayed in that table of contents.
32 * BOOK_NUM_NONE No special styling will applied and the editor will be able to do what ever thay want in the title
33 * BOOK_NUM_NUMBERS Chapters and subchapters are numbered (1, 1.1, 1.2, 2, ...)
34 * BOOK_NUM_BULLETS Subchapters are indented and displayed with bullets
35 * BOOK_NUM_INDENTED Subchapters are indented
36 */
45573d5a
PS
37define('BOOK_NUM_NONE', '0');
38define('BOOK_NUM_NUMBERS', '1');
39define('BOOK_NUM_BULLETS', '2');
40define('BOOK_NUM_INDENTED', '3');
41
f4de8461
DB
42/**
43 * The following defines are used to define the navigation style used within a book.
44 * BOOK_LINK_TOCONLY Only the table of contents is shown, in a side region.
45 * BOOK_LINK_IMAGE Arrows link to previous/next/exit pages, in addition to the TOC.
46 * BOOK_LINK_TEXT Page names and arrows link to previous/next/exit pages, in addition to the TOC.
47 */
48define ('BOOK_LINK_TOCONLY', '0');
49define ('BOOK_LINK_IMAGE', '1');
50define ('BOOK_LINK_TEXT', '2');
51
2c1e98e6 52/**
ad1c11bd
PS
53 * Preload book chapters and fix toc structure if necessary.
54 *
55 * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, hidden'
56 * and extra 'parent, number, subchapters, prev, next'.
57 * Please note the content/text of chapters is not included.
58 *
b962f030 59 * @param stdClass $book
ad1c11bd 60 * @return array of id=>chapter
2c1e98e6 61 */
b1a9b01e 62function book_preload_chapters($book) {
2c1e98e6 63 global $DB;
b1a9b01e 64 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter, title, hidden');
ad1c11bd
PS
65 if (!$chapters) {
66 return array();
67 }
68
69 $prev = null;
70 $prevsub = null;
2c1e98e6 71
ad1c11bd
PS
72 $first = true;
73 $hidesub = true;
74 $parent = null;
75 $pagenum = 0; // chapter sort
76 $i = 0; // main chapter num
77 $j = 0; // subchapter num
29656fe2 78 foreach ($chapters as $id => $ch) {
ad1c11bd
PS
79 $oldch = clone($ch);
80 $pagenum++;
81 $ch->pagenum = $pagenum;
82 if ($first) {
83 // book can not start with a subchapter
84 $ch->subchapter = 0;
85 $first = false;
86 }
87 if (!$ch->subchapter) {
ad1c11bd 88 if ($ch->hidden) {
b1a9b01e
PS
89 if ($book->numbering == BOOK_NUM_NUMBERS) {
90 $ch->number = 'x';
91 } else {
92 $ch->number = null;
93 }
ad1c11bd
PS
94 } else {
95 $i++;
96 $ch->number = $i;
97 }
98 $j = 0;
99 $prevsub = null;
100 $hidesub = $ch->hidden;
101 $parent = $ch->id;
102 $ch->parent = null;
2cd972c1 103 $ch->subchapters = array();
ad1c11bd 104 } else {
ad1c11bd 105 $ch->parent = $parent;
2cd972c1 106 $ch->subchapters = null;
ad1c11bd
PS
107 $chapters[$parent]->subchapters[$ch->id] = $ch->id;
108 if ($hidesub) {
109 // all subchapters in hidden chapter must be hidden too
bc953600 110 $ch->hidden = 1;
e355240d 111 }
ad1c11bd 112 if ($ch->hidden) {
b1a9b01e
PS
113 if ($book->numbering == BOOK_NUM_NUMBERS) {
114 $ch->number = 'x';
115 } else {
116 $ch->number = null;
117 }
ad1c11bd
PS
118 } else {
119 $j++;
120 $ch->number = $j;
23b02c4f 121 }
e355240d 122 }
fb909757 123
ad1c11bd
PS
124 if ($oldch->subchapter != $ch->subchapter or $oldch->pagenum != $ch->pagenum or $oldch->hidden != $ch->hidden) {
125 // update only if something changed
126 $DB->update_record('book_chapters', $ch);
127 }
128 $chapters[$id] = $ch;
e355240d 129 }
ad1c11bd
PS
130
131 return $chapters;
e355240d
PS
132}
133
3542c4f4
EL
134/**
135 * Returns the title for a given chapter
136 *
137 * @param int $chid
138 * @param array $chapters
139 * @param stdClass $book
140 * @param context_module $context
141 * @return string
142 */
b1a9b01e
PS
143function book_get_chapter_title($chid, $chapters, $book, $context) {
144 $ch = $chapters[$chid];
145 $title = trim(format_string($ch->title, true, array('context'=>$context)));
146 $numbers = array();
147 if ($book->numbering == BOOK_NUM_NUMBERS) {
148 if ($ch->parent and $chapters[$ch->parent]->number) {
149 $numbers[] = $chapters[$ch->parent]->number;
150 }
151 if ($ch->number) {
152 $numbers[] = $ch->number;
153 }
154 }
155
156 if ($numbers) {
157 $title = implode('.', $numbers).' '.$title;
158 }
159
160 return $title;
161}
162
3542c4f4 163/**
f6fa4b7b 164 * Add the book TOC sticky block to the default region
3542c4f4
EL
165 *
166 * @param array $chapters
167 * @param stdClass $chapter
168 * @param stdClass $book
169 * @param stdClass $cm
170 * @param bool $edit
171 */
ad1c11bd
PS
172function book_add_fake_block($chapters, $chapter, $book, $cm, $edit) {
173 global $OUTPUT, $PAGE;
174
8cb50b2a 175 $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
ad1c11bd 176
ad1c11bd
PS
177 $bc = new block_contents();
178 $bc->title = get_string('toc', 'mod_book');
bdb66a46 179 $bc->attributes['class'] = 'block block_book_toc';
ad1c11bd
PS
180 $bc->content = $toc;
181
f6fa4b7b
ME
182 $defaultregion = $PAGE->blocks->get_default_region();
183 $PAGE->blocks->add_fake_block($bc, $defaultregion);
ad1c11bd
PS
184}
185
ee952cbb
PS
186/**
187 * Generate toc structure
188 *
189 * @param array $chapters
190 * @param stdClass $chapter
191 * @param stdClass $book
192 * @param stdClass $cm
193 * @param bool $edit
ee952cbb
PS
194 * @return string
195 */
196function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
197 global $USER, $OUTPUT;
198
fb909757 199 $toc = '';
3542c4f4
EL
200 $nch = 0; // Chapter number
201 $ns = 0; // Subchapter number
ee952cbb
PS
202 $first = 1;
203
1fc9e895 204 $context = context_module::instance($cm->id);
ee952cbb
PS
205
206 switch ($book->numbering) {
29656fe2 207 case BOOK_NUM_NONE:
bdb66a46 208 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none clearfix'));
29656fe2
EL
209 break;
210 case BOOK_NUM_NUMBERS:
bdb66a46 211 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered clearfix'));
29656fe2
EL
212 break;
213 case BOOK_NUM_BULLETS:
bdb66a46 214 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets clearfix'));
29656fe2
EL
215 break;
216 case BOOK_NUM_INDENTED:
bdb66a46 217 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented clearfix'));
29656fe2 218 break;
ee952cbb
PS
219 }
220
3542c4f4 221 if ($edit) { // Teacher's TOC
5b7e511e 222 $toc .= html_writer::start_tag('ul');
f6ca19ae 223 $i = 0;
29656fe2 224 foreach ($chapters as $ch) {
f6ca19ae 225 $i++;
dc888a31 226 $title = trim(format_string($ch->title, true, array('context'=>$context)));
f6ca19ae 227 if (!$ch->subchapter) {
5b7e511e
TT
228
229 if ($first) {
bdb66a46 230 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
231 } else {
232 $toc .= html_writer::end_tag('ul');
233 $toc .= html_writer::end_tag('li');
bdb66a46 234 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
235 }
236
f6ca19ae
PS
237 if (!$ch->hidden) {
238 $nch++;
239 $ns = 0;
240 if ($book->numbering == BOOK_NUM_NUMBERS) {
241 $title = "$nch $title";
242 }
243 } else {
244 if ($book->numbering == BOOK_NUM_NUMBERS) {
245 $title = "x $title";
246 }
5b7e511e 247 $title = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
f6ca19ae 248 }
f6ca19ae 249 } else {
5b7e511e
TT
250
251 if ($first) {
bdb66a46 252 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 253 $toc .= html_writer::start_tag('ul');
bdb66a46 254 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 255 } else {
bdb66a46 256 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
257 }
258
f6ca19ae
PS
259 if (!$ch->hidden) {
260 $ns++;
261 if ($book->numbering == BOOK_NUM_NUMBERS) {
262 $title = "$nch.$ns $title";
263 }
264 } else {
265 if ($book->numbering == BOOK_NUM_NUMBERS) {
b0b30cb6
AA
266 if (empty($chapters[$ch->parent]->hidden)) {
267 $title = "$nch.x $title";
268 } else {
269 $title = "x.x $title";
270 }
f6ca19ae 271 }
5b7e511e 272 $title = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
f6ca19ae
PS
273 }
274 }
275
276 if ($ch->id == $chapter->id) {
5b7e511e 277 $toc .= html_writer::tag('strong', $title);
f6ca19ae 278 } else {
5b7e511e 279 $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($title)));
f6ca19ae 280 }
bdb66a46
FM
281
282 $toc .= html_writer::start_tag('div', array('class' => 'action-list'));
f6ca19ae 283 if ($i != 1) {
5b7e511e
TT
284 $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '1', 'sesskey' => $USER->sesskey)),
285 $OUTPUT->pix_icon('t/up', get_string('up')), array('title' => get_string('up')));
f6ca19ae
PS
286 }
287 if ($i != count($chapters)) {
5b7e511e
TT
288 $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '0', 'sesskey' => $USER->sesskey)),
289 $OUTPUT->pix_icon('t/down', get_string('down')), array('title' => get_string('down')));
f6ca19ae 290 }
5b7e511e
TT
291 $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)),
292 $OUTPUT->pix_icon('t/edit', get_string('edit')), array('title' => get_string('edit')));
293 $toc .= html_writer::link(new moodle_url('delete.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
294 $OUTPUT->pix_icon('t/delete', get_string('delete')), array('title' => get_string('delete')));
f6ca19ae 295 if ($ch->hidden) {
5b7e511e
TT
296 $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
297 $OUTPUT->pix_icon('t/show', get_string('show')), array('title' => get_string('show')));
f6ca19ae 298 } else {
5b7e511e
TT
299 $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
300 $OUTPUT->pix_icon('t/hide', get_string('hide')), array('title' => get_string('hide')));
f6ca19ae 301 }
5b7e511e
TT
302 $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'pagenum' => $ch->pagenum, 'subchapter' => $ch->subchapter)),
303 $OUTPUT->pix_icon('add', get_string('addafter', 'mod_book'), 'mod_book'), array('title' => get_string('addafter', 'mod_book')));
bdb66a46 304 $toc .= html_writer::end_tag('div');
5b7e511e
TT
305
306 if (!$ch->subchapter) {
307 $toc .= html_writer::start_tag('ul');
308 } else {
309 $toc .= html_writer::end_tag('li');
310 }
f6ca19ae
PS
311 $first = 0;
312 }
5b7e511e
TT
313
314 $toc .= html_writer::end_tag('ul');
315 $toc .= html_writer::end_tag('li');
316 $toc .= html_writer::end_tag('ul');
317
3542c4f4 318 } else { // Normal students view
5b7e511e 319 $toc .= html_writer::start_tag('ul');
29656fe2 320 foreach ($chapters as $ch) {
dc888a31 321 $title = trim(format_string($ch->title, true, array('context'=>$context)));
f6ca19ae
PS
322 if (!$ch->hidden) {
323 if (!$ch->subchapter) {
324 $nch++;
325 $ns = 0;
5b7e511e
TT
326
327 if ($first) {
bdb66a46 328 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
329 } else {
330 $toc .= html_writer::end_tag('ul');
331 $toc .= html_writer::end_tag('li');
bdb66a46 332 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
333 }
334
f6ca19ae
PS
335 if ($book->numbering == BOOK_NUM_NUMBERS) {
336 $title = "$nch $title";
337 }
f6ca19ae
PS
338 } else {
339 $ns++;
5b7e511e
TT
340
341 if ($first) {
bdb66a46 342 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 343 $toc .= html_writer::start_tag('ul');
bdb66a46 344 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e 345 } else {
bdb66a46 346 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
5b7e511e
TT
347 }
348
f6ca19ae
PS
349 if ($book->numbering == BOOK_NUM_NUMBERS) {
350 $title = "$nch.$ns $title";
351 }
352 }
353 if ($ch->id == $chapter->id) {
5b7e511e 354 $toc .= html_writer::tag('strong', $title);
f6ca19ae 355 } else {
5b7e511e 356 $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($title)));
f6ca19ae 357 }
5b7e511e
TT
358
359 if (!$ch->subchapter) {
360 $toc .= html_writer::start_tag('ul');
361 } else {
362 $toc .= html_writer::end_tag('li');
363 }
364
f6ca19ae
PS
365 $first = 0;
366 }
367 }
5b7e511e
TT
368
369 $toc .= html_writer::end_tag('ul');
370 $toc .= html_writer::end_tag('li');
371 $toc .= html_writer::end_tag('ul');
372
f6ca19ae
PS
373 }
374
5b7e511e 375 $toc .= html_writer::end_tag('div');
f6ca19ae 376
3542c4f4 377 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
f6ca19ae 378
ee952cbb 379 return $toc;
f6ca19ae 380}
dd9b772b
PS
381
382
383/**
384 * File browsing support class
3542c4f4
EL
385 *
386 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
387 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
dd9b772b
PS
388 */
389class book_file_info extends file_info {
3542c4f4 390 /** @var stdClass Course object */
dd9b772b 391 protected $course;
3542c4f4 392 /** @var stdClass Course module object */
dd9b772b 393 protected $cm;
3542c4f4 394 /** @var array Available file areas */
dd9b772b 395 protected $areas;
3542c4f4 396 /** @var string File area to browse */
dd9b772b
PS
397 protected $filearea;
398
3542c4f4
EL
399 /**
400 * Constructor
401 *
402 * @param file_browser $browser file_browser instance
403 * @param stdClass $course course object
404 * @param stdClass $cm course module object
405 * @param stdClass $context module context
406 * @param array $areas available file areas
407 * @param string $filearea file area to browse
408 */
dd9b772b
PS
409 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
410 parent::__construct($browser, $context);
411 $this->course = $course;
412 $this->cm = $cm;
413 $this->areas = $areas;
414 $this->filearea = $filearea;
415 }
416
417 /**
418 * Returns list of standard virtual file/directory identification.
419 * The difference from stored_file parameters is that null values
420 * are allowed in all fields
421 * @return array with keys contextid, filearea, itemid, filepath and filename
422 */
423 public function get_params() {
424 return array('contextid'=>$this->context->id,
425 'component'=>'mod_book',
426 'filearea' =>$this->filearea,
427 'itemid' =>null,
428 'filepath' =>null,
429 'filename' =>null);
430 }
431
432 /**
433 * Returns localised visible name.
434 * @return string
435 */
436 public function get_visible_name() {
437 return $this->areas[$this->filearea];
438 }
439
440 /**
441 * Can I add new files or directories?
442 * @return bool
443 */
444 public function is_writable() {
445 return false;
446 }
447
448 /**
449 * Is directory?
450 * @return bool
451 */
452 public function is_directory() {
453 return true;
454 }
455
456 /**
457 * Returns list of children.
458 * @return array of file_info instances
459 */
460 public function get_children() {
b8de2621
MG
461 return $this->get_filtered_children('*', false, true);
462 }
463
464 /**
465 * Help function to return files matching extensions or their count
466 *
467 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
43aadf04
MG
468 * @param bool|int $countonly if false returns the children, if an int returns just the
469 * count of children but stops counting when $countonly number of children is reached
b8de2621
MG
470 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
471 * @return array|int array of file_info instances or the count
472 */
473 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
dd9b772b 474 global $DB;
b8de2621
MG
475 $params = array('contextid' => $this->context->id,
476 'component' => 'mod_book',
477 'filearea' => $this->filearea,
478 'bookid' => $this->cm->instance);
479 $sql = 'SELECT DISTINCT bc.id, bc.pagenum
480 FROM {files} f, {book_chapters} bc
481 WHERE f.contextid = :contextid
482 AND f.component = :component
483 AND f.filearea = :filearea
484 AND bc.bookid = :bookid
485 AND bc.id = f.itemid';
486 if (!$returnemptyfolders) {
487 $sql .= ' AND filename <> :emptyfilename';
488 $params['emptyfilename'] = '.';
489 }
490 list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
491 $sql .= ' '.$sql2;
492 $params = array_merge($params, $params2);
43aadf04 493 if ($countonly === false) {
b8de2621
MG
494 $sql .= ' ORDER BY bc.pagenum';
495 }
dd9b772b 496
b8de2621 497 $rs = $DB->get_recordset_sql($sql, $params);
dd9b772b 498 $children = array();
b8de2621
MG
499 foreach ($rs as $record) {
500 if ($child = $this->browser->get_file_info($this->context, 'mod_book', $this->filearea, $record->id)) {
501 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
502 $children[] = $child;
503 }
504 }
43aadf04 505 if ($countonly !== false && count($children) >= $countonly) {
b8de2621 506 break;
dd9b772b
PS
507 }
508 }
b8de2621 509 $rs->close();
43aadf04 510 if ($countonly !== false) {
b8de2621
MG
511 return count($children);
512 }
dd9b772b
PS
513 return $children;
514 }
515
b8de2621
MG
516 /**
517 * Returns list of children which are either files matching the specified extensions
518 * or folders that contain at least one such file.
519 *
520 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
521 * @return array of file_info instances
522 */
523 public function get_non_empty_children($extensions = '*') {
524 return $this->get_filtered_children($extensions, false);
525 }
526
527 /**
528 * Returns the number of children which are either files matching the specified extensions
529 * or folders containing at least one such file.
530 *
b8de2621 531 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
43aadf04 532 * @param int $limit stop counting after at least $limit non-empty children are found
b8de2621
MG
533 * @return int
534 */
43aadf04
MG
535 public function count_non_empty_children($extensions = '*', $limit = 1) {
536 return $this->get_filtered_children($extensions, $limit);
b8de2621
MG
537 }
538
dd9b772b
PS
539 /**
540 * Returns parent file_info instance
541 * @return file_info or null for root
542 */
543 public function get_parent() {
544 return $this->browser->get_file_info($this->context);
545 }
546}