MDL-33198 book: Adding h tags to book titles to increase accessibility while printing...
[moodle.git] / mod / book / tool / print / locallib.php
CommitLineData
b962f030 1<?php
b15ef0b0 2// This file is part of Moodle - http://moodle.org/
b962f030
PS
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/>.
16
17/**
2cd972c1 18 * Book print lib
b962f030 19 *
3542c4f4
EL
20 * @package booktool_print
21 * @copyright 2011 Petr Skoda {@link http://skodak.org}
b962f030
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die;
26
27require_once(dirname(__FILE__).'/lib.php');
28require_once($CFG->dirroot.'/mod/book/locallib.php');
29
30/**
31 * Generate toc structure and titles
32 *
33 * @param array $chapters
34 * @param stdClass $book
35 * @param stdClass $cm
36 * @return array
37 */
38function booktool_print_get_toc($chapters, $book, $cm) {
39 $first = true;
40 $titles = array();
41
1fc9e895 42 $context = context_module::instance($cm->id);
b962f030 43
3542c4f4 44 $toc = ''; // Representation of toc (HTML).
b962f030
PS
45
46 switch ($book->numbering) {
079be74d 47 case BOOK_NUM_NONE:
5b7e511e 48 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_none'));
079be74d
EL
49 break;
50 case BOOK_NUM_NUMBERS:
5b7e511e 51 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_numbered'));
079be74d
EL
52 break;
53 case BOOK_NUM_BULLETS:
5b7e511e 54 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_bullets'));
079be74d
EL
55 break;
56 case BOOK_NUM_INDENTED:
5b7e511e 57 $toc .= html_writer::start_tag('div', array('class' => 'book_toc_indented'));
079be74d 58 break;
b962f030
PS
59 }
60
5b7e511e 61 $toc .= html_writer::tag('a', '', array('name' => 'toc')); // Representation of toc (HTML).
b962f030 62
58ee763b 63 $toc .= html_writer::tag('h2', get_string('toc', 'mod_book'), array('class' => 'book_chapter_title'));
5b7e511e 64 $toc .= html_writer::start_tag('ul');
079be74d 65 foreach ($chapters as $ch) {
b962f030
PS
66 if (!$ch->hidden) {
67 $title = book_get_chapter_title($ch->id, $chapters, $book, $context);
68 if (!$ch->subchapter) {
5b7e511e
TT
69
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 }
77
b962f030 78 } else {
5b7e511e
TT
79
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 }
87
b962f030
PS
88 }
89 $titles[$ch->id] = $title;
5b7e511e
TT
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 }
b962f030
PS
96 $first = false;
97 }
98 }
5b7e511e
TT
99
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');
104
3542c4f4 105 $toc = str_replace('<ul></ul>', '', $toc); // Cleanup of invalid structures.
b962f030
PS
106
107 return array($toc, $titles);
108}