ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjCourseAccess.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=0);
4 
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(
54  );
55  }
56 
60  public static function checkCondition(
61  int $a_trigger_obj_id,
62  string $a_operator,
63  string $a_value,
64  int $a_usr_id
65  ): bool {
66  switch ($a_operator) {
68  return ilCourseParticipants::_hasPassed($a_trigger_obj_id, $a_usr_id);
69  }
70  return false;
71  }
72 
76  public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
77  {
78  if (is_null($user_id)) {
79  $user_id = $this->user->getId();
80  }
81  if ($this->user->getId() === $user_id) {
82  $participants = ilCourseParticipant::_getInstanceByObjId($obj_id, $user_id);
83  } else {
84  $participants = ilCourseParticipants::_getInstanceByObjId($obj_id);
85  }
86 
87  switch ($cmd) {
88  case "view":
89  if ($participants->isBlocked($user_id) && $participants->isAssigned($user_id)) {
90  $this->access->addInfoItem(
92  $this->lng->txt("crs_status_blocked")
93  );
94  return false;
95  }
96  break;
97 
98  case 'leave':
99  // Regular member
100  if ($permission == 'leave') {
101  $limit = null;
102  if (!ilObjCourse::mayLeave($obj_id, $user_id, $limit)) {
103  $this->access->addInfoItem(
105  sprintf(
106  $this->lng->txt("crs_cancellation_end_rbac_info"),
108  )
109  );
110  return false;
111  }
112 
113  if (!$participants->isAssigned($user_id)) {
114  return false;
115  }
116  }
117  break;
118 
119  case 'leaveWaitList':
120  // Waiting list
121  if ($permission == 'join') {
122  if (!ilCourseWaitingList::_isOnList($user_id, $obj_id)) {
123  return false;
124  }
125  return true;
126  }
127  break;
128 
129  case 'join':
130 
131  if (ilCourseWaitingList::_isOnList($user_id, $obj_id)) {
132  return false;
133  }
134  break;
135  }
136 
137  switch ($permission) {
138  case 'visible':
139  $visible = null;
140  $active = self::_isActivated($obj_id, $visible);
141  $tutor = $this->rbacSystem->checkAccessOfUser($user_id, 'write', $ref_id);
142  if (!$active) {
143  $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt("offline"));
144  }
145  if (!$tutor && !$active && !$visible) {
146  return false;
147  }
148  break;
149 
150  case 'read':
151  $tutor = $this->rbacSystem->checkAccessOfUser($user_id, 'write', $ref_id);
152  if ($tutor) {
153  return true;
154  }
155  $active = self::_isActivated($obj_id);
156  if (!$active) {
157  $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt("offline"));
158  return false;
159  }
160  if ($participants->isBlocked($user_id) && $participants->isAssigned($user_id)) {
161  $this->access->addInfoItem(
163  $this->lng->txt("crs_status_blocked")
164  );
165  return false;
166  }
167  break;
168 
169  case 'join':
170  if (!self::_registrationEnabled($obj_id)) {
171  return false;
172  }
173 
174  if ($participants->isAssigned($user_id)) {
175  return false;
176  }
177  break;
178 
179  case 'leave':
180  return ilObjCourse::mayLeave($obj_id, $user_id);
181  }
182  return true;
183  }
184 
188  public static function _getCommands(): array
189  {
190  $commands = array();
191  $commands[] = array("permission" => "crs_linked", "cmd" => "", "lang_var" => "view", "default" => true);
192 
193  $commands[] = array("permission" => "join", "cmd" => "join", "lang_var" => "join");
194 
195  // on waiting list
196  $commands[] = array('permission' => "join", "cmd" => "leaveWaitList", "lang_var" => "leave_waiting_list");
197 
198  // regualar users
199  $commands[] = array('permission' => "leave", "cmd" => "leave", "lang_var" => "crs_unsubscribe");
200 
202  $webdav_obj = new ilObjWebDAV();
203  $commands[] = $webdav_obj->retrieveWebDAVCommandArrayForActionMenu();
204  }
205 
206  $commands[] = array("permission" => "write",
207  "cmd" => "enableAdministrationPanel",
208  "lang_var" => "edit_content"
209  );
210  $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "settings");
211  return $commands;
212  }
213 
217  public static function _checkGoto(string $target): bool
218  {
219  global $DIC;
220 
221  $ilAccess = $DIC['ilAccess'];
222  $ilUser = $DIC['ilUser'];
223 
224  $t_arr = explode("_", $target);
225 
226  // registration codes
227  if (isset($t_arr[2]) && substr($t_arr[2], 0, 5) == 'rcode' && $ilUser->getId() != ANONYMOUS_USER_ID) {
228  self::$using_code = true;
229  return true;
230  }
231 
232  if ($t_arr[0] != "crs" || ((int) $t_arr[1]) <= 0) {
233  return false;
234  }
235 
236  // checking for read results in endless loop, if read is given
237  // but visible is not given (-> see bug 5323)
238  if ($ilAccess->checkAccess("read", "", $t_arr[1]) ||
239  $ilAccess->checkAccess("visible", "", $t_arr[1])) {
240  //if ($ilAccess->checkAccess("visible", "", $t_arr[1]))
241  return true;
242  }
243  return false;
244  }
245 
246  public static function _lookupViewMode(int $a_id): int
247  {
248  global $DIC;
249 
250  $ilDB = $DIC->database();
251 
252  $query = "SELECT view_mode FROM crs_settings WHERE obj_id = " . $ilDB->quote($a_id, 'integer') . " ";
253  $res = $ilDB->query($query);
254  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
255  return $row->view_mode;
256  }
258  }
259 
260  public static function _isActivated(int $a_obj_id, ?bool &$a_visible_flag = null, bool $a_mind_member_view = true): bool
261  {
262  // #7669
263  if ($a_mind_member_view) {
264  if (ilMemberViewSettings::getInstance()->isActive()) {
265  $a_visible_flag = true;
266  return true;
267  }
268  }
270  $ref_id = array_pop($ref_id);
271  $a_visible_flag = true;
272 
274  switch ($item['timing_type']) {
276  if (time() < $item['timing_start'] || time() > $item['timing_end']) {
277  $a_visible_flag = $item['visible'];
278  return false;
279  }
280  // fallthrough
281 
282  // no break
283  default:
284  return true;
285  }
286  }
287 
288  public static function _registrationEnabled(int $a_obj_id): bool
289  {
290  global $DIC;
291 
292  $ilDB = $DIC->database();
293  $type = null;
294 
295  $query = "SELECT * FROM crs_settings " .
296  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
297 
298  $reg_start = $reg_end = 0;
299  $res = $ilDB->query($query);
300  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
301  $type = $row->sub_limitation_type;
302  $reg_start = $row->sub_start;
303  $reg_end = $row->sub_end;
304  }
305 
306  switch ($type) {
308  return true;
309 
311  return false;
312 
314  if (time() > $reg_start && time() < $reg_end) {
315  return true;
316  }
317  // no break
318  default:
319  return false;
320  }
321  }
322 
323  public static function lookupRegistrationInfo(int $a_obj_id): array
324  {
325  global $DIC;
326 
327  $ilDB = $DIC->database();
328  $ilUser = $DIC->user();
329  $lng = $DIC->language();
330 
331  $query = 'SELECT sub_limitation_type, sub_start, sub_end, sub_mem_limit, sub_max_members FROM crs_settings ' .
332  'WHERE obj_id = ' . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER);
333  $res = $ilDB->query($query);
334 
335  $info = array();
336  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
337  $info['reg_info_start'] = new ilDateTime($row->sub_start, IL_CAL_UNIX);
338  $info['reg_info_end'] = new ilDateTime($row->sub_end, IL_CAL_UNIX);
339  $info['reg_info_type'] = (int) $row->sub_limitation_type;
340  $info['reg_info_max_members'] = (int) $row->sub_max_members;
341  $info['reg_info_mem_limit'] = (int) $row->sub_mem_limit;
342  }
343 
344  $registration_possible = true;
345 
346  // Limited registration
347  if (($info['reg_info_type'] ?? 0) == ilCourseConstants::SUBSCRIPTION_LIMITED) {
348  $dt = new ilDateTime(time(), IL_CAL_UNIX);
349  if (ilDateTime::_before($dt, $info['reg_info_start'])) {
350  $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg_start');
351  $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_start']);
352  } elseif (ilDateTime::_before($dt, $info['reg_info_end'])) {
353  $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg_end');
354  $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_end']);
355  } else {
356  $registration_possible = false;
357  $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg_period');
358  $info['reg_info_list_prop']['value'] = $lng->txt('crs_list_reg_noreg');
359  }
360  } elseif (($info['reg_info_type'] ?? 0) == ilCourseConstants::SUBSCRIPTION_UNLIMITED) {
361  $registration_possible = true;
362  } else {
363  $registration_possible = false;
364  $info['reg_info_list_prop']['property'] = $lng->txt('crs_list_reg');
365  $info['reg_info_list_prop']['value'] = $lng->txt('crs_list_reg_noreg');
366  }
367 
368  if (($info['reg_info_mem_limit'] ?? false) && $info['reg_info_max_members'] && $registration_possible) {
369  // Check for free places
370  $part = ilCourseParticipant::_getInstanceByObjId($a_obj_id, $ilUser->getId());
371 
372  $info['reg_info_list_size'] = ilCourseWaitingList::lookupListSize($a_obj_id);
373  if ($info['reg_info_list_size']) {
374  $info['reg_info_free_places'] = 0;
375  } else {
376  $info['reg_info_free_places'] = max(0, $info['reg_info_max_members'] - $part->getNumberOfMembers());
377  }
378 
379  if ($info['reg_info_free_places']) {
380  $info['reg_info_list_prop_limit']['property'] = $lng->txt('crs_list_reg_limit_places');
381  $info['reg_info_list_prop_limit']['value'] = $info['reg_info_free_places'];
382  } else {
383  $info['reg_info_list_prop_limit']['property'] = '';
384  $info['reg_info_list_prop_limit']['value'] = $lng->txt('crs_list_reg_limit_full');
385  }
386  }
387  return $info;
388  }
389 
390  public static function _isOffline(int $obj_id): bool
391  {
392  $dummy = null;
393  return !self::_isActivated($obj_id, $dummy, false);
394  }
395 
399  public static function _preloadData(array $obj_ids, array $ref_ids): void
400  {
401  global $DIC;
402 
403  $ilUser = $DIC['ilUser'];
404  $lng = $DIC['lng'];
405 
406  $lng->loadLanguageModule("crs");
407 
408  ilCourseWaitingList::_preloadOnListInfo([$ilUser->getId()], $obj_ids);
409 
410  $repository = new ilUserCertificateRepository();
411  $coursePreload = new ilCertificateObjectsForUserPreloader($repository);
412  $coursePreload->preLoad($ilUser->getId(), $obj_ids);
413 
415  self::$booking_repo = $f->getRepoWithContextObjCache($obj_ids);
416  }
417 
419  {
420  return self::$booking_repo;
421  }
422 
423  public static function _usingRegistrationCode(): bool
424  {
425  return self::$using_code;
426  }
427 
428  public static function lookupPeriodInfo(int $a_obj_id): array
429  {
430  global $DIC;
431 
432  $ilDB = $DIC->database();
433  $lng = $DIC->language();
434 
435  $start = $end = null;
436  $query = 'SELECT period_start, period_end, period_time_indication FROM crs_settings ' .
437  'WHERE obj_id = ' . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER);
438 
439  $res = $ilDB->query($query);
440  while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
441  if (!$row->period_time_indication) {
442  $start = ($row->period_start
443  ? new \ilDate($row->period_start, IL_CAL_DATETIME)
444  : null);
445  $end = ($row->period_end
446  ? new \ilDate($row->period_end, IL_CAL_DATETIME)
447  : null);
448  } else {
449  $start = ($row->period_start
450  ? new \ilDateTime($row->period_start, IL_CAL_DATETIME, \ilTimeZone::UTC)
451  : null);
452  $end = ($row->period_end
453  ? new \ilDateTime($row->period_end, IL_CAL_DATETIME, \ilTimeZone::UTC)
454  : null);
455  }
456  }
457  if ($start && $end) {
458  $lng->loadLanguageModule('crs');
459 
460  return
461  [
462  'crs_start' => $start,
463  'crs_end' => $end,
464  'property' => $lng->txt('crs_period'),
465  'value' => ilDatePresentation::formatPeriod($start, $end)
466  ];
467  }
468  return [];
469  }
470 }
static getConditionOperators()
Get operators.
$res
Definition: ltiservices.php:69
static _preloadOnListInfo(array $a_usr_ids, array $a_obj_ids)
Preload on list info.
const IL_CAL_DATETIME
const ANONYMOUS_USER_ID
Definition: constants.php:27
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
$type
static checkCondition(int $a_trigger_obj_id, string $a_operator, string $a_value, int $a_usr_id)
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 lookupRegistrationInfo(int $a_obj_id)
static _getInstanceByObjId(int $a_obj_id, int $a_usr_id)
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
Interface for condition handling.
loadLanguageModule(string $a_module)
Load language module.
const IL_CAL_UNIX
static _hasPassed(int $a_obj_id, int $a_usr_id)
Check if user has passed course.
static mayLeave(int $a_course_id, int $a_user_id=0, &$a_date=null)
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
static _getInstanceByObjId(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static ilBookingReservationDBRepository $booking_repo
static _lookupViewMode(int $a_id)
static _isOffline(int $obj_id)
$query
static _isActivated(int $a_obj_id, ?bool &$a_visible_flag=null, bool $a_mind_member_view=true)
static _registrationEnabled(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getItem(int $ref_id)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
static lookupPeriodInfo(int $a_obj_id)
static _checkGoto(string $target)
$ilUser
Definition: imgupload.php:34
static lookupListSize(int $a_obj_id)
static _isOnList(int $a_usr_id, int $a_obj_id)
_checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id=null)
static _preloadData(array $obj_ids, array $ref_ids)
Preload data.