aboutsummaryrefslogtreecommitdiff
path: root/lib/activities/Context.php
blob: 170552872662ad8caf3b46c927b2d5b67007fe9c (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
<?php

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

namespace Studip\Activity;

abstract class Context
{
    public static $objectTypes = [
        'documents',
        'message',
        'news',
        'participants',
        'schedule',
        'wiki',
        'courseware',
        'forum'
    ];

    public static $contexTypes = [
        'system',
        'course',
        'institute',
        'user'
    ];

    protected
        $provider,
        $observer;


    /**
     * return array, listing all active providers in this context
     *
     * @return array
     */
    abstract protected function getProvider();

    /**
     * get id denoting the context (user_id, course_id, institute_id, ...)
     *
     * @return string
     */
    abstract public function getRangeId();

    /**
     * get type of context (f.e. user, system, course, institute, ...)
     *
     * @return string
     */
    abstract public function getContextType();

    /**
     * get type of context (f.e. user, system, course, institute, ...)
     *
     * @return string
     */
    abstract public function getContextFullname($format = 'default');

    /**
     * Return user for who wants to watch his and related activities
     *
     * @return object  a user object
     */
    public function getObserver()
    {
        return $this->observer;
    }

    /**
     * get list of activities as array for the current context
     *
     * @param \Studip\Activity\Filter $filter
     *
     * @return array
     */
    public function getActivities(Filter $filter)
    {
        $providers = $this->filterProvider($this->getProvider(), $filter);

        $query = 'context = ? AND context_id = ? AND mkdate >= ? AND mkdate <= ? ORDER BY mkdate DESC';
        $params = [$this->getContextType(), $this->getRangeId(), $filter->getStartDate(), $filter->getEndDate()];

        if ($filter->getContext() !== null && $filter->getContextId() !== null) {
            // if a single context is provided and this context does not match, do not return any activites
            if ($this->getRangeId() != $filter->getContextId()) {
                return null;
            }

            $params = [$filter->getContext(), $filter->getContextId(), $filter->getStartDate(), $filter->getEndDate()];
        }

        if(\in_array($filter->getObjectType(), $this::$objectTypes)) {
            $query = 'object_type = ? AND ' . $query;
            \array_unshift($params, $filter->getObjectType());

            //Object ID Filter only available when object type is set
            if($filter->getObjectId() !== null && \strlen($filter->getObjectId()) > 0) {
                $query = 'object_id = ? AND ' . $query;
                \array_unshift($params, $filter->getObjectId());
            }
        }
        $activities = Activity::findAndMapBySQL(
            function ($activity) use ($providers) {
                if (isset($providers[$activity->provider])) {                        // provider is available
                    $activity->setContextObject($this);
                    if ($providers[$activity->provider]->getActivityDetails($activity)) {
                        return $activity;
                    }
                }
            },
            $query,
            $params
        );
        return array_filter($activities);
    }

    /**
     * Add a provider to this context
     *
     * @param string $provider    the name for the provider
     * @param string $class_name  the class that belongs to the provider
     */
    protected function addProvider($class_name)
    {
        $reflectionClass = new \ReflectionClass($class_name);
        $this->provider[$class_name] =  $reflectionClass->newInstanceArgs();
    }

    /**
     * Filter the passed the providers to match the passed filter
     *
     * @param type $providers
     * @param \Studip\Activity\Filter $filter
     * @return type
     */
    protected function filterProvider($providers, Filter $filter)
    {
        $filtered_providers = [];

        if (empty($filter->getType())) {
            $filtered_providers = $providers;
        } else {
            foreach ($providers as $provider) {
                $ctype = $this->getContextType();
                $filtered_classes = $filter->getType()->$ctype;

                if (is_array($filtered_classes)) {
                    foreach ($filtered_classes as $class) {
                        $iclass = 'Studip\\Activity\\' .ucfirst($class) .'Provider';
                        if ($provider instanceof $iclass) {
                            $filtered_providers[$iclass] =  $provider;
                        }
                    }
                }
            }
        }

        return $filtered_providers;
    }
}