aboutsummaryrefslogtreecommitdiff
path: root/lib/modules/CoreOverview.php
blob: 5550ed3f9731642e71c8c433b63a0f3043452127 (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
<?php

/*
 *  Copyright (c) 2012  Rasmus Fuhse <fuhse@data-quest.de>
 *
 *  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.
 */

class CoreOverview extends CorePlugin implements StudipModuleExtended
{
    /**
     * {@inheritdoc}
     */
    public function getIconNavigation($course_id, $last_visit, $user_id)
    {
        $sql = "SELECT COUNT(nw.news_id) AS count,
                       COUNT(IF((nw.chdate > IFNULL(b.visitdate, :threshold) AND nw.user_id !=:user_id), nw.news_id, NULL)) AS neue
                FROM news_range AS a
                LEFT JOIN news AS nw
                  ON a.news_id = nw.news_id
                     AND UNIX_TIMESTAMP() BETWEEN date AND date + expire
                LEFT JOIN object_user_visits AS b
                  ON b.object_id = a.news_id
                     AND b.user_id = :user_id
                     AND b.plugin_id = :plugin_id
                WHERE a.range_id = :course_id
                GROUP BY a.range_id";

        $statement = DBManager::get()->prepare($sql);
        $statement->bindValue(':user_id', $user_id);
        $statement->bindValue(':course_id', $course_id);
        $statement->bindValue(':threshold', object_get_visit_threshold());
        $statement->bindValue(':plugin_id', $this->getPluginId());
        $statement->execute();
        $result = $statement->fetch(PDO::FETCH_ASSOC);
        if (!$result) {
            return null;
        }

        $base_url = get_object_type($course_id, ['sem']) ? 'course' : 'institute';
        $nav = new Navigation('Ankündigungen', "dispatch.php/$base_url/overview");
        if ($result['neue']) {
            $nav->setURL('?new_news=true');
            $nav->setImage(Icon::create('news', Icon::ROLE_ATTENTION, [
                'title' => sprintf(
                    ngettext(
                        '%1$d Ankündigung, %2$d neue',
                        '%1$d Ankündigungen, %2$d neue',
                        $result['count']
                    ),
                    $result['count'],
                    $result['neue']
                )
            ]));
            $nav->setBadgeNumber($result['neue']);
        } elseif ($result['count']) {
            $nav->setImage(Icon::create('news'));
            $nav->setLinkAttributes([
                'title' => sprintf(
                    ngettext(
                        '%d Ankündigung',
                        '%d Ankündigungen',
                        $result['count']
                    ),
                    $result['count']
                )
            ]);
        }
        return $nav;
    }

    public function getManyIconNavigation(array $course_ids, ?string $user_id = null): array
    {
        $sql = "SELECT news_r.range_id,
                       COUNT(news.news_id) AS count,
                       COUNT(IF((news.chdate > IFNULL(b.visitdate, :threshold) AND news.user_id !=:user_id), news.news_id, NULL)) AS neue
                FROM news_range AS news_r
                JOIN news
                  ON news_r.news_id = news.news_id
                     AND UNIX_TIMESTAMP() BETWEEN date AND date + expire
                LEFT JOIN object_user_visits AS b
                  ON b.object_id = news_r.news_id
                     AND b.user_id = :user_id
                     AND b.plugin_id = :plugin_id
                WHERE news_r.range_id IN (:course_ids)
                GROUP BY news_r.range_id";
        $results = DBManager::get()->fetchAll($sql, [
            ':user_id' => $user_id,
            ':course_ids' => $course_ids,
            ':threshold' => object_get_visit_threshold(),
            ':plugin_id' => $this->getPluginId(),
        ]);

        $navs = [];
        foreach ($results as $result) {
            $nav = new Navigation(_('Ankündigungen'), '');
            if ($result['neue']) {
                $nav->setURL('?new_news=true');
                $nav->setImage(Icon::create('news', Icon::ROLE_ATTENTION));
                $nav->setLinkAttributes([
                    'title' => sprintf(
                        ngettext(
                            '%1$d Ankündigung, %2$d neue',
                            '%1$d Ankündigungen, %2$d neue',
                            $result['count']
                        ),
                        $result['count'],
                        $result['neue']
                    )
                ]);
                $nav->setBadgeNumber($result['neue']);
            } elseif ($result['count']) {
                $nav->setImage(Icon::create('news'));
                $nav->setLinkAttributes([
                    'title' => sprintf(
                        ngettext(
                            '%d Ankündigung',
                            '%d Ankündigungen',
                            $result['count']
                        ),
                        $result['count']
                    )
                ]);
            }
            $navs[$result['range_id']] = $nav;
        }
        return $navs;
    }

    /**
     * {@inheritdoc}
     */
    public function getTabNavigation($course_id)
    {
        $object_type = get_object_type($course_id, ['sem', 'inst']);
        if ($object_type === 'sem') {
            $course = Course::find($course_id);
            $sem_class = $GLOBALS['SEM_CLASS'][$GLOBALS['SEM_TYPE'][$course->status]['class']] ?: SemClass::getDefaultSemClass();
        } else {
            $institute = Institute::find($course_id);
            $sem_class = SemClass::getDefaultInstituteClass($institute->type);
        }

        $navigation = new Navigation(_('Übersicht'));
        $navigation->setImage(Icon::create('seminar', Icon::ROLE_INFO_ALT));
        $navigation->setActiveImage(Icon::create('seminar', Icon::ROLE_INFO));
        if ($object_type !== 'sem') {
            $navigation->addSubNavigation('info', new Navigation(_('Kurzinfo'), 'dispatch.php/institute/overview'));
            $range_tree_node = RangeTreeNode::findOneByStudip_object_id($course_id);
            if ($range_tree_node) {
                $navigation->addSubNavigation(
                    'courses',
                    new Navigation(_('Veranstaltungen'),
                        'dispatch.php/search/courses',
                        [
                            'node_id' => 'RangeTreeNode_' . $range_tree_node->id,
                            'type' => 'rangetree'
                        ]
                    )
                );
                $navigation->addSubNavigation('schedule', new Navigation(_('Veranstaltungs-Stundenplan'), 'dispatch.php/institute/schedule/index/' . $course_id));
            }
            if ($GLOBALS['perm']->have_studip_perm('admin', $course_id)) {
                $navigation->addSubNavigation('admin', new Navigation(_('Administration der Einrichtung'), 'dispatch.php/institute/basicdata/index?new_inst=TRUE'));
            }
        } else {
            $navigation->addSubNavigation('info', new Navigation(_('Kurzinfo'), 'dispatch.php/course/overview'));
            if (!$sem_class['studygroup_mode']) {
                $navigation->addSubNavigation('details', new Navigation(_('Details'), 'dispatch.php/course/details/'));
            }
        }
        return ['main' => $navigation];
    }

    /**
     * {@inheritdoc}
     */
    public function getMetadata()
    {
        return [
            'displayname' => _('Übersicht'),
            'summary' => _('Ankündigungen, Termine, Fragebögen & Details'),
            'icon' => Icon::create('home2', Icon::ROLE_INFO),
            'icon_clickable' => Icon::create('home2')
        ];
    }

    public function getInfoTemplate($course_id)
    {
        // TODO: Implement getInfoTemplate() method.
        return null;
    }
}