aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/searchtypes/StudygroupSearch.class.php
blob: 9bfef6ca73c5148ac7da545d4e4d6890e417c850 (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
<?php
# Lifter010: TODO
/**
 * StudygroupSearch.class.php
 * class to add Studygroup search to Quicksearch
 *
 * 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      Michaela Brückner <brueckner@data-quest>
 * @copyright   2024 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */

class StudygroupSearch extends SearchType
{

    /**
     * title of the search like "search for courses" or just "courses"
     * @return string
     */
    public function getTitle(): string
    {
        return _('Studiengruppen suchen');
    }

    /**
     * Returns the results to a given keyword. To get the results is the
     * job of this routine and it does not even need to come from a database.
     * The results should be an array in the form
     * array (
     *   array($key, $name),
     *   array($key, $name),
     *   ...
     * )
     * where $key is an identifier like user_id and $name is a displayed text
     * that should appear to represent that ID.
     * @param keyword: string
     * @param array $contextual_data an associative array with more variables
     * @param int $limit maximum number of results (default: all)
     * @param int $offset return results starting from this row (default: 0)
     * @return array
     */
    public function getResults($keyword, $contextual_data = [], $limit = PHP_INT_MAX, $offset = 0): array
    {
        $search_helper = new StudipSemSearchHelper();
        $search_helper->setParams(
            [
                'quick_search' => $keyword,
                'qs_choose' => $contextual_data['search_sem_qs_choose'] ?? 'all',
                'sem' => $contextual_data['search_sem_sem'] ?? 'all',
                'category' => $contextual_data['search_sem_category'] ?? null,
                'scope_choose' => $contextual_data['search_sem_scope_choose'] ?? null,
                'range_choose' => $contextual_data['search_sem_range_choose'] ?? null,
            ],
            !(is_object($GLOBALS['perm'])
                && $GLOBALS['perm']->have_perm(
                    Config::Get()->SEM_VISIBILITY_PERM)));
        $search_helper->doSearch();
        $result = $search_helper->getSearchResultAsArray();

        if (empty($result)) {
            return [];
        }

        $query = "SELECT s.Seminar_id, CONCAT_WS(' ', s.VeranstaltungsNummer, s.name, CONCAT(' (',
            IF(semester_courses.semester_id IS NULL,  '" . _('unbegrenzt') . "',
                IF(COUNT(DISTINCT semester_courses.semester_id) > 1, CONCAT_WS(' - ', (SELECT start_semester.name FROM `semester_data` AS start_semester WHERE start_semester.semester_id = semester_courses.semester_id ORDER BY `beginn` ASC LIMIT 1), (SELECT end_semester.name FROM `semester_data` AS end_semester WHERE end_semester.semester_id = semester_courses.semester_id ORDER BY `beginn` DESC LIMIT 1)), sem1.name)), ')')) AS Name
                   FROM seminare AS s
                   LEFT JOIN semester_courses ON (semester_courses.course_id = s.Seminar_id)
                   LEFT JOIN `semester_data` sem1 ON (semester_courses.semester_id = sem1.semester_id)
                   LEFT JOIN seminar_user AS su ON (su.Seminar_id = s.Seminar_id AND su.status='dozent')
                   LEFT JOIN auth_user_md5 USING (user_id)
                   WHERE s.Seminar_id IN (?)
                   GROUP BY s.Seminar_id";
        if (Config::get()->IMPORTANT_SEMNUMBER) {
            $query .= " ORDER BY s.VeranstaltungsNummer, s.Name";
        } else {
            $query .= " ORDER BY s.Name";
        }
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            array_slice($result, $offset, $limit) ?: ''
        ]);
        return $statement->fetchAll(PDO::FETCH_NUM);
    }


    /**
     * Returns the path to this file, so that this class can be autoloaded and is
     * always available when necessary.
     * Should be: "return __file__;"
     *
     * @return string   path to this file
     */
    public function includePath(): string
    {
        return studip_relative_path(__FILE__);
    }
}