aboutsummaryrefslogtreecommitdiff
path: root/lib/activities/Stream.php
blob: 6b64eff6cfbf6dd6e430b2ebad787b7f8232a533 (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
197
198
199
200
201
202
203
204
205
206
207
208
<?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 Stream implements \ArrayAccess, \Countable, \IteratorAggregate
{
    private $activities;

    /**
     * creates a stream representing the activities for the passed contexts,
     * filter by time (if any)
     *
     * @param array $contexts All contexts that need to be considered
     * @param \Studip\Activity\Filter $filter
     *
     * @throws \InvalidArgumentException
     */
    public function __construct($contexts, Filter $filter)
    {
        if (!is_array($contexts)) {
            $contexts = [$contexts];
        }

        foreach ($contexts as $context) {
            if (!$context instanceof Context) {
                throw new \InvalidArgumentException();
            }
        }

        if (!$filter instanceof Filter) {
            throw new \InvalidArgumentException();
        }

        //fetch avaible contextes in given timespan
        $available_contexts = \DBManager::get()->fetchGroupedPairs(
            "SELECT DISTINCT context,context_id FROM activities WHERE mkdate BETWEEN ? AND ?",
            [$filter->getStartDate(), $filter->getEndDate()]);

        //fetch activities only for contextes with known activities
        $activities = array_flatten(array_values(array_filter(array_map(
            function ($context) use ($filter, $available_contexts) {
                if (isset($available_contexts[$context->getContextType()])
                    && in_array($context->getRangeId(), $available_contexts[$context->getContextType()])) {
                        return $context->getActivities($filter);
                }
            }, $contexts))
        ));

        $new_activities = [];

        foreach ($activities as $activity) {
            // generate an id for the activity, considering some basic object parameters
            $id = md5($activity->provider . $activity->content .
                    $activity->verb . $activity->object_type . $activity->mkdate);

            if (isset($new_activities[$id])) {
                $url = key($activity->object_url);
                $name = current($activity->object_url);
                next($activity->object_url);
                $new_activities[$id]->addUrl($url, $name);
            } else {
                $new_activities[$id] = $activity;
            }
        }

        // sort activites by their mkdate
        usort($new_activities, function($a, $b) {
            return $b->mkdate - $a->mkdate;
        });

        $this->activities = $new_activities;
    }

    /**
     * ArrayAccess: Check whether the given offset exists.
     *
     * @todo Add bool return type when Stud.IP requires PHP8 minimal
     */
    #[\ReturnTypeWillChange]
    public function offsetExists($offset)
    {
        return isset($this->activities[$offset]);
    }

    /**
     * ArrayAccess: Get the value at the given offset.
     *
     * @todo Add mixed return type when Stud.IP requires PHP8 minimal
     */
    #[\ReturnTypeWillChange]
    public function offsetGet($offset)
    {
        return $this->activities[$offset];
    }

    /**
     * ArrayAccess: Set the value at the given offset.
     *
     * @todo Add void return type when Stud.IP requires PHP8 minimal
     */
    #[\ReturnTypeWillChange]
    public function offsetSet($offset, $value)
    {
        $this->activities[$offset] = $value;
    }

    /**
     * ArrayAccess: unset the value at the given offset (not applicable)
     *
     * @todo Add void return type when Stud.IP requires PHP8 minimal
     */
    #[\ReturnTypeWillChange]
    public function offsetUnset($offset)
    {
        unset($this->activities[$offset]);
    }

    /**
     * IteratorAggregate
     *
     * @todo Add \Traversable return type when Stud.IP requires PHP8 minimal
     */
    #[\ReturnTypeWillChange]
    public function getIterator()
    {
        return new \ArrayIterator($this->activities);
    }

    /**
     * Countable
     *
     * @todo Add int return type when Stud.IP requires PHP8 minimal
     */
    #[ReturnTypeWillChange]
    public function count()
    {
        return count($this->activities);
    }

    /**
     * return representation of the current stream as an array
     *
     * @return array
     */
    public function toArray()
    {
        $activities = [];

        foreach ($this as $key => $activity) {
            $activities[$key] = $activity->toArray();

            // add i18n auto generated title prefix
            $title = '';

            // $class       = '\\Studip\\Activity\\' . ucfirst($activity->provider) . 'Provider';
            $class       = $activity->provider;
            $object_text = $class::getLexicalField();

            if (in_array($activity->actor_id, ['____%system%____', 'system']) !== false) {
                $actor = _('Stud.IP');
            } elseif ($activity->actor_type === 'anonymous') {
                $actor = _('Anonym');
            } else {
                $actor = get_fullname($activity->actor_id);
            }
            $context_name = $activity->getContextObject()->getContextFullname();

            switch ($activity->context) {
                case 'course':
                    $title = $actor .' '
                        . sprintf($activity->verbToText(),
                            $object_text . sprintf(_(' im Kurs "%s"'), $context_name)
                        );
                break;

                case 'institute':
                    $title = $actor .' '
                        . sprintf($activity->verbToText(),
                            $object_text . sprintf(_(' in der Einrichtung "%s"'), $context_name)
                        );
                break;

                case 'system':
                    $title = $actor .' '
                        . sprintf($activity->verbToText(), _('allen')) .' '
                        . $object_text;
                break;

                case 'user':
                    $title = $actor .' '
                        . sprintf($activity->verbToText(), $context_name) .' '
                        . $object_text;
                break;

            }

            $activities[$key]['title'] = $title;
        }

        return $activities;
    }
}