ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjCourseAccess.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=0);
20
27{
28 protected static bool $using_code = false;
30
32 protected ilObjUser $user;
33 protected ilLanguage $lng;
35
36 public function __construct()
37 {
38 global $DIC;
39
40 $this->access = $DIC->access();
41 $this->user = $DIC->user();
42 $this->lng = $DIC->language();
43 $this->rbacSystem = $DIC->rbac()->system();
44 }
45
50 public static function getConditionOperators(): array
51 {
52 return array(
55 );
56 }
57
61 public static function checkCondition(
62 int $a_trigger_obj_id,
63 string $a_operator,
64 string $a_value,
65 int $a_usr_id
66 ): bool {
67 switch ($a_operator) {
69 return ilCourseParticipants::_hasPassed($a_trigger_obj_id, $a_usr_id);
70
72 $self = new self();
73 return $self->isObjectiveResultRangeAchieved($a_usr_id, $a_trigger_obj_id, $a_value);
74 }
75 return false;
76 }
77
78 protected function isObjectiveResultRangeAchieved(int $user_id, int $trigger_obj_id, string $a_value): bool
79 {
80 $value_arr = unserialize($a_value);
81 if ($value_arr === false) {
82 return false;
83 }
84 $min_percentage = $value_arr['min_percentage'] ?? 0;
85 $max_percentage = $value_arr['max_percentage'] ?? 0;
86 $objective = $value_arr['objective'] ?? 0;
87
88 $user_result = ilLOUserResults::lookupResult(
89 $trigger_obj_id,
91 $objective,
93 );
94 $result_percentage = $user_result['result_perc'] ?? 0;
95 return
96 ($result_percentage >= $min_percentage) &&
97 ($result_percentage <= $max_percentage);
98 }
99
103 public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
104 {
105 if (is_null($user_id)) {
106 $user_id = $this->user->getId();
107 }
108 if ($this->user->getId() === $user_id) {
109 $participants = ilCourseParticipant::_getInstanceByObjId($obj_id, $user_id);
110 } else {
111 $participants = ilCourseParticipants::_getInstanceByObjId($obj_id);
112 }
113
114 switch ($cmd) {
115 case "view":
116 if ($participants->isBlocked($user_id) && $participants->isAssigned($user_id)) {
117 $this->access->addInfoItem(
119 $this->lng->txt("crs_status_blocked")
120 );
121 return false;
122 }
123 break;
124
125 case 'leave':
126 // Regular member
127 if ($permission == 'leave') {
128 $limit = null;
129 if (!ilObjCourse::mayLeave($obj_id, $user_id, $limit)) {
130 $this->access->addInfoItem(
132 sprintf(
133 $this->lng->txt("crs_cancellation_end_rbac_info"),
135 )
136 );
137 return false;
138 }
139
140 if (!$participants->isAssigned($user_id)) {
141 return false;
142 }
143 }
144 break;
145
146 case 'leaveWaitList':
147 // Waiting list
148 if ($permission == 'join') {
150 return false;
151 }
152 return true;
153 }
154 break;
155
156 case 'join':
157
159 return false;
160 }
161 break;
162 }
163
164 switch ($permission) {
165 case 'visible':
166 $visible = null;
167 $active = self::_isActivated($obj_id, $visible);
168 $tutor = $this->rbacSystem->checkAccessOfUser($user_id, 'write', $ref_id);
169 if (!$active) {
170 $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt("offline"));
171 }
172 if (!$tutor && !$active && !$visible) {
173 return false;
174 }
175 break;
176
177 case 'read':
178 $tutor = $this->rbacSystem->checkAccessOfUser($user_id, 'write', $ref_id);
179 if ($tutor) {
180 return true;
181 }
182 $active = self::_isActivated($obj_id);
183 if (!$active) {
184 $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt("offline"));
185 return false;
186 }
187 if ($participants->isBlocked($user_id) && $participants->isAssigned($user_id)) {
188 $this->access->addInfoItem(
190 $this->lng->txt("crs_status_blocked")
191 );
192 return false;
193 }
194 break;
195
196 case 'join':
197 if (!self::_registrationEnabled($obj_id)) {
198 return false;
199 }
200
201 if ($participants->isAssigned($user_id)) {
202 return false;
203 }
204 break;
205
206 case 'leave':
207 return ilObjCourse::mayLeave($obj_id, $user_id);
208 }
209 return true;
210 }
211
215 public static function _getCommands(): array
216 {
217 $commands = array();
218 $commands[] = array("permission" => "crs_linked", "cmd" => "", "lang_var" => "view", "default" => true);
219
220 $commands[] = array("permission" => "join", "cmd" => "join", "lang_var" => "join");
221
222 // on waiting list
223 $commands[] = array('permission' => "join", "cmd" => "leaveWaitList", "lang_var" => "leave_waiting_list");
224
225 // regualar users
226 $commands[] = array('permission' => "leave", "cmd" => "leave", "lang_var" => "crs_unsubscribe");
227
229 $webdav_obj = new ilObjWebDAV();
230 $commands[] = $webdav_obj->retrieveWebDAVCommandArrayForActionMenu();
231 }
232
233 $commands[] = array("permission" => "write",
234 "cmd" => "enableAdministrationPanel",
235 "lang_var" => "edit_content"
236 );
237 $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "settings");
238 return $commands;
239 }
240
244 public static function _checkGoto(string $target): bool
245 {
246 global $DIC;
247
248 $ilAccess = $DIC['ilAccess'];
249 $ilUser = $DIC['ilUser'];
250
251 $t_arr = explode("_", $target);
252
253 // registration codes
254 if (isset($t_arr[2]) && substr($t_arr[2], 0, 5) == 'rcode' && $ilUser->getId() != ANONYMOUS_USER_ID) {
255 self::$using_code = true;
256 return true;
257 }
258
259 if ($t_arr[0] != "crs" || ((int) $t_arr[1]) <= 0) {
260 return false;
261 }
262
263 // checking for read results in endless loop, if read is given
264 // but visible is not given (-> see bug 5323)
265 if ($ilAccess->checkAccess("read", "", (int) $t_arr[1]) ||
266 $ilAccess->checkAccess("visible", "", (int) $t_arr[1])) {
267 //if ($ilAccess->checkAccess("visible", "", $t_arr[1]))
268 return true;
269 }
270 return false;
271 }
272
273 public static function _lookupViewMode(int $a_id): int
274 {
275 global $DIC;
276
277 $ilDB = $DIC->database();
278
279 $query = "SELECT view_mode FROM crs_settings WHERE obj_id = " . $ilDB->quote($a_id, 'integer') . " ";
280 $res = $ilDB->query($query);
281 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
282 return $row->view_mode;
283 }
285 }
286
287 public static function _isActivated(int $a_obj_id, ?bool &$a_visible_flag = null, bool $a_mind_member_view = true): bool
288 {
289 // #7669
290 if ($a_mind_member_view) {
291 if (ilMemberViewSettings::getInstance()->isActive()) {
292 $a_visible_flag = true;
293 return true;
294 }
295 }
297 $ref_id = array_pop($ref_id);
298 $a_visible_flag = true;
299
301 switch ($item['timing_type']) {
303 if (time() < $item['timing_start'] || time() > $item['timing_end']) {
304 $a_visible_flag = $item['visible'];
305 return false;
306 }
307 // fallthrough
308
309 // no break
310 default:
311 return true;
312 }
313 }
314
315 public static function _registrationEnabled(int $a_obj_id): bool
316 {
317 global $DIC;
318
319 $ilDB = $DIC->database();
320 $type = null;
321
322 $query = "SELECT * FROM crs_settings " .
323 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
324
325 $reg_start = $reg_end = 0;
326 $res = $ilDB->query($query);
327 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
328 $type = $row->sub_limitation_type;
329 $reg_start = $row->sub_start;
330 $reg_end = $row->sub_end;
331 }
332
333 switch ($type) {
335 return true;
336
338 return false;
339
341 if (time() > $reg_start && time() < $reg_end) {
342 return true;
343 }
344 // no break
345 default:
346 return false;
347 }
348 }
349
350 public static function lookupRegistrationInfo(int $a_obj_id): array
351 {
352 global $DIC;
353
354 $ilDB = $DIC->database();
355 $ilUser = $DIC->user();
356 $lng = $DIC->language();
357
358 $query = 'SELECT sub_limitation_type, sub_start, sub_end, sub_mem_limit, sub_max_members FROM crs_settings ' .
359 'WHERE obj_id = ' . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER);
360 $res = $ilDB->query($query);
361
362 $info = array();
363 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
364 $info['reg_info_start'] = new ilDateTime($row->sub_start, IL_CAL_UNIX);
365 $info['reg_info_end'] = new ilDateTime($row->sub_end, IL_CAL_UNIX);
366 $info['reg_info_type'] = (int) $row->sub_limitation_type;
367 $info['reg_info_max_members'] = (int) $row->sub_max_members;
368 $info['reg_info_mem_limit'] = (int) $row->sub_mem_limit;
369 }
370
371 $registration_possible = true;
372
373 // Limited registration
374 if (($info['reg_info_type'] ?? 0) == ilCourseConstants::SUBSCRIPTION_LIMITED) {
375 $dt = new ilDateTime(time(), IL_CAL_UNIX);
376 if (ilDateTime::_before($dt, $info['reg_info_start'])) {
377 $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg_start');
378 $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_start']);
379 } elseif (ilDateTime::_before($dt, $info['reg_info_end'])) {
380 $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg_end');
381 $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_end']);
382 } else {
383 $registration_possible = false;
384 $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg_period');
385 $info['reg_info_list_prop']['value'] = $lng->txt('crs_list_reg_noreg');
386 }
387 } elseif (($info['reg_info_type'] ?? 0) == ilCourseConstants::SUBSCRIPTION_UNLIMITED) {
388 $registration_possible = true;
389 } else {
390 $registration_possible = false;
391 $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg');
392 $info['reg_info_list_prop']['value'] = $lng->txt('crs_list_reg_noreg');
393 }
394
395 if (($info['reg_info_mem_limit'] ?? false) && $info['reg_info_max_members'] && $registration_possible) {
396 // Check for free places
397 $part = ilCourseParticipant::_getInstanceByObjId($a_obj_id, $ilUser->getId());
398
399 $info['reg_info_list_size'] = ilCourseWaitingList::lookupListSize($a_obj_id);
400 if ($info['reg_info_list_size']) {
401 $info['reg_info_free_places'] = 0;
402 } else {
403 $info['reg_info_free_places'] = max(0, $info['reg_info_max_members'] - $part->getNumberOfMembers());
404 }
405
406 if ($info['reg_info_free_places']) {
407 $info['reg_info_list_prop_limit']['property'] = $lng->txt('crs_list_reg_limit_places');
408 $info['reg_info_list_prop_limit']['value'] = $info['reg_info_free_places'];
409 } else {
410 $info['reg_info_list_prop_limit']['property'] = '';
411 $info['reg_info_list_prop_limit']['value'] = $lng->txt('crs_list_reg_limit_full');
412 }
413 }
414 return $info;
415 }
416
417 public static function _isOffline(int $obj_id): bool
418 {
419 $dummy = null;
420 return !self::_isActivated($obj_id, $dummy, false);
421 }
422
426 public static function _preloadData(array $obj_ids, array $ref_ids): void
427 {
428 global $DIC;
429
430 $ilUser = $DIC['ilUser'];
431 $lng = $DIC['lng'];
432
433 $lng->loadLanguageModule("crs");
434
435 ilCourseWaitingList::_preloadOnListInfo([$ilUser->getId()], $obj_ids);
436
437 $repository = new ilUserCertificateRepository();
438 $coursePreload = new ilCertificateObjectsForUserPreloader($repository);
439 $coursePreload->preLoad($ilUser->getId(), $obj_ids);
440
441 self::$booking_repo = $DIC->bookingManager()->internal()->repo()->reservationWithContextObjCache($obj_ids);
442 }
443
444 public static function getBookingInfoRepo(): ?\ILIAS\BookingManager\Reservations\ReservationDBRepository
445 {
446 return self::$booking_repo;
447 }
448
449 public static function _usingRegistrationCode(): bool
450 {
451 return self::$using_code;
452 }
453
454 public static function lookupPeriodInfo(int $a_obj_id): array
455 {
456 global $DIC;
457
458 $ilDB = $DIC->database();
459 $lng = $DIC->language();
460
461 $start = $end = null;
462 $query = 'SELECT period_start, period_end, period_time_indication FROM crs_settings ' .
463 'WHERE obj_id = ' . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER);
464
465 $res = $ilDB->query($query);
466 while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
467 if (!$row->period_time_indication) {
468 $start = ($row->period_start
469 ? new \ilDate($row->period_start, IL_CAL_DATETIME)
470 : null);
471 $end = ($row->period_end
472 ? new \ilDate($row->period_end, IL_CAL_DATETIME)
473 : null);
474 } else {
475 $start = ($row->period_start
476 ? new \ilDateTime($row->period_start, IL_CAL_DATETIME, \ilTimeZone::UTC)
477 : null);
478 $end = ($row->period_end
479 ? new \ilDateTime($row->period_end, IL_CAL_DATETIME, \ilTimeZone::UTC)
480 : null);
481 }
482 }
483 if ($start && $end) {
484 $lng->loadLanguageModule('crs');
485
486 return
487 [
488 'crs_start' => $start,
489 'crs_end' => $end,
490 'property' => $lng->txt('crs_period'),
491 'value' => ilDatePresentation::formatPeriod($start, $end)
492 ];
493 }
494 return [];
495 }
496}
Repo class for reservations Acts on tables booking_reservation (rw), booking_reservation_group (rw) a...
const IL_CAL_UNIX
const IL_CAL_DATETIME
const string OPERATOR_RESULT_RANGE_PERCENTAGE
static _getInstanceByObjId(int $a_obj_id, int $a_usr_id)
static _getInstanceByObjId(int $a_obj_id)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
static lookupResult(int $a_course_obj_id, int $a_user_id, int $a_objective_id, int $a_tst_type)
language handling
Class ilObjCourseAccess.
static lookupRegistrationInfo(int $a_obj_id)
static _registrationEnabled(int $a_obj_id)
static checkCondition(int $a_trigger_obj_id, string $a_operator, string $a_value, int $a_usr_id)
@inheritDoc
static getConditionOperators()
Get operators.
static _preloadData(array $obj_ids, array $ref_ids)
Preload data.
static ILIAS BookingManager Reservations ReservationDBRepository $booking_repo
static _checkGoto(string $target)
@inheritDoc
static _lookupViewMode(int $a_id)
static lookupPeriodInfo(int $a_obj_id)
static _getCommands()
@inheritDoc
static _isActivated(int $a_obj_id, ?bool &$a_visible_flag=null, bool $a_mind_member_view=true)
_checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id=null)
@inheritDoc
isObjectiveResultRangeAchieved(int $user_id, int $trigger_obj_id, string $a_value)
static _isOffline(int $obj_id)
Type-specific implementation of general status, has to be overwritten if object type does not support...
static mayLeave(int $a_course_id, int $a_user_id=0, &$a_date=null)
User class.
Class ilObjectAccess.
static getItem(int $ref_id)
static _getAllReferences(int $id)
get all reference ids for object ID
static _hasPassed(int $a_obj_id, int $a_usr_id)
Check if user has passed course.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
static lookupListSize(int $a_obj_id)
static _isOnList(int $a_usr_id, int $a_obj_id)
static _preloadOnListInfo(array $a_usr_ids, array $a_obj_ids)
Preload on list info.
const ANONYMOUS_USER_ID
Definition: constants.php:27
$info
Definition: entry_point.php:21
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
Interface for condition handling.
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26