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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
<?php
/**
* Context.php - Helper class to handle the currently selected Stud.IP-object
*
* Usage:
* Context::getId()
* -> to retrieve id of current Stud.IP-object
*
* Context::isCours()
* -> check if context is of type course/seminar
*
* Context::get()
* -> get sorm-object for current context
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Till Glöggler <tgloeggl@uos.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class Context
{
/**
* Constants to check for type of context
*/
const COURSE = 'course';
const INSTITUTE = 'institute';
const USER = 'user';
/**
* storage for the current context
*/
private static $context = null;
private static $type = null;
/**
* Load context for passed id.
*
* @param string $id seminar_id, institute_id or user_id
*/
private static function loadContext($id)
{
$possible_sorms = ['Course', 'Institute'];
foreach($possible_sorms as $sorm) {
if ($context = $sorm::find($id)) {
self::$context = $context;
self::$type = strtolower($sorm);
}
}
}
/**
* Return sorm-object of currently active Stud.IP object
*
* @return Course|Institute sorm-object of current context
*/
public static function get()
{
return self::$context;
}
/**
* Return id of currently active Stud.IP object
*
* @return string md5-hash
*/
public static function getId()
{
if (!self::$context) {
return null;
}
return self::$context->getId();
}
/**
* Return type of currently active Stud.IP object. To easily check what type
* of object that is, check the return value against
* Context::COURSE, Context::INSTITUTE or Context::USER
*
* @return mixed one of Context::COURSE, Context::INSTITUTE or Context::USER
*/
public static function getType()
{
return self::$type;
}
/**
* Checks if current context is a seminar
*
* @return bool
*/
public static function isCourse()
{
return self::getType() === self::COURSE;
}
/**
* Checks if current context is an institute
*
* @return bool
*/
public static function isInstitute()
{
return self::getType() === self::INSTITUTE;
}
/**
* Checks if current context is an user
*
* @return bool
*/
public static function isUser()
{
return self::getType() === self::USER;
}
/**
* Get string representation of object-type fpr legacy support
*
* @deprecated
*
* @return string returns 'sem' or 'inst'
*/
public static function getClass()
{
switch (self::getType()) {
case self::COURSE:
return 'sem';
case self::INSTITUTE:
return 'inst';
}
throw new UnexpectedValueException('Invalid context type');
}
/**
* Get SemClass-number (kind of) for current context. Only works for
* seminar or institute
*
* @deprecated
*
* @return int
*/
public static function getArtNum()
{
if (self::isCourse()) {
return self::get()->status;
}
if (self::isInstitute()) {
return self::get()->type;
}
throw new UnexpectedValueException('Invalid context type');
}
/**
* Return human readable text for current context, excluding user
*
* @deprecated
*
* @return string
*/
public static function getTypeName()
{
switch (self::getType()) {
case self::COURSE:
return _('Veranstaltung');
case self::INSTITUTE:
return _('Einrichtung');
}
throw new UnexpectedValueException('Invalid context type');
}
/**
* Get Fullname of current context, to use it in the page-title
*
* @return string or null if no context is available
*/
public static function getHeaderLine()
{
if (!self::$context) {
return null;
}
return self::get()->getFullname();
}
/**
* Set the context to the object denoted by the passed id.
* Usually there is no need to call this on your own, since seminar_open
* already does this for you!
*
* @param string $id
*
* @throws AccessDeniedException
*/
public static function set($id)
{
global $perm, $auth;
self::close();
self::loadContext($id);
if (!self::getType()) {
return;
}
if (self::isCourse() || self::isInstitute()) {
$GLOBALS['SessionSeminar'] = $id;
}
URLHelper::addLinkParam('cid', $GLOBALS['SessionSeminar']);
if (self::isCourse()) {
$course = self::get();
Seminar::setInstance(new Seminar($course));
// check if current user can access the object
if (!$perm->get_studip_perm($course['Seminar_id'])) {
if ($course['lesezugriff'] > 0 || !Config::get()->ENABLE_FREE_ACCESS) {
// redirect to login page if user is not logged in
$auth->login_if($auth->auth['uid'] === 'nobody');
if (!$perm->get_studip_perm($course['Seminar_id'])) {
throw new AccessDeniedException();
}
}
}
// if the aux data is forced for this seminar forward all user that havent made an input to this site
if ($course['aux_lock_rule_forced']
&& !$perm->have_studip_perm('tutor', $course['Seminar_id'])
&& !match_route('dispatch.php/course/members/additional_input')
&& !match_route('dispatch.php/course/change_view/*'))
{
$count = DatafieldEntryModel::countBySql(
'range_id = ? AND sec_range_id = ?',
[$GLOBALS['user']->id, $course['Seminar_id']]
);
if (!$count) {
header('Location: ' . URLHelper::getURL('dispatch.php/course/members/additional_input'));
page_close();
die;
}
}
} else if (self::isInstitute()) {
// check if current user can access the object
$no_access = (!Config::get()->ENABLE_FREE_ACCESS ||
(Config::get()->ENABLE_FREE_ACCESS == 'courses_only'))
&& !$perm->have_perm('user');
if ($no_access) {
// redirect to login page if user is not logged in
$auth->login_if($auth->auth['uid'] === 'nobody');
if (!$perm->have_perm('user')) {
throw new AccessDeniedException();
}
}
}
}
/**
* "Close" the current context
*/
public static function close()
{
self::$context = null;
self::$type = null;
URLHelper::removeLinkParam('cid');
unset($GLOBALS['SessionSeminar']);
}
}
|