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