-
Notifications
You must be signed in to change notification settings - Fork 20
/
ApiEventsClient.php
176 lines (146 loc) · 5.26 KB
/
ApiEventsClient.php
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
<?php
use Opencast\Models\OCConfig;
class ApiEventsClient extends OCRestClient
{
public static $me;
public $serviceName = 'ApiEvents';
public function __construct($config_id = 1)
{
if ($config = OCConfig::getConfigForService('apievents', $config_id)) {
parent::__construct($config);
} else {
throw new Exception (_('Die Konfiguration wurde nicht korrekt angegeben'));
}
}
/**
* [getEpisode description]
* @param [type] $episode_id [description]
* @return [type] [description]
*/
public function getEpisode($episode_id, $with_publications = false)
{
list($data, $code) = $this->getJSON('/' . $episode_id . '?withpublications=' . json_encode($with_publications), [], true, true);
return [$code, $data];
}
/**
* getEpisodes() - retrieves episode metadata for a given series identifier
* from connected Opencast
*
* @param string series_id Identifier for a Series
*
* @return array response of episodes
*/
public function getEpisodes($series_id, $refresh = false)
{
$cache = StudipCacheFactory::getCache();
$cache_key = 'oc_episodesforseries/' . $series_id;
$episodes = $cache->read($cache_key);
if ($refresh || $episodes === false || $GLOBALS['perm']->have_perm('tutor')) {
$service_url = '/?sign=false&withacl=false&withmetadata=false&withscheduling=false&withpublications=true&filter=is_part_of:'
. $series_id . '&sort=&limit=0&offset=0';
if ($episodes = $this->getJSON($service_url)) {
foreach ($episodes as $key => $val) {
$episodes[$key]->id = $val->identifier;
}
$cache->write($cache_key, serialize($episodes), 7200);
return $episodes ?: [];
} else {
return [];
}
} else {
return unserialize($episodes) ?: [];
}
}
public function getAclForEpisode($series_id, $episode_id)
{
static $acl;
if (!$acl[$series_id]) {
$params = [
'withacl' => 'true',
'filter' => sprintf(
'is_part_of:%s,status:EVENTS.EVENTS.STATUS.PROCESSED',
$series_id
)
];
$data = $this->getJSON('?' . http_build_query($params));
if (is_array($data)) foreach ($data as $episode) {
$acl[$series_id][$episode->identifier] = $episode->acl;
}
}
return $acl[$series_id][$episode_id];
}
public function getACL($episode_id)
{
return json_decode(json_encode($this->getJSON('/' . $episode_id . '/acl')), true);
}
public function getBySeries($series_id, $params = [])
{
$events = $this->getJSON('/?filter=is_part_of:' .
$series_id . ',status:EVENTS.EVENTS.STATUS.PROCESSED', $params);
return array_reduce($events, function ($events, $event) {
$events[$event->identifier] = $event;
return $events;
}, []);
}
public function getAllScheduledEvents()
{
static $events;
if (!$events) {
$params = [
'filter' => 'status:EVENTS.EVENTS.STATUS.SCHEDULED',
];
$data = $this->getJSON('?' . http_build_query($params));
if (is_array($data)) foreach ($data as $event) {
$events[$event->identifier] = $event;
}
}
return $events;
}
public function getVisibilityForEpisode($series_id, $episode_id, $course_id = null)
{
if (is_null($course_id)) {
$course_id = Context::getId();
}
$acls = self::getAclForEpisode($series_id, $episode_id);
$vis_conf = !is_null(CourseConfig::get($course_id)->COURSE_HIDE_EPISODES)
? boolval(CourseConfig::get($course_id)->COURSE_HIDE_EPISODES)
: \Config::get()->OPENCAST_HIDE_EPISODES;
$default = $vis_conf
? 'invisible'
: 'visible';
if (empty($acls)) {
OCModel::setVisibilityForEpisode($course_id, $episode_id, $default);
return $default;
}
// check, if the video is free for all
foreach ($acls as $acl) {
if ($acl->role == 'ROLE_ANONYMOUS'
&& $acl->action == 'read'
&& $acl->allow == true
) {
return 'free';
}
}
// check, if the video is free for course
foreach ($acls as $acl) {
if ($acl->role == $course_id . '_Learner'
&& $acl->action == 'read'
&& $acl->allow == true
) {
return 'visible';
}
}
// check, if the video is free for lecturers
foreach ($acls as $acl) {
if ($acl->role == $course_id . '_Instructor'
&& $acl->action == 'read'
&& $acl->allow == true
) {
return 'invisible';
}
}
// nothing found, return default visibility
OCModel::setVisibilityForEpisode($course_id, $episode_id, $default);
return $default;
}
}