aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Lvgruppe.php
blob: 138d28e4d811b8dc7435528bf8b8e85b6f1ea754 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
/**
 * Lvgruppe.php
 * model class for Lehrveranstaltungsgruppen (table mvv_lvgruppe)
 *
 * 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      Peter Thienel <thienel@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       3.5
 *
 * @property string $id alias column for lvgruppe_id
 * @property string $lvgruppe_id database column
 * @property string $name database column
 * @property I18NString|null $alttext database column
 * @property string $author_id database column
 * @property string $editor_id database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property SimpleORMapCollection|Modulteil[] $modulteile has_and_belongs_to_many Modulteil
 * @property SimpleORMapCollection|Course[] $courses has_and_belongs_to_many Course
 * @property SimpleORMapCollection|ArchivedCourse[] $archived_courses has_and_belongs_to_many ArchivedCourse
 * @property mixed $count_seminare additional field
 * @property mixed $count_archiv additional field
 * @property mixed $count_modulteile additional field
 * @property mixed $count_semester additional field
 */

class Lvgruppe extends ModuleManagementModelTreeItem
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'mvv_lvgruppe';

        $config['has_and_belongs_to_many']['modulteile'] = [
            'class_name' => Modulteil::class,
            'thru_table' => 'mvv_lvgruppe_modulteil',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_and_belongs_to_many']['courses'] = [
            'class_name' => Course::class,
            'thru_table' => 'mvv_lvgruppe_seminar',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];
        $config['has_and_belongs_to_many']['archived_courses'] = [
            'class_name' => ArchivedCourse::class,
            'thru_table' => 'mvv_lvgruppe_seminar',
            'on_delete' => 'delete',
            'on_store' => 'store'
        ];

        $config['additional_fields']['count_seminare']['get'] =
            function($lvgruppe) { return $lvgruppe->count_seminare; };
        $config['additional_fields']['count_seminare']['set'] = false;
        $config['additional_fields']['count_archiv']['get'] =
            function($lvgruppe) { return $lvgruppe->count_archiv; };
        $config['additional_fields']['count_archiv']['set'] = false;
        $config['additional_fields']['count_modulteile']['get'] =
            function($lvgruppe) { return $lvgruppe->count_modulteile; };
        $config['additional_fields']['count_modulteile']['set'] = false;
        $config['additional_fields']['count_semester']['get'] =
            function($lvgruppe) { return $lvgruppe->count_semester; };
        $config['additional_fields']['count_semester']['set'] = false;

        $config['i18n_fields']['alttext'] = true;

        parent::configure($config);
    }

    private $count_seminare;
    private $count_archiv;
    private $count_modulteile;
    private $count_semester;

    public function __construct($id = null)
    {
        parent::__construct($id);
        $this->object_real_name = _('Lehrveranstaltungsgruppe');
    }

    /**
     * @see ModuleManagementModel::getClassDisplayName
     */
    public static function getClassDisplayName($long = false)
    {
        return _('Lehrveranstaltungsgruppe');
    }

    /**
     * Returns all or a specified (by row count and offset) number of
     * Abschluesse sorted and filtered by given parameters and enriched with
     * some additional fields. This function is mainly used in the list view.
     *
     * @param string $sortby Field name to order by.
     * @param string $order ASC or DESC direction of order.
     * @param array $filter Key-value pairs of filed names and values
     * to filter the result set.
     * @param int $row_count The max number of objects to return.
     * @param int $offset The first object to return in a result set.
     * @param string An id of a semester to restrict the result to groups
     * assigned to courses span the semesters time range.
     * @return object A SimpleORMapCollection of Abschluss objects.
     */
    public static function getAllEnriched($sortby = 'name', $order = 'ASC',
            $filter = null, $row_count = null, $offset = null,
            $semester_id = null)
    {
        $sortby = self::createSortStatement($sortby, $order, 'name',
                ['count_seminare', 'count_modulteile', 'count_archiv']);
        $params = [];
        if (!is_null($filter)) {
            $filter_sql = is_array($filter)
                    ? self::getFilterSql($filter, true) : $filter;
        } else {
            $filter_sql = '';
        }
        // get result filtered by a given semester
        $semester_join = '';
        if ($semester_id) {
            $semester = Semester::find($semester_id);
            if ($semester) {
                $filter_sql = trim($filter_sql) ? $filter_sql . ' AND' : $filter_sql . ' WHERE';
                $filter_sql .= ' seminare.start_time <= :beginn '
                        . 'AND (semester_courses.semester_id IS NULL OR semester_courses.semester_id = :semester_id) '
                        . 'AND (start_sem.beginn <= :ende AND '
                        . 'IF(ISNULL(end_sem.ende), 1, end_sem.ende >= :beginn)) ';
                $params = [
                    ':semester_id' => $semester->semester_id,
                    ':beginn' => $semester->beginn,
                    ':ende' => $semester->ende
                ];
                $semester_join = 'LEFT JOIN mvv_modul ON mvv_modul.modul_id = mvv_modulteil.modul_id '
                . 'LEFT JOIN semester_data as start_sem ON start_sem.semester_id = mvv_modul.start '
                . 'LEFT JOIN semester_data as end_sem ON end_sem.semester_id = mvv_modul.end '
                . 'LEFT JOIN semester_courses ON (semester_courses.course_id = seminare.Seminar_id) ';
            }
        }
        $query = 'SELECT mvv_lvgruppe.*, mvv_lvgruppe.lvgruppe_id AS lvg_id, '
                . 'COUNT(DISTINCT seminare.seminar_id) AS `count_seminare`, '
                . 'COUNT(DISTINCT archiv.seminar_id) AS `count_archiv`, '
                // get ALL assigned modulteile, not only the modulteile from modules
                // where the responsible institute is set by the filter
                // (maybe the filter is set to the institutes where the user has a mvv role)
                . 'COALESCE((SELECT COUNT(DISTINCT modulteil_id) FROM mvv_lvgruppe_modulteil '
                    . 'WHERE mvv_lvgruppe_modulteil.lvgruppe_id = lvg_id '
                    . 'GROUP BY lvgruppe_id), 0) AS `count_modulteile` '
                . 'FROM mvv_lvgruppe '
                . 'LEFT JOIN mvv_lvgruppe_seminar USING(lvgruppe_id) '
                . 'LEFT JOIN archiv ON mvv_lvgruppe_seminar.seminar_id = archiv.seminar_id '
                . 'LEFT JOIN seminare ON mvv_lvgruppe_seminar.seminar_id = seminare.seminar_id '
                . 'LEFT JOIN mvv_lvgruppe_modulteil USING(lvgruppe_id) '
                . 'LEFT JOIN mvv_modulteil USING(modulteil_id) '
                . 'LEFT JOIN mvv_modul_inst USING(modul_id) '
                . $semester_join
                . $filter_sql
                . 'GROUP BY lvgruppe_id '
                . 'ORDER BY ' . $sortby;
        return parent::getEnrichedByQuery($query, $params, $row_count, $offset);
    }

    /**
     * Returns the number of LV-Gruppen optionally reduced by
     * filter criteria.
     *
     * @param array $filter Key-value pairs of filed names and values
     * to filter the result.
     * @param string An id of a semester to restrict the result to groups
     * assigned to courses span the semesters time range.
     * @return type
     */
    public static function getCount($filter = null /*, $semester_id = null*/)
    {
        if (!is_null($filter)) {
            $filter_sql = is_array($filter)
                    ? self::getFilterSql($filter, true) : $filter;
        } else {
            $filter_sql = '';
        }
        $num_args = func_num_args();
        $semester_join = '';
        $params = [];
        // get result filtered by a given semester
        if ($num_args == 2) {
            $semester_id = func_get_arg(1);
            $semester = Semester::find($semester_id);
            if ($semester) {
                if (trim($filter_sql)) {
                    $filter_sql .= ' AND';
                } else {
                    $filter_sql .= ' WHERE';
                }

                $filter_sql = trim($filter_sql) ? $filter_sql  : ' AND';
                $filter_sql .= ' seminare.start_time <= :beginn '
                        . 'AND (semester_courses.semester_id IS NULL OR semester_courses.semester_id = :semester_id) '
                        . 'AND (start_sem.beginn <= :ende AND '
                        . 'IF(ISNULL(end_sem.ende), 1, end_sem.ende >= :beginn)) ';
                $params = [
                    ':semester_id' => $semester->semester_id,
                    ':beginn' => $semester->beginn,
                    ':ende' => $semester->ende
                ];
                $semester_join = 'LEFT JOIN mvv_modul ON mvv_modul.modul_id = mvv_modulteil.modul_id '
                . 'LEFT JOIN semester_data as start_sem ON start_sem.semester_id = mvv_modul.start '
                . 'LEFT JOIN semester_data as end_sem ON end_sem.semester_id = mvv_modul.end '
                . 'LEFT JOIN semester_courses ON (semester_courses.course_id = seminare.Seminar_id) ';
            }
        }
        $query = 'SELECT COUNT(DISTINCT(mvv_lvgruppe.lvgruppe_id)) '
                . 'FROM mvv_lvgruppe '
                . 'LEFT JOIN mvv_lvgruppe_seminar USING(lvgruppe_id) '
                . 'LEFT JOIN seminare ON mvv_lvgruppe_seminar.seminar_id = seminare.seminar_id '
                . 'LEFT JOIN mvv_lvgruppe_modulteil USING(lvgruppe_id) '
                . 'LEFT JOIN mvv_modulteil USING(modulteil_id) '
                . 'LEFT JOIN mvv_modul_inst USING(modul_id) '
                . $semester_join
                . $filter_sql;
        $db = DBManager::get()->prepare($query);
        $db->execute($params);
        return $db->fetchColumn(0);
    }

    /**
     * Retrieves all LV-Gruppen by given search term. The term is compared with
     * the name of the LV-Gruppe, the code and the name of related modules
     *
     * @param string $term The search term.
     * @param array|string $filter An array with filter options or a where part.
     * @return object A SimpleORMapCollection of LV-Gruppen.
     */
    public static function findBySearchTerm($term, $filter = null)
    {
        if (!is_null($filter)) {
            $filter_sql = is_array($filter)
                    ? self::getFilterSql($filter) : $filter;
        } else {
            $filter_sql = '';
        }

        $term = '%' . $term . '%';
        return parent::getEnrichedByQuery(
                "SELECT mvv_lvgruppe.*, GROUP_CONCAT(DISTINCT mvv_modul.modul_id SEPARATOR ',') "
                . 'AS `assigned_modul_ids` '
                . 'FROM mvv_lvgruppe '
                . 'INNER JOIN mvv_lvgruppe_modulteil USING(lvgruppe_id) '
                . 'INNER JOIN mvv_modulteil USING(modulteil_id) '
                . 'INNER JOIN mvv_modul USING(modul_id) '
                . 'INNER JOIN mvv_modul_deskriptor USING(modul_id) '
                . 'INNER JOIN mvv_modul_inst USING(modul_id) '
                . 'INNER JOIN mvv_stgteilabschnitt_modul USING(modul_id) '
                . 'INNER JOIN mvv_stgteilabschnitt USING(abschnitt_id) '
                . 'INNER JOIN mvv_stgteilversion USING(version_id) '
                . 'LEFT JOIN semester_data start_sem '
                . 'ON (mvv_modul.start = start_sem.semester_id) '
                . 'LEFT JOIN semester_data end_sem '
                . 'ON (mvv_modul.end = end_sem.semester_id) '
                . 'WHERE (mvv_lvgruppe.name LIKE :search_term OR mvv_modul.code LIKE :search_term '
                . 'OR mvv_modul_deskriptor.bezeichnung LIKE :search_term) '
                . $filter_sql
                . 'GROUP BY lvgruppe_id '
                . 'ORDER BY `name`', [':search_term' => $term]);
    }

    /**
     * Retrieves all LV-Gruppen related to the Modulteil with given id.
     *
     * @param string $modulteil_id The id of a Modulteil.
     * @return object A SimpleORMapCollection of LV-Gruppen.
     */
    public static function findByModulteil($modulteil_id)
    {
        return parent::getEnrichedByQuery('SELECT mlg.* '
                . 'FROM mvv_lvgruppe mlg '
                . 'INNER JOIN mvv_lvgruppe_modulteil mlm USING(lvgruppe_id) '
                . 'WHERE mlm.modulteil_id = ? '
                . 'ORDER BY `position`,`mkdate`', [$modulteil_id]);
    }

    /**
     * Retrieves all LV-Gruppen related to the course with given id.
     *
     * @param string $seminar_id The id of a course.
     * @return object A SimpleORMapCollection of LV-Gruppen.
     */
    public static function findBySeminar($seminar_id)
    {
        return parent::getEnrichedByQuery('SELECT mlg.* '
                . 'FROM mvv_lvgruppe mlg '
                . 'INNER JOIN mvv_lvgruppe_seminar mls USING(lvgruppe_id) '
                . 'WHERE mls.seminar_id = ? '
                . 'ORDER BY `name`', [$seminar_id]);
    }

    /**
     * Returns all institutes assigned to Module.
     *
     * @param string $sortby DB field to sort by.
     * @param string $order ASC or DESC
     * @param array $filter Array of filter.
     * @return array Array of found Fachbereiche.
     */
    public static function getAllAssignedInstitutes($sortby = 'name',
            $order = 'ASC', $filter = null, $row_count = null, $offset = null)
    {
        $sortby = Fachbereich::createSortStatement($sortby, $order, 'name',
                ['count_module']);
        return Fachbereich::getEnrichedByQuery('SELECT Institute.*, '
                . 'Institute.Name as `name`, '
                . 'Institute.Institut_id AS institut_id, '
                . 'COUNT(DISTINCT(lvgruppe_id)) as count_objects '
                . 'FROM mvv_lvgruppe_modulteil '
                . 'INNER JOIN mvv_modulteil USING(modulteil_id) '
                . 'INNER JOIN mvv_modul_inst USING(modul_id) '
                . 'INNER JOIN Institute '
                . 'ON mvv_modul_inst.institut_id = Institute.Institut_id '
                . Fachbereich::getFilterSql($filter, true)
                . 'GROUP BY institut_id ORDER BY ' . $sortby
                , [], $row_count, $offset);
    }

    /**
     * Assigns the given seminar to this lvgruppe.
     *
     * @param string $seminar_id
     * @return int|boolean
     */
    public function addSeminar($seminar_id)
    {
        return LvgruppeSeminar::get([$this->getId(), $seminar_id])
                ->store();
    }

    /**
     * Assigns the given course to the given LvGruppen.
     *
     * @param array Array of ids
     * @return int The number of assigned LvGruppen.
     */
    public static function setLvgruppen($seminar_id, $lvgruppen_ids)
    {
        $old = Lvgruppe::findBySeminar($seminar_id);
        $removed = array_diff($old->pluck('id'), $lvgruppen_ids);
        $added = array_diff($lvgruppen_ids, $old->pluck('id'));
        foreach ($removed as $one) {
            $count_removed += $old->findOneBy('id', $one)->removeSeminar($seminar_id);
        }
        foreach ($added as $one) {
            $count_added += Lvgruppe::get($one)->addSeminar($seminar_id);
        }
        return count($old) + $count_added - $count_removed;
    }

    /**
     * Removes the seminar from this Lvgruppe.
     *
     * @param type $seminar_id
     * @return boolean Always true...
     */
    public function removeSeminar($seminar_id)
    {
        return LvgruppeSeminar::get([$this->getId(), $seminar_id])
                ->delete();
    }

    /**
     * @see MvvTreeItem::getTrailParentId()
     */
    public function getTrailParentId()
    {
        return ($_SESSION['MVV/Lvgruppe/trail_parent_id']);
    }

    /**
     * @see MvvTreeItem::getTrailParent()
     */
    public function getTrailParent()
    {
        return Modul::findCached($this->getTrailParentId());
    }

    /**
     * @see MvvTreeItem::getChildren()
     */
    public function getChildren()
    {
        return [];
    }

    /**
     * @see MvvTreeItem::hasChildren()
     */
    public function hasChildren()
    {
        return false;
    }

    /**
     * @see MvvTreeItem::isAssignable()
     */
    public function isAssignable()
    {
        return true;
    }

    /**
     * @see MvvTreeItem::getParents()
     */
    public function getParents($mode = null)
    {
         return Modulteil::findByLvgruppe($this->getId());
    }

    /**
     * Retrieves courses this LV-Gruppe is assigned to. Filtered by a given
     * semester considering the global visibility or the the visibility
     * for a given user.
     *
     * @param string $semester_id The id of a semester.
     * @param mixed $only_visible Boolean true retrieves only visible courses, false
     * retrieves all courses. If $only_visible is an user id it depends on the users
     * status which courses will be retrieved.
     * @return array An array of course data.
     */
    public function getAssignedCoursesBySemester($semester_id, $only_visible = true)
    {
        $semester = Semester::find($semester_id);
        if ($semester) {
            $sql = 'SELECT seminar_id, Name, VeranstaltungsNummer, sem.visible '
                . 'FROM seminare sem '
                . 'LEFT JOIN mvv_lvgruppe_seminar mls USING(seminar_id) '
                . 'LEFT JOIN semester_courses ON (semester_courses.course_id = sem.Seminar_id) '
                . 'WHERE mls.lvgruppe_id = :id '
                . 'AND ((sem.start_time <= :semester_beginn '
                . 'AND semester_courses.semester_id = :semester_id) '
                . 'OR (sem.start_time BETWEEN :semester_beginn AND :semester_ende) '
                . 'OR (sem.start_time <= :semester_beginn AND semester_courses.semester_id IS NULL)) ';
            if ($only_visible === false) {
                $stmt = DBManager::get()->prepare($sql);
                $stmt->execute([
                    ':id' => $this->getId(),
                    ':semester_id' => $semester->semester_id,
                    ':semester_beginn' => $semester->beginn,
                    ':semester_ende' => $semester->ende
                ]);
            } else if ($only_visible === true) {
                $stmt = DBManager::get()->prepare($sql . ' AND sem.visible = 1 ');
                $stmt->execute([
                    ':id' => $this->getId(),
                    ':semester_id' => $semester->semester_id,
                    ':semester_beginn' => $semester->beginn,
                    ':semester_ende' => $semester->ende
                ]);
            } else {
                $user_perm = $GLOBALS['perm']->get_perm($only_visible);
                if ($user_perm == 'root') {
                    $stmt = DBManager::get()->prepare($sql);
                    $stmt->execute([
                        ':id' => $this->getId(),
                        ':semester_id' => $semester->semester_id,
                        ':semester_beginn' => $semester->beginn,
                        ':semester_ende' => $semester->ende
                    ]);
                } else if ($user_perm == 'admin') {
                    $perm_institute_ids = [];
                    foreach (Institute::getMyInstitutes($only_visible) as $perm_institute) {
                        $perm_institute_ids[] = $perm_institute['Institut_id'];
                    }
                    $stmt = DBManager::get()->prepare($sql
                        . 'AND (sem.visible = 1 OR (sem.visible = 0 '
                        . 'AND sem.institut_id IN (:perm_institutes))) ');
                    $stmt->execute([
                        ':id' => $this->getId(),
                        ':semester_id' => $semester->semester_id,
                        ':semester_beginn' => $semester->beginn,
                        ':semester_ende' => $semester->ende,
                        ':perm_institutes' => $perm_institute_ids
                    ]);
                } else {
                    $stmt = DBManager::get()->prepare('SELECT seminar_id, Name, '
                        . 'VeranstaltungsNummer, sem.visible '
                        . 'FROM seminare sem '
                        . 'LEFT JOIN mvv_lvgruppe_seminar mls USING(seminar_id) '
                        . 'LEFT JOIN semester_courses ON (semester_courses.course_id = sem.Seminar_id) '
                        . 'INNER JOIN seminar_user USING(seminar_id) '
                        . 'WHERE mls.lvgruppe_id = :id '
                        . 'AND ((sem.start_time <= :semester_beginn '
                        . 'AND semester_courses.semester_id = :semester_id) '
                        . 'OR (sem.start_time BETWEEN :semester_beginn AND :semester_ende) '
                        . 'OR (sem.start_time <= :semester_beginn AND semester_courses.semester_id IS NULL)) '
                        . 'AND (sem.visible = 1 OR (sem.visible = 0 AND seminar_user.user_id = :user_id))');
                    $stmt->execute([
                        ':id' => $this->getId(),
                        ':semester_id' => $semester->semester_id,
                        ':semester_beginn' => $semester->beginn,
                        ':semester_ende' => $semester->ende,
                        ':user_id' => $only_visible
                    ]);
                }
            }
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        return [];
    }

    /**
     * Returns all courses assigned to this LV-Gruppe grouped by semesters.
     *
     * @param bool $only_visible Return only visible courses.
     * @param string $semester_id Return only this semester.
     * @return array All assigned courses grouped by semesters.
     */
    public function getAllAssignedCourses($only_visible = false, $semester_id = null)
    {
        $sem_start_times = [];

        if ($semester_id) {
            $semester = Semester::find($semester_id);
            if (!$semester) {
                return [];
            }
            $sem_start_times[$semester->id] = $semester->beginn;
        } else {
            $sem_start_times = SimpleORMapCollection::createFromArray(
                    Semester::getAll())->toGroupedArray('id', 'beginn');
            $sem_start_times = array_map(
                    function ($sem) { return $sem['beginn']; }
                    , $sem_start_times);
        }
        $visible_sql = $only_visible ? ' AND visible = 1' : '';
        $courses = [];
        $stmt = DBManager::get()->prepare('SELECT seminar_id, Name, '
                . 'VeranstaltungsNummer, visible, INTERVAL(start_time,'
                . join(',', $sem_start_times)
                . ') AS sem_number, '
                . 'IF(duration_time=-1,' . count($sem_start_times)
                . ',INTERVAL(start_time+duration_time,'
                . join(',', $sem_start_times)
                . ')) AS sem_number_end FROM seminare '
                . 'INNER JOIN mvv_lvgruppe_seminar USING(seminar_id) '
                . 'WHERE lvgruppe_id = ? ' . $visible_sql
                . ' AND start_time <= ' . end($sem_start_times)
                . ' ORDER BY sem_number DESC, Name');
        $stmt->execute([$this->getId()]);
        $sem_ids = array_keys($sem_start_times);
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $course) {
            if ($course['sem_number'] == 0) $course['sem_number'] = 1;
            for ($i = $course['sem_number']; $i <= $course['sem_number_end']; $i++) {
                $courses[$sem_ids[$i-1]][] = $course;
            }
        }
        return $courses;
    }

    /**
     * Returns all archived courses previously assigned to this LV-Gruppe.
     *
     * @return array All archived courses.
     */
    public function getArchivedCourses()
    {
        $stmt = DBManager::get()->prepare('SELECT seminar_id, name, semester '
                . 'FROM mvv_lvgruppe_seminar '
                . 'INNER JOIN archiv USING(seminar_id) '
                . 'WHERE lvgruppe_id = ? '
                . 'ORDER BY start_time ASC');
        $stmt->execute([$this->getId()]);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * Returns a default name for a new LV-Gruppe derived from a given
     * modulteil.
     *
     * @param string $modulteil_id The id of a Modulteil.
     * @return string The default name.
     */
    public static function getDefaultName($modulteil_id)
    {
        $name = '';
        /*
         * Gießen
        $modul = Modul::find($modul_id);
        if ($modul) {
            $name = $modul->responsible_institute->institute->getShortName();
            $short_name_modul = $modul->getDeskriptor(
                    $GLOBALS['MVV_MODUL_DESKRIPTOR']['SPRACHE']['default'])
                    ->bezeichnung_kurz;
            $name .= $short_name_modul ? ' ' . $short_name_modul : '';
        }
         *
         */
        // Augsburg
        $modulteil = Modulteil::findCached($modulteil_id);
        if ($modulteil) {
            $name = $modulteil->getDeskriptor()->bezeichnung;
            //$name = $name_modulteil ? ' ' . $name_modulteil : '';
        }
        return $name;
    }

    public function validate()
    {
        $ret = parent::validate();
        if ($this->isDirty()) {
            $messages = [];
            $rejected = false;

            // The name of the Fach must be longer than 4 characters
            if (mb_strlen($this->name) < 4) {
                $ret['name'] = true;
                $messages[] = _('Der Name der Lehrveranstaltungsgruppe ist zu kurz (mindestens 4 Zeichen).');
                $rejected = true;
            } else {
                if ($this->isNew()) {
                    // The name of the Lehrveranstaltungsgruppe has to be unique
                    $existing = $this->findBySql('name = ' . DBManager::get()->quote($this->name));
                    if (sizeof($existing)) {
                        $ret['name'] = true;
                        $messages[] = sprintf(_('Es existiert bereits eine Lehrveranstaltungsgruppe mit dem Namen "%s"!'),
                                $this->name);
                        $rejected = true;
                    }
                }
            }
            if ($rejected) {
                throw new InvalidValuesException(join("\n", $messages), $ret);
            }
        }
        return $ret;
    }

}