aboutsummaryrefslogtreecommitdiff
path: root/lib/activities/Activity.php
blob: b46c9b23be44da67ba33cd2d7943a70b557adc65 (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
<?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 Activity extends \SimpleORMap
{
    public
        $object_url,
        $object_route;

    private $context_object;

    const GC_MAX_DAYS = 366; // Garbage collector removes activities after 366 days

    private static $allowed_verbs = [
        'answered',
        'attempted',
        'attended',
        'completed',
        'created',
        'deleted',
        'edited',
        'experienced',
        'failed',
        'imported',
        'interacted',
        'passed',
        'shared',
        'sent',
        'voided'
    ];

    /**
     * {@inheritdoc}
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'activities';
        $config['additional_fields']['object_url'] = ['get' => 'getUrlList'];
        $config['additional_fields']['object_route'] = ['get' => 'getRoute'];

        parent::configure($config);
    }

    /**
     * return a string representation for this activity
     *
     * @return string
     */
    public function __toString()
    {
        return $this->content['content'];
    }

    /**
     * set one of the allowed verbs
     *
     * @param string $verb
     *
     * @throws \InvalidArgumentException
     */
    public function setVerb($verb)
    {
        if (is_null($verb)) {
            return;
        }

        if (in_array($verb, self::$allowed_verbs) === false) {
            throw new \InvalidArgumentException("That verb is not allowed.");
        }

        $this->content['verb'] = $verb;
    }

    /**
     * Add a url to the list of urls
     *
     * @param type $url
     * @param type $name
     */
    public function addUrl($url, $name)
    {
        $this->object_url[$url] = $name;
    }

    /**
     * Return assoc array of urls
     * [[url => description]]
     *
     * @return Array
     */
    public function getUrlList()
    {
        return $this->object_url ?: [];
    }

    /**
     * Return api route of the content object
     *
     * @return string
     */
    public function getRoute()
    {
        return $this->object_route;
    }

    public function setContextObject(Context $context)
    {
        $this->context_object = $context;
    }

    public function getContextObject()
    {
        return $this->context_object;
    }

    /**
     * Returns a format string as placeholder for the object in question
     * (in a grammatical / lexical sense)
     *
     * @return string
     */
    public function verbToText()
    {
        $translation = [
            'answered'    => _('beantwortete %s'),
            'attempted'   => _('versuchte %s'),
            'attended'    => _('nahm teil an %s'),
            'completed'   => _('beendete %s'),
            'created'     => _('erstellte %s'),
            'deleted'     => _('löschte %s'),
            'edited'      => _('bearbeitete %s'),
            'experienced' => _('erlebte %s'),
            'failed'      => _('verfehlte %s'),
            'imported'    => _('importierte %s'),
            'interacted'  => _('interagierte mit %s'),
            'passed'      => _('bestand %s'),
            'shared'      => _('teilte %s'),
            'sent'        => _('sendete %s'),
            'set'         => _('stellte %s'),
            'voided'      => _('löschte %s')
        ];

        return ($translation[$this->verb]);
    }

    /**
     * Garbage collector for the activities.
     * Removes all activites older than GC_MAX_DAYS (default: 366).
     */
    public static function doGarbageCollect()
    {
        $stmt = \DBManager::get()->prepare('DELETE FROM activities WHERE mkdate < ?');

        $stmt->execute([
            time() - self::GC_MAX_DAYS * 24 * 60 * 60]
        );

        //Expire Cache
        \StudipCacheFactory::getCache()->expire('activity/oldest_activity');
    }

    /**
     * Returns the oldest existing activity
     *
     * @return Array
     */
    public static function getOldestActivity()
    {
        $cache = \StudipCacheFactory::getCache();
        $cache_key = 'activity/oldest_activity';

        if (!$activity = unserialize($cache->read($cache_key))) {
            $activity = self::findBySQL('1 ORDER BY mkdate ASC LIMIT 1');

            if (!empty($activity)) {
                $cache->write($cache_key, serialize($activity));
            } else {
                return false;
            }
        }

        return $activity;
    }


}