| 1441 |
ariadna |
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/>.
|
|
|
16 |
|
|
|
17 |
namespace core\output;
|
|
|
18 |
|
|
|
19 |
use moodle_url;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Stores one tab
|
|
|
23 |
*
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
* @package core
|
|
|
26 |
* @copyright Marina Glancy
|
|
|
27 |
*/
|
|
|
28 |
class tabobject implements renderable, templatable {
|
|
|
29 |
/** @var string unique id of the tab in this tree, it is used to find selected and/or inactive tabs */
|
|
|
30 |
public $id;
|
|
|
31 |
/** @var moodle_url|string link */
|
|
|
32 |
public $link;
|
|
|
33 |
/** @var string text on the tab */
|
|
|
34 |
public $text;
|
|
|
35 |
/** @var string title under the link, by defaul equals to text */
|
|
|
36 |
public $title;
|
|
|
37 |
/** @var bool whether to display a link under the tab name when it's selected */
|
|
|
38 |
public $linkedwhenselected = false;
|
|
|
39 |
/** @var bool whether the tab is inactive */
|
|
|
40 |
public $inactive = false;
|
|
|
41 |
/** @var bool indicates that this tab's child is selected */
|
|
|
42 |
public $activated = false;
|
|
|
43 |
/** @var bool indicates that this tab is selected */
|
|
|
44 |
public $selected = false;
|
|
|
45 |
/** @var array stores children tabobjects */
|
|
|
46 |
public $subtree = [];
|
|
|
47 |
/** @var int level of tab in the tree, 0 for root (instance of tabtree), 1 for the first row of tabs */
|
|
|
48 |
public $level = 1;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Constructor
|
|
|
52 |
*
|
|
|
53 |
* @param string $id unique id of the tab in this tree, it is used to find selected and/or inactive tabs
|
|
|
54 |
* @param string|moodle_url $link
|
|
|
55 |
* @param string $text text on the tab
|
|
|
56 |
* @param string $title title under the link, by defaul equals to text
|
|
|
57 |
* @param bool $linkedwhenselected whether to display a link under the tab name when it's selected
|
|
|
58 |
*/
|
|
|
59 |
public function __construct($id, $link = null, $text = '', $title = '', $linkedwhenselected = false) {
|
|
|
60 |
$this->id = $id;
|
|
|
61 |
$this->link = $link;
|
|
|
62 |
$this->text = $text;
|
|
|
63 |
$this->title = $title ? $title : $text;
|
|
|
64 |
$this->linkedwhenselected = $linkedwhenselected;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Travels through tree and finds the tab to mark as selected, all parents are automatically marked as activated
|
|
|
69 |
*
|
|
|
70 |
* @param string $selected the id of the selected tab (whatever row it's on),
|
|
|
71 |
* if null marks all tabs as unselected
|
|
|
72 |
* @return bool whether this tab is selected or contains selected tab in its subtree
|
|
|
73 |
*/
|
|
|
74 |
protected function set_selected($selected) {
|
|
|
75 |
if ((string)$selected === (string)$this->id) {
|
|
|
76 |
$this->selected = true;
|
|
|
77 |
// This tab is selected. No need to travel through subtree.
|
|
|
78 |
return true;
|
|
|
79 |
}
|
|
|
80 |
foreach ($this->subtree as $subitem) {
|
|
|
81 |
if ($subitem->set_selected($selected)) {
|
|
|
82 |
// This tab has child that is selected. Mark it as activated. No need to check other children.
|
|
|
83 |
$this->activated = true;
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
return false;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Travels through tree and finds a tab with specified id
|
|
|
92 |
*
|
|
|
93 |
* @param string $id
|
|
|
94 |
* @return tabtree|null
|
|
|
95 |
*/
|
|
|
96 |
public function find($id) {
|
|
|
97 |
if ((string)$this->id === (string)$id) {
|
|
|
98 |
return $this;
|
|
|
99 |
}
|
|
|
100 |
foreach ($this->subtree as $tab) {
|
|
|
101 |
if ($obj = $tab->find($id)) {
|
|
|
102 |
return $obj;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
return null;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Allows to mark each tab's level in the tree before rendering.
|
|
|
110 |
*
|
|
|
111 |
* @param int $level
|
|
|
112 |
*/
|
|
|
113 |
protected function set_level($level) {
|
|
|
114 |
$this->level = $level;
|
|
|
115 |
foreach ($this->subtree as $tab) {
|
|
|
116 |
$tab->set_level($level + 1);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Export for template.
|
|
|
122 |
*
|
|
|
123 |
* @param renderer_base $output Renderer.
|
|
|
124 |
* @return object
|
|
|
125 |
*/
|
|
|
126 |
public function export_for_template(renderer_base $output) {
|
|
|
127 |
if ($this->inactive || ($this->selected && !$this->linkedwhenselected) || $this->activated) {
|
|
|
128 |
$link = null;
|
|
|
129 |
} else {
|
|
|
130 |
$link = $this->link;
|
|
|
131 |
}
|
|
|
132 |
$active = $this->activated || $this->selected;
|
|
|
133 |
|
|
|
134 |
return (object) [
|
|
|
135 |
'id' => $this->id,
|
|
|
136 |
'link' => is_object($link) ? $link->out(false) : $link,
|
|
|
137 |
'text' => $this->text,
|
|
|
138 |
'title' => $this->title,
|
|
|
139 |
'inactive' => !$active && $this->inactive,
|
|
|
140 |
'active' => $active,
|
|
|
141 |
'level' => $this->level,
|
|
|
142 |
];
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// Alias this class to the old name.
|
|
|
147 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
148 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
149 |
class_alias(tabobject::class, \tabobject::class);
|