aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/calendar/CalendarInstscheduleModel.php
blob: efb06de0d067b35c86e45fc1929cd6569156d7ec (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
<?php
# Lifter010: TODO

/*
 * This class is the model for the institute-calendar for seminars
 *
 * 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      Till Glöggler <tgloeggl@uos.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */

define('DEFAULT_COLOR_SEM', 2);
define('DEFAULT_COLOR_NEW', 3);
define('DEFAULT_COLOR_VIRTUAL', 1);

/**
 * Pseudo-namespace containing helper methods for the calendar of institutes.
 *
 * @since      2.0
 */
class CalendarInstscheduleModel
{

    /**
     * Returns a schedule entry for a course
     *
     * @param string  $seminar_id  the ID of the course
     * @param string  $user_id     the ID of the user
     * @param string  $cycle_id    optional; if given, specifies the ID of the entry
     * @return array  an array containing the properties of the entry
     */
    static function getSeminarEntry($seminar_id, $user_id, $cycle_id = false)
    {
        $ret = [];

        $sem = new Seminar($seminar_id);
        foreach ($sem->getCycles() as $cycle) {
            if (!$cycle_id || $cycle->getMetaDateID() == $cycle_id) {
                $entry = [];

                $entry['id'] = $seminar_id;
                $entry['cycle_id'] = $cycle->getMetaDateId();
                $entry['start_formatted'] = sprintf("%02d", $cycle->getStartStunde()) .':'
                    . sprintf("%02d", $cycle->getStartMinute());
                $entry['end_formatted'] = sprintf("%02d", $cycle->getEndStunde()) .':'
                    . sprintf("%02d", $cycle->getEndMinute());

                $entry['start']   = ((int)$cycle->getStartStunde() * 100) + ($cycle->getStartMinute());
                $entry['end']     = ((int)$cycle->getEndStunde() * 100) + ($cycle->getEndMinute());
                $entry['day']     = $cycle->getDay();
                $entry['content'] = $sem->getNumber() . ' ' . $sem->getName();
                $entry['url']     = URLHelper::getLink('dispatch.php/calendar/instschedule/entry/' . $seminar_id
                                  . '/' . $cycle->getMetaDateId());
                $entry['onClick'] = "function(id) { STUDIP.Instschedule.showSeminarDetails('$seminar_id', '"
                                  . $cycle->getMetaDateId() ."'); }";

                $entry['title']   = '';
                $ret[] = $entry;
            }
        }

        return $ret;
    }


    /**
     * Returns an array of CalendarColumn's, containing the seminar-entries
     * for the passed user (in the passed semester belonging to the passed institute)
     * The start- and end-hour are used to constrain the returned
     * entries to the passed time-period. The passed days constrain the entries
     * to these.
     *
     * @param string  $user_id       the ID of the user
     * @param array   $semester      an array containing the "beginn" of the semester
     * @param int     $start_hour    the start hour
     * @param int     $end_hour      the end hour
     * @param string  $institute_id  the ID of the institute
     * @param array   $days          the days to be displayed
     *
     * @return array  an array containing the entries
     */
    static function getInstituteEntries($user_id, $semester, $start_hour, $end_hour, $institute_id, $days)
    {
        // fetch seminar-entries, show invisible seminars if the user has enough perms
        $visibility_perms = $GLOBALS['perm']->have_perm(Config::get()->SEM_VISIBILITY_PERM);

        $inst_ids = [];
        $institut = new Institute($institute_id);

        if (!$institut->isFaculty() || $GLOBALS['user']->cfg->MY_INSTITUTES_INCLUDE_CHILDREN) {
            // If the institute is not a faculty or the child insts are included,
            // pick the institute IDs of the faculty/institute and of all sub-institutes.
            $inst_ids[] = $institute_id;
            if ($institut->isFaculty()) {
                foreach ($institut->sub_institutes->pluck("Institut_id") as $institut_id) {
                    $inst_ids[] = $institut_id;
                }
            }
        } else {
            // If the institute is a faculty and the child insts are not included,
            // pick only the institute id of the faculty:
            $inst_ids[] = $institute_id;
        }

        $stmt = DBManager::get()->prepare("SELECT * FROM seminare
            LEFT JOIN seminar_inst ON (seminare.Seminar_id = seminar_inst.seminar_id)
            LEFT JOIN semester_courses ON (semester_courses.course_id = seminare.Seminar_id)
            WHERE seminar_inst.institut_id IN (:institute)
                AND (start_time <= :begin AND (semester_courses.semester_id IS NULL OR semester_courses.semester_id = :semester_id))
                     "
                    . (!$visibility_perms ? " AND visible='1'" : ""));

        $stmt->bindValue(':begin', $semester['beginn']);
        $stmt->bindValue(':semester_id', $semester['id']);
        $stmt->bindValue(':institute', $inst_ids, StudipPDO::PARAM_ARRAY);
        $stmt->execute();

        while ($entry = $stmt->fetch()) {
            $seminars[$entry['Seminar_id']] = $entry;
        }

        if (is_array($seminars)) foreach ($seminars as $data) {
            $entries = self::getSeminarEntry($data['Seminar_id'], $user_id);

            foreach ($entries as $entry) {
                unset($entry['url']);
                $entry['onClick'] = 'function(id) { STUDIP.Instschedule.showInstituteDetails(id); }';
                $entry['visible'] = 1;

                if (($entry['start'] >= $start_hour * 100 && $entry['start'] <= $end_hour * 100
                    || $entry['end'] >= $start_hour * 100 && $entry['end'] <= $end_hour * 100)) {

                    $entry['color'] = DEFAULT_COLOR_SEM;

                    $day_number = ($entry['day'] + 6) % 7;
                    if (!isset($ret[$day_number])) {
                        $ret[$day_number] = CalendarColumn::create($day_number);
                    }
                    $ret[$day_number]->addEntry($entry);
                }
            }
        }

        return CalendarScheduleModel::addDayChooser($ret, $days, 'instschedule');
    }
}