ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilObjGroupAccess.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
31 {
32  protected static bool $using_code = false;
33 
37  public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
38  {
39  global $DIC;
40 
41  $ilUser = $DIC['ilUser'];
42  $lng = $DIC['lng'];
43  $ilAccess = $DIC['ilAccess'];
44 
45  if (is_null($user_id)) {
46  $user_id = $ilUser->getId();
47  }
48 
49  switch ($cmd) {
50  case "info":
51 
53  $ilAccess->addInfoItem(ilAccessInfo::IL_STATUS_INFO, $lng->txt("info_is_member"));
54  } else {
55  $ilAccess->addInfoItem(ilAccessInfo::IL_STATUS_INFO, $lng->txt("info_is_not_member"));
56  }
57  break;
58 
59  case "join":
60 
61  if (!self::_registrationEnabled($obj_id)) {
62  return false;
63  }
64 
65  if (ilGroupWaitingList::_isOnList($ilUser->getId(), $obj_id)) {
66  return false;
67  }
68 
70  return false;
71  }
72  break;
73 
74  case 'leave':
75  // Regular member
76  if ($permission == 'leave') {
77  $limit = null;
78  if (!ilObjGroup::mayLeave($obj_id, $user_id, $limit)) {
79  $ilAccess->addInfoItem(
81  sprintf($lng->txt("grp_cancellation_end_rbac_info"), ilDatePresentation::formatDate($limit))
82  );
83  return false;
84  }
85 
87  return false;
88  }
89  }
90  break;
91 
92  case 'leaveWaitList':
93  // Waiting list
94  if ($permission == 'join') {
95  if (!ilGroupWaitingList::_isOnList($ilUser->getId(), $obj_id)) {
96  return false;
97  }
98  }
99  break;
100  }
101 
102  switch ($permission) {
103  case 'leave':
104  return ilObjGroup::mayLeave($obj_id, $user_id);
105  }
106  return true;
107  }
108 
112  public static function _getCommands(): array
113  {
114  $commands = array();
115  $commands[] = array("permission" => "grp_linked", "cmd" => "", "lang_var" => "show", "default" => true);
116 
117  $commands[] = array("permission" => "join", "cmd" => "join", "lang_var" => "join");
118 
119  // on waiting list
120  $commands[] = array('permission' => "join", "cmd" => "leaveWaitList", "lang_var" => "leave_waiting_list");
121 
122  // regualar users
123  $commands[] = array('permission' => "leave", "cmd" => "leave", "lang_var" => "grp_btn_unsubscribe");
124 
126  $webdav_obj = new ilObjWebDAV();
127  $commands[] = $webdav_obj->retrieveWebDAVCommandArrayForActionMenu();
128  }
129 
130  $commands[] = array("permission" => "write", "cmd" => "enableAdministrationPanel", "lang_var" => "edit_content");
131  $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "settings");
132 
133  return $commands;
134  }
135 
139  public static function _checkGoto(string $target): bool
140  {
141  global $DIC;
142 
143  $ilAccess = $DIC->access();
144  $ilUser = $DIC->user();
145 
146  $t_arr = explode("_", $target);
147  // registration codes
148  if (substr((string) ($t_arr[2] ?? ""), 0, 5) === 'rcode' and $ilUser->getId() != ANONYMOUS_USER_ID) {
149  self::$using_code = true;
150  return true;
151  }
152 
153  if ($t_arr[0] != "grp" || ((int) $t_arr[1]) <= 0) {
154  return false;
155  }
156 
157  if ($ilAccess->checkAccess("read", "", (int) $t_arr[1]) ||
158  $ilAccess->checkAccess("visible", "", (int) $t_arr[1])) {
159  return true;
160  }
161  return false;
162  }
163 
164  public static function _registrationEnabled(int $a_obj_id): bool
165  {
166  global $DIC;
167 
168  $ilDB = $DIC->database();
169  $query = "SELECT * FROM grp_settings " .
170  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
171 
172  $res = $ilDB->query($query);
173 
174  $enabled = $unlimited = false;
175  $start = $end = 0;
176  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
177  $enabled = $row->registration_enabled;
178  $unlimited = $row->registration_unlimited;
179  $start = $row->registration_start;
180  $end = $row->registration_end;
181  }
182 
183  if (!$enabled) {
184  return false;
185  }
186  if ($unlimited) {
187  return true;
188  }
189  $start = new ilDateTime($start, IL_CAL_DATETIME);
190  $end = new ilDateTime($end, IL_CAL_DATETIME);
191  $time = new ilDateTime(time(), IL_CAL_UNIX);
192  return ilDateTime::_after($time, $start) and ilDateTime::_before($time, $end);
193  }
194 
195 
199  public static function _preloadData(array $obj_ids, array $ref_ids): void
200  {
201  global $DIC;
202 
203  $ilDB = $DIC->database();
204  $ilUser = $DIC->user();
205 
206  ilGroupWaitingList::_preloadOnListInfo([$ilUser->getId()], $obj_ids);
207  }
208 
209  public static function lookupRegistrationInfo(int $a_obj_id): array
210  {
211  global $DIC;
212 
213  $ilDB = $DIC->database();
214  $lng = $DIC->language();
215 
216  $query = 'SELECT registration_type, registration_enabled, registration_unlimited, registration_start, ' .
217  'registration_end, registration_mem_limit, registration_max_members FROM grp_settings ' .
218  'WHERE obj_id = ' . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER);
219  $res = $ilDB->query($query);
220 
221  $info = array();
222  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
223  $info['reg_info_start'] = new ilDateTime($row->registration_start, IL_CAL_DATETIME);
224  $info['reg_info_end'] = new ilDateTime($row->registration_end, IL_CAL_DATETIME);
225  $info['reg_info_type'] = $row->registration_type;
226  $info['reg_info_mem_limit'] = $row->registration_mem_limit;
227  $info['reg_info_unlimited'] = $row->registration_unlimited;
228 
229  $info['reg_info_max_members'] = 0;
230  if ($info['reg_info_mem_limit']) {
231  $info['reg_info_max_members'] = $row->registration_max_members;
232  }
233 
234  $info['reg_info_enabled'] = $row->registration_enabled;
235  }
236 
237  $registration_possible = $info['reg_info_enabled'];
238 
239  // Limited registration (added $registration_possible, see bug 0010157)
240  if (!$info['reg_info_unlimited'] && $registration_possible) {
241  $dt = new ilDateTime(time(), IL_CAL_UNIX);
242  if (ilDateTime::_before($dt, $info['reg_info_start'])) {
243  $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg_start');
244  $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_start']);
245  } elseif (ilDateTime::_before($dt, $info['reg_info_end'])) {
246  $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg_end');
247  $info['reg_info_list_prop']['value'] = ilDatePresentation::formatDate($info['reg_info_end']);
248  } else {
249  $registration_possible = false;
250  $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg_period');
251  $info['reg_info_list_prop']['value'] = $lng->txt('grp_list_reg_noreg');
252  }
253  } else {
254  // added !$registration_possible, see bug 0010157
255  if (!$registration_possible) {
256  $registration_possible = false;
257  $info['reg_info_list_prop']['property'] = $lng->txt('grp_list_reg');
258  $info['reg_info_list_prop']['value'] = $lng->txt('grp_list_reg_noreg');
259  }
260  }
261 
262  if ($info['reg_info_mem_limit'] && $info['reg_info_max_members'] && $registration_possible) {
263  // Check for free places
264  $part = ilGroupParticipants::_getInstanceByObjId($a_obj_id);
265 
266  $info['reg_info_list_size'] = ilCourseWaitingList::lookupListSize($a_obj_id);
267  if ($info['reg_info_list_size']) {
268  $info['reg_info_free_places'] = 0;
269  } else {
270  $info['reg_info_free_places'] = max(0, $info['reg_info_max_members'] - $part->getCountMembers());
271  }
272 
273  if ($info['reg_info_free_places']) {
274  $info['reg_info_list_prop_limit']['property'] = $lng->txt('grp_list_reg_limit_places');
275  $info['reg_info_list_prop_limit']['value'] = $info['reg_info_free_places'];
276  } else {
277  $info['reg_info_list_prop_limit']['property'] = '';
278  $info['reg_info_list_prop_limit']['value'] = $lng->txt('grp_list_reg_limit_full');
279  }
280  }
281 
282  return $info;
283  }
284 
290  public static function lookupPeriodInfo(int $a_obj_id): ?array
291  {
292  global $DIC;
293 
294  $ilDB = $DIC->database();
295  $lng = $DIC->language();
296 
297  $start = $end = null;
298  $query = 'SELECT period_start, period_end, period_time_indication FROM grp_settings ' .
299  'WHERE obj_id = ' . $ilDB->quote($a_obj_id, ilDBConstants::T_INTEGER);
300 
301  $res = $ilDB->query($query);
302  while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
303  if (!$row->period_time_indication) {
304  $start = ($row->period_start
305  ? new \ilDate($row->period_start, IL_CAL_DATETIME)
306  : null);
307  $end = ($row->period_end
308  ? new \ilDate($row->period_end, IL_CAL_DATETIME)
309  : null);
310  } else {
311  $start = ($row->period_start
312  ? new \ilDateTime($row->period_start, IL_CAL_DATETIME, \ilTimeZone::UTC)
313  : null);
314  $end = ($row->period_end
315  ? new \ilDateTime($row->period_end, IL_CAL_DATETIME, \ilTimeZone::UTC)
316  : null);
317  }
318  }
319  if ($start && $end) {
320  $lng->loadLanguageModule('grp');
321 
322  return
323  [
324  'property' => $lng->txt('grp_period'),
325  'value' => ilDatePresentation::formatPeriod($start, $end)
326  ];
327  }
328  return null;
329  }
330 
331  public static function _usingRegistrationCode(): bool
332  {
333  return self::$using_code;
334  }
335 }
static lookupRegistrationInfo(int $a_obj_id)
$res
Definition: ltiservices.php:69
static _isParticipant(int $a_ref_id, int $a_usr_id)
Static function to check if a user is a participant of the container object.
static _preloadOnListInfo(array $a_usr_ids, array $a_obj_ids)
Preload on list info.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ilObjUser $user=null,)
static lookupPeriodInfo(int $a_obj_id)
const IL_CAL_DATETIME
const ANONYMOUS_USER_ID
Definition: constants.php:27
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 mayLeave(int $a_group_id, int $a_user_id=null, ?ilDate &$a_date=null)
static _registrationEnabled(int $a_obj_id)
_checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id=null)
const IL_CAL_UNIX
static _checkGoto(string $target)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:66
static _after(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
global $DIC
Definition: shib_login.php:25
static _getInstanceByObjId(int $a_obj_id)
Get singleton instance.
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ilObjUser $user=null)
Format a period of two dates Shows: 14.
global $lng
Definition: privfeed.php:32
static lookupListSize(int $a_obj_id)
static _isOnList(int $a_usr_id, int $a_obj_id)
static _preloadData(array $obj_ids, array $ref_ids)