aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipLvgruppeSelection.class.php
blob: a5da9cb3f1aaec1b9af29341d34161e463f8c478 (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
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
<?php
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO

/*
 * Copyright (C) 2013 - Peter Thienel <thienel@data-quest.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.
 */


/**
 * Objects of this class represent the state of the LV-Gruppe selection form.
 */
class StudipLvgruppeSelection {

    private $selected;
    private $showAll;
    private $areas;
    private $searchKey;
    private $searchResult;

    /**
     * This constructor can be called with or without a course ID. If a course ID
     * has been sent, the selected lvgruppen are populated by that course's already
     * chosen lvgruppen. If no course ID is given, it is assumed that you are
     * creating a new course at the moment.
     *
     * @param  string     optional; the ID of the course to prepopulate the form
     *                    with
     *
     * @return void
     */
    public function __construct($course_id = null)
    {
        $this->selected = self::getRootItem();
        $this->showAll = FALSE;

        $this->areas = [];

        $this->searchKey = '';
        $this->clearSearchResult();

        if (isset($course_id)) {
            $this->populateAreasForCourse($course_id);
        }
    }

    /**
      * Returns the not really existing root of the tree.
      *
      * @return object     the root tree object
      */
     public static function getRootItem()
     {
        $root = new MvvTreeRoot();
        return $root;
     }

    /**
     * This method populates this instance with the already chosen LV-Gruppen.
     *
     * @param  string     the course's ID
     *
     * @return void
     */
    private function populateAreasForCourse($id)
    {
        $lvgruppen = Lvgruppe::findBySeminar($id);
        $this->setLvgruppen($lvgruppen);
        $this->sortAreas();
    }


    /**
     * Sorts the internal representation of the areas by their paths according to
     * the current locale.
     *
     * @return void
     */
    private function sortAreas()
    {
        // MVV: sort by name of LvGruppe
        uasort($this->areas, function ($a, $b) {
            return strcoll($a->getDisplayName(), $b->getDisplayName());
        });
    }


    /**
     * @return string     the current search term
     */
    public function getSearchKey()
    {
        return $this->searchKey;
    }


    /**
     * @param  string     a search term
     *
     * @return object     this instance
     */
    public function setSearchKey($searchKey)
    {
        $this->searchKey = (string) $searchKey;

        $this->clearSearchResult();
        return $this;
    }


    /**
     * @return bool       returns TRUE if the search key was set meaning that
     *                    we are currently searching; returns FALSE otherwise
     */
    public function searched()
    {
        return $this->searchKey !== '';
    }


    /**
     * Clears the current search result.
     *
     * @return object     this instance
     */
    public function clearSearchResult()
    {
        $this->searchResult = null;
        return $this;
    }


    /**
     * Returns an array of search results.
     *
     * @return array      an array of search results
     */
    public function getSearchResult()
    {

        # no search key -> return empty array
        if ($this->searchKey === '') {
            return [];
        }

        if (is_null($this->searchResult)) {
            $lvgruppen = Lvgruppe::findBySearchTerm($this->searchKey);
            foreach ($lvgruppen as $lvgruppe) {
                $this->searchResult[$lvgruppe->id] = $lvgruppe;
            }
            usort($this->searchResult, [__CLASS__, 'sortSearchResult']);
        }

        return $this->searchResult;
    }

    public static function sortSearchResult($a, $b)
    {
        // sort by display name
        return strcmp($a->getDisplayName(), $b->getDisplayName());
    }


    /**
     * @return object     the currently selected lvgruppe
     */
    public function getSelected()
    {
        return $this->selected;
    }


    /**
     * Sets the selected tree item.
     *
     * @param  mixed $selected Either the id of a tree item to select or the
     * tree item object itself
     * @return object this instance
     */
    public function setSelected($selected, $type = null)
    {
        if (!is_object($selected) && !is_null($type)) {
            $reflection = new ReflectionClass($type);
            if (!$reflection->implementsInterface('MvvTreeItem')) {
                throw new InvalidArgumentException('Wrong type of tree element.');
            }
            if ($type != 'MvvTreeRoot') {
                $this->selected = $type::find(explode('_', $selected));
            }
        } else {
            $this->selected = $selected;
        }
        return $this;
    }


    /**
     * @return bool       returns TRUE if the subtrees should be expanded
     *                    completely or FALSE otherwise
     */
    public function getShowAll()
    {
        return $this->showAll;
    }


    /**
     * @param  bool       the new state of the expansion of subtrees
     *
     * @return object     this instance
     */
    public function setShowAll($showAll)
    {
        $this->showAll = $showAll;
     //   $this->selected = new MvvTreeRoot();
        return $this;
    }


    /**
     * Toggles the state of the expansion of subtrees.
     *
     * @return object     this instance
     */
    public function toggleShowAll()
    {
        $this->showAll = !$this->showAll;
        return $this;
    }


    /**
     * Returns all the IDs of the already selected LV-Gruppen.
     *
     * @return array      an array with IDs of the selected LV-Gruppen
     */
    public function getLvGruppenIDs()
    {
        return array_keys($this->areas);
    }


    /**
     * Returns all the selected LV-Gruppen.
     *
     * @return array      an array of LV-Gruppen representing the selected
     *                    LV-Gruppen
     */
    public function getAreas()
    {
        return $this->areas;
    }


    /**
     * Sets the LV-Gruppen of this selection. One can provide either MD5ish ID
     * strings or instances of Lvgruppe.
     *
     * @param  array      an array of either MD5ish ID strings or Lvgruppe
     *
     * @return object     the called instance itself
     */
    public function setLvgruppen($areas)
    {
        $this->areas = [];
        foreach ($areas as $area) {
            $this->add($area);
        }
        return $this;
    }


    /**
     * Returns true if this LV-Gruppe is selected, false otherwise.
     *
     * @param  mixed      the id of a LV-Gruppe or the LV-Gruppe object itself
     * @return bool       returns true if selected, false otherwise
     */
    public function includes($area)
    {
        $id = is_object($area) ? $area->getId() : $area;
        return isset($this->areas[$id]);
    }


    /**
     * @return integer    returns the number of the selected LV-Gruppen
     */
    public function size()
    {
        return count($this->areas);
    }


    /**
     * This method adds an area to the selected LV-Gruppen.
     *
     * @param  mixed     the ID of the LV-Gruppe to add or a LV-Gruppe object
     *
     * @return object     this instance
     */
    public function add($area)
    {
        # convert to an object
        if (!is_object($area)) {
            $area = Lvgruppe::find($area);
        }
        $id = $area->getId();
        if (!isset($this->areas[$id])) {
            $this->areas[$id] = $area;
        }
        $this->sortAreas();
        return $this;
    }


    /**
     * This method removes given LV-Gruppe from the already selected LV-Gruppen.
     *
     * @param  mixed     the ID of the LV-Gruppe to add or a LV-Gruppe object
     *
     * @return object     this instance
     */
    public function remove($area)
    {
        if (is_object($area)) {
            $area = $area->getId();
        }
        if (isset($this->areas[(string) $area])) {
            unset($this->areas[$area]);
        }
        return $this;
    }


    /**
     * Returns the trail -- the path from the root of the tree of MVV objects down
     * to the currently selected LV-Gruppe.
     *
     * @return array an array of MVV objects
     */
    public function getTrail()
    {
        $area = $this->selected;
        $trail = [implode('_', (array) $area->getId()) => $area];
        while ($parent = $area->getTrailParent()) {
            $trail[implode('_', (array) $parent->getId())] = $parent;
            $area = $parent;
        }
        $trail['root'] = new MvvTreeRoot();
        return array_reverse($trail, true);
    }
}