ILIAS  release_8 Revision v8.24
class.ilParticipant.php
Go to the documentation of this file.
1<?php
18declare(strict_types=1);
19
26abstract class ilParticipant
27{
28 protected const MEMBERSHIP_ADMIN = 1;
29 protected const MEMBERSHIP_TUTOR = 2;
30 protected const MEMBERSHIP_MEMBER = 3;
31
32 private int $obj_id = 0;
33 private int $usr_id = 0;
34 protected string $type = '';
35 private int $ref_id = 0;
36
37 private string $component = '';
38
39 private array $roles = [];
40 private array $role_data = [];
41 private bool $participants = false;
42 private bool $admins = false;
43 private bool $tutors = false;
44 private bool $members = false;
45
46 private ?int $numMembers = null;
47 private array $member_roles = [];
48 private array $participants_status = array();
49
51 protected ilDBInterface $db;
56
57 protected function __construct(string $a_component_name, int $a_obj_id, int $a_usr_id)
58 {
59 global $DIC;
60
61 $this->obj_id = $a_obj_id;
62 $this->usr_id = $a_usr_id;
63 $this->type = ilObject::_lookupType($a_obj_id);
64 $ref_ids = ilObject::_getAllReferences($this->obj_id);
65 $this->ref_id = current($ref_ids);
66 $this->component = $a_component_name;
67
68 $this->recommended_content_manager = new ilRecommendedContentManager();
69 $this->db = $DIC->database();
70 $this->rbacReview = $DIC->rbac()->review();
71 $this->rbacAdmin = $DIC->rbac()->admin();
72 $this->objectDataCache = $DIC['ilObjDataCache'];
73 $this->eventHandler = $DIC->event();
74
75 $this->readParticipant();
76 $this->readParticipantStatus();
77 }
78
79 public static function updateMemberRoles(int $a_obj_id, int $a_usr_id, int $a_role_id, int $a_status): void
80 {
81 global $DIC;
82
83 $ilDB = $DIC->database();
84
85 $a_membership_role_type = self::getMembershipRoleType($a_role_id);
86 switch ($a_membership_role_type) {
88 $update_fields = array('admin' => array('integer', $a_status ? 1 : 0));
89 $update_string = ('admin = ' . $ilDB->quote($a_status ? 1 : 0, 'integer'));
90 break;
91
93 $update_fields = array('tutor' => array('integer', $a_status ? 1 : 0));
94 $update_string = ('tutor = ' . $ilDB->quote($a_status ? 1 : 0, 'integer'));
95 break;
96
98 default:
99 $current_status = self::lookupStatusByMembershipRoleType($a_obj_id, $a_usr_id, $a_membership_role_type);
100
101 if ($a_status) {
102 $new_status = $current_status + 1;
103 }
104 if (!$a_status) {
105 $new_status = $current_status - 1;
106 if ($new_status < 0) {
107 $new_status = 0;
108 }
109 }
110
111 $update_fields = array('member' => array('integer', $new_status));
112 $update_string = ('member = ' . $ilDB->quote($new_status, 'integer'));
113 break;
114 }
115
116 $query = 'SELECT count(*) num FROM obj_members ' .
117 'WHERE obj_id = ' . $ilDB->quote($a_obj_id, 'integer') . ' ' .
118 'AND usr_id = ' . $ilDB->quote($a_usr_id, 'integer');
119 $res = $ilDB->query($query);
120
121 $found = false;
122 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
123 if ($row->num) {
124 $found = true;
125 }
126 }
127 if (!$found) {
128 $ilDB->replace(
129 'obj_members',
130 array(
131 'obj_id' => array('integer', $a_obj_id),
132 'usr_id' => array('integer', $a_usr_id)
133 ),
134 $update_fields
135 );
136 } else {
137 $query = 'UPDATE obj_members SET ' .
138 $update_string . ' ' .
139 'WHERE obj_id = ' . $ilDB->quote($a_obj_id, 'integer') . ' ' .
140 'AND usr_id = ' . $ilDB->quote($a_usr_id, 'integer');
141
142 $ilDB->manipulate($query);
143 }
144
145 $query = 'DELETE from obj_members ' .
146 'WHERE obj_id = ' . $ilDB->quote($a_obj_id, 'integer') . ' ' .
147 'AND usr_id = ' . $ilDB->quote($a_usr_id, 'integer') . ' ' .
148 'AND admin = ' . $ilDB->quote(0, 'integer') . ' ' .
149 'AND tutor = ' . $ilDB->quote(0, 'integer') . ' ' .
150 'AND member = ' . $ilDB->quote(0, 'integer');
151 $ilDB->manipulate($query);
152 }
153
154 public static function getMembershipRoleType(int $a_role_id): int
155 {
156 $title = ilObject::_lookupTitle($a_role_id);
157 switch (substr($title, 0, 8)) {
158 case 'il_crs_a':
159 case 'il_grp_a':
161
162 case 'il_crs_t':
164
165 case 'il_crs_m':
166 default:
168
169 }
170 }
171
172 public static function lookupStatusByMembershipRoleType(
173 int $a_obj_id,
174 int $a_usr_id,
175 int $a_membership_role_type
176 ): int {
177 global $DIC;
178
179 $ilDB = $DIC->database();
180 $query = 'SELECT * FROM obj_members ' .
181 'WHERE obj_id = ' . $ilDB->quote($a_obj_id, 'integer') . ' ' .
182 'AND usr_id = ' . $ilDB->quote($a_usr_id, ilDBConstants::T_INTEGER) . ' ';
183 $res = $ilDB->query($query);
184 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
185 switch ($a_membership_role_type) {
187 return (int) $row->admin;
188
190 return (int) $row->tutor;
191
193 return (int) $row->member;
194 }
195 }
196 return 0;
197 }
198
203 protected function getComponent(): string
204 {
205 return $this->component;
206 }
207
208 public function getUserId(): int
209 {
210 return $this->usr_id;
211 }
212
213 public function isBlocked(): bool
214 {
215 return (bool) ($this->participants_status[$this->getUserId()]['blocked'] ?? false);
216 }
217
221 public function isContact(): bool
222 {
223 return (bool) ($this->participants_status[$this->getUserId()]['contact'] ?? false);
224 }
225
226 public function isAssigned(): bool
227 {
228 return $this->participants;
229 }
230
231 public function isMember(): bool
232 {
233 return $this->members;
234 }
235
236 public function isAdmin(): bool
237 {
238 return $this->admins;
239 }
240
241 public function isTutor(): bool
242 {
243 return $this->tutors;
244 }
245
246 public function isParticipant(): bool
247 {
248 return $this->participants;
249 }
250
251 public function getNumberOfMembers(): int
252 {
253 if ($this->numMembers === null) {
254 $this->numMembers = $this->rbacReview->getNumberOfAssignedUsers($this->member_roles);
255 }
256 return $this->numMembers;
257 }
258
259 protected function readParticipant(): void
260 {
261 $this->roles = $this->rbacReview->getRolesOfRoleFolder($this->ref_id, false);
262 $this->member_roles = [];
263 foreach ($this->roles as $role_id) {
264 $title = $this->objectDataCache->lookupTitle($role_id);
265 switch (substr($title, 0, 8)) {
266 case 'il_crs_m':
267 $this->member_roles[] = $role_id;
268 $this->role_data[ilParticipants::IL_CRS_MEMBER] = $role_id;
269 if ($this->rbacReview->isAssigned($this->getUserId(), $role_id)) {
270 $this->participants = true;
271 $this->members = true;
272 }
273 break;
274
275 case 'il_crs_a':
276 $this->role_data[ilParticipants::IL_CRS_ADMIN] = $role_id;
277 if ($this->rbacReview->isAssigned($this->getUserId(), $role_id)) {
278 $this->participants = true;
279 $this->admins = true;
280 }
281 break;
282
283 case 'il_crs_t':
284 $this->role_data[ilParticipants::IL_CRS_TUTOR] = $role_id;
285 if ($this->rbacReview->isAssigned($this->getUserId(), $role_id)) {
286 $this->participants = true;
287 $this->tutors = true;
288 }
289 break;
290
291 case 'il_grp_a':
292 $this->role_data[ilParticipants::IL_GRP_ADMIN] = $role_id;
293 if ($this->rbacReview->isAssigned($this->getUserId(), $role_id)) {
294 $this->participants = true;
295 $this->admins = true;
296 }
297 break;
298
299 case 'il_grp_m':
300 $this->member_roles[] = $role_id;
301 $this->role_data[ilParticipants::IL_GRP_MEMBER] = $role_id;
302 if ($this->rbacReview->isAssigned($this->getUserId(), $role_id)) {
303 $this->participants = true;
304 $this->members = true;
305 }
306 break;
307
308 default:
309
310 $this->member_roles[] = $role_id;
311 if ($this->rbacReview->isAssigned($this->getUserId(), $role_id)) {
312 $this->participants = true;
313 $this->members = true;
314 }
315 break;
316 }
317 }
318 }
319
320 protected function readParticipantStatus(): void
321 {
322 $query = "SELECT * FROM obj_members " .
323 "WHERE obj_id = " . $this->db->quote($this->obj_id, 'integer') . " " .
324 'AND usr_id = ' . $this->db->quote($this->getUserId(), 'integer');
325
326 $res = $this->db->query($query);
327 $this->participants_status = array();
328 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
329 $this->participants_status[$this->getUserId()]['blocked'] = (bool) $row->blocked;
330 $this->participants_status[$this->getUserId()]['notification'] = (bool) $row->notification;
331 $this->participants_status[$this->getUserId()]['passed'] = (bool) $row->passed;
332 $this->participants_status[$this->getUserId()]['contact'] = (bool) $row->contact;
333 }
334 }
335
336 public function add(int $a_usr_id, int $a_role): bool
337 {
338 if ($this->rbacReview->isAssignedToAtLeastOneGivenRole($a_usr_id, $this->roles)) {
339 return false;
340 }
341
342 switch ($a_role) {
345 $this->admins = true;
346 break;
347
349 $this->tutors = true;
350 break;
351
354 $this->members = true;
355 break;
356
357 }
358
359 $this->rbacAdmin->assignUser($this->role_data[$a_role], $a_usr_id);
360 $this->addRecommendation($a_usr_id);
361
362 // Delete subscription request
363 $this->deleteSubscriber($a_usr_id);
364
365 ilWaitingList::deleteUserEntry($a_usr_id, $this->obj_id);
366
367 $this->eventHandler->raise(
368 $this->getComponent(),
369 "addParticipant",
370 array(
371 'obj_id' => $this->obj_id,
372 'usr_id' => $a_usr_id,
373 'role_id' => $a_role
374 )
375 );
376 return true;
377 }
378
379 public function delete(int $a_usr_id): void
380 {
381 $this->recommended_content_manager->removeObjectRecommendation($a_usr_id, $this->ref_id);
382 foreach ($this->roles as $role_id) {
383 $this->rbacAdmin->deassignUser($role_id, $a_usr_id);
384 }
385
386 $query = "DELETE FROM obj_members " .
387 "WHERE usr_id = " . $this->db->quote($a_usr_id, 'integer') . " " .
388 "AND obj_id = " . $this->db->quote($this->obj_id, 'integer');
389 $res = $this->db->manipulate($query);
390
391 $this->eventHandler->raise(
392 $this->getComponent(),
393 "deleteParticipant",
394 array(
395 'obj_id' => $this->obj_id,
396 'usr_id' => $a_usr_id
397 )
398 );
399 }
400
401 public function deleteSubscriber(int $a_usr_id): void
402 {
403 $query = "DELETE FROM il_subscribers " .
404 "WHERE usr_id = " . $this->db->quote($a_usr_id, 'integer') . " " .
405 "AND obj_id = " . $this->db->quote($this->obj_id, 'integer') . " ";
406 $res = $this->db->manipulate($query);
407 }
408
409 public function addRecommendation($a_usr_id): void
410 {
411 // deactivated for now, see discussion at
412 // https://docu.ilias.de/goto_docu_wiki_wpage_5620_1357.html
413 //$this->recommended_content_manager->addObjectRecommendation($a_usr_id, $this->ref_id);
414 }
415
416 public function updateContact(int $a_usr_id, bool $a_contact): void
417 {
418 $this->db->manipulate(
419 'UPDATE obj_members SET ' .
420 'contact = ' . $this->db->quote($a_contact, 'integer') . ' ' .
421 'WHERE obj_id = ' . $this->db->quote($this->obj_id, 'integer') . ' ' .
422 'AND usr_id = ' . $this->db->quote($a_usr_id, 'integer')
423 );
424 $this->participants_status[$a_usr_id]['contact'] = $a_contact;
425 }
426
427 public function updateNotification(int $a_usr_id, bool $a_notification): void
428 {
429 $this->participants_status[$a_usr_id]['notification'] = $a_notification;
430
431 $query = "SELECT * FROM obj_members " .
432 "WHERE obj_id = " . $this->db->quote($this->obj_id, 'integer') . " " .
433 "AND usr_id = " . $this->db->quote($a_usr_id, 'integer');
434 $res = $this->db->query($query);
435 if ($res->numRows()) {
436 $query = "UPDATE obj_members SET " .
437 "notification = " . $this->db->quote((int) $a_notification, 'integer') . " " .
438 "WHERE obj_id = " . $this->db->quote($this->obj_id, 'integer') . " " .
439 "AND usr_id = " . $this->db->quote($a_usr_id, 'integer');
440 } else {
441 $query = "INSERT INTO obj_members (notification,obj_id,usr_id,passed,blocked) " .
442 "VALUES ( " .
443 $this->db->quote((int) $a_notification, 'integer') . ", " .
444 $this->db->quote($this->obj_id, 'integer') . ", " .
445 $this->db->quote($a_usr_id, 'integer') . ", " .
446 $this->db->quote(0, 'integer') . ", " .
447 $this->db->quote(0, 'integer') .
448 ") ON DUPLICATE KEY UPDATE notification = VALUES(notification)";
449 }
450 $this->db->manipulate($query);
451 }
452
453 public function checkLastAdmin(array $a_usr_ids): bool
454 {
455 $admin_role_id =
456 $this->type === 'crs' ?
457 $this->role_data[ilParticipants::IL_CRS_ADMIN] :
458 $this->role_data[ilParticipants::IL_GRP_ADMIN];
459
460 $query = "
461 SELECT COUNT(rolesusers.usr_id) cnt
462
463 FROM object_data rdata
464
465 LEFT JOIN rbac_ua rolesusers
466 ON rolesusers.rol_id = rdata.obj_id
467
468 WHERE rdata.obj_id = %s
469 ";
470
471 $query .= ' AND ' . $this->db->in('rolesusers.usr_id', $a_usr_ids, true, 'integer');
472 $res = $this->db->queryF($query, array('integer'), array($admin_role_id));
473
474 $data = $this->db->fetchAssoc($res);
475 return $data['cnt'] > 0;
476 }
477}
Global event handler.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupTitle(int $obj_id)
Base class for course and group participant.
checkLastAdmin(array $a_usr_ids)
static getMembershipRoleType(int $a_role_id)
ilRbacAdmin $rbacAdmin
ilDBInterface $db
addRecommendation($a_usr_id)
updateNotification(int $a_usr_id, bool $a_notification)
add(int $a_usr_id, int $a_role)
getComponent()
Get component name Used for event handling.
ilAppEventHandler $eventHandler
updateContact(int $a_usr_id, bool $a_contact)
ilObjectDataCache $objectDataCache
static lookupStatusByMembershipRoleType(int $a_obj_id, int $a_usr_id, int $a_membership_role_type)
isContact()
Check if user is contact for current object.
deleteSubscriber(int $a_usr_id)
ilRecommendedContentManager $recommended_content_manager
static updateMemberRoles(int $a_obj_id, int $a_usr_id, int $a_role_id, int $a_status)
__construct(string $a_component_name, int $a_obj_id, int $a_usr_id)
ilRbacReview $rbacReview
Class ilRbacAdmin Core functions for role based access control.
class ilRbacReview Contains Review functions of core Rbac.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static deleteUserEntry(int $a_usr_id, int $a_obj_id)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query