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/>.
20 * @package booktool_print
21 * @copyright 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(dirname(__FILE__).'/lib.php');
28 require_once($CFG->dirroot.'/mod/book/locallib.php');
31 * Generate toc structure and titles
33 * @param array $chapters
34 * @param stdClass $book
38 function booktool_print_get_toc($chapters, $book, $cm) {
42 $context = context_module::instance($cm->id);
44 $toc = ''; // Representation of toc (HTML).
46 switch ($book->numbering) {
48 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none'));
50 case BOOK_NUM_NUMBERS:
51 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered'));
53 case BOOK_NUM_BULLETS:
54 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets'));
56 case BOOK_NUM_INDENTED:
57 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented'));
61 $toc .= html_writer::tag('a', '', array('name' => 'toc')); // Representation of toc (HTML).
63 $toc .= html_writer::tag('h2', get_string('toc', 'mod_book'), array('class' => 'book_chapter_title'));
64 $toc .= html_writer::start_tag('ul');
65 foreach ($chapters as $ch) {
67 $title = book_get_chapter_title($ch->id, $chapters, $book, $context);
68 if (!$ch->subchapter) {
71 $toc .= html_writer::start_tag('li');
73 $toc .= html_writer::end_tag('ul');
74 $toc .= html_writer::end_tag('li');
75 $toc .= html_writer::start_tag('li');
81 $toc .= html_writer::start_tag('li');
82 $toc .= html_writer::start_tag('ul');
83 $toc .= html_writer::start_tag('li');
85 $toc .= html_writer::start_tag('li');
89 $titles[$ch->id] = $title;
90 $toc .= html_writer::link(new moodle_url('#ch'.$ch->id), $title, array('title' => s($title)));
91 if (!$ch->subchapter) {
92 $toc .= html_writer::start_tag('ul');
94 $toc .= html_writer::end_tag('li');
100 $toc .= html_writer::end_tag('ul');
101 $toc .= html_writer::end_tag('li');
102 $toc .= html_writer::end_tag('ul');
103 $toc .= html_writer::end_tag('div');
105 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
107 return array($toc, $titles);