ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCourseParticipants.php
Go to the documentation of this file.
1<?php
18declare(strict_types=0);
19
25{
26 protected const COMPONENT_NAME = 'components/ILIAS/Course';
27
28 protected static array $instances = [];
29
33 public function __construct(int $a_obj_id)
34 {
35 // ref based constructor
36 $refs = ilObject::_getAllReferences($a_obj_id);
37 parent::__construct(self::COMPONENT_NAME, array_pop($refs));
38 }
39
40 public static function _getInstanceByObjId(int $a_obj_id): ilCourseParticipants
41 {
42 if (isset(self::$instances[$a_obj_id]) && self::$instances[$a_obj_id]) {
43 return self::$instances[$a_obj_id];
44 }
45 return self::$instances[$a_obj_id] = new ilCourseParticipants($a_obj_id);
46 }
47
48 public function add(int $a_usr_id, int $a_role): bool
49 {
50 if (parent::add($a_usr_id, $a_role)) {
51 $this->addRecommendation($a_usr_id);
52 return true;
53 }
54 return false;
55 }
56
57 public static function getMemberRoles(int $a_ref_id): array
58 {
59 global $DIC;
60
61 $rbacreview = $DIC['rbacreview'];
62 $lrol = $rbacreview->getRolesOfRoleFolder($a_ref_id, false);
63 $roles = array();
64 foreach ($lrol as $role) {
65 $title = ilObject::_lookupTitle($role);
66 switch (substr($title, 0, 8)) {
67 case 'il_crs_a':
68 case 'il_crs_t':
69 case 'il_crs_m':
70 continue 2;
71
72 default:
73 $roles[$role] = $role;
74 }
75 }
76 return $roles;
77 }
78
79 public function addSubscriber(int $a_usr_id): void
80 {
81 parent::addSubscriber($a_usr_id);
82 $this->eventHandler->raise(
83 "components/ILIAS/Course",
84 'addSubscriber',
85 array(
86 'obj_id' => $this->getObjId(),
87 'usr_id' => $a_usr_id
88 )
89 );
90 }
91
92 public function updatePassed(
93 int $a_usr_id,
94 bool $a_passed,
95 bool $a_manual = false,
96 bool $a_no_origin = false
97 ): void {
98 $this->participants_status[$a_usr_id]['passed'] = $a_passed;
99 self::_updatePassed($this->obj_id, $a_usr_id, $a_passed, $a_manual, $a_no_origin);
100 }
101
102 public static function _updatePassed(
103 int $a_obj_id,
104 int $a_usr_id,
105 bool $a_passed,
106 bool $a_manual = false,
107 bool $a_no_origin = false
108 ): void {
109 global $DIC;
110
111 $ilDB = $DIC['ilDB'];
112 $ilUser = $DIC['ilUser'];
113 $ilAppEventHandler = $DIC->event();
114
115 // #11600
116 $origin = -1;
117 if ($a_manual) {
118 $origin = $ilUser->getId();
119 }
120
121 $query = "SELECT passed FROM obj_members " .
122 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
123 "AND usr_id = " . $ilDB->quote($a_usr_id, 'integer');
124 $res = $ilDB->query($query);
125 $update_query = '';
126 if ($res->numRows()) {
127 // #9284 - only needs updating when status has changed
128 $old = $ilDB->fetchAssoc($res);
129 if ((int) $old["passed"] !== (int) $a_passed) {
130 $update_query = "UPDATE obj_members SET " .
131 "passed = " . $ilDB->quote($a_passed, 'integer') . ", " .
132 "origin = " . $ilDB->quote($origin, 'integer') . ", " .
133 "origin_ts = " . $ilDB->quote(time(), 'integer') . " " .
134 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
135 "AND usr_id = " . $ilDB->quote($a_usr_id, 'integer');
136 }
137 } else {
138 // when member is added we should not set any date
139 // see ilObjCourse::checkLPStatusSync()
140 if ($a_no_origin && !$a_passed) {
141 $origin = 0;
142 $origin_ts = 0;
143 } else {
144 $origin_ts = time();
145 }
146
147 $update_query = "INSERT INTO obj_members (passed,obj_id,usr_id,notification,blocked,origin,origin_ts) " .
148 "VALUES ( " .
149 $ilDB->quote((int) $a_passed, 'integer') . ", " .
150 $ilDB->quote($a_obj_id, 'integer') . ", " .
151 $ilDB->quote($a_usr_id, 'integer') . ", " .
152 $ilDB->quote(0, 'integer') . ", " .
153 $ilDB->quote(0, 'integer') . ", " .
154 $ilDB->quote($origin, 'integer') . ", " .
155 $ilDB->quote($origin_ts, 'integer') . ")
156 ON DUPLICATE KEY UPDATE
157 passed = VALUES(passed),
158 origin = VALUES(origin),
159 origin_ts = VALUES(origin_ts)
160 ";
161 }
162 if (strlen($update_query)) {
163 $ilDB->manipulate($update_query);
164 if ($a_passed) {
165 $ilAppEventHandler->raise('components/ILIAS/Course', 'participantHasPassedCourse', array(
166 'obj_id' => $a_obj_id,
167 'usr_id' => $a_usr_id,
168 ));
169 }
170 }
171 }
172
173 public function getPassedInfo(int $a_usr_id): ?array
174 {
175 $sql = "SELECT origin, origin_ts" .
176 " FROM obj_members" .
177 " WHERE obj_id = " . $this->ilDB->quote($this->obj_id, "integer") .
178 " AND usr_id = " . $this->ilDB->quote($a_usr_id, "integer");
179 $set = $this->ilDB->query($sql);
180 $row = $this->ilDB->fetchAssoc($set);
181 if ($row && $row["origin"]) {
182 return [
183 "user_id" => (int) $row["origin"],
184 "timestamp" => new ilDateTime((int) $row["origin_ts"], IL_CAL_UNIX)
185 ];
186 }
187 return null;
188 }
189
190 public function sendNotification(int $a_type, int $a_usr_id, bool $a_force_sending_mail = false): void
191 {
193 $mail->forceSendingMail($a_force_sending_mail);
194
195 switch ($a_type) {
198 $mail->setRefId($this->ref_id);
199 $mail->setRecipients(array($a_usr_id));
200 $mail->send();
201 break;
202
205 $mail->setRefId($this->ref_id);
206 $mail->setRecipients(array($a_usr_id));
207 $mail->send();
208 break;
209
212 $mail->setRefId($this->ref_id);
213 $mail->setRecipients(array($a_usr_id));
214 $mail->send();
215 break;
216
219 $mail->setRefId($this->ref_id);
220 $mail->setRecipients(array($a_usr_id));
221 $mail->send();
222 break;
223
226 $mail->setRefId($this->ref_id);
227 $mail->setRecipients(array($a_usr_id));
228 $mail->send();
229 break;
230
233 $mail->setRefId($this->ref_id);
234 $mail->setRecipients(array($a_usr_id));
235 $mail->send();
236 break;
237
240 $mail->setRefId($this->ref_id);
241 $mail->setRecipients(array($a_usr_id));
242 $mail->send();
243 break;
244
247 $mail->setRefId($this->ref_id);
248 $mail->setRecipients(array($a_usr_id));
249 $mail->send();
250 break;
251
254 $mail->setRefId($this->ref_id);
255 $mail->setRecipients(array($a_usr_id));
256 $mail->send();
257 break;
258
260 $wl = new ilCourseWaitingList($this->obj_id);
261 $pos = $wl->getPosition($a_usr_id);
262
264 $mail->setRefId($this->ref_id);
265 $mail->setRecipients(array($a_usr_id));
266 $mail->setAdditionalInformation(array('position' => $pos));
267 $mail->send();
268 break;
269
271 $this->sendSubscriptionRequestToAdmins($a_usr_id);
272 break;
273
275 $this->sendNotificationToAdmins($a_usr_id);
276 break;
277 }
278 }
279
280 public function sendUnsubscribeNotificationToAdmins(int $a_usr_id): void
281 {
284 $mail->setAdditionalInformation(array('usr_id' => $a_usr_id));
285 $mail->setRefId($this->ref_id);
286 $mail->setRecipients($this->getNotificationRecipients());
287 $mail->send();
288 }
289
290 public function sendSubscriptionRequestToAdmins(int $a_usr_id): void
291 {
294 $mail->setAdditionalInformation(array('usr_id' => $a_usr_id));
295 $mail->setRefId($this->ref_id);
296 $mail->setRecipients($this->getNotificationRecipients());
297 $mail->send();
298 }
299
300 public function sendNotificationToAdmins(int $a_usr_id): void
301 {
304 $mail->setAdditionalInformation(array('usr_id' => $a_usr_id));
305 $mail->setRefId($this->ref_id);
306 $mail->setRecipients($this->getNotificationRecipients());
307 $mail->send();
308 }
309
310 public static function getDateTimeOfPassed(int $a_obj_id, int $a_usr_id): string
311 {
312 global $DIC;
313
314 $ilDB = $DIC->database();
315 $sql = "SELECT origin_ts FROM obj_members" .
316 " WHERE usr_id = " . $ilDB->quote($a_usr_id, "integer") .
317 " AND obj_id = " . $ilDB->quote($a_obj_id, "integer") .
318 " AND passed = " . $ilDB->quote(1, "integer");
319 $res = $ilDB->query($sql);
320 $res = $ilDB->fetchAssoc($res);
321 if (($res["origin_ts"] ?? 0) > 0) {
322 return date("Y-m-d H:i:s", $res["origin_ts"]);
323 }
324 return '';
325 }
326
327 public static function getPassedUsersForObjects(array $a_obj_ids, array $a_usr_ids): array
328 {
329 global $DIC;
330
331 $ilDB = $DIC->database();
332 $sql = "SELECT usr_id,obj_id FROM obj_members" .
333 " WHERE " . $ilDB->in("usr_id", $a_usr_ids, false, "integer") .
334 " AND " . $ilDB->in("obj_id", $a_obj_ids, false, "integer") .
335 " AND passed = " . $ilDB->quote(1, "integer");
336 $set = $ilDB->query($sql);
337 $res = [];
338 while ($row = $ilDB->fetchAssoc($set)) {
339 $row['usr_id'] = (int) $row['usr_id'];
340 $row['obj_id'] = (int) $row['obj_id'];
341 $res[] = $row;
342 }
343 return $res;
344 }
345}
const IL_CAL_UNIX
static getDateTimeOfPassed(int $a_obj_id, int $a_usr_id)
static _getInstanceByObjId(int $a_obj_id)
updatePassed(int $a_usr_id, bool $a_passed, bool $a_manual=false, bool $a_no_origin=false)
sendNotification(int $a_type, int $a_usr_id, bool $a_force_sending_mail=false)
sendSubscriptionRequestToAdmins(int $a_usr_id)
static getMemberRoles(int $a_ref_id)
add(int $a_usr_id, int $a_role)
static _updatePassed(int $a_obj_id, int $a_usr_id, bool $a_passed, bool $a_manual=false, bool $a_no_origin=false)
sendUnsubscribeNotificationToAdmins(int $a_usr_id)
static getPassedUsersForObjects(array $a_obj_ids, array $a_usr_ids)
@classDescription Date and time handling
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupTitle(int $obj_id)
Base class for course and group participants.
addRecommendation(int $a_usr_id)
Add desktop item @access public.
$res
Definition: ltiservices.php:69
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26