aboutsummaryrefslogtreecommitdiff
path: root/lib/activities/DocumentsProvider.php
blob: 0a770deea5924297e4b2ee9f3fb98c9bfc5fc177 (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
<?php

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

namespace Studip\Activity;

class DocumentsProvider implements ActivityProvider
{

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

        $document = \FileRef::find($activity->object_id);

        // check, if current observer has access to document
        if (!$document || !$activity->getContextObject() || !$document->folder->getTypedFolder()->isFileDownloadable($document, $activity->getContextObject()->getObserver()->id)) {
            return false;
        }

        if ($activity->context == "course") {
            $url = \URLHelper::getUrl("dispatch.php/course/files/flat?cid={$activity->context_id}");
            $route = \URLHelper::getURL('api.php/file/' . $activity->object_id, NULL, true);

            $activity->object_url = [
                $url => _('Zum Dateibereich der Veranstaltung')
            ];
        } elseif ($activity->context == "institute") {
            $url = \URLHelper::getUrl("dispatch.php/institute/files/flat?cid={$activity->context_id}");
            $route= null;

            $activity->object_url = [
                $url => _('Zum Dateibereich der Einrichtung')
            ];
        }

        $activity->object_route = $route;

        return true;
    }

    /**
     * posts an activity for a given notification event
     *
     * @param String $event a notification for an activity
     * @param \FileRef  $document information which a relevant for the activity
     */
    public static function postActivity($event, $file_ref)
    {
        $user_id = $file_ref->user_id;
        $file_name = $file_ref->name;
        $course_id = $file_ref->folder->range_id;
        $file_id = $file_ref->id;

        $type     = $file_ref->folder->range_type;
        if ($type == 'course') {
            $course = \Course::find($course_id);
        } elseif ($type == 'institute') {
            $course = \Institute::find($course_id);
        }

        if (!isset($course)) {
            return;
        }

        if (in_array($event, ['FileRefDidCreate'])) {
            $verb = 'created';
            if ($type == 'course') {
                $summary = _('Die Datei %s wurde von %s in der Veranstaltung "%s" hochgeladen.');
            } else {
                $summary = _('Die Datei %s wurde von %s in der Einrichtung "%s" hochgeladen.');
            }
            $summary = sprintf($summary,$file_name, get_fullname($user_id) ,$course->name);
            $mkdate = $file_ref->mkdate;
        } elseif (in_array($event, ['FileRefDidUpdate'])) {
            $verb = 'edited';
            if ($type == 'course') {
                $summary = _('Die Datei %s wurde von %s in der Veranstaltung "%s" aktualisiert.');
            } else {
                $summary = _('Die Datei %s wurde von %s in der Einrichtung "%s" aktualisiert.');
            }
            $summary = sprintf($summary,$file_name, get_fullname($user_id), $course->name);
            $mkdate = $file_ref->chdate;
        } elseif (in_array($event, ['FileRefDidDelete'])) {
            $verb = 'voided';
            if ($type == 'course') {
                $summary = _('Die Datei %s wurde von %s in der Veranstaltung "%s" gelöscht.');
            } else {
                $summary = _('Die Datei %s wurde von %s in der Einrichtung "%s" gelöscht.');
            }
            $summary = sprintf($summary,$file_name, get_fullname($user_id), $course->name);
            $mkdate = $file_ref->chdate;
        } else {
            return;
        }

        if (isset($verb)) {
            $activity = Activity::create(
                [
                    'provider'     => __CLASS__,
                    'context'      => $type,
                    'context_id'   => $course_id,
                    'content'      => $summary,
                    'actor_type'   => 'user',      // who initiated the activity?
                    'actor_id'     => $user_id,    // id of initiator
                    'verb'         => $verb,       // the activity type
                    'object_id'    => $file_id,    // the id of the referenced object
                    'object_type'  => 'documents', // type of activity object
                    'mkdate'       =>  $mkdate
                ]
            );
        }
    }

    /**
     * {@inheritdoc}
     */
    public static function getLexicalField()
    {
        return _('eine Datei');
    }
}