aboutsummaryrefslogtreecommitdiff
path: root/lib/activities/Context.php
blob: 3f84452cef3d8941077964d871677395adc17f10 (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
<?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
{
    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);
        $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;
                    }
                }
            },
            'context = ? AND context_id = ?  AND mkdate >= ? AND mkdate <= ? ORDER BY mkdate DESC'
            ,
            [$this->getContextType(), $this->getRangeId(), $filter->getStartDate(), $filter->getEndDate()]);
        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;
    }
}