aboutsummaryrefslogtreecommitdiff
path: root/lib/activities/NewsProvider.php
blob: 8f1c5f46da03b67eee175bccdd8c9c5e8729eb53 (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
<?php

/**
 * @author      Till Glöggler <tgloeggl@uos.de>
 * @author      André Klaßen <klassen@elan-ev.de>
 * @license     GPL 2 or later
 */

namespace Studip\Activity;

class NewsProvider implements ActivityProvider
{
    private function getUrlForContext($news, $activity)
    {
        switch ($activity->context) {
            case 'course':
                return [
                    \URLHelper::getUrl('dispatch.php/course/overview/?cid=' . $activity->context_id . '&contentbox_type=news&contentbox_open=' . $activity->object_id) => _('Ankündigungen in der Veranstaltung')
                ];
            break;

            case 'institute':
                return [
                    \URLHelper::getUrl('dispatch.php/institute/overview?auswahl=' . $activity->context_id) => _('Ankündigungen in der Einrichtung')
                ];
            break;

            case 'system':
                return [
                    \URLHelper::getUrl('dispatch.php/start?contentbox_type=news&contentbox_open='. $news->getId() .'#'. $news->getId()) => _('Ankündigungen auf der Startseite')
                ];
            break;

            case 'user':
                return [
                    \URLHelper::getUrl('dispatch.php/profile/?username='. get_username($activity->context_id)
                        . '&contentbox_type=news&contentbox_open='. $news->getId() .'#'. $news->getId()) => _('Ankündigungen auf der Profilseite')
                ];
            break;
        }
    }

    /**
     * posts an activity for a given notification event
     *
     * @param String $event a notification for an activity
     * @param String  $news
     */
    public static function postActivity($event, $news)
    {
        // delete any old activities for this id
        $activities = Activity::findBySql('object_id = ?', [$news->id]);

        foreach ($activities as $activity) {
            $activity->delete();
        }

        $mkdate = time();

        // iterate over every news-range and create approbriate activity
        foreach ($news->news_ranges as $range) {
            $context_id = $range->range_id;

            switch ($range->type) {
                case 'user':
                    $context = 'user';
                    break;
                case 'inst':
                case 'fak':
                    $context = 'institute';
                    break;
                case 'sem':
                    $context = 'course';
                    break;
                case 'global':
                    $context = 'system';
                    $context_id = 'system';
                    break;
            }
            if (isset($context)) {
                $activity = Activity::create(
                    [
                        'provider'    => __CLASS__,
                        'context'     => $context,
                        'context_id'  => $context_id,
                        'content'     => null,
                        'actor_type'  => 'user',         // who initiated the activity?
                        'actor_id'    => $news->user_id, // id of initiator
                        'verb'        => 'created',      // the activity type
                        'object_id'   => $news->id,      // the id of the referenced object
                        'object_type' => 'news',         // type of activity object
                        'mkdate'      => $mkdate
                    ]
                );
            }

        }
    }


    /**
     * get the details for the passed activity
     *
     * @param object $activity the activity to fill with details, passed by reference
     */
    public function getActivityDetails($activity)
    {
        $news = new \StudipNews($activity->object_id);

        // do not show unpublished news
        if ($news->date > time()) {
            return false;
        }

        $activity->content = '<b>' . htmlReady((string) $news->topic)
            .'</b><br>'. formatReady((string) $news->body);

        $url = self::getUrlForContext($news, $activity);

        $activity->object_url = $url;

        return true;
    }
    /**
     * {@inheritdoc}
     */
    public static function getLexicalField()
    {
        return _('eine Ankündigung');
    }

}