2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Book module local lib functions
21 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
27 require_once(__DIR__.'/lib.php');
28 require_once($CFG->libdir.'/filelib.php');
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
37 define('BOOK_NUM_NONE', '0');
38 define('BOOK_NUM_NUMBERS', '1');
39 define('BOOK_NUM_BULLETS', '2');
40 define('BOOK_NUM_INDENTED', '3');
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.
48 define ('BOOK_LINK_TOCONLY', '0');
49 define ('BOOK_LINK_IMAGE', '1');
50 define ('BOOK_LINK_TEXT', '2');
53 * Preload book chapters and fix toc structure if necessary.
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.
59 * @param stdClass $book
60 * @return array of id=>chapter
62 function book_preload_chapters($book) {
64 $chapters = $DB->get_records('book_chapters', array('bookid'=>$book->id), 'pagenum', 'id, pagenum, subchapter, title, hidden');
75 $pagenum = 0; // chapter sort
76 $i = 0; // main chapter num
77 $j = 0; // subchapter num
78 foreach ($chapters as $id => $ch) {
81 $ch->pagenum = $pagenum;
83 // book can not start with a subchapter
87 if (!$ch->subchapter) {
89 if ($book->numbering == BOOK_NUM_NUMBERS) {
100 $hidesub = $ch->hidden;
103 $ch->subchapters = array();
105 $ch->parent = $parent;
106 $ch->subchapters = null;
107 $chapters[$parent]->subchapters[$ch->id] = $ch->id;
109 // all subchapters in hidden chapter must be hidden too
113 if ($book->numbering == BOOK_NUM_NUMBERS) {
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);
128 $chapters[$id] = $ch;
135 * Returns the title for a given chapter
138 * @param array $chapters
139 * @param stdClass $book
140 * @param context_module $context
143 function book_get_chapter_title($chid, $chapters, $book, $context) {
144 $ch = $chapters[$chid];
145 $title = trim(format_string($ch->title, true, array('context'=>$context)));
147 if ($book->numbering == BOOK_NUM_NUMBERS) {
148 if ($ch->parent and $chapters[$ch->parent]->number) {
149 $numbers[] = $chapters[$ch->parent]->number;
152 $numbers[] = $ch->number;
157 $title = implode('.', $numbers).' '.$title;
164 * Add the book TOC sticky block to the default region
166 * @param array $chapters
167 * @param stdClass $chapter
168 * @param stdClass $book
169 * @param stdClass $cm
172 function book_add_fake_block($chapters, $chapter, $book, $cm, $edit) {
173 global $OUTPUT, $PAGE;
175 $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
177 $bc = new block_contents();
178 $bc->title = get_string('toc', 'mod_book');
179 $bc->attributes['class'] = 'block block_book_toc';
182 $defaultregion = $PAGE->blocks->get_default_region();
183 $PAGE->blocks->add_fake_block($bc, $defaultregion);
187 * Generate toc structure
189 * @param array $chapters
190 * @param stdClass $chapter
191 * @param stdClass $book
192 * @param stdClass $cm
196 function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
197 global $USER, $OUTPUT;
200 $nch = 0; // Chapter number
201 $ns = 0; // Subchapter number
204 $context = context_module::instance($cm->id);
206 switch ($book->numbering) {
208 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none clearfix'));
210 case BOOK_NUM_NUMBERS:
211 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered clearfix'));
213 case BOOK_NUM_BULLETS:
214 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets clearfix'));
216 case BOOK_NUM_INDENTED:
217 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented clearfix'));
221 if ($edit) { // Teacher's TOC
222 $toc .= html_writer::start_tag('ul');
224 foreach ($chapters as $ch) {
226 $title = trim(format_string($ch->title, true, array('context' => $context)));
227 $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false)));
230 if (!$ch->subchapter) {
233 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
235 $toc .= html_writer::end_tag('ul');
236 $toc .= html_writer::end_tag('li');
237 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
243 if ($book->numbering == BOOK_NUM_NUMBERS) {
244 $title = "$nch $title";
248 if ($book->numbering == BOOK_NUM_NUMBERS) {
251 $titleout = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
256 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
257 $toc .= html_writer::start_tag('ul');
258 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
260 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
265 if ($book->numbering == BOOK_NUM_NUMBERS) {
266 $title = "$nch.$ns $title";
270 if ($book->numbering == BOOK_NUM_NUMBERS) {
271 if (empty($chapters[$ch->parent]->hidden)) {
272 $title = "$nch.x $title";
274 $title = "x.x $title";
277 $titleout = html_writer::tag('span', $title, array('class' => 'dimmed_text'));
281 if ($ch->id == $chapter->id) {
282 $toc .= html_writer::tag('strong', $titleout);
284 $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $titleout,
285 array('title' => $titleunescaped));
288 $toc .= html_writer::start_tag('div', array('class' => 'action-list'));
290 $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '1', 'sesskey' => $USER->sesskey)),
291 $OUTPUT->pix_icon('t/up', get_string('movechapterup', 'mod_book', $title)),
292 array('title' => get_string('movechapterup', 'mod_book', $titleunescaped)));
294 if ($i != count($chapters)) {
295 $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '0', 'sesskey' => $USER->sesskey)),
296 $OUTPUT->pix_icon('t/down', get_string('movechapterdown', 'mod_book', $title)),
297 array('title' => get_string('movechapterdown', 'mod_book', $titleunescaped)));
299 $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)),
300 $OUTPUT->pix_icon('t/edit', get_string('editchapter', 'mod_book', $title)),
301 array('title' => get_string('editchapter', 'mod_book', $titleunescaped)));
302 $toc .= html_writer::link(new moodle_url('delete.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
303 $OUTPUT->pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)),
304 array('title' => get_string('deletechapter', 'mod_book', $titleunescaped)));
306 $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
307 $OUTPUT->pix_icon('t/show', get_string('showchapter', 'mod_book', $title)),
308 array('title' => get_string('showchapter', 'mod_book', $titleunescaped)));
310 $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)),
311 $OUTPUT->pix_icon('t/hide', get_string('hidechapter', 'mod_book', $title)),
312 array('title' => get_string('hidechapter', 'mod_book', $titleunescaped)));
314 $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'pagenum' => $ch->pagenum, 'subchapter' => $ch->subchapter)),
315 $OUTPUT->pix_icon('add', get_string('addafter', 'mod_book'), 'mod_book'), array('title' => get_string('addafter', 'mod_book')));
316 $toc .= html_writer::end_tag('div');
318 if (!$ch->subchapter) {
319 $toc .= html_writer::start_tag('ul');
321 $toc .= html_writer::end_tag('li');
326 $toc .= html_writer::end_tag('ul');
327 $toc .= html_writer::end_tag('li');
328 $toc .= html_writer::end_tag('ul');
330 } else { // Normal students view
331 $toc .= html_writer::start_tag('ul');
332 foreach ($chapters as $ch) {
333 $title = trim(format_string($ch->title, true, array('context'=>$context)));
335 if (!$ch->subchapter) {
340 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
342 $toc .= html_writer::end_tag('ul');
343 $toc .= html_writer::end_tag('li');
344 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
347 if ($book->numbering == BOOK_NUM_NUMBERS) {
348 $title = "$nch $title";
354 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
355 $toc .= html_writer::start_tag('ul');
356 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
358 $toc .= html_writer::start_tag('li', array('class' => 'clearfix'));
361 if ($book->numbering == BOOK_NUM_NUMBERS) {
362 $title = "$nch.$ns $title";
365 if ($ch->id == $chapter->id) {
366 $toc .= html_writer::tag('strong', $title);
368 $toc .= html_writer::link(new moodle_url('view.php',
369 array('id' => $cm->id, 'chapterid' => $ch->id)),
370 $title, array('title' => s($titleunescaped)));
373 if (!$ch->subchapter) {
374 $toc .= html_writer::start_tag('ul');
376 $toc .= html_writer::end_tag('li');
383 $toc .= html_writer::end_tag('ul');
384 $toc .= html_writer::end_tag('li');
385 $toc .= html_writer::end_tag('ul');
389 $toc .= html_writer::end_tag('div');
391 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
398 * File browsing support class
400 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
401 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
403 class book_file_info extends file_info {
404 /** @var stdClass Course object */
406 /** @var stdClass Course module object */
408 /** @var array Available file areas */
410 /** @var string File area to browse */
416 * @param file_browser $browser file_browser instance
417 * @param stdClass $course course object
418 * @param stdClass $cm course module object
419 * @param stdClass $context module context
420 * @param array $areas available file areas
421 * @param string $filearea file area to browse
423 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
424 parent::__construct($browser, $context);
425 $this->course = $course;
427 $this->areas = $areas;
428 $this->filearea = $filearea;
432 * Returns list of standard virtual file/directory identification.
433 * The difference from stored_file parameters is that null values
434 * are allowed in all fields
435 * @return array with keys contextid, filearea, itemid, filepath and filename
437 public function get_params() {
438 return array('contextid'=>$this->context->id,
439 'component'=>'mod_book',
440 'filearea' =>$this->filearea,
447 * Returns localised visible name.
450 public function get_visible_name() {
451 return $this->areas[$this->filearea];
455 * Can I add new files or directories?
458 public function is_writable() {
466 public function is_directory() {
471 * Returns list of children.
472 * @return array of file_info instances
474 public function get_children() {
475 return $this->get_filtered_children('*', false, true);
479 * Help function to return files matching extensions or their count
481 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
482 * @param bool|int $countonly if false returns the children, if an int returns just the
483 * count of children but stops counting when $countonly number of children is reached
484 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
485 * @return array|int array of file_info instances or the count
487 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
489 $params = array('contextid' => $this->context->id,
490 'component' => 'mod_book',
491 'filearea' => $this->filearea,
492 'bookid' => $this->cm->instance);
493 $sql = 'SELECT DISTINCT bc.id, bc.pagenum
494 FROM {files} f, {book_chapters} bc
495 WHERE f.contextid = :contextid
496 AND f.component = :component
497 AND f.filearea = :filearea
498 AND bc.bookid = :bookid
499 AND bc.id = f.itemid';
500 if (!$returnemptyfolders) {
501 $sql .= ' AND filename <> :emptyfilename';
502 $params['emptyfilename'] = '.';
504 list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
506 $params = array_merge($params, $params2);
507 if ($countonly === false) {
508 $sql .= ' ORDER BY bc.pagenum';
511 $rs = $DB->get_recordset_sql($sql, $params);
513 foreach ($rs as $record) {
514 if ($child = $this->browser->get_file_info($this->context, 'mod_book', $this->filearea, $record->id)) {
515 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
516 $children[] = $child;
519 if ($countonly !== false && count($children) >= $countonly) {
524 if ($countonly !== false) {
525 return count($children);
531 * Returns list of children which are either files matching the specified extensions
532 * or folders that contain at least one such file.
534 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
535 * @return array of file_info instances
537 public function get_non_empty_children($extensions = '*') {
538 return $this->get_filtered_children($extensions, false);
542 * Returns the number of children which are either files matching the specified extensions
543 * or folders containing at least one such file.
545 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
546 * @param int $limit stop counting after at least $limit non-empty children are found
549 public function count_non_empty_children($extensions = '*', $limit = 1) {
550 return $this->get_filtered_children($extensions, $limit);
554 * Returns parent file_info instance
555 * @return file_info or null for root
557 public function get_parent() {
558 return $this->browser->get_file_info($this->context);