ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarVisibility.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const HIDDEN = 0;
29 public const VISIBLE = 1;
30
31 protected static array $instances = array();
32 protected int $user_id = 0;
33 protected int $ref_id = 0;
34 protected int $obj_id = 0;
35 protected array $hidden = array();
36 protected array $visible = array();
37 protected int $forced_visible = 0;
38
39 protected ilDBInterface $db;
40
41 private function __construct(int $a_user_id, int $a_ref_id = 0)
42 {
43 global $DIC;
44 $this->db = $DIC->database();
45 $this->user_id = $a_user_id;
46 $this->ref_id = $a_ref_id;
47 $this->obj_id = ilObject::_lookupObjId($a_ref_id);
48 $this->read();
49 }
50
51 public static function _getInstanceByUserId(int $a_user_id, int $a_ref_id = 0): ilCalendarVisibility
52 {
53 if (!isset(self::$instances[$a_user_id][$a_ref_id])) {
54 self::$instances[$a_user_id][$a_ref_id] = new ilCalendarVisibility($a_user_id, $a_ref_id);
55 }
56 return self::$instances[$a_user_id][$a_ref_id];
57 }
58
59 public static function _deleteCategories(int $a_cat_id): void
60 {
61 global $DIC;
62
63 $ilDB = $DIC->database();
64 $query = "DELETE FROM cal_cat_visibility " .
65 "WHERE cat_id = " . $ilDB->quote($a_cat_id, 'integer') . " ";
66 $ilDB->manipulate($query);
67 }
68
69 public static function _deleteUser(int $a_user_id): void
70 {
71 global $DIC;
72
73 $ilDB = $DIC->database();
74
75 $query = "DELETE FROM cal_cat_visibility " .
76 "WHERE user_id = " . $ilDB->quote($a_user_id, 'integer') . " ";
77 $ilDB->manipulate($query);
78 }
79
83 public function filterHidden(array $categories, array $category_info): array
84 {
85 $hidden = array();
86 foreach ($category_info as $cat_id => $info) {
87 $subitem_ids = [];
88 if (array_key_exists('subitem_ids', $info) && is_array($info['subitem_ids'])) {
89 $subitem_ids = $info['subitem_ids'];
90 }
91
92 if ($this->isHidden($cat_id, $info)) {
93 $hidden = array_merge((array) $hidden, $subitem_ids, array($cat_id));
94 }
95 }
96 return array_diff($categories, $hidden);
97 }
98
99 protected function isHidden(int $a_cat_id, array $info): bool
100 {
101 // personal desktop
102 if ($this->obj_id == 0) {
103 return in_array($a_cat_id, $this->hidden);
104 }
105
106 // crs/grp, always show current object and objects that have been selected due to
107 // current container ref id
108 if (
110 ($info["obj_id"] == $this->obj_id || $info["source_ref_id"] == $this->ref_id)
111 ) {
112 return false;
113 }
114 return !in_array($a_cat_id, $this->visible);
115 }
116
117 public function isAppointmentVisible(int $a_cal_id): bool
118 {
119 foreach (ilCalendarCategoryAssignments::_lookupCategories($a_cal_id) as $cat_id) {
120 if (in_array($cat_id, $this->hidden)) {
121 return true;
122 }
123 }
124 return false;
125 }
126
127 public function getHidden(): array
128 {
129 return $this->hidden;
130 }
131
132 public function getVisible(): array
133 {
134 return $this->visible;
135 }
136
137 public function hideSelected(array $a_hidden): void
138 {
139 $this->hidden = $a_hidden;
140 }
141
142 public function showSelected(array $a_visible): void
143 {
144 $this->visible = $a_visible;
145 }
146
147 public function save(): void
148 {
149 $this->delete();
150 foreach ($this->hidden as $hidden) {
151 if ($hidden === $this->forced_visible) {
152 continue;
153 }
154 $query = "INSERT INTO cal_cat_visibility (user_id, cat_id, obj_id, visible) " .
155 "VALUES ( " .
156 $this->db->quote($this->user_id, 'integer') . ", " .
157 $this->db->quote($hidden, 'integer') . ", " .
158 $this->db->quote($this->obj_id, 'integer') . ", " .
159 $this->db->quote(self::HIDDEN, 'integer') .
160 ")";
161 $this->db->manipulate($query);
162 }
163 foreach ($this->visible as $visible) {
164 if ($visible === $this->forced_visible) {
165 continue;
166 }
167 $query = "INSERT INTO cal_cat_visibility (user_id, cat_id, obj_id, visible) " .
168 "VALUES ( " .
169 $this->db->quote($this->user_id, 'integer') . ", " .
170 $this->db->quote($visible, 'integer') . ", " .
171 $this->db->quote($this->obj_id, 'integer') . ", " .
172 $this->db->quote(self::VISIBLE, 'integer') .
173 ")";
174 $this->db->manipulate($query);
175 }
176 }
177
178 public function delete(?int $a_cat_id = null): void
179 {
180 if ($a_cat_id) {
181 $query = "DELETE FROM cal_cat_visibility " .
182 "WHERE user_id = " . $this->db->quote($this->user_id, 'integer') . " " .
183 "AND obj_id = " . $this->db->quote($this->obj_id, 'integer') . " " .
184 "AND cat_id = " . $this->db->quote($a_cat_id, 'integer') . " ";
185 } else {
186 $query = "DELETE FROM cal_cat_visibility " .
187 "WHERE user_id = " . $this->db->quote($this->user_id, 'integer') . " " .
188 "AND obj_id = " . $this->db->quote($this->obj_id, 'integer');
189 }
190 $this->db->manipulate($query);
191 }
192
193 protected function read(): void
194 {
195 $query = "SELECT * FROM cal_cat_visibility " .
196 "WHERE user_id = " . $this->db->quote($this->user_id, 'integer') . " " .
197 " AND obj_id = " . $this->db->quote($this->obj_id, 'integer');
198 $res = $this->db->query($query);
199 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
200 if ($row->visible == self::HIDDEN) {
201 $this->hidden[] = $row->cat_id;
202 }
203 if ($row->visible == self::VISIBLE) {
204 $this->visible[] = $row->cat_id;
205 }
206 }
207 }
208
209 public function forceVisibility(int $a_cat_id): void
210 {
211 $this->forced_visible = $a_cat_id;
212 if (($key = array_search($a_cat_id, $this->hidden)) !== false) {
213 unset($this->hidden[$key]);
214 }
215 if (!in_array($a_cat_id, $this->visible)) {
216 $this->visible[] = $a_cat_id;
217 }
218 }
219}
Stores selection of hidden calendars for a specific user.
static _deleteUser(int $a_user_id)
static _deleteCategories(int $a_cat_id)
isHidden(int $a_cat_id, array $info)
static _getInstanceByUserId(int $a_user_id, int $a_ref_id=0)
filterHidden(array $categories, array $category_info)
Filter hidden categories (and hidden subitem categories) from category array.
__construct(int $a_user_id, int $a_ref_id=0)
static _lookupObjId(int $ref_id)
$info
Definition: entry_point.php:21
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26