aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ForumActivity.php
blob: 20ebeb699edc6fffb6a265318045a01fee32dc92 (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
<?php
/**
 * File - description
 *
 * 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     https://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 */

class ForumActivity
{
    /**
     * Post activity for new forum post
     *
     * @param string $event
     * @param string $topic_id
     * @param array $post
     */
    public static function newEntry($event, $topic_id, $post)
    {
        $verb = $post['depth'] == 3 ? 'answered' : 'created';

        if ($verb == 'created') {
            if ($post['depth'] == 1) {
                $summary = _('%s hat im Forum der Veranstaltung "%s" einen Bereich erstellt.');
            } else {
                $summary = _('%s hat im Forum der Veranstaltung "%s" ein Thema erstellt.');
            }
        } else {
            $summary = _('%s hat im Forum der Veranstaltung "%s" auf ein Thema geantwortet.');
        }

        self::post($post, $verb, $summary);
    }

    /**
     * Post activity for updating a forum post
     * @param string $event
     * @param string $topic_id
     * @param string $post
     */
    public static function updateEntry($event, $topic_id, $post)
    {
        $summary = _('%s hat im Forum der Veranstaltung "%s" einen Beitrag editiert.');

        if ($post['user_id'] == $GLOBALS['user']->id) {
            $content = sprintf(
                _('%s hat seinen eigenen Beitrag vom %s editiert.'),
                self::getPostUsername($post),
                date('d.m.y, H:i', $post['mkdate'])
            );
        } else {
            $content = sprintf(
                _('%s hat den Beitrag von %s vom %s editiert.'),
                get_fullname($GLOBALS['user']->id),
                self::getPostUsername($post),
                date('d.m.y, H:i', $post['mkdate'])
            );
        }

        self::post($post, 'edited', $summary, $content);
    }

    /**
     * Post activity for deleting a forum post
     * $param string $event
     * @param string $topic_id
     * @param string $post
     */
    public static function deleteEntry($event, $topic_id, $post)
    {
        // Remove all previous activities for the post
        Studip\Activity\Activity::deleteBySQL(
            "provider = ? AND object_type = 'forum' AND object_id = ?",
            [Studip\Activity\ForumProvider::class, $topic_id]
        );

        $summary = _('%s hat im Forum der Veranstaltung "%s" einen Beitrag gelöscht.');

        if ($post['user_id'] == $GLOBALS['user']->id) {
            $content = sprintf(
                _('%s hat seinen Beitrag vom %s gelöscht.'),
                self::getPostUsername($post),
                date('d.m.y, H:i', $post['mkdate'])
            );
        } else {
            $content = sprintf(
                _('%s hat den Beitrag von %s vom %s gelöscht.'),
                get_fullname($GLOBALS['user']->id),
                self::getPostUsername($post),
                date('d.m.y, H:i', $post['mkdate'])
            );
        }

        self::post($post, 'deleted', $summary, $content);
    }

    private static function post($post, $verb, $summary, $content = null)
    {
        // skip system-created entries like "Allgemeine Diskussionen"
        if (!$post['user_id']) {
            return;
        }

        $range_id = $post['seminar_id'];
        $type     = get_object_type($range_id);

        $obj = get_object_name($range_id, $type);

        $data = [
            'provider'     => 'Studip\Activity\ForumProvider',
            'context'      => $type === 'sem' ? 'course' : 'institute',
            'context_id'   => $post['seminar_id'],
            'content'      => null,
            'actor_type'   => 'user',             // who initiated the activity?
            'actor_id'     => $post['user_id'],   // id of initiator
            'verb'         => $verb,              // the activity type
            'object_id'    => $post['topic_id'],  // the id of the referenced object
            'object_type'  => 'forum',            // type of activity object
            'mkdate'       => $post['mkdate'] ?: time()
        ];

        if ($post['anonymous']) {
            $data['actor_type'] = 'anonymous';
            $data['actor_id']   = '';
        }

        $activity = Studip\Activity\Activity::create($data);
    }

    /**
     * Returns the poster's name for a forum post.
     *
     * @param array $post
     * @return string
     */
    private static function getPostUsername($post)
    {
        if ($post['anonymous']) {
            return _('Anonym');
        }

        return get_fullname($post['user_id']);
    }
}