1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
<?php
/**
* ModuleManagementModelTreeItem.php
* Parent class of MVV-Objects used in tree and path views
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Peter Thienel <thienel@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 3.5
*/
abstract class ModuleManagementModelTreeItem extends ModuleManagementModel implements MvvTreeItem
{
/**
* The default route through the MVV object structure.
*
* @var array
*/
public static $TRAIL_DEFAULT = [
'MvvCourse',
'Lvgruppe',
'Modulteil',
'Modul',
'StgteilabschnittModul',
'StgteilAbschnitt',
'StgteilVersion',
'StudiengangTeil',
'Studiengang',
'Abschluss',
'AbschlussKategorie'
];
/**
* An array of functions to filter mvv objects during path creation.
* The class name is the key and the filter function the value.
*
* @var array
*/
protected static $object_filter = [];
/**
* @see MvvTreeItem::getTrailParentId()
*/
public function getTrailParentId()
{
return ($_SESSION['MVV/' . get_class() . '/trail_parent_id']);
}
public function getTrails($types = null, $mode = null, $path = null, $in_recursion = false)
{
$path = $path ?: self::$TRAIL_DEFAULT;
$types = $types ?: $path;
$trails = [];
$class_name = get_class($this);
$next = $path[array_search($class_name, $path) + 1];
$parents = $this->getParents($next);
foreach ($parents as $parent) {
if ($parent) {
if ($this->checkFilter($parent)) {
foreach ($parent->getTrails($types, $mode, $path, true) as $trail) {
if (in_array($class_name, $types)) {
$trail[$class_name] = $this;
}
if (!$in_recursion) {
if (($mode & MvvTreeItem::TRAIL_SHOW_INCOMPLETE)
|| count($trail) == count($types)) {
$trails[] = $trail;
}
} else {
$trails[] = $trail;
}
}
}
}
}
if (empty($trails) && in_array($class_name, $types)) {
$trails = [[$class_name => $this]];
}
return $trails;
}
/**
* Checks trails object filter.
*
* @param MvvTreeItem $item The item to check.
* @return boolean True if item has passed the check.
*/
private function checkFilter(MvvTreeItem $item)
{
$filter = self::$object_filter[get_class($item)];
if ($filter && is_callable($filter)) {
$checked = $filter($item);
if (!$checked) {
return false;
}
}
return true;
}
protected static function sortTrails($trail_a, $trail_b)
{
}
/**
* Returns whether this object is assignable to courses.
*
* @return boolean True if the object is assignable.
*/
public function isAssignable()
{
return false;
}
/**
* @see MvvTreeItem::hasChildren()
*/
public function hasChildren()
{
return count($this->getChildren()) > 0;
}
/**
* Formats the trails to pathes. The path consists of alle names of the
* objects of a trail glued together with the given delimiter.
*
* @param array $trails All trails as array.
* @param string $delimiter A string used as the "glue".
* @param int $display_options Display options set by constants defined
* in class ModuleManagementModel.
* @return type
*/
public static function getPathes($trails, $delimiter = ' ยท ',
$display_options = self::DISPLAY_DEFAULT)
{
$pathes = [];
foreach ($trails as $trail) {
$pathes[] = join($delimiter, array_map(
function($a) use ($display_options) {
return $a->getDisplayName($display_options);
}, $trail));
}
sort($pathes, SORT_LOCALE_STRING);
return $pathes;
}
/**
* Filters trails by given object types.
*
* @param array $trails An array of trails.
* @param array $filter_objects An array of object class names.
* @return array The filtered trails.
*/
public static function filterTrails($trails, $filter_objects)
{
$filtered_trails = [];
$trail_keys = [];
foreach ($trails as $trail) {
$temp_trail = [];
$temp_keys = [];
foreach ($trail as $trail_object) {
if (in_array(get_class($trail_object), $filter_objects)) {
$temp_keys[] = $trail_object->getId();
$temp_trail[get_class($trail_object)] = $trail_object;
}
}
// return only unique trails
// (checked by the keys of the trails objects)
if (!in_array($temp_keys, $trail_keys)) {
$filtered_trails[] = $temp_trail;
}
$trail_keys[] = $temp_keys;
}
return $filtered_trails;
}
/**
* Stores filter function to restrict pathes only to objects fulfilling
* all conditions defined in this function.
*
* @param string $class_name The name of the class.
* @param Closure $filter_func The function defining the filter.
* @param array $params Parameters used by filter function.
* @throws InvalidArgumentException
*/
public static function setObjectFilter($class_name, $filter_func)
{
if (in_array('MvvTreeItem', class_implements($class_name))) {
self::$object_filter[$class_name] = $filter_func;
} else {
throw new InvalidArgumentException();
}
}
}
|