ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarSharedStatus.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
26{
27 public const STATUS_ACCEPTED = 1;
28 public const STATUS_DECLINED = 2;
29 public const STATUS_DELETED = 3;
30
31 protected ?ilDBInterface $db;
32
33 private int $usr_id = 0;
34
35 private array $calendars = array();
36 private array $writable = array();
37
38 public function __construct(int $a_usr_id)
39 {
40 global $DIC;
41 $this->usr_id = $a_usr_id;
42 $this->db = $DIC->database();
43 $this->read();
44 }
45
46 public function isAccepted(int $a_cal_id): bool
47 {
48 return
49 isset($this->calendars[$a_cal_id]) &&
50 $this->calendars[$a_cal_id] == self::STATUS_ACCEPTED;
51 }
52
53 public function isDeclined(int $a_cal_id): bool
54 {
55 return
56 isset($this->calendars[$a_cal_id]) &&
57 $this->calendars[$a_cal_id] == self::STATUS_DECLINED;
58 }
59
60 public static function getAcceptedCalendars(int $a_usr_id): array
61 {
62 global $DIC;
63
64 $ilDB = $DIC['ilDB'];
65 $query = "SELECT cal_id FROM cal_shared_status " .
66 "WHERE status = " . $ilDB->quote(self::STATUS_ACCEPTED, 'integer') . " " .
67 "AND usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " ";
68 $res = $ilDB->query($query);
69 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
70 $cal_ids[] = $row->cal_id;
71 }
72 return $cal_ids ?? [];
73 }
74
75 public static function hasStatus(int $a_usr_id, int $a_calendar_id): bool
76 {
77 global $DIC;
78
79 $ilDB = $DIC['ilDB'];
80 $query = "SELECT * FROM cal_shared_status " .
81 "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
82 "AND cal_id = " . $ilDB->quote($a_calendar_id, 'integer') . " ";
83 $res = $ilDB->query($query);
84 return (bool) $res->numRows();
85 }
86
87 public static function deleteUser(int $a_usr_id): void
88 {
89 global $DIC;
90
91 $ilDB = $DIC['ilDB'];
92 $query = "DELETE FROM cal_shared_status " .
93 "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " ";
94 $res = $ilDB->manipulate($query);
95 }
96
97 public static function deleteCalendar(int $a_calendar_id): void
98 {
99 global $DIC;
100
101 $ilDB = $DIC['ilDB'];
102 $query = "DELETE FROM cal_shared_status " .
103 "WHERE cal_id = " . $ilDB->quote($a_calendar_id, 'integer') . " ";
104 $res = $ilDB->manipulate($query);
105 }
106
107 public static function deleteStatus(int $a_id, int $a_calendar_id): void
108 {
109 global $DIC;
110
111 $ilDB = $DIC['ilDB'];
112 $rbacreview = $DIC['rbacreview'];
113
114 if (ilObject::_lookupType($a_id) == 'usr') {
115 $query = "DELETE FROM cal_shared_status " .
116 "WHERE cal_id = " . $ilDB->quote($a_calendar_id, 'integer') . " " .
117 "AND usr_id = " . $ilDB->quote($a_id, 'integer') . " ";
118 $res = $ilDB->manipulate($query);
119 } elseif (ilObject::_lookupType($a_id) == 'role') {
120 $assigned_users = $rbacreview->assignedUsers($a_id);
121
122 if (!count($assigned_users)) {
123 return;
124 }
125
126 $query = "DELETE FROM cal_shared_status " .
127 "WHERE cal_id = " . $ilDB->quote($a_calendar_id, 'integer') . " " .
128 "AND " . $ilDB->in('usr_id', $assigned_users, false, 'integer');
129 $res = $ilDB->manipulate($query);
130 }
131 }
132
133 public function accept(int $a_calendar_id): void
134 {
135 self::deleteStatus($this->usr_id, $a_calendar_id);
136 $query = "INSERT INTO cal_shared_status (cal_id,usr_id,status) " .
137 "VALUES ( " .
138 $this->db->quote($a_calendar_id, 'integer') . ", " .
139 $this->db->quote($this->usr_id, 'integer') . ", " .
140 $this->db->quote(self::STATUS_ACCEPTED, 'integer') . " " .
141 ")";
142 $res = $this->db->manipulate($query);
143
144 $this->calendars[$a_calendar_id] = self::STATUS_ACCEPTED;
145 }
146
147 public function decline(int $a_calendar_id): void
148 {
149 self::deleteStatus($this->usr_id, $a_calendar_id);
150 $query = "INSERT INTO cal_shared_status (cal_id,usr_id,status) " .
151 "VALUES ( " .
152 $this->db->quote($a_calendar_id, 'integer') . ", " .
153 $this->db->quote($this->usr_id, 'integer') . ", " .
154 $this->db->quote(self::STATUS_DECLINED, 'integer') . " " .
155 ")";
156 $res = $this->db->manipulate($query);
157 $this->calendars[$a_calendar_id] = self::STATUS_DECLINED;
158 }
159
160 protected function read()
161 {
162 $query = "SELECT * FROM cal_shared_status " .
163 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " ";
164 $res = $this->db->query($query);
165 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
166 $this->calendars[(int) $row->cal_id] = (int) $row->status;
167 }
168 }
169
170 public function getOpenInvitations(): array
171 {
172 $shared = ilCalendarShared::getSharedCalendarsForUser($this->usr_id);
173
174 $invitations = array();
175 foreach ($shared as $data) {
176 if ($this->isDeclined($data['cal_id']) || $this->isAccepted($data['cal_id'])) {
177 continue;
178 }
179
180 $tmp_calendar = new ilCalendarCategory($data['cal_id']);
181
182 $invitations[] = array(
183 'cal_id' => (int) $data['cal_id'],
184 'create_date' => $data['create_date'],
185 'obj_type' => $data['obj_type'],
186 'name' => $tmp_calendar->getTitle(),
187 'owner' => $tmp_calendar->getObjId(),
188 'apps' => count(ilCalendarCategoryAssignments::_getAssignedAppointments(array((int) $data['cal_id']))),
189 'accepted' => $this->isAccepted((int) $data['cal_id']),
190 'declined' => $this->isDeclined((int) $data['cal_id'])
191 );
192 }
193 return $invitations;
194 }
195}
static _getAssignedAppointments(array $a_cat_id)
Get assigned apointments.
Stores calendar categories.
Stores status (accepted/declined) of shared calendars.
static getAcceptedCalendars(int $a_usr_id)
static deleteCalendar(int $a_calendar_id)
static deleteStatus(int $a_id, int $a_calendar_id)
static hasStatus(int $a_usr_id, int $a_calendar_id)
static getSharedCalendarsForUser(int $a_usr_id=0)
static _lookupType(int $id, bool $reference=false)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26