. /** * Book module local lib functions * * @package mod_book * @copyright 2010-2011 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die; require_once(__DIR__.'/lib.php'); require_once($CFG->libdir.'/filelib.php'); /** * The following defines are used to define how the chapters and subchapters of a book should be displayed in that table of contents. * BOOK_NUM_NONE No special styling will applied and the editor will be able to do what ever thay want in the title * BOOK_NUM_NUMBERS Chapters and subchapters are numbered (1, 1.1, 1.2, 2, ...) * BOOK_NUM_BULLETS Subchapters are indented and displayed with bullets * BOOK_NUM_INDENTED Subchapters are indented */ define('BOOK_NUM_NONE', '0'); define('BOOK_NUM_NUMBERS', '1'); define('BOOK_NUM_BULLETS', '2'); define('BOOK_NUM_INDENTED', '3'); /** * The following defines are used to define the navigation style used within a book. * BOOK_LINK_TOCONLY Only the table of contents is shown, in a side region. * BOOK_LINK_IMAGE Arrows link to previous/next/exit pages, in addition to the TOC. * BOOK_LINK_TEXT Page names and arrows link to previous/next/exit pages, in addition to the TOC. */ define ('BOOK_LINK_TOCONLY', '0'); define ('BOOK_LINK_IMAGE', '1'); define ('BOOK_LINK_TEXT', '2'); /** * Preload book chapters and fix toc structure if necessary. * * Returns array of chapters with standard 'pagenum', 'id, pagenum, subchapter, title, content, contentformat, hidden' * and extra 'parent, number, subchapters, prev, next'. * Please note the content/text of chapters is not included. * * @param stdClass $book * @return array of id=>chapter */ function book_preload_chapters($book) { global $DB; $chapters = $DB->get_records('book_chapters', array('bookid' => $book->id), 'pagenum', 'id, pagenum, subchapter, title, content, contentformat, hidden'); if (!$chapters) { return array(); } $prev = null; $prevsub = null; $first = true; $hidesub = true; $parent = null; $pagenum = 0; // chapter sort $i = 0; // main chapter num $j = 0; // subchapter num foreach ($chapters as $id => $ch) { $oldch = clone($ch); $pagenum++; $ch->pagenum = $pagenum; if ($first) { // book can not start with a subchapter $ch->subchapter = 0; $first = false; } if (!$ch->subchapter) { if ($ch->hidden) { if ($book->numbering == BOOK_NUM_NUMBERS) { $ch->number = 'x'; } else { $ch->number = null; } } else { $i++; $ch->number = $i; } $j = 0; $prevsub = null; $hidesub = $ch->hidden; $parent = $ch->id; $ch->parent = null; $ch->subchapters = array(); } else { $ch->parent = $parent; $ch->subchapters = null; $chapters[$parent]->subchapters[$ch->id] = $ch->id; if ($hidesub) { // all subchapters in hidden chapter must be hidden too $ch->hidden = 1; } if ($ch->hidden) { if ($book->numbering == BOOK_NUM_NUMBERS) { $ch->number = 'x'; } else { $ch->number = null; } } else { $j++; $ch->number = $j; } } if ($oldch->subchapter != $ch->subchapter or $oldch->pagenum != $ch->pagenum or $oldch->hidden != $ch->hidden) { // update only if something changed $DB->update_record('book_chapters', $ch); } $chapters[$id] = $ch; } return $chapters; } /** * Returns the title for a given chapter * * @param int $chid * @param array $chapters * @param stdClass $book * @param context_module $context * @return string */ function book_get_chapter_title($chid, $chapters, $book, $context) { $ch = $chapters[$chid]; $title = trim(format_string($ch->title, true, array('context'=>$context))); $numbers = array(); if ($book->numbering == BOOK_NUM_NUMBERS) { if ($ch->parent and $chapters[$ch->parent]->number) { $numbers[] = $chapters[$ch->parent]->number; } if ($ch->number) { $numbers[] = $ch->number; } } if ($numbers) { $title = implode('.', $numbers) . '. ' . $title; } return $title; } /** * Add the book TOC sticky block to the default region. * * @param array $chapters The Chapters in the book * @param stdClass $chapter The current chapter * @param stdClass $book The book * @param stdClass $cm The course module * @param bool|null $edit Whether the user is editing */ function book_add_fake_block($chapters, $chapter, $book, $cm, $edit = null) { global $PAGE, $USER; if ($edit === null) { if (has_capability('mod/book:edit', context_module::instance($cm->id))) { if (isset($USER->editing)) { $edit = $USER->editing; } else { $edit = 0; } } else { $edit = 0; } } $toc = book_get_toc($chapters, $chapter, $book, $cm, $edit); $bc = new block_contents(); $bc->title = get_string('toc', 'mod_book'); $bc->attributes['class'] = 'block block_book_toc'; $bc->content = $toc; $defaultregion = $PAGE->blocks->get_default_region(); $PAGE->blocks->add_fake_block($bc, $defaultregion); } /** * Generate toc structure * * @param array $chapters * @param stdClass $chapter * @param stdClass $book * @param stdClass $cm * @param bool $edit * @return string */ function book_get_toc($chapters, $chapter, $book, $cm, $edit) { global $USER, $OUTPUT; $toc = ''; $nch = 0; // Chapter number $ns = 0; // Subchapter number $first = 1; $context = context_module::instance($cm->id); $viewhidden = has_capability('mod/book:viewhiddenchapters', $context); switch ($book->numbering) { case BOOK_NUM_NONE: $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none clearfix')); break; case BOOK_NUM_NUMBERS: $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered clearfix')); break; case BOOK_NUM_BULLETS: $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets clearfix')); break; case BOOK_NUM_INDENTED: $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented clearfix')); break; } if ($edit) { // Editing on (Teacher's TOC). $toc .= html_writer::start_tag('ul'); $i = 0; foreach ($chapters as $ch) { $i++; $title = trim(format_string($ch->title, true, array('context' => $context))); $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false))); $titleout = $title; if (!$ch->subchapter) { if ($first) { $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } else { $toc .= html_writer::end_tag('ul'); $toc .= html_writer::end_tag('li'); $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } if (!$ch->hidden) { $nch++; $ns = 0; if ($book->numbering == BOOK_NUM_NUMBERS) { $title = "$nch. $title"; $titleout = $title; } } else { if ($book->numbering == BOOK_NUM_NUMBERS) { $title = "x. $title"; } $titleout = html_writer::tag('span', $title, array('class' => 'dimmed_text')); } } else { if ($first) { $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); $toc .= html_writer::start_tag('ul'); $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } else { $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } if (!$ch->hidden) { $ns++; if ($book->numbering == BOOK_NUM_NUMBERS) { $title = "$nch.$ns. $title"; $titleout = $title; } } else { if ($book->numbering == BOOK_NUM_NUMBERS) { if (empty($chapters[$ch->parent]->hidden)) { $title = "$nch.x. $title"; } else { $title = "x.x. $title"; } } $titleout = html_writer::tag('span', $title, array('class' => 'dimmed_text')); } } if ($ch->id == $chapter->id) { $toc .= html_writer::tag('strong', $titleout); } else { $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $titleout, array('title' => $titleunescaped)); } $toc .= html_writer::start_tag('div', array('class' => 'action-list')); if ($i != 1) { $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '1', 'sesskey' => $USER->sesskey)), $OUTPUT->pix_icon('t/up', get_string('movechapterup', 'mod_book', $title)), array('title' => get_string('movechapterup', 'mod_book', $titleunescaped))); } if ($i != count($chapters)) { $toc .= html_writer::link(new moodle_url('move.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'up' => '0', 'sesskey' => $USER->sesskey)), $OUTPUT->pix_icon('t/down', get_string('movechapterdown', 'mod_book', $title)), array('title' => get_string('movechapterdown', 'mod_book', $titleunescaped))); } $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'id' => $ch->id)), $OUTPUT->pix_icon('t/edit', get_string('editchapter', 'mod_book', $title)), array('title' => get_string('editchapter', 'mod_book', $titleunescaped))); $deleteaction = new confirm_action(get_string('deletechapter', 'mod_book', $titleunescaped)); $toc .= $OUTPUT->action_icon( new moodle_url('delete.php', [ 'id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => sesskey(), 'confirm' => 1, ]), new pix_icon('t/delete', get_string('deletechapter', 'mod_book', $title)), $deleteaction, ['title' => get_string('deletechapter', 'mod_book', $titleunescaped)] ); if ($ch->hidden) { $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)), $OUTPUT->pix_icon('t/show', get_string('showchapter', 'mod_book', $title)), array('title' => get_string('showchapter', 'mod_book', $titleunescaped))); } else { $toc .= html_writer::link(new moodle_url('show.php', array('id' => $cm->id, 'chapterid' => $ch->id, 'sesskey' => $USER->sesskey)), $OUTPUT->pix_icon('t/hide', get_string('hidechapter', 'mod_book', $title)), array('title' => get_string('hidechapter', 'mod_book', $titleunescaped))); } $buttontitle = get_string('addafterchapter', 'mod_book', ['title' => $ch->title]); $toc .= html_writer::link(new moodle_url('edit.php', array('cmid' => $cm->id, 'pagenum' => $ch->pagenum, 'subchapter' => $ch->subchapter)), $OUTPUT->pix_icon('add', $buttontitle, 'mod_book'), array('title' => $buttontitle)); $toc .= html_writer::end_tag('div'); if (!$ch->subchapter) { $toc .= html_writer::start_tag('ul'); } else { $toc .= html_writer::end_tag('li'); } $first = 0; } $toc .= html_writer::end_tag('ul'); $toc .= html_writer::end_tag('li'); $toc .= html_writer::end_tag('ul'); } else { // Editing off. Normal students, teachers view. $toc .= html_writer::start_tag('ul'); foreach ($chapters as $ch) { $title = trim(format_string($ch->title, true, array('context'=>$context))); $titleunescaped = trim(format_string($ch->title, true, array('context' => $context, 'escape' => false))); if (!$ch->hidden || ($ch->hidden && $viewhidden)) { if (!$ch->subchapter) { $nch++; $ns = 0; if ($first) { $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } else { $toc .= html_writer::end_tag('ul'); $toc .= html_writer::end_tag('li'); $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } if ($book->numbering == BOOK_NUM_NUMBERS) { $title = "$nch. $title"; } } else { $ns++; if ($first) { $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); $toc .= html_writer::start_tag('ul'); $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } else { $toc .= html_writer::start_tag('li', array('class' => 'clearfix')); } if ($book->numbering == BOOK_NUM_NUMBERS) { $title = "$nch.$ns. $title"; } } $cssclass = ($ch->hidden && $viewhidden) ? 'dimmed_text' : ''; if ($ch->id == $chapter->id) { $toc .= html_writer::tag('strong', $title, array('class' => $cssclass)); } else { $toc .= html_writer::link(new moodle_url('view.php', array('id' => $cm->id, 'chapterid' => $ch->id)), $title, array('title' => s($titleunescaped), 'class' => $cssclass)); } if (!$ch->subchapter) { $toc .= html_writer::start_tag('ul'); } else { $toc .= html_writer::end_tag('li'); } $first = 0; } } $toc .= html_writer::end_tag('ul'); $toc .= html_writer::end_tag('li'); $toc .= html_writer::end_tag('ul'); } $toc .= html_writer::end_tag('div'); $toc = str_replace('