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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
<?php
/**
* Studienbereich... TODO
*
* Copyright (C) 2008 - Marcus Lunzenauer <mlunzena@uos.de>
*
* 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.
*
* @package studip
*
* @author mlunzena
* @author André Noack <noack@data-quest.de>
* @copyright (c) Authors
*
* @property string sem_tree_id database column
* @property string id alias column for sem_tree_id
* @property string parent_id database column
* @property string priority database column
* @property string info database column
* @property string name database column
* @property string studip_object_id database column
* @property string type database column
* @property SimpleORMapCollection _children has_many StudipStudyArea
* @property Institute institute belongs_to Institute
* @property StudipStudyArea _parent belongs_to StudipStudyArea
* @property SimpleORMapCollection courses has_and_belongs_to_many Course
*/
class StudipStudyArea extends SimpleORMap
{
/**
* This constant represents the key of the root area.
*/
const ROOT = 'root';
protected static function configure($config = [])
{
$config['db_table'] = 'sem_tree';
$config['has_many']['_children'] = [
'class_name' => StudipStudyArea::class,
'assoc_foreign_key' => 'parent_id',
'assoc_func' => 'findByParent',
'on_delete' => 'delete',
'on_store' => 'store',
];
$config['has_and_belongs_to_many']['courses'] = [
'class_name' => Course::class,
'thru_table' => 'seminar_sem_tree',
];
$config['belongs_to']['institute'] = [
'class_name' => Institute::class,
'foreign_key' => 'studip_object_id',
];
$config['belongs_to']['_parent'] = [
'class_name' => StudipStudyArea::class,
'foreign_key' => 'parent_id',
];
parent::configure($config);
}
/**
* This is required, if the nodes are added backwards
*/
public $required_children = [];
/**
* Returns the children of the study area with the specified ID.
*/
static function findByParent($parent_id)
{
return self::findByparent_id($parent_id, "ORDER BY priority,name");
}
/**
* Returns the study area with the specified ID.
*/
public static function find($id)
{
$result = NULL;
if ($id === self::ROOT) {
$result = self::getRootArea();
}
else {
$result = parent::find($id);
}
return $result;
}
/**
* Get a string representation of this study area.
*/
public function __toString()
{
return $this->id;
}
/**
* Get the comment of this study area.
*/
public function getInfo()
{
return $this->content['info'];
}
/**
* Set the comment of this study area.
*/
public function setInfo($info)
{
$this->content['info'] = (string) $info;
return $this;
}
/**
* Get the display name of this study area.
*/
public function getName()
{
if ($this->studip_object_id) {
return $this->institute->name;
}
return $this->content['name'];
}
/**
* Set the display name of this study area.
*/
public function setName($name)
{
$this->content['name'] = (string) $name;
return $this;
}
/**
* Get the parent ID of this study area.
*/
public function getParentId()
{
return $this->content['parent_id'];
}
/**
* Get the parent.
*/
public function getParent()
{
$result = NULL;
if ($this->getID() !== self::ROOT) {
$result = $this->_parent;
}
return $result;
}
/**
* Set the parent of this study area.
*/
public function setParentId($parent_id)
{
$this->content['parent_id'] = (string) $parent_id;
$this->resetRelation('parent');
return $this;
}
/**
* get the type of this study area.
*/
public function getType()
{
return $this->content['type'];
}
/**
* set the type of this study area.
*/
public function setType($type)
{
$this->content['type'] = (int) $type;
return $this;
}
/**
* get the name of the type of this study area, see $SEM_TREE_TYPES in config.inc.php
*
* @return string
*/
public function getTypeName()
{
if(isset($GLOBALS['SEM_TREE_TYPES'][$this->getType()]['name'])){
return $GLOBALS['SEM_TREE_TYPES'][$this->getType()]['name'];
} else {
return '';
}
}
/**
* is this study area editable, see $SEM_TREE_TYPES in config.inc.php
*
* @return bool
*/
public function isEditable()
{
if(isset($GLOBALS['SEM_TREE_TYPES'][$this->getType()]['editable'])){
return (bool)$GLOBALS['SEM_TREE_TYPES'][$this->getType()]['editable'];
} else {
return false;
}
}
/**
* is this study area hidden, see $SEM_TREE_TYPES in config.inc.php
*
* @return bool
*/
public function isHidden()
{
if (isset($GLOBALS['SEM_TREE_TYPES'][$this->getType()]['hidden'])) {
return (bool) $GLOBALS['SEM_TREE_TYPES'][$this->getType()]['hidden'];
} else {
return false;
}
}
/**
* Get the path along the sem_tree to this study area.
*
* @param string optional; TODO
*
* @return mixed TODO
*/
public function getPath($separator = NULL)
{
$path = [];
$area = $this;
while ($area) {
if ($area->getName() != '') {
$path[] = $area->getName();
}
if ($area->getParentId() == self::ROOT) {
break;
}
$area = $area->getParent();
}
$path = array_reverse($path);
return isset($separator)
? join($separator, $path)
: $path;
}
/**
* Get the priority of this study area.
*/
public function getPriority()
{
return $this->content['priority'];
}
/**
* Set the priority of this study area.
*/
public function setPriority($priority)
{
$this->content['priority'] = (int) $priority;
return $this;
}
/**
* Get the studip_object_id of this study area.
*/
public function getStudipObjectId()
{
return $this->studip_object_id;
}
/**
* Set the studip_object_id of this study area.
*/
public function setStudipObjectId($id)
{
$this->studip_object_id = (string) $id;
$this->resetRelation('institute');
return $this;
}
/**
* Returns the children of this study area.
*/
public function getChildren()
{
return $this->_children;
}
/**
* Returns1 TRUE if the area has children.
*/
public function hasChildren()
{
return sizeof($this->_children) > 0;
}
/**
* Returns TRUE if this area is the root.
*/
public function isRoot()
{
return $this->getId() === self::ROOT;
}
/**
* Returns TRUE if this area can be select.
*/
public function isAssignable()
{
$cfg = Config::GetInstance();
$leaves_too = $cfg->getValue('SEM_TREE_ALLOW_BRANCH_ASSIGN');
if ($leaves_too) {
return !$this->isRoot() && !$this->isHidden();
} else {
return !$this->isRoot() && !$this->isHidden() && !$this->hasChildren();
}
}
/**
* is this study area considered a study modul?, see $SEM_TREE_TYPES in config.inc.php
*
* @return bool
*/
public function isModule()
{
return isset($GLOBALS['SEM_TREE_TYPES'][$this->getType()]['is_module']);
}
/**
* Get an associative array of all study areas of a course. The array
* contains StudipStudyArea instances
*
* @param id the course's ID
*
* @return SimpleCollection a SimpleORMapCollection of that course's study areas
*/
public static function getStudyAreasForCourse($id)
{
$course = Course::find($id);
return $course ? $course->study_areas : new SimpleCollection();
}
/**
* Returns the not really existing root study area.
*
* @return object the root study area object
*/
public static function getRootArea()
{
$root = new StudipStudyArea();
$root->setID(self::ROOT);
$root->setName(Config::get()->UNI_NAME_CLEAN);
return $root;
}
/**
* Search for study areas whose name matches the given search term.
*
* @param string the seach term
*
* @return type <description>
*/
public static function search($searchTerm)
{
$query =
"sem_tree_id IN (
SELECT sem_tree_id FROM sem_tree st1 WHERE name LIKE :searchTerm
UNION DISTINCT
SELECT sem_tree_id FROM Institute i
INNER JOIN sem_tree st2 ON st2.studip_object_id = i.Institut_id
WHERE i.Name LIKE :searchTerm )
ORDER BY priority";
return self::findBySql($query, ['searchTerm' => "%$searchTerm%"]);
}
/**
* Takes an array of StudyArea objects and produces the tree to the root node
*
* @param array $nodes All required nodes in the tree
* @return StudipStudyArea the root node
*/
public static function backwards($nodes)
{
// create the dummy root
$root = static::getRootArea();
$hashmap = [];
$i = 0;
// let the backwardssearch begin
while ($nodes && $i < 99) {
//clear cache
$newNodes = [];
//process nodes on this level
foreach ($nodes as $node) {
// if we know the node already place there
if ($hashmap[$node->parent_id]) {
$cached = $hashmap[$node->parent_id];
$cached->required_children[$node->id] = $node;
} else {
// if we have a node that is directly under root
if ($node->parent_id == $root->id) {
$root->required_children[$node->id] = $node;
} else {
// else store in hashmap and continue
$hashmap[$node->parent_id] = $node->_parent;
$node->_parent->required_children[$node->id] = $node;
$newNodes[$node->id] = $node->_parent;
}
}
}
$nodes = $newNodes;
$i++;
}
// plant the tree
return $root;
}
}
|