ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilWaitingList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26abstract class ilWaitingList
27{
28 public static array $is_on_list = [];
29 private int $obj_id = 0;
30 private array $user_ids = [];
31 private array $users = [];
32 protected ilDBInterface $db;
34
35 public function __construct(int $a_obj_id)
36 {
37 global $DIC;
38
39 $this->db = $DIC->database();
40 $this->eventHandler = $DIC->event();
41 $this->obj_id = $a_obj_id;
42 if ($a_obj_id) {
43 $this->read();
44 }
45 }
46
47 public static function lookupListSize(int $a_obj_id): int
48 {
49 global $DIC;
50
51 $ilDB = $DIC->database();
52 $query = 'SELECT count(usr_id) num from crs_waiting_list WHERE obj_id = ' . $ilDB->quote($a_obj_id, 'integer');
53 $res = $ilDB->query($query);
54 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
55 return (int) $row->num;
56 }
57 return 0;
58 }
59
60 public static function _deleteAll(int $a_obj_id): void
61 {
62 global $DIC;
63
64 $ilDB = $DIC->database();
65
66 $query = "DELETE FROM crs_waiting_list WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
67 $res = $ilDB->manipulate($query);
68 }
69
70 public static function _deleteUser(int $a_usr_id): void
71 {
72 global $DIC;
73
74 $ilDB = $DIC->database();
75 $query = "DELETE FROM crs_waiting_list WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer');
76 $res = $ilDB->manipulate($query);
77 }
78
79 public static function deleteUserEntry(int $a_usr_id, int $a_obj_id): void
80 {
81 global $DIC;
82
83 $ilDB = $DIC->database();
84 $query = "DELETE FROM crs_waiting_list " .
85 "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . ' ' .
86 "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
87 $ilDB->query($query);
88 }
89
90 public function getObjId(): int
91 {
92 return $this->obj_id;
93 }
94
95 public function addToList(int $a_usr_id): bool
96 {
97 if ($this->isOnList($a_usr_id)) {
98 return false;
99 }
100 $query = "INSERT INTO crs_waiting_list (obj_id,usr_id,sub_time) " .
101 "VALUES (" .
102 $this->db->quote($this->getObjId(), 'integer') . ", " .
103 $this->db->quote($a_usr_id, 'integer') . ", " .
104 $this->db->quote(time(), 'integer') . " " .
105 ")";
106 $res = $this->db->manipulate($query);
107 $this->read();
108 return true;
109 }
110
111 public function updateSubscriptionTime(int $a_usr_id, int $a_subtime): void
112 {
113 $query = "UPDATE crs_waiting_list " .
114 "SET sub_time = " . $this->db->quote($a_subtime, 'integer') . " " .
115 "WHERE usr_id = " . $this->db->quote($a_usr_id, 'integer') . " " .
116 "AND obj_id = " . $this->db->quote($this->getObjId(), 'integer') . " ";
117 $res = $this->db->manipulate($query);
118 }
119
120 public function removeFromList(int $a_usr_id): bool
121 {
122 $query = "DELETE FROM crs_waiting_list " .
123 " WHERE obj_id = " . $this->db->quote($this->getObjId(), 'integer') . " " .
124 " AND usr_id = " . $this->db->quote($a_usr_id, 'integer') . " ";
125 $affected = $this->db->manipulate($query);
126 $this->read();
127 return $affected > 0;
128 }
129
130 public function isOnList(int $a_usr_id): bool
131 {
132 return isset($this->users[$a_usr_id]);
133 }
134
135 public static function _isOnList(int $a_usr_id, int $a_obj_id): bool
136 {
137 global $DIC;
138
139 $ilDB = $DIC['ilDB'];
140 if (isset(self::$is_on_list[$a_usr_id][$a_obj_id])) {
141 return self::$is_on_list[$a_usr_id][$a_obj_id];
142 }
143
144 $query = "SELECT usr_id " .
145 "FROM crs_waiting_list " .
146 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
147 "AND usr_id = " . $ilDB->quote($a_usr_id, 'integer');
148 $res = $ilDB->query($query);
149 return (bool) $res->numRows();
150 }
151
158 public static function _preloadOnListInfo(array $a_usr_ids, array $a_obj_ids): void
159 {
160 global $DIC;
161
162 $ilDB = $DIC['ilDB'];
163 foreach ($a_usr_ids as $usr_id) {
164 foreach ($a_obj_ids as $obj_id) {
165 self::$is_on_list[$usr_id][$obj_id] = false;
166 }
167 }
168 $query = "SELECT usr_id, obj_id " .
169 "FROM crs_waiting_list " .
170 "WHERE " .
171 $ilDB->in("obj_id", $a_obj_ids, false, "integer") . " AND " .
172 $ilDB->in("usr_id", $a_usr_ids, false, "integer");
173 $res = $ilDB->query($query);
174 while ($rec = $ilDB->fetchAssoc($res)) {
175 self::$is_on_list[(int) $rec["usr_id"]][(int) $rec["obj_id"]] = true;
176 }
177 }
178
179 public function getCountUsers(): int
180 {
181 return count($this->users);
182 }
183
184 public function getPosition(int $a_usr_id): int
185 {
186 return isset($this->users[$a_usr_id]) ? $this->users[$a_usr_id]['position'] : -1;
187 }
188
194 public function getAllUsers(): array
195 {
196 return $this->users;
197 }
198
204 public function getUser(int $a_usr_id): array
205 {
206 return $this->users[$a_usr_id] ?? [];
207 }
208
212 public function getUserIds(): array
213 {
214 return $this->user_ids;
215 }
216
217 private function read(): void
218 {
219 $this->users = [];
220 $query = "SELECT * FROM crs_waiting_list " .
221 "WHERE obj_id = " . $this->db->quote($this->getObjId(), 'integer') . " ORDER BY sub_time";
222
223 $res = $this->db->query($query);
224 $counter = 0;
225 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
226 ++$counter;
227 $this->users[(int) $row->usr_id]['position'] = $counter;
228 $this->users[(int) $row->usr_id]['time'] = (int) $row->sub_time;
229 $this->users[(int) $row->usr_id]['usr_id'] = (int) $row->usr_id;
230
231 $this->user_ids[] = (int) $row->usr_id;
232 }
233 }
234}
Global event handler.
Base class for course and group waiting lists.
static deleteUserEntry(int $a_usr_id, int $a_obj_id)
static lookupListSize(int $a_obj_id)
ilAppEventHandler $eventHandler
__construct(int $a_obj_id)
addToList(int $a_usr_id)
updateSubscriptionTime(int $a_usr_id, int $a_subtime)
static _deleteUser(int $a_usr_id)
ilDBInterface $db
getAllUsers()
get all users on waiting list @access public
getPosition(int $a_usr_id)
static _deleteAll(int $a_obj_id)
getUser(int $a_usr_id)
get user
static array $is_on_list
removeFromList(int $a_usr_id)
static _isOnList(int $a_usr_id, int $a_obj_id)
isOnList(int $a_usr_id)
static _preloadOnListInfo(array $a_usr_ids, array $a_obj_ids)
Preload on list info.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$counter