Newer
Older
<?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 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 implements StudipTreeNode
/**
* 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']['_parent'] = [
'class_name' => StudipStudyArea::class,
'foreign_key' => 'parent_id',
];
$config = self::registerCachableCallbacks($config);
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.
*/
public static function findByParent($parent_id)
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
{
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(): string
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
{
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;
}
/**
* 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.
*
*/
public static function search($searchTerm)
{
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
}
/**
* 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
$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;
}
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
public static function getNode($id): StudipTreeNode
{
if ($id === 'root') {
return static::build([
'id' => 'root',
'name' => Config::get()->UNI_NAME_CLEAN,
]);
}
return static::find($id);
}
public static function getCourseNodes(string $course_id): array
{
return Course::find($course_id)->study_areas->getArrayCopy();
}
public function getDescription(): string
{
return $this->getInfo();
}
/**
* @see StudipTreeNode::getImage()
*/
public function getImage()
{
return null;
}
public function hasChildNodes(): bool
{
return count($this->_children) > 0;
}
/**
* @see StudipTreeNode::getChildNodes()
*/
public function getChildNodes(bool $onlyVisible = false): array
{
if ($onlyVisible) {
$visibleTypes = array_filter($GLOBALS['SEM_TREE_TYPES'], function ($t) {
return isset($t['hidden']) ? !$t['hidden'] : true;
});
return static::findBySQL(
"`parent_id` = :parent AND `type` IN (:types) ORDER BY `priority`, `name`",
['parent' => $this->id, 'types' => $visibleTypes]
);
} else {
return static::findByParent_id($this->id, "ORDER BY `priority`, `name`");
}
}
/**
* @see StudipTreeNode::countCourses()
*/
public function countCourses($semester_id = 'all', $semclass = 0, $with_children = false) :int
{
if ($semester_id !== 'all') {
$query = "SELECT COUNT(DISTINCT t.`seminar_id`)
FROM `seminar_sem_tree` t
JOIN `seminare` s ON (s.`Seminar_id` = t.`seminar_id`)
LEFT JOIN `semester_courses` sc ON (t.`seminar_id` = sc.`course_id`)
WHERE t.`sem_tree_id` IN (:ids)
AND (
sc.`semester_id` = :semester
OR sc.`semester_id` IS NULL
)";
$parameters = [
'ids' => $with_children ? $this->getDescendantIds() : [$this->id],
'semester' => $semester_id
];
} else {
$query = "SELECT COUNT(DISTINCT t.`seminar_id`)
FROM `seminar_sem_tree` t
JOIN `seminare` s ON (s.`Seminar_id` = t.`seminar_id`)
WHERE `sem_tree_id` IN (:ids)";
$parameters = ['ids' => $with_children ? $this->getDescendantIds() : [$this->id]];
}
if (!$GLOBALS['perm']->have_perm(Config::get()->SEM_VISIBILITY_PERM)) {
$query .= " AND s.`visible` = 1";
}
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
if ($semclass !== 0) {
$query .= " AND s.`status` IN (:types)";
$parameters['types'] = array_map(
function ($type) {
return $type['id'];
},
array_filter(
SemType::getTypes(),
function ($t) use ($semclass) { return $t['class'] === $semclass; }
)
);
}
return $this->id === 'root' && !$with_children ? 0 : DBManager::get()->fetchColumn($query, $parameters);
}
public function getCourses(
$semester_id = 'all',
$semclass = 0,
$searchterm = '',
$with_children = false,
array $courses = []
): array
{
if ($semester_id !== 'all') {
$query = "SELECT DISTINCT s.*
FROM `seminare` s
JOIN `seminar_sem_tree` t ON (t.`seminar_id` = s.`Seminar_id`)
LEFT JOIN `semester_courses` sem ON (sem.`course_id` = s.`Seminar_id`)
WHERE t.`sem_tree_id` IN (:ids)
AND (
sem.`semester_id` = :semester
OR sem.`semester_id` IS NULL
)";
$parameters = [
'ids' => $with_children ? $this->getDescendantIds() : [$this->id],
'semester' => $semester_id
];
} else {
$query = "SELECT DISTINCT s.*
FROM `seminare` s
JOIN `seminar_sem_tree` t ON (t.`seminar_id` = s.`Seminar_id`)
WHERE t.`sem_tree_id` IN (:ids)";
$parameters = ['ids' => $with_children ? $this->getDescendantIds() : [$this->id]];
}
if (!$GLOBALS['perm']->have_perm(Config::get()->SEM_VISIBILITY_PERM)) {
$query .= " AND s.`visible` = 1";
}
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
if ($semclass !== 0) {
$query .= " AND s.`status` IN (:types)";
$parameters['types'] = array_map(
function ($type) {
return $type['id'];
},
array_filter(
SemType::getTypes(),
function ($t) use ($semclass) { return $t['class'] === $semclass; }
)
);
}
if ($searchterm) {
$query .= " AND s.`Name` LIKE :searchterm";
$parameters['searchterm'] = '%' . trim($searchterm) . '%';
}
if ($courses) {
$query .= " AND t.`seminar_id` IN (:courses)";
$parameters['courses'] = $courses;
}
if (Config::get()->IMPORTANT_SEMNUMBER) {
$query .= " ORDER BY s.`start_time`, s.`VeranstaltungsNummer`, s.`Name`";
} else {
$query .= " ORDER BY s.`start_time`, s.`Name`";
}
return DBManager::get()->fetchAll($query, $parameters, 'Course::buildExisting');
}
public function getAncestors(): array
{
$path = [
[
'id' => $this->id,
'name' => $this->getName(),
'classname' => static::class
]
];
if ($this->parent_id) {
$path = array_merge($this->getNode($this->parent_id)->getAncestors(), $path);
}
return $path;
}

Thomas Hackl
committed
/**
* Constructs an index from the level hierarchy, This index is a number,
* containing the "depth" level and the priority on this level. For example,
* a node on level 2 with priority 3 will get an index of 23.
*
* @return int
*/
public function getIndex()
{
$level = 1;
$index = (string) $level . (string) $this->priority;
$current = $this;
while ($current->getParent()) {
$current = $current->getParent();
$index .= $level . $current->priority;
$level++;
}
return $index;
}