MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / settings / renderer.php
CommitLineData
3406acde 1<?php
f25a6839
SH
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/>.
16
17/**
18 * Settings block
19 *
20 * @package block_settings
21 * @copyright 2010 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
3406acde
SH
24
25class block_settings_renderer extends plugin_renderer_base {
26
27 public function settings_tree(settings_navigation $navigation) {
28 $count = 0;
29 foreach ($navigation->children as &$child) {
30 $child->preceedwithhr = ($count!==0);
bf5eebe8
SK
31 if ($child->display) {
32 $count++;
33 }
3406acde 34 }
f8895446
JO
35 $navigationattrs = array(
36 'class' => 'block_tree list',
37 'role' => 'tree',
38 'data-ajax-loader' => 'block_navigation/site_admin_loader');
39 $content = $this->navigation_node($navigation, $navigationattrs);
3406acde
SH
40 if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
41 $content = $this->output->box($content, 'block_tree_box', $navigation->id);
42 }
43 return $content;
44 }
45
6759dc35
SL
46 /**
47 * Build the navigation node.
48 *
49 * @param navigation_node $node the navigation node object.
50 * @param array $attrs list of attributes.
51 * @param int $depth the depth, default to 1.
52 * @return string the navigation node code.
53 */
f8895446 54 protected function navigation_node(navigation_node $node, $attrs=array(), $depth = 1) {
3406acde
SH
55 $items = $node->children;
56
57 // exit if empty, we don't want an empty ul element
58 if ($items->count()==0) {
59 return '';
60 }
61
62 // array of nested li elements
63 $lis = array();
f8895446 64 $number = 0;
3406acde 65 foreach ($items as $item) {
f8895446 66 $number++;
3406acde
SH
67 if (!$item->display) {
68 continue;
69 }
70
7081714d 71 $isbranch = ($item->children->count()>0 || $item->nodetype==navigation_node::NODETYPE_BRANCH);
7081714d
SH
72
73 if ($isbranch) {
74 $item->hideicon = true;
75 }
a71b983d 76
3406acde 77 $content = $this->output->render($item);
560cea83 78 $id = $item->id ? $item->id : html_writer::random_id();
a71b983d
CB
79 $ulattr = ['id' => $id . '_group', 'role' => 'group'];
80 $liattr = ['class' => [$item->get_css_type(), 'depth_'.$depth], 'tabindex' => '-1'];
81 $pattr = ['class' => ['tree_item'], 'role' => 'treeitem'];
82 $pattr += !empty($item->id) ? ['id' => $item->id] : [];
83 $hasicon = (!$isbranch && $item->icon instanceof renderable);
3406acde 84
7081714d 85 if ($isbranch) {
a71b983d 86 $liattr['class'][] = 'contains_branch';
6759dc35
SL
87 if (!$item->forceopen || (!$item->forceopen && $item->collapse) || ($item->children->count() == 0
88 && $item->nodetype == navigation_node::NODETYPE_BRANCH)) {
a71b983d 89 $pattr += ['aria-expanded' => 'false'];
f8895446 90 } else {
a71b983d 91 $pattr += ['aria-expanded' => 'true'];
f8895446
JO
92 }
93 if ($item->requiresajaxloading) {
a71b983d
CB
94 $pattr['data-requires-ajax'] = 'true';
95 $pattr['data-loaded'] = 'false';
96 } else {
97 $pattr += ['aria-owns' => $id . '_group'];
f8895446 98 }
7081714d 99 } else if ($hasicon) {
a71b983d
CB
100 $liattr['class'][] = 'item_with_icon';
101 $pattr['class'][] = 'hasicon';
7081714d 102 }
3406acde 103 if ($item->isactive === true) {
a71b983d
CB
104 $liattr['class'][] = 'current_branch';
105 }
106 if (!empty($item->classes) && count($item->classes) > 0) {
107 $pattr['class'] = array_merge($pattr['class'], $item->classes);
3406acde 108 }
f8895446 109 $nodetextid = 'label_' . $depth . '_' . $number;
a71b983d 110
3406acde 111 // class attribute on the div item which only contains the item content
a71b983d 112 $pattr['class'][] = 'tree_item';
7081714d 113 if ($isbranch) {
a71b983d 114 $pattr['class'][] = 'branch';
3406acde 115 } else {
a71b983d 116 $pattr['class'][] = 'leaf';
3406acde 117 }
a71b983d
CB
118
119 $liattr['class'] = join(' ', $liattr['class']);
120 $pattr['class'] = join(' ', $pattr['class']);
121
122 if (isset($pattr['aria-expanded']) && $pattr['aria-expanded'] === 'false') {
123 $ulattr += ['aria-hidden' => 'true'];
3406acde 124 }
a71b983d
CB
125
126 $content = html_writer::tag('p', $content, $pattr) . $this->navigation_node($item, $ulattr, $depth + 1);
3406acde
SH
127 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
128 $content = html_writer::empty_tag('hr') . $content;
129 }
f8895446 130 $liattr['aria-labelledby'] = $nodetextid;
3406acde
SH
131 $content = html_writer::tag('li', $content, $liattr);
132 $lis[] = $content;
133 }
6c721bbf
SH
134
135 if (count($lis)) {
f8895446
JO
136 if (empty($attrs['role'])) {
137 $attrs['role'] = 'group';
138 }
6c721bbf
SH
139 return html_writer::tag('ul', implode("\n", $lis), $attrs);
140 } else {
141 return '';
142 }
3406acde
SH
143 }
144
145 public function search_form(moodle_url $formtarget, $searchvalue) {
6a80186e
MM
146 $data = [
147 'action' => $formtarget->out(false),
148 'label' => get_string('searchinsettings', 'admin'),
149 'searchvalue' => $searchvalue
150 ];
151 return $this->render_from_template('block_settings/search_form', $data);
3406acde
SH
152 }
153
bf5eebe8 154}