aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/globalsearch/GlobalSearchCourses.php
blob: a02e6915e5ccdaf562a6ddaf7162131ad428e61b (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
<?php
/**
 * Global search module for courses
 *
 * @author      Thomas Hackl <thomas.hackl@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.1
 */
class GlobalSearchCourses extends GlobalSearchModule implements GlobalSearchFulltext
{
    /**
     * Returns the displayname for this module
     *
     * @return string
     */
    public static function getName()
    {
        return _('Veranstaltungen');
    }

    /**
     * Returns the filters that are displayed in the sidebar of the global search.
     *
     * @return array Filters for this class.
     */
    public static function getFilters()
    {
        return ['semester', 'institute', 'seminar_type'];
    }

    /**
     * Transforms the search request into an sql statement, that provides the id (same as getId) as type and
     * the object id, that is later passed to the filter.
     *
     * This function is required to make use of the mysql union parallelism
     *
     * @param string $search the input query string
     * @param array $filter an array with search limiting filter information (e.g. 'category', 'semester', etc.)
     * @return string SQL Query to discover elements for the search
     */
    public static function getSQL($search, $filter, $limit)
    {
        if (!$search) {
            return null;
        }
        $search = str_replace(' ', '% ', $search);
        $query = DBManager::get()->quote("%{$search}%");

        $language_name = 'courses.`Name`';
        $language_join = '';
        if (I18N::isEnabled() && $_SESSION['_language'] !== I18NString::getDefaultLanguage()) {
            $language_name = 'IFNULL(`i18n`.`value`, courses.`Name`)';
            $language_join = "LEFT JOIN `i18n`
                                ON `i18n`.`object_id` = courses.`Seminar_id`
                                  AND `i18n`.`table` = 'seminare'
                                  AND `i18n`.`field` = 'name'
                                  AND `lang` = " . DBManager::get()->quote($_SESSION['_language']);
        }

        $visibility = '';
        $semester_join = '';
        $institute_condition = '';
        $seminar_type_condition = '';
        $semester_condition = '';

        // visibility
        if (!$GLOBALS['perm']->have_perm(Config::get()->SEM_VISIBILITY_PERM)) {
            $visibility = "courses.`visible` = 1 AND ";
        }

        // generate SQL for the given sidebar filter (semester, institute, seminar_type)
        if (
            !empty($filter['category'])
            && in_array($filter['category'], [self::class, 'show_all_categories'])
        ) {
            if (!empty($filter['semester'])) {
                if ($filter['semester'] === 'future') {
                    $semester = Semester::findCurrent();
                    $next_semester = Semester::findNext();

                    $semester_ids = [$semester->id];
                    if ($next_semester) {
                        $semester_ids[] = $next_semester->id;
                    }
                } else {
                    $semester = Semester::findByTimestamp($filter['semester']);
                    $semester_ids = [$semester->id];
                }
                $semester_condition = "
                    AND (
                        semester_courses.semester_id IS NULL OR semester_courses.semester_id IN (" . implode(',', array_map([DBManager::get(), 'quote'], $semester_ids)) . ")
                    ) ";
            }
            if (!empty($filter['institute'])) {
                $institutes = self::getInstituteIdsForSQL($filter['institute']);
                $institute_condition = " AND `courses`.`Institut_id` IN (" . DBManager::get()->quote($institutes) . ") ";
            }
            if (!empty($filter['seminar_type'])) {
                $seminar_types = self::getSeminarTypesForSQL($filter['seminar_type']);
                $seminar_type_condition = " AND `courses`.`status` IN (" . DBManager::get()->quote($seminar_types) . ") ";
            }
        }

        $sql = "SELECT SQL_CALC_FOUND_ROWS courses.`Seminar_id`,
                       {$language_name} AS `Name`,
                       courses.`VeranstaltungsNummer`, courses.`status`
                FROM `seminare` AS courses
                LEFT JOIN `semester_courses` ON courses.`seminar_id` = `semester_courses`.`course_id`
                LEFT JOIN `semester_data` USING (`semester_id`)
                {$language_join}
                JOIN `seminar_user` u ON (u.`Seminar_id` = courses.`Seminar_id` AND u.`status` = 'dozent')
                JOIN `auth_user_md5` a ON (a.`user_id` = u.`user_id`)
                WHERE {$visibility}
                    (
                        {$language_name} LIKE {$query}
                        OR courses.`VeranstaltungsNummer` LIKE {$query}
                        OR CONCAT(a.`Nachname`, ', ', a.`Vorname`, ' ', a.`Nachname`) LIKE {$query}
                    )
                {$institute_condition}
                {$seminar_type_condition}
                {$semester_condition}
                GROUP BY courses.Seminar_id
                ORDER BY MAX(`semester_data`.`beginn`) DESC";

        if (Config::get()->IMPORTANT_SEMNUMBER) {
            $sql .= ", courses.`VeranstaltungsNummer`";
        }

        $sql .= ", `Name`";
        $sql .= " LIMIT " . $limit;

        return $sql;
    }

    /**
     * Returns an array of information for the found element. Following informations (key: description) are necessary
     *
     * - name: The name of the object
     * - url: The url to send the user to when he clicks the link
     *
     * Additional informations are:
     *
     * - additional: Subtitle for the hit
     * - expand: Url if the user further expands the search
     * - img: Avatar for the
     *
     * @param array $data
     * @param string $search
     * @return array
     */
    public static function filter($data, $search)
    {
        $course = Course::buildExisting($data);
        $turnus_string = implode(' ', $course->getAllDatesInSemester()->toStringArray());
        //Shorten, if string too long (add link for details.php)
        if (mb_strlen($turnus_string) > 70) {
            $turnus_string = htmlReady(mb_substr($turnus_string, 0, mb_strpos(mb_substr($turnus_string, 70, mb_strlen($turnus_string)), ',') + 71));
            $turnus_string .= ' ... <a href="' . URLHelper::getURL("dispatch.php/course/details/index/{$course->id}") . '">(' . _('mehr') . ')</a>';
        } else {
            $turnus_string = htmlReady($turnus_string);
        }
        $lecturers = $course->getMembersWithStatus('dozent');
        $semester = $course->start_semester;

        // If you are not root, perhaps not all available subcourses are visible.
        $visibleChildren = $course->children;
        if (!$GLOBALS['perm']->have_perm(Config::get()->SEM_VISIBILITY_PERM)) {
            $visibleChildren = $visibleChildren->filter(function($c) {
                return $c->visible;
            });
        }
        $result_children = [];
        foreach ($visibleChildren as $child) {
            $result_children[] = self::filter($child, $search);
        }

        //admission state
        $admission_state = "";
        if (Config::get()->COURSE_SEARCH_SHOW_ADMISSION_STATE) {
            switch (self::getStatusCourseAdmission($course->id,
                $course->admission_prelim)) {
                case 1:
                    $admission_state = Icon::create(
                        'decline-circle',
                        Icon::ROLE_STATUS_YELLOW,
                        tooltip2(_('Eingeschränkter Zugang'))
                    )->asSvg();
                break;
                case 2:
                    $admission_state = Icon::create(
                        'decline-circle',
                        Icon::ROLE_STATUS_RED,
                        tooltip2(_('Kein Zugang'))
                    )->asSvg();
                break;
                default:
                $admission_state = Icon::create(
                    'check-circle',
                    Icon::ROLE_STATUS_GREEN,
                    tooltip2(_('Uneingeschränkter Zugang'))
                )->asSvg();
            }
        }

        $result = [
            'id'            => $course->id,
            'number'        => self::mark($course->veranstaltungsnummer, $search),
            'name'          => self::mark($course->getFullName(), $search),
            'url'           => URLHelper::getURL("dispatch.php/course/details/index/{$course->id}", [], true),
            'date'          => htmlReady($semester->short_name),
            'dates'         => $turnus_string,
            'has_children'  => count($course->children) > 0,
            'children'      => $result_children,
            'additional'    => implode(', ',
                array_filter(
                    array_map(
                        function ($lecturer, $index) use ($search, $course) {
                            if ($index < 3) {
                                return self::mark($lecturer->getUserFullname(), $search);
                            } else if ($index == 3) {
                                return '... (' . _('mehr') . ')';
                            }
                        },
                        $lecturers,
                        array_keys($lecturers)
                    )
                )
            ),
            'expand'     => self::getSearchURL($search),
            'admission_state' => $admission_state,
        ];
        if ($course->getSemClass()->offsetGet('studygroup_mode')) {
            $avatar = StudygroupAvatar::getAvatar($course->id);
        } else {
            $avatar = CourseAvatar::getAvatar($course->id);
        }
        $result['img'] = $avatar->getUrl(Avatar::MEDIUM);
        return $result;
    }

    /**
     * Enables fulltext (MATCH AGAINST) search by creating the corresponding indices.
     */
    public static function enable()
    {
        DBManager::get()->exec("ALTER TABLE `seminare` ADD FULLTEXT INDEX globalsearch (`VeranstaltungsNummer`, `Name`)");
        DBManager::get()->exec("ALTER TABLE `sem_types` ADD FULLTEXT INDEX globalsearch (`Name`)");
    }

    /**
     * Disables fulltext (MATCH AGAINST) search by removing the corresponding indices.
     */
    public static function disable()
    {
        DBManager::get()->exec("DROP INDEX globalsearch ON `seminare`");
        DBManager::get()->exec("DROP INDEX globalsearch ON `sem_types`");
    }

    /**
     * Returns the URL that can be called for a full search.
     *
     * @param string $searchterm what to search for?
     * @return string URL to the full search, containing the searchterm and the category
     */
    public static function getSearchURL($searchterm)
    {
        return URLHelper::getURL('dispatch.php/search/globalsearch', [
            'q'        => $searchterm,
            'category' => self::class
        ]);
    }

    /**
     * Returns the admission status for a course.
     *
     * @param string $seminar_id Id of the course
     * @param bool   $prelim     State of preliminary setting
     * @return int
     */
    public static function getStatusCourseAdmission($seminar_id, $prelim)
    {
        $sql = "SELECT COUNT(`type`) AS `types`,
                       SUM(IF(`type` = 'LockedAdmission', 1, 0)) AS `type_locked`
                FROM `seminar_courseset`
	            INNER JOIN `courseset_rule` USING (`set_id`)
	            WHERE `seminar_id` = ?
                GROUP BY `set_id`";

	    $stmt = DBManager::get()->prepare($sql);
	    $stmt->execute([$seminar_id]);
	    $result = $stmt->fetch();

        if (!empty($result['types'])) {
            if ($result['type_locked']) {
                return 2;
            }
            return 1;
        }

        if ($prelim) {
            return 1;
        }
        return 0;
    }

}