Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Perspective
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 15
1332
0.00% covered (danger)
0.00%
0 / 1
 fromSimpleXMLElement
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
 fromLegacyToolbarAction
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 addSection
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
132
 getExtension
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIcon
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isVisible
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getChildren
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBinding
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __toPhpCode
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program 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 this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2014 (original work) Open Assessment Technologies SA;
19 *
20 *
21 */
22
23namespace oat\tao\model\menu;
24
25use oat\oatbox\PhpSerializable;
26
27class Perspective extends MenuElement implements PhpSerializable
28{
29    public const GROUP_DEFAULT = 'main';
30
31    public const GROUP_SETTINGS = 'settings';
32
33    public const GROUP_INVISIBLE = 'invisible';
34
35    private $data = [];
36
37    private $children = [];
38
39    /**
40     * @param \SimpleXMLElement $node
41     * @param $structureExtensionId
42     * @return static
43     */
44    public static function fromSimpleXMLElement(\SimpleXMLElement $node, $structureExtensionId)
45    {
46        $data = [
47            'id'       => (string) $node['id'],
48            'group'    => $node['group']
49                ? (string)$node['group']
50                : ($node['visible'] == 'true'
51                    ? self::GROUP_DEFAULT
52                    : self::GROUP_INVISIBLE),
53            'name'      => (string) $node['name'],
54            'binding'     => isset($node['binding']) ? (string)$node['binding'] : null,
55            'description' => (string) $node->description,
56            'extension' => $structureExtensionId,
57            'level'     => (string) $node['level'],
58            'icon'      => isset($node->icon) ? Icon::fromSimpleXMLElement($node->icon, $structureExtensionId) : null
59        ];
60        $sections = [];
61        foreach ($node->xpath("sections/section") as $sectionNode) {
62            $sections[] = Section::fromSimpleXMLElement($sectionNode, $structureExtensionId);
63        }
64        return new static($data, $sections);
65    }
66
67    /**
68     * Generate a Perspective from a legacy ToolbarAction
69     *
70     * @param \SimpleXMLElement $node
71     * @param $structureExtensionId
72     * @return static
73     */
74    public static function fromLegacyToolbarAction(\SimpleXMLElement $node, $structureExtensionId)
75    {
76        $data = [
77            'id' => (string)$node['id'],
78            'extension' => $structureExtensionId,
79            'name' => (string)$node['title'],
80            'level' => (int)$node['level'],
81            'description' => empty($text) ? null : $text,
82            'binding' => isset($node['binding'])
83                ? (string)$node['binding']
84                : (isset($node['js']) ? (string)$node['js'] : null),
85            'structure' => isset($node['structure']) ? (string)$node['structure'] : null,
86            'group' => self::GROUP_SETTINGS,
87            'icon' => isset($node['icon'])
88                ? Icon::fromArray(['id' => (string)$node['icon']], $structureExtensionId)
89                : null
90        ];
91        $children = [];
92        if (isset($node['structure'])) {
93            $children = [];
94            // (string)$node['structure']
95        }
96        return new static($data, $children);
97    }
98
99    /**
100     * @param $data
101     * @param $sections
102     * @param int $version
103     */
104    public function __construct($data, $sections, $version = self::SERIAL_VERSION)
105    {
106        parent::__construct($data['id'], $version);
107        $this->data = $data;
108        $this->children = $sections;
109    }
110
111    /**
112     * @param Section $section
113     */
114    public function addSection(Section $section)
115    {
116        $existingKey = false;
117        foreach ($this->children as $key => $existingSection) {
118            if ($existingSection->getId() == $section->getId()) {
119                $existingKey = $key;
120                break;
121            }
122        }
123        if ($existingKey !== false) {
124            switch ($section->getPolicy()) {
125                case Section::POLICY_MERGE:
126                    $currentSection = $this->children[$existingKey];
127                    foreach ($section->getTrees() as $tree) {
128                        $currentSection->addTree($tree);
129                    }
130                    /** @var Action $action */
131                    foreach ($section->getActions() as $action) {
132                        /** @var Action $currentAction */
133                        foreach ($currentSection->getActions() as $currentAction) {
134                            if ($currentAction->getId() == $action->getId()) {
135                                $currentSection->removeAction($currentAction);
136                                break;
137                            }
138                        }
139                        $currentSection->addAction($action);
140                    }
141                    break;
142                case Section::POLICY_OVERRIDE:
143                    $this->children[$existingKey] = $section;
144                    break;
145                default:
146                    throw new \common_exception_Error();
147            }
148        } else {
149            $this->children[] = $section;
150        }
151    }
152
153    /**
154     * @return mixed
155     */
156    public function getExtension()
157    {
158        return $this->data['extension'];
159    }
160
161    /**
162     * @return mixed
163     */
164    public function getName()
165    {
166        return $this->data['name'];
167    }
168
169    public function getDescription()
170    {
171        return $this->data['description'];
172    }
173
174    /**
175     * @return Icon
176     */
177    public function getIcon()
178    {
179        return $this->data['icon'];
180    }
181
182    public function getGroup()
183    {
184        return $this->data['group'];
185    }
186
187    public function getLevel()
188    {
189        return $this->data['level'];
190    }
191
192    /**
193     * @deprecated
194     * @return boolean
195     */
196    public function isVisible()
197    {
198        return $this->getGroup() == self::GROUP_INVISIBLE;
199    }
200
201    public function getChildren()
202    {
203        return $this->children;
204    }
205
206    public function getBinding()
207    {
208        return $this->data['binding'];
209    }
210
211    public function getUrl()
212    {
213        return _url('index', null, null, ['structure' => $this->getId(), 'ext' => $this->getExtension()]);
214    }
215
216    public function __toPhpCode()
217    {
218        return "new " . __CLASS__ . "("
219            . \common_Utils::toPHPVariableString($this->data) . ','
220            . \common_Utils::toPHPVariableString($this->children) . ','
221            . \common_Utils::toPHPVariableString(self::SERIAL_VERSION)
222        . ")";
223    }
224}