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 |
/**
|
|
|
20 |
* Stores tabs list
|
|
|
21 |
*
|
|
|
22 |
* Example how to print a single line tabs:
|
|
|
23 |
* $rows = array(
|
|
|
24 |
* new tabobject(...),
|
|
|
25 |
* new tabobject(...)
|
|
|
26 |
* );
|
|
|
27 |
* echo $OUTPUT->tabtree($rows, $selectedid);
|
|
|
28 |
*
|
|
|
29 |
* Multiple row tabs may not look good on some devices but if you want to use them
|
|
|
30 |
* you can specify ->subtree for the active tabobject.
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2013 Marina Glancy
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @since Moodle 2.5
|
|
|
35 |
* @package core
|
|
|
36 |
* @category output
|
|
|
37 |
*/
|
|
|
38 |
class tabtree extends tabobject {
|
|
|
39 |
/**
|
|
|
40 |
* Constuctor
|
|
|
41 |
*
|
|
|
42 |
* It is highly recommended to call constructor when list of tabs is already
|
|
|
43 |
* populated, this way you ensure that selected and inactive tabs are located
|
|
|
44 |
* and attribute level is set correctly.
|
|
|
45 |
*
|
|
|
46 |
* @param array $tabs array of tabs, each of them may have it's own ->subtree
|
|
|
47 |
* @param string|null $selected which tab to mark as selected, all parent tabs will
|
|
|
48 |
* automatically be marked as activated
|
|
|
49 |
* @param array|string|null $inactive list of ids of inactive tabs, regardless of
|
|
|
50 |
* their level. Note that you can as weel specify tabobject::$inactive for separate instances
|
|
|
51 |
*/
|
|
|
52 |
public function __construct($tabs, $selected = null, $inactive = null) {
|
|
|
53 |
$this->subtree = $tabs;
|
|
|
54 |
if ($selected !== null) {
|
|
|
55 |
$this->set_selected($selected);
|
|
|
56 |
}
|
|
|
57 |
if ($inactive !== null) {
|
|
|
58 |
if (is_array($inactive)) {
|
|
|
59 |
foreach ($inactive as $id) {
|
|
|
60 |
if ($tab = $this->find($id)) {
|
|
|
61 |
$tab->inactive = true;
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
} else if ($tab = $this->find($inactive)) {
|
|
|
65 |
$tab->inactive = true;
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
$this->set_level(0);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Export for template.
|
|
|
73 |
*
|
|
|
74 |
* @param renderer_base $output Renderer.
|
|
|
75 |
* @return \stdClass
|
|
|
76 |
*/
|
|
|
77 |
public function export_for_template(renderer_base $output) {
|
|
|
78 |
$tabs = [];
|
|
|
79 |
$secondrow = false;
|
|
|
80 |
|
|
|
81 |
foreach ($this->subtree as $tab) {
|
|
|
82 |
$tabs[] = $tab->export_for_template($output);
|
|
|
83 |
if (!empty($tab->subtree) && ($tab->level == 0 || $tab->selected || $tab->activated)) {
|
|
|
84 |
$secondrow = new tabtree($tab->subtree);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return (object) [
|
|
|
89 |
'tabs' => $tabs,
|
|
|
90 |
'secondrow' => $secondrow ? $secondrow->export_for_template($output) : false,
|
|
|
91 |
];
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Alias this class to the old name.
|
|
|
96 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
97 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
98 |
class_alias(tabtree::class, \tabtree::class);
|