MDL-33198 book: Adding h tags to book titles to increase accessibility while printing...
[moodle.git] / mod / book / tool / print / locallib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
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/>.
17 /**
18  * Book print lib
19  *
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
23  */
25 defined('MOODLE_INTERNAL') || die;
27 require_once(dirname(__FILE__).'/lib.php');
28 require_once($CFG->dirroot.'/mod/book/locallib.php');
30 /**
31  * Generate toc structure and titles
32  *
33  * @param array $chapters
34  * @param stdClass $book
35  * @param stdClass $cm
36  * @return array
37  */
38 function booktool_print_get_toc($chapters, $book, $cm) {
39     $first = true;
40     $titles = array();
42     $context = context_module::instance($cm->id);
44     $toc = ''; // Representation of toc (HTML).
46     switch ($book->numbering) {
47         case BOOK_NUM_NONE:
48             $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none'));
49             break;
50         case BOOK_NUM_NUMBERS:
51             $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered'));
52             break;
53         case BOOK_NUM_BULLETS:
54             $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets'));
55             break;
56         case BOOK_NUM_INDENTED:
57             $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented'));
58             break;
59     }
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) {
66         if (!$ch->hidden) {
67             $title = book_get_chapter_title($ch->id, $chapters, $book, $context);
68             if (!$ch->subchapter) {
70                 if ($first) {
71                     $toc .= html_writer::start_tag('li');
72                 } else {
73                     $toc .= html_writer::end_tag('ul');
74                     $toc .= html_writer::end_tag('li');
75                     $toc .= html_writer::start_tag('li');
76                 }
78             } else {
80                 if ($first) {
81                     $toc .= html_writer::start_tag('li');
82                     $toc .= html_writer::start_tag('ul');
83                     $toc .= html_writer::start_tag('li');
84                 } else {
85                     $toc .= html_writer::start_tag('li');
86                 }
88             }
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');
93             } else {
94                 $toc .= html_writer::end_tag('li');
95             }
96             $first = false;
97         }
98     }
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);