ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarShared.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const TYPE_USR = 1;
29 public const TYPE_ROLE = 2;
30
31 private int $calendar_id;
32
33 private array $shared = array();
34 private array $shared_users = array();
35 private array $shared_roles = array();
36
37 protected ilDBInterface $db;
39
40 public function __construct(int $a_calendar_id)
41 {
42 global $DIC;
43
44 $this->calendar_id = $a_calendar_id;
45 $this->db = $DIC->database();
46 $this->rbacreview = $DIC->rbac()->review();
47 $this->read();
48 }
49
50 public static function deleteByCalendar(int $a_cal_id): void
51 {
52 global $DIC;
53
54 $ilDB = $DIC['ilDB'];
55 $query = "DELETE FROM cal_shared WHERE cal_id = " . $ilDB->quote($a_cal_id, 'integer') . " ";
56 $res = $ilDB->manipulate($query);
57 }
58
62 public static function deleteByUser(int $a_user_id): void
63 {
64 global $DIC;
65
66 $ilDB = $DIC['ilDB'];
67 $query = "DELETE FROM cal_shared WHERE obj_id = " . $ilDB->quote($a_user_id, 'integer') . " ";
68 $res = $ilDB->manipulate($query);
69 }
70
74 public static function isSharedWithUser(int $a_usr_id, int $a_calendar_id): bool
75 {
76 global $DIC;
77
78 $ilDB = $DIC['ilDB'];
79 $rbacreview = $DIC['rbacreview'];
80
81 $query = 'SELECT * FROM cal_shared ' .
82 "WHERE cal_id = " . $ilDB->quote($a_calendar_id, 'integer') . " ";
83 $res = $ilDB->query($query);
84 $obj_ids = [];
85 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
86 $obj_ids[(int) $row->obj_id] = (string) $row->obj_type;
87 }
88 $assigned_roles = $rbacreview->assignedRoles($a_usr_id);
89 foreach ($obj_ids as $id => $type) {
90 switch ($type) {
91 case self::TYPE_USR:
92 if ($a_usr_id == $id) {
93 return true;
94 }
95 break;
96 case self::TYPE_ROLE:
97 if (in_array($id, $assigned_roles)) {
98 return true;
99 }
100 break;
101 }
102 }
103 return false;
104 }
105
106 public static function getSharedCalendarsForUser(int $a_usr_id = 0): array
107 {
108 global $DIC;
109
110 $ilDB = $DIC['ilDB'];
111 $ilUser = $DIC['ilUser'];
112 $rbacreview = $DIC['rbacreview'];
113
114 if (!$a_usr_id) {
115 $a_usr_id = $ilUser->getId();
116 }
117
118 $query = "SELECT * FROM cal_shared " .
119 "WHERE obj_type = " . $ilDB->quote(self::TYPE_USR, 'integer') . " " .
120 "AND obj_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
121 "ORDER BY create_date";
122 $res = $ilDB->query($query);
123 $calendars = array();
124 $shared = [];
125 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
126 $calendars[] = (int) $row->cal_id;
127
128 $shared[(int) $row->cal_id]['cal_id'] = (int) $row->cal_id;
129 $shared[(int) $row->cal_id]['create_date'] = (string) $row->create_date;
130 $shared[(int) $row->cal_id]['obj_type'] = (string) $row->obj_type;
131 }
132
133 $assigned_roles = $rbacreview->assignedRoles($ilUser->getId());
134
135 $query = "SELECT * FROM cal_shared " .
136 "WHERE obj_type = " . $ilDB->quote(self::TYPE_ROLE, 'integer') . " " .
137 "AND " . $ilDB->in('obj_id', $assigned_roles, false, 'integer');
138
139 $res = $ilDB->query($query);
140 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
141 if (in_array($row->cal_id, $calendars)) {
142 continue;
143 }
144 if (ilCalendarCategories::_isOwner($ilUser->getId(), (int) $row->cal_id)) {
145 continue;
146 }
147
148 $shared[(int) $row->cal_id]['cal_id'] = (int) $row->cal_id;
149 $shared[(int) $row->cal_id]['create_date'] = (string) $row->create_date;
150 $shared[(int) $row->cal_id]['obj_type'] = (string) $row->obj_type;
151 }
152 return $shared;
153 }
154
155 public function getCalendarId(): int
156 {
157 return $this->calendar_id;
158 }
159
160 public function getShared(): array
161 {
162 return $this->shared;
163 }
164
165 public function getUsers(): array
166 {
167 return $this->shared_users;
168 }
169
170 public function getRoles(): array
171 {
172 return $this->shared_roles;
173 }
174
175 public function isShared(int $a_obj_id): bool
176 {
177 return isset($this->shared[$a_obj_id]);
178 }
179
180 public function isEditableForUser(int $a_user_id): bool
181 {
182 foreach ($this->shared as $info) {
183 if (!$info['writable']) {
184 continue;
185 }
186
187 switch ($info['obj_type']) {
188 case self::TYPE_USR:
189 if ($info['obj_id'] == $a_user_id) {
190 return true;
191 }
192 break;
193
194 case self::TYPE_ROLE:
195 if ($this->rbacreview->isAssigned($a_user_id, $info['obj_id'])) {
196 return true;
197 }
198 break;
199 }
200 }
201 return false;
202 }
203
204 public function share(int $a_obj_id, int $a_type, bool $a_writable = false): void
205 {
206 if ($this->isShared($a_obj_id)) {
207 return;
208 }
209 $query = "INSERT INTO cal_shared (cal_id,obj_id,obj_type,create_date,writable) " .
210 "VALUES ( " .
211 $this->db->quote($this->getCalendarId(), 'integer') . ", " .
212 $this->db->quote($a_obj_id, 'integer') . ", " .
213 $this->db->quote($a_type, 'integer') . ", " .
214 $this->db->now() . ", " .
215 $this->db->quote((int) $a_writable, 'integer') . ' ' .
216 ")";
217
218 $res = $this->db->manipulate($query);
219 $this->read();
220 }
221
222 public function stopSharing(int $a_obj_id): void
223 {
224 if (!$this->isShared($a_obj_id)) {
225 return;
226 }
227 $query = "DELETE FROM cal_shared WHERE cal_id = " . $this->db->quote($this->getCalendarId(), 'integer') . " " .
228 "AND obj_id = " . $this->db->quote($a_obj_id, 'integer') . " ";
229 $res = $this->db->manipulate($query);
230
232 $this->read();
233 }
234
235 protected function read(): void
236 {
237 $this->shared = $this->shared_users = $this->shared_roles = array();
238 $query = "SELECT * FROM cal_shared WHERE cal_id = " . $this->db->quote($this->getCalendarId(), 'integer');
239 $res = $this->db->query($query);
240 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
241 switch ($row->obj_type) {
242 case self::TYPE_USR:
243 $this->shared_users[(int) $row->obj_id]['obj_id'] = (int) $row->obj_id;
244 $this->shared_users[(int) $row->obj_id]['obj_type'] = (string) $row->obj_type;
245 $this->shared_users[(int) $row->obj_id]['create_date'] = (string) $row->create_date;
246 $this->shared_users[(int) $row->obj_id]['writable'] = (bool) $row->writable;
247 break;
248
249 case self::TYPE_ROLE:
250 $this->shared_roles[(int) $row->obj_id]['obj_id'] = (int) $row->obj_id;
251 $this->shared_roles[(int) $row->obj_id]['obj_type'] = (string) $row->obj_type;
252 $this->shared_roles[(int) $row->obj_id]['create_date'] = (string) $row->create_date;
253 $this->shared_roles[(int) $row->obj_id]['writable'] = (bool) $row->writable;
254 break;
255 }
256
257 $this->shared[(int) $row->obj_id]['obj_id'] = (int) $row->obj_id;
258 $this->shared[(int) $row->obj_id]['obj_type'] = (string) $row->obj_type;
259 $this->shared[(int) $row->obj_id]['create_date'] = (string) $row->create_date;
260 $this->shared[(int) $row->obj_id]['writable'] = (bool) $row->writable;
261 }
262 }
263}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _isOwner(int $a_usr_id, int $a_cal_id)
check if user is owner of a category
static deleteStatus(int $a_id, int $a_calendar_id)
Handles shared calendars.
share(int $a_obj_id, int $a_type, bool $a_writable=false)
static getSharedCalendarsForUser(int $a_usr_id=0)
static deleteByUser(int $a_user_id)
Delete all entries for a specific user.
__construct(int $a_calendar_id)
isEditableForUser(int $a_user_id)
static deleteByCalendar(int $a_cal_id)
static isSharedWithUser(int $a_usr_id, int $a_calendar_id)
is shared with user
class ilRbacReview Contains Review functions of core Rbac.
assignedRoles(int $a_usr_id)
get all assigned roles to a given user
$info
Definition: entry_point.php:21
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26