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 The Chapters in the book
167 * @param stdClass $chapter The current chapter
168 * @param stdClass $book The book
169 * @param stdClass $cm The course module
170 * @param bool $edit Whether the user is editing
172 function book_add_fake_block($chapters, $chapter, $book, $cm, $edit = null) {
175 if ($edit === null) {
176 if (has_capability('mod/book:edit', context_module
::instance($cm->id
))) {
177 if (isset($USER->editing
)) {
178 $edit = $USER->editing
;
187 $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit, 0);
189 $bc = new block_contents();
190 $bc->title
= get_string('toc', 'mod_book');
191 $bc->attributes
['class'] = 'block block_book_toc';
194 $defaultregion = $PAGE->blocks
->get_default_region();
195 $PAGE->blocks
->add_fake_block($bc, $defaultregion);
199 * Generate toc structure
201 * @param array $chapters
202 * @param stdClass $chapter
203 * @param stdClass $book
204 * @param stdClass $cm
208 function book_get_toc($chapters, $chapter, $book, $cm, $edit) {
209 global $USER, $OUTPUT;
212 $nch = 0; // Chapter number
213 $ns = 0; // Subchapter number
216 $context = context_module
::instance($cm->id
);
218 switch ($book->numbering
) {
220 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_none clearfix'));
222 case BOOK_NUM_NUMBERS
:
223 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_numbered clearfix'));
225 case BOOK_NUM_BULLETS
:
226 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_bullets clearfix'));
228 case BOOK_NUM_INDENTED
:
229 $toc .= html_writer
::start_tag('div', array('class' => 'book_toc_indented clearfix'));
233 if ($edit) { // Teacher's TOC
234 $toc .= html_writer
::start_tag('ul');
236 foreach ($chapters as $ch) {
238 $title = trim(format_string($ch->title
, true, array('context' => $context)));
239 $titleunescaped = trim(format_string($ch->title
, true, array('context' => $context, 'escape' => false)));
242 if (!$ch->subchapter
) {
245 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
247 $toc .= html_writer
::end_tag('ul');
248 $toc .= html_writer
::end_tag('li');
249 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
255 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
256 $title = "$nch. $title";
260 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
261 $title = "x. $title";
263 $titleout = html_writer
::tag('span', $title, array('class' => 'dimmed_text'));
268 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
269 $toc .= html_writer
::start_tag('ul');
270 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
272 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
277 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
278 $title = "$nch.$ns. $title";
282 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
283 if (empty($chapters[$ch->parent
]->hidden
)) {
284 $title = "$nch.x. $title";
286 $title = "x.x. $title";
289 $titleout = html_writer
::tag('span', $title, array('class' => 'dimmed_text'));
293 if ($ch->id
== $chapter->id
) {
294 $toc .= html_writer
::tag('strong', $titleout);
296 $toc .= html_writer
::link(new moodle_url('view.php', array('id' => $cm->id
, 'chapterid' => $ch->id
)), $titleout,
297 array('title' => $titleunescaped));
300 $toc .= html_writer
::start_tag('div', array('class' => 'action-list'));
302 $toc .= html_writer
::link(new moodle_url('move.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'up' => '1', 'sesskey' => $USER->sesskey
)),
303 $OUTPUT->pix_icon('t/up', get_string('movechapterup', 'mod_book', $title)),
304 array('title' => get_string('movechapterup', 'mod_book', $titleunescaped)));
306 if ($i != count($chapters)) {
307 $toc .= html_writer
::link(new moodle_url('move.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'up' => '0', 'sesskey' => $USER->sesskey
)),
308 $OUTPUT->pix_icon('t/down', get_string('movechapterdown', 'mod_book', $title)),
309 array('title' => get_string('movechapterdown', 'mod_book', $titleunescaped)));
311 $toc .= html_writer
::link(new moodle_url('edit.php', array('cmid' => $cm->id
, 'id' => $ch->id
)),
312 $OUTPUT->pix_icon('t/edit', get_string('editchapter', 'mod_book', $title)),
313 array('title' => get_string('editchapter', 'mod_book', $titleunescaped)));
315 $deleteaction = new confirm_action(get_string('deletechapter', 'mod_book', $titleunescaped));
316 $toc .= $OUTPUT->action_icon(
317 new moodle_url('delete.php', [
319 'chapterid' => $ch->id
,
320 'sesskey' => sesskey(),
323 new pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)),
325 ['title' => get_string('deletechapter', 'mod_book', $titleunescaped)]
329 $toc .= html_writer
::link(new moodle_url('show.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'sesskey' => $USER->sesskey
)),
330 $OUTPUT->pix_icon('t/show', get_string('showchapter', 'mod_book', $title)),
331 array('title' => get_string('showchapter', 'mod_book', $titleunescaped)));
333 $toc .= html_writer
::link(new moodle_url('show.php', array('id' => $cm->id
, 'chapterid' => $ch->id
, 'sesskey' => $USER->sesskey
)),
334 $OUTPUT->pix_icon('t/hide', get_string('hidechapter', 'mod_book', $title)),
335 array('title' => get_string('hidechapter', 'mod_book', $titleunescaped)));
337 $toc .= html_writer
::link(new moodle_url('edit.php', array('cmid' => $cm->id
, 'pagenum' => $ch->pagenum
, 'subchapter' => $ch->subchapter
)),
338 $OUTPUT->pix_icon('add', get_string('addafter', 'mod_book'), 'mod_book'), array('title' => get_string('addafter', 'mod_book')));
339 $toc .= html_writer
::end_tag('div');
341 if (!$ch->subchapter
) {
342 $toc .= html_writer
::start_tag('ul');
344 $toc .= html_writer
::end_tag('li');
349 $toc .= html_writer
::end_tag('ul');
350 $toc .= html_writer
::end_tag('li');
351 $toc .= html_writer
::end_tag('ul');
353 } else { // Normal students view
354 $toc .= html_writer
::start_tag('ul');
355 foreach ($chapters as $ch) {
356 $title = trim(format_string($ch->title
, true, array('context'=>$context)));
357 $titleunescaped = trim(format_string($ch->title
, true, array('context' => $context, 'escape' => false)));
359 if (!$ch->subchapter
) {
364 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
366 $toc .= html_writer
::end_tag('ul');
367 $toc .= html_writer
::end_tag('li');
368 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
371 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
372 $title = "$nch. $title";
378 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
379 $toc .= html_writer
::start_tag('ul');
380 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
382 $toc .= html_writer
::start_tag('li', array('class' => 'clearfix'));
385 if ($book->numbering
== BOOK_NUM_NUMBERS
) {
386 $title = "$nch.$ns. $title";
389 if ($ch->id
== $chapter->id
) {
390 $toc .= html_writer
::tag('strong', $title);
392 $toc .= html_writer
::link(new moodle_url('view.php',
393 array('id' => $cm->id
, 'chapterid' => $ch->id
)),
394 $title, array('title' => s($titleunescaped)));
397 if (!$ch->subchapter
) {
398 $toc .= html_writer
::start_tag('ul');
400 $toc .= html_writer
::end_tag('li');
407 $toc .= html_writer
::end_tag('ul');
408 $toc .= html_writer
::end_tag('li');
409 $toc .= html_writer
::end_tag('ul');
413 $toc .= html_writer
::end_tag('div');
415 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
421 * Returns book chapters tagged with a specified tag.
423 * This is a callback used by the tag area mod_book/book_chapters to search for book chapters
424 * tagged with a specific tag.
426 * @param core_tag_tag $tag
427 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
428 * are displayed on the page and the per-page limit may be bigger
429 * @param int $fromctx context id where the link was displayed, may be used by callbacks
430 * to display items in the same context first
431 * @param int $ctx context id where to search for records
432 * @param bool $rec search in subcontexts as well
433 * @param int $page 0-based number of page being displayed
434 * @return \core_tag\output\tagindex
436 function mod_book_get_tagged_chapters($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = true, $page = 0) {
438 $perpage = $exclusivemode ?
20 : 5;
440 // Build the SQL query.
441 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
442 $query = "SELECT bc.id, bc.title, bc.bookid, bc.hidden,
443 cm.id AS cmid, c.id AS courseid, c.shortname, c.fullname, $ctxselect
444 FROM {book_chapters} bc
445 JOIN {book} b ON b.id = bc.bookid
446 JOIN {modules} m ON m.name='book'
447 JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = b.id
448 JOIN {tag_instance} tt ON bc.id = tt.itemid
449 JOIN {course} c ON cm.course = c.id
450 JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel
451 WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component
452 AND cm.deletioninprogress = 0
453 AND bc.id %ITEMFILTER% AND c.id %COURSEFILTER%";
455 $params = array('itemtype' => 'book_chapters', 'tagid' => $tag->id
, 'component' => 'mod_book',
456 'coursemodulecontextlevel' => CONTEXT_MODULE
);
459 $context = $ctx ? context
::instance_by_id($ctx) : context_system
::instance();
460 $query .= $rec ?
' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
461 $params['contextid'] = $context->id
;
462 $params['path'] = $context->path
.'/%';
465 $query .= " ORDER BY ";
467 // In order-clause specify that modules from inside "fromctx" context should be returned first.
468 $fromcontext = context
::instance_by_id($fromctx);
469 $query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
470 $params['fromcontextid'] = $fromcontext->id
;
471 $params['frompath'] = $fromcontext->path
.'/%';
473 $query .= ' c.sortorder, cm.id, bc.id';
475 $totalpages = $page +
1;
477 // Use core_tag_index_builder to build and filter the list of items.
478 $builder = new core_tag_index_builder('mod_book', 'book_chapters', $query, $params, $page * $perpage, $perpage +
1);
479 while ($item = $builder->has_item_that_needs_access_check()) {
480 context_helper
::preload_from_record($item);
481 $courseid = $item->courseid
;
482 if (!$builder->can_access_course($courseid)) {
483 $builder->set_accessible($item, false);
486 $modinfo = get_fast_modinfo($builder->get_course($courseid));
487 // Set accessibility of this item and all other items in the same course.
488 $builder->walk(function ($taggeditem) use ($courseid, $modinfo, $builder) {
489 if ($taggeditem->courseid
== $courseid) {
491 if (($cm = $modinfo->get_cm($taggeditem->cmid
)) && $cm->uservisible
) {
492 if (empty($taggeditem->hidden
)) {
495 $accessible = has_capability('mod/book:viewhiddenchapters', context_module
::instance($cm->id
));
498 $builder->set_accessible($taggeditem, $accessible);
503 $items = $builder->get_items();
504 if (count($items) > $perpage) {
505 $totalpages = $page +
2; // We don't need exact page count, just indicate that the next page exists.
509 // Build the display contents.
511 $tagfeed = new core_tag\output\tagfeed
();
512 foreach ($items as $item) {
513 context_helper
::preload_from_record($item);
514 $modinfo = get_fast_modinfo($item->courseid
);
515 $cm = $modinfo->get_cm($item->cmid
);
516 $pageurl = new moodle_url('/mod/book/view.php', array('chapterid' => $item->id
, 'b' => $item->bookid
));
517 $pagename = format_string($item->title
, true, array('context' => context_module
::instance($item->cmid
)));
518 $pagename = html_writer
::link($pageurl, $pagename);
519 $courseurl = course_get_url($item->courseid
, $cm->sectionnum
);
520 $cmname = html_writer
::link($cm->url
, $cm->get_formatted_name());
521 $coursename = format_string($item->fullname
, true, array('context' => context_course
::instance($item->courseid
)));
522 $coursename = html_writer
::link($courseurl, $coursename);
523 $icon = html_writer
::link($pageurl, html_writer
::empty_tag('img', array('src' => $cm->get_icon_url())));
524 $tagfeed->add($icon, $pagename, $cmname.'<br>'.$coursename);
527 $content = $OUTPUT->render_from_template('core_tag/tagfeed',
528 $tagfeed->export_for_template($OUTPUT));
530 return new core_tag\output\tagindex
($tag, 'mod_book', 'book_chapters', $content,
531 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
536 * File browsing support class
538 * @copyright 2010-2011 Petr Skoda {@link http://skodak.org}
539 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
541 class book_file_info
extends file_info
{
542 /** @var stdClass Course object */
544 /** @var stdClass Course module object */
546 /** @var array Available file areas */
548 /** @var string File area to browse */
554 * @param file_browser $browser file_browser instance
555 * @param stdClass $course course object
556 * @param stdClass $cm course module object
557 * @param stdClass $context module context
558 * @param array $areas available file areas
559 * @param string $filearea file area to browse
561 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
562 parent
::__construct($browser, $context);
563 $this->course
= $course;
565 $this->areas
= $areas;
566 $this->filearea
= $filearea;
570 * Returns list of standard virtual file/directory identification.
571 * The difference from stored_file parameters is that null values
572 * are allowed in all fields
573 * @return array with keys contextid, filearea, itemid, filepath and filename
575 public function get_params() {
576 return array('contextid'=>$this->context
->id
,
577 'component'=>'mod_book',
578 'filearea' =>$this->filearea
,
585 * Returns localised visible name.
588 public function get_visible_name() {
589 return $this->areas
[$this->filearea
];
593 * Can I add new files or directories?
596 public function is_writable() {
604 public function is_directory() {
609 * Returns list of children.
610 * @return array of file_info instances
612 public function get_children() {
613 return $this->get_filtered_children('*', false, true);
617 * Help function to return files matching extensions or their count
619 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
620 * @param bool|int $countonly if false returns the children, if an int returns just the
621 * count of children but stops counting when $countonly number of children is reached
622 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
623 * @return array|int array of file_info instances or the count
625 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
627 $params = array('contextid' => $this->context
->id
,
628 'component' => 'mod_book',
629 'filearea' => $this->filearea
,
630 'bookid' => $this->cm
->instance
);
631 $sql = 'SELECT DISTINCT bc.id, bc.pagenum
632 FROM {files} f, {book_chapters} bc
633 WHERE f.contextid = :contextid
634 AND f.component = :component
635 AND f.filearea = :filearea
636 AND bc.bookid = :bookid
637 AND bc.id = f.itemid';
638 if (!$returnemptyfolders) {
639 $sql .= ' AND filename <> :emptyfilename';
640 $params['emptyfilename'] = '.';
642 list($sql2, $params2) = $this->build_search_files_sql($extensions, 'f');
644 $params = array_merge($params, $params2);
645 if ($countonly === false) {
646 $sql .= ' ORDER BY bc.pagenum';
649 $rs = $DB->get_recordset_sql($sql, $params);
651 foreach ($rs as $record) {
652 if ($child = $this->browser
->get_file_info($this->context
, 'mod_book', $this->filearea
, $record->id
)) {
653 if ($returnemptyfolders ||
$child->count_non_empty_children($extensions)) {
654 $children[] = $child;
657 if ($countonly !== false && count($children) >= $countonly) {
662 if ($countonly !== false) {
663 return count($children);
669 * Returns list of children which are either files matching the specified extensions
670 * or folders that contain at least one such file.
672 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
673 * @return array of file_info instances
675 public function get_non_empty_children($extensions = '*') {
676 return $this->get_filtered_children($extensions, false);
680 * Returns the number of children which are either files matching the specified extensions
681 * or folders containing at least one such file.
683 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
684 * @param int $limit stop counting after at least $limit non-empty children are found
687 public function count_non_empty_children($extensions = '*', $limit = 1) {
688 return $this->get_filtered_children($extensions, $limit);
692 * Returns parent file_info instance
693 * @return file_info or null for root
695 public function get_parent() {
696 return $this->browser
->get_file_info($this->context
);