aboutsummaryrefslogtreecommitdiff
path: root/lib/models/RangeTreeNode.php
blob: 0d677808b788016051cbc1cd37fb7b60f137ebe4 (plain)
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
<?php

/**
 * RangeTreeNode.php
 * model class for table range_tree
 *
 * 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      Thomas Hackl <hackl@data-quest.de>
 * @copyright   2022 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       5.3
 *
 * @property string $id alias column for item_id
 * @property string $item_id database column
 * @property string $parent_id database column
 * @property int $level database column
 * @property int $priority database column
 * @property string $name database column
 * @property string|null $studip_object database column
 * @property string|null $studip_object_id database column
 * @property SimpleORMapCollection|RangeTreeNode[] $children has_many RangeTreeNode
 * @property Institute|null $institute belongs_to Institute
 * @property RangeTreeNode $parent belongs_to RangeTreeNode
 */
class RangeTreeNode extends SimpleORMap implements StudipTreeNode
{
    use StudipTreeNodeCachableTrait;
    use StudipTreeNodeCourseTrait;

    protected static function configure($config = [])
    {
        $config['db_table'] = 'range_tree';

        $config['belongs_to']['institute'] = [
            'class_name'  => Institute::class,
            'foreign_key' => 'studip_object_id',
        ];
        $config['belongs_to']['parent'] = [
            'class_name'  => RangeTreeNode::class,
            'foreign_key' => 'parent_id',
        ];
        $config['has_many']['children'] = [
            'class_name'  => RangeTreeNode::class,
            'assoc_foreign_key' => 'parent_id',
            'order_by' => 'ORDER BY priority, name',
            'on_delete' => 'delete',
        ];

        $config = self::registerCachableCallbacks($config);

        parent::configure($config);
    }

    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
    {
        $nodes = [];
        foreach (Course::find($course_id)->institutes as $institute) {
            $range = self::findOneByStudip_object_id($institute->id);
            if ($range) {
                $nodes[] = $range;
            }
        }
        return $nodes;
    }

    public function getName(): string
    {
        if ($this->id === 'root') {
            return Config::get()->UNI_NAME_CLEAN;
        }

        if ($this->institute) {
           return (string) $this->institute->name;
        }

        return $this->content['name'];
    }

    public function getDescription(): string
    {
        return '';
    }

    public function getImage()
    {
        return $this->institute ?
            Avatar::getAvatar($this->studip_object_id) :
            Icon::create('institute');
    }

    public function hasChildNodes(): bool
    {
        return count($this->children) > 0;
    }

    /**
     * @see StudipTreeNode::getChildNodes()
     */
    public function getChildNodes(bool $onlyVisible = false): array
    {
        return self::findByParent_id($this->id);
    }

    /**
     * @see StudipTreeNode::countCourses()
     */
    public function countCourses($semester_id = '', $semclass = 0, $with_children = false): int
    {
        if (!$this->institute && !$with_children) {
            return 0;
        }

        [$condition, $parameters] = $this->getCoursesCondition(
            'i',
            $semester_id,
            $semclass
        );

        $query = "SELECT COUNT(DISTINCT i.`seminar_id`) FROM `seminar_inst` i {$condition}";

        if ($with_children) {
            $query .= " AND i.`institut_id` IN (
                    SELECT DISTINCT `studip_object_id` FROM `range_tree` WHERE `item_id` IN (:ids)
                )";
            $parameters['ids'] = array_merge([$this->id], $this->getDescendantIds());
        } else {
            $query .= " AND i.`institut_id` = :id";
            $parameters['id'] = $this->studip_object_id;
        }

        return DBManager::get()->fetchColumn($query, $parameters);
    }

    public function getCourses(
        $semester_id = 'all',
        $semclass = 0,
        $searchterm = '',
        $with_children = false,
        array $courses = []
    ): array
    {
        [$condition, $parameters, $order_by] = $this->getCoursesCondition(
            'i',
            $semester_id,
            $semclass,
            $searchterm,
            $courses
        );

        $query = "SELECT DISTINCT s.* FROM `seminar_inst` AS i {$condition}";

        if ($with_children) {
            $query .= " AND i.`institut_id` IN (
                    SELECT DISTINCT `studip_object_id` FROM `range_tree` WHERE `item_id` IN (:ids)
                )";
            $parameters['ids'] = array_merge([$this->id], $this->getDescendantIds());
        } else {
            $query .= " AND i.`institut_id` = :id";
            $parameters['id'] = $this->studip_object_id;
        }

        $query .= " ORDER BY " . implode(', ', $order_by);

        return DBManager::get()->fetchAll($query, $parameters, 'Course::buildExisting');
    }

    public function getAncestors(): array
    {
        $path = [
            [
                'id' => $this->id,
                'name' => $this->getName(),
                'classname' => self::class
            ]
        ];

        if ($this->parent_id) {
            $path = array_merge($this->getNode($this->parent_id)->getAncestors(), $path);
        }

        return $path;
    }

}