aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/searchtypes/MyCoursesSearch.php
blob: 1bdb25086bee29cd51dff49b52c7cd9d8c703d55 (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
<?php
/**
 * MyCoursesSearch.php
 * Search only in own courses.
 *
 * 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 <thomas.hackl@uni-passau.de>
 * @copyright   2015 Stud.IP Core-Group
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
*/

class MyCoursesSearch extends StandardSearch
{
    private $perm_level;
    private $parameters;
    protected $additional_sql_conditions;

    /**
     *
     * @param string $search
     *
     * @param string $perm_level
     *
     * @param string $additional_sql_conditions An additional SQL snippet
     *      consisting of conditions. This snippet is appended to the
     *      default conditions.
     *
     * @return void
     */
    public function __construct($search, $perm_level = 'dozent', $parameters = [], $additional_sql_conditions = '')
    {
        parent::__construct($search);

        $this->perm_level = $perm_level;
        $this->parameters = $parameters;
        $this->additional_sql_conditions = trim($additional_sql_conditions);
    }


    /**
     * returns the title/description of the searchfield
     *
     * @return string title/description
     */
    public function getTitle()
    {
        return _('Veranstaltung suchen');
    }

    /**
     * returns the results of a search
     * Use the contextual_data variable to send more variables than just the input
     * to the SQL. QuickSearch for example sends all other variables of the same
     * <form>-tag here.
     * @param string $input the search-word(s)
     * @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: array(array(), ...)
     */
    public function getResults($input, $contextual_data = [], $limit = PHP_INT_MAX, $offset = 0)
    {
        $sql = $this->getSQL();
        if (!$sql) {
            return [];
        }
        if ($offset || $limit != PHP_INT_MAX) {
            $sql .= sprintf(' LIMIT %d, %d', $offset, $limit);
        }

        $statement = DBManager::get()->prepare($sql, [PDO::FETCH_NUM]);
        $statement->execute(array_merge(
            $this->parameters,
            $contextual_data,
            [':input' => "%{$input}%"]
        ));
        $results = $statement->fetchAll();
        return $results;
    }

    /**
     * returns a sql-string appropriate for the searchtype of the current class
     *
     * @return string
     */
    private function getSQL()
    {
        $semnumber = Config::get()->IMPORTANT_SEMNUMBER;
        $semester_text = "CONCAT(
            '(',
            IF(semester_data.semester_id IS NULL, '" . _('unbegrenzt') . "',
                GROUP_CONCAT(semester_data.`name` SEPARATOR ', ')),
            ')'
        )";

        $conditions = implode(' AND ', $this->getConditions());

        switch ($this->perm_level) {
            // Roots see everything, everywhere.
            case 'root':
                $query = "SELECT DISTINCT
                            s.`Seminar_id`,
                            CONCAT_WS(' ', s.`VeranstaltungsNummer`, s.`Name`, {$semester_text})
                          FROM `seminare` s
                          LEFT JOIN semester_courses ON (s.Seminar_id = semester_courses.course_id)
                          LEFT JOIN `semester_data` ON (semester_data.semester_id = semester_courses.semester_id)
                          WHERE {$conditions}
                          GROUP BY s.Seminar_id ";
                if ($semnumber) {
                    $query .= " ORDER BY MAX(semester_data.`beginn`) DESC, s.`VeranstaltungsNummer`, s.`Name`";
                } else {
                    $query .= " ORDER BY MAX(semester_data.beginn) DESC, s.`Name`";
                }
                return $query;
            // Admins see everything at their assigned institutes.
            case 'admin':
                $sem_inst = Config::get()->ALLOW_ADMIN_RELATED_INST ? 'si' : 's';
                $query = "SELECT DISTINCT
                            s.`Seminar_id`,
                            CONCAT_WS(' ', s.`VeranstaltungsNummer`, s.`Name`, {$semester_text})
                          FROM `seminare` s
                          JOIN `seminar_inst` si USING (Seminar_id)
                          LEFT JOIN semester_courses ON (s.Seminar_id = semester_courses.course_id)
                          LEFT JOIN `semester_data` ON (semester_data.semester_id = semester_courses.semester_id)
                          WHERE {$conditions}
                            AND {$sem_inst}.`institut_id` IN (:institutes)
                          GROUP BY s.Seminar_id ";
                if ($semnumber) {
                    $query .= " ORDER BY MAX(semester_data.`beginn`) DESC, s.`VeranstaltungsNummer`, s.`Name`";
                } else {
                    $query .= " ORDER BY MAX(semester_data.`beginn`) DESC, s.`Name`";
                }
                return $query;
            // non-admins search all their administrable courses.
            default:
                $query = "SELECT DISTINCT
                            s.`Seminar_id`,
                            CONCAT_WS(' ', s.`VeranstaltungsNummer`, s.`Name`, {$semester_text}),
                            s.`VeranstaltungsNummer` AS num,
                            s.`Name`,
                            MAX(semester_data.beginn) AS beginn
                          FROM `seminare` s
                          JOIN `seminar_user` su ON (s.`Seminar_id` = su.`Seminar_id`)
                          LEFT JOIN semester_courses ON (s.Seminar_id = semester_courses.course_id)
                          LEFT JOIN `semester_data` ON (semester_data.semester_id = semester_courses.semester_id)
                          WHERE {$conditions}
                            AND su.`user_id` = :userid
                            AND su.`status` IN ('dozent','tutor')
                          GROUP BY s.Seminar_id ";

                if (Config::get()->DEPUTIES_ENABLE) {
                    $query .= " UNION ";
                    $query .= "SELECT DISTINCT
                                 s.`Seminar_id`,
                                 CONCAT_WS(' ', s.`VeranstaltungsNummer`, ' ', s.`Name`, {$semester_text}),
                                 s.`VeranstaltungsNummer` AS num,
                                 s.`Name`,
                                 MAX(semester_data.beginn) AS beginn
                               FROM `seminare` s
                               JOIN `deputies` d ON (s.`Seminar_id` = d.`range_id`)
                               LEFT JOIN semester_courses ON (s.Seminar_id = semester_courses.course_id)
                               LEFT JOIN `semester_data` ON (semester_data.semester_id = semester_courses.semester_id)
                               WHERE {$conditions}
                                 AND d.`user_id` = :userid
                               GROUP BY s.Seminar_id";
                }

                if ($semnumber) {
                    $query .= " ORDER BY beginn DESC, num, `Name`";
                } else {
                    $query .= " ORDER BY beginn DESC, `Name`";
                }

                return $query;
        }
    }

    /**
     * Returns the default conditions use by all searches as a list.
     *
     * @return array
     */
    private function getConditions(): array
    {
        $conditions = [
            '(s.`VeranstaltungsNummer` LIKE :input OR s.`Name` LIKE :input)',
            's.`Seminar_id` NOT IN (:exclude)',
        ];

        if (isset($this->parameters['semtypes'])) {
            $conditions[] = 's.`status` NOT IN (:semtypes)';
        }

        if (isset($this->parameters['semesters'])) {
            $conditions[] = 'semester_data.`semester_id` IN (:semesters)';
        }

        if ($this->additional_sql_conditions) {
            $conditions[] = $this->additional_sql_conditions;
        }

        return $conditions;
    }

    /**
     * A very simple overwrite of the same method from SearchType class.
     * returns the absolute path to this class for autoincluding this class.
     *
     * @return: path to this class
     */
    public function includePath()
    {
        return studip_relative_path(__FILE__);
    }
}