ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCalendarCategoryAssignments.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
25 {
26  protected ilDBInterface $db;
27 
28  protected int $cal_entry_id = 0;
29  protected array $assignments = [];
30 
31  public function __construct(int $a_cal_entry_id)
32  {
33  global $DIC;
34 
35  $this->db = $DIC->database();
36  $this->cal_entry_id = $a_cal_entry_id;
37  $this->read();
38  }
39 
40  public static function _lookupCategories(int $a_cal_id): array
41  {
42  global $DIC;
43 
44  $ilDB = $DIC['ilDB'];
45  $query = "SELECT cat_id FROM cal_cat_assignments " .
46  "WHERE cal_id = " . $ilDB->quote($a_cal_id, 'integer') . " ";
47  $res = $ilDB->query($query);
48  $cat_ids = [];
49  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
50  $cat_ids[] = (int) $row->cat_id;
51  }
52  return $cat_ids;
53  }
54 
55  public static function _lookupCategory(int $a_cal_id): int
56  {
57  if (count($cats = self::_lookupCategories($a_cal_id))) {
58  return $cats[0];
59  }
60  return 0;
61  }
62 
67  public static function _getAppointmentCalendars(array $a_cal_ids): array
68  {
69  global $DIC;
70 
71  $ilDB = $DIC['ilDB'];
72  $query = "SELECT * FROM cal_cat_assignments " .
73  "WHERE " . $ilDB->in('cal_id', $a_cal_ids, false, 'integer');
74  $res = $ilDB->query($query);
75  $map = [];
76  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
77  $map[(int) $row->cal_id] = (int) $row->cat_id;
78  }
79  return $map;
80  }
81 
87  public static function _getAssignedAppointments(array $a_cat_id): array
88  {
89  global $DIC;
90 
91  $ilDB = $DIC['ilDB'];
92  $query = "SELECT * FROM cal_cat_assignments " .
93  "WHERE " . $ilDB->in('cat_id', $a_cat_id, false, 'integer');
94 
95  $res = $ilDB->query($query);
96  $cal_ids = [];
97  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
98  $cal_ids[] = (int) $row->cal_id;
99  }
100  return $cal_ids;
101  }
102 
107  public static function lookupNumberOfAssignedAppointments(array $a_cat_ids): int
108  {
109  global $DIC;
110 
111  $ilDB = $DIC['ilDB'];
112  $query = 'SELECT COUNT(*) num FROM cal_cat_assignments ' .
113  'WHERE ' . $ilDB->in('cat_id', $a_cat_ids, false, 'integer');
114  $res = $ilDB->query($query);
115  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
116  return (int) $row->num;
117  }
118  return 0;
119  }
120 
125  public static function _getAutoGeneratedAppointmentsByObjId(int $a_obj_id): array
126  {
127  global $DIC;
128 
129  $ilDB = $DIC['ilDB'];
130  $query = "SELECT ce.cal_id FROM cal_categories cc " .
131  "JOIN cal_cat_assignments cca ON cc.cat_id = cca.cat_id " .
132  "JOIN cal_entries ce ON cca.cal_id = ce.cal_id " .
133  "WHERE auto_generated = 1 " .
134  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
135  $res = $ilDB->query($query);
136  $apps = [];
137  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
138  $apps[] = (int) $row->cal_id;
139  }
140  return $apps;
141  }
142 
146  public static function _deleteByAppointmentId(int $a_app_id): void
147  {
148  global $DIC;
149 
150  $ilDB = $DIC['ilDB'];
151  $query = "DELETE FROM cal_cat_assignments " .
152  "WHERE cal_id = " . $ilDB->quote($a_app_id, 'integer') . " ";
153  $res = $ilDB->manipulate($query);
154  }
155 
163  public static function _deleteByCategoryId(int $a_cat_id): void
164  {
165  global $DIC;
166 
167  $ilDB = $DIC['ilDB'];
168  $query = "DELETE FROM cal_cat_assignments " .
169  "WHERE cat_id = " . $ilDB->quote($a_cat_id, 'integer') . " ";
170  $res = $ilDB->manipulate($query);
171  }
172 
176  public function getFirstAssignment(): ?int
177  {
178  return $this->assignments[0] ?? null;
179  }
180 
184  public function getAssignments(): array
185  {
186  return $this->assignments;
187  }
188 
189  public function addAssignment(int $a_cal_cat_id): void
190  {
191  $query = "INSERT INTO cal_cat_assignments (cal_id,cat_id) " .
192  "VALUES ( " .
193  $this->db->quote($this->cal_entry_id, 'integer') . ", " .
194  $this->db->quote($a_cal_cat_id, 'integer') . " " .
195  ")";
196  $res = $this->db->manipulate($query);
197  $this->assignments[] = $a_cal_cat_id;
198  }
199 
200  public function deleteAssignment(int $a_cat_id): void
201  {
202  $query = "DELETE FROM cal_cat_assignments " .
203  "WHERE cal_id = " . $this->db->quote($this->cal_entry_id, 'integer') . ", " .
204  "AND cat_id = " . $this->db->quote($a_cat_id, 'integer') . " ";
205  $res = $this->db->manipulate($query);
206 
207  if (($key = array_search($a_cat_id, $this->assignments)) !== false) {
208  unset($this->assignments[$key]);
209  }
210  }
211 
212  public function deleteAssignments(): void
213  {
214  $query = "DELETE FROM cal_cat_assignments " .
215  "WHERE cal_id = " . $this->db->quote($this->cal_entry_id, 'integer') . " ";
216  $res = $this->db->manipulate($query);
217  }
218 
219  private function read()
220  {
221  $query = "SELECT * FROM cal_cat_assignments " .
222  "WHERE cal_id = " . $this->db->quote($this->cal_entry_id, 'integer') . " ";
223 
224  $res = $this->db->query($query);
225  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
226  $this->assignments[] = (int) $row->cat_id;
227  }
228  }
229 }
$res
Definition: ltiservices.php:66
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _deleteByAppointmentId(int $a_app_id)
Delete appointment assignment.
global $DIC
Definition: shib_login.php:22
static _getAssignedAppointments(array $a_cat_id)
Get assigned apointments.
static _deleteByCategoryId(int $a_cat_id)
Delete assignments by category id public.
static _getAutoGeneratedAppointmentsByObjId(int $a_obj_id)
get automatic generated appointments of category