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

/**
 * Objects of this class represent the state of the study area selection form.
 *
 * Note: Many of the methods return "$this" to let you easily cascade method
 * calls: $selection->toggleShowAll()->setSelected("012345etc.");
 *
 * @package   studip
 *
 * @author    mlunzena
 * @copyright (c) Authors
 */

class StudipStudyAreaSelection {

    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 areas are populated by that course's already
     * chosen study areas. 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
     */
    function __construct($course_id = NULL) {
        $this->selected = StudipStudyArea::getRootArea();
        $this->showAll = FALSE;

        $this->areas = [];

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

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


    /**
     * This method populates this instance with the already chosen study areas.
     *
     * @param  string     the course's ID
     *
     * @return void
     */
    private function populateAreasForCourse($id) {
        $areas = StudipStudyArea::getStudyAreasForCourse($id);
        $this->setAreas($areas);
        $this->sortAreas();
    }


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


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


    /**
     * @param  string     a search term
     *
     * @return object     this instance
     */
    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
     */
    function searched() {
        return $this->searchKey !== '';
    }


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


    /**
     * Returns an array of search results. This result is memoized for
     * performance though setting the search key anew clears the memo again.
     *
     * @return array      an array of search results
     */
    function getSearchResult() {

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

        # not memoized yet, do so now
        if (is_null($this->searchResult)) {
            $this->searchResult = StudipStudyArea::search($this->searchKey);
            usort($this->searchResult, [__CLASS__, 'sortSearchResult']);
        }

        return $this->searchResult;
    }

    static function sortSearchResult($a, $b) {
        return strcmp($a->getPath('·'), $b->getPath('·'));
    }


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


    /**
     * @param  mixed      either an MD5ish ID of the study area to select or the
     *                    area object itself
     *
     * @return object     this instance
     */
    function setSelected($selected) {
      if (!is_object($selected)) {
        $this->selected = StudipStudyArea::find($selected);
      }
      else {
            $this->selected = $selected;
        }
        return $this;
    }


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


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


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


    /**
     * Returns all the IDs of the selected study areas.
     *
     * @return array      an array of MD5ish strings representing the IDs of the
     *                    selected study areas
     */
    function getAreaIDs() {
        return array_keys($this->areas);
    }


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


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


    /**
     * @param  mixed      the MD5ish ID of a study area or the area object itself
     *
     * @return bool       returns TRUE if this area is selected, FALSE otherwise
     */
    function includes($area) {
        $id = is_object($area) ? $area->getID() : $area;
        return isset($this->areas[$id]);
    }


    /**
     * @return integer    returns the number of the selected study areas
     */
    function size() {
        return sizeof($this->areas);
    }


    /**
     * This method adds an area to the selected study areas.
     *
     * @param  string     the MD5ish ID of the study area to add
     *
     * @return object     this instance
     */
    function add($area) {
        # convert to an object
        if (!is_object($area)) {
            $area = StudipStudyArea::find($area);
        }
        $id = $area->getID();
        if (!isset($this->areas[$id])) {
            $this->areas[$id] = $area;
        }
        $this->sortAreas();
        return $this;
    }


    /**
     * This method removes an area from the selected study areas.
     *
     * @param  string     the MD5ish ID of the study area to add
     *
     * @return object     this instance
     */
    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 study areas down
     * to the currently selected area.
     *
     * # TODO (mlunzena) this has to be refactored as well
     *
     * @return array      an array of study areas; currently each item is an
     *                    hashmap containing the ID of each area using the key
     *                    'id' and the name of the study area using the key 'name'
     */
    function getTrail() {
        $area = $this->selected;
        $trail = [$area->getID() => $area];
        while ($parent = $area->getParent()) {
            $trail[$parent->getID()] = $parent;
            $area = $parent;
        }
        $trail[StudipStudyArea::ROOT] = StudipStudyArea::getRootArea();
        return array_reverse($trail, TRUE);
    }
}