ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilBookingObjectsTableGUI.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected string $process_class;
26  protected \ILIAS\UI\Renderer $ui_renderer;
27  protected \ILIAS\UI\Factory $ui_factory;
29  protected ilObjUser $user;
30  protected int $ref_id;
31  protected int $pool_id;
32  protected bool $has_schedule;
33  protected bool $may_edit;
34  protected bool $may_assign;
35  protected ?int $overall_limit = null;
36  protected array $reservations = array();
37  protected int $current_bookings;
38  protected array $advmd;
39  protected array $filter;
41  protected bool $active_management;
42  protected \ilGlobalTemplateInterface $main_tpl;
43 
44  public function __construct(
45  object $a_parent_obj,
46  string $a_parent_cmd,
47  int $a_ref_id,
48  int $a_pool_id,
49  bool $a_pool_has_schedule,
50  ?int $a_pool_overall_limit,
51  bool $active_management = true
52  ) {
53  global $DIC;
54  $this->main_tpl = $DIC->ui()->mainTemplate();
55 
56  $this->ctrl = $DIC->ctrl();
57  $this->lng = $DIC->language();
58  $this->access = $DIC->access();
59  $this->user = $DIC->user();
60  $ilCtrl = $DIC->ctrl();
61  $lng = $DIC->language();
62  $ilAccess = $DIC->access();
63  $this->ui_factory = $DIC->ui()->factory();
64  $this->ui_renderer = $DIC->ui()->renderer();
65 
66  $this->ref_id = $a_ref_id;
67  $this->pool_id = $a_pool_id;
68  $this->has_schedule = $a_pool_has_schedule;
69  $this->overall_limit = $a_pool_overall_limit;
70  $this->active_management = $active_management;
71  $this->may_edit = ($this->active_management &&
72  $ilAccess->checkAccess('write', '', $this->ref_id));
73  $this->may_assign = ($this->active_management &&
74  $ilAccess->checkAccess('write', '', $this->ref_id));
75 
76  $this->advmd = ilObjBookingPool::getAdvancedMDFields($this->ref_id);
77 
78  $this->setId("bkobj");
79 
80  parent::__construct($a_parent_obj, $a_parent_cmd);
81 
82  $this->setTitle($lng->txt("book_booking_objects"));
83 
84  // $this->setLimit(9999);
85 
86  $this->addColumn($this->lng->txt("title"), "title");
87 
88  $cols = $this->getSelectableColumns();
89  foreach ($this->getSelectedColumns() as $col) {
90  $this->addColumn($cols[$col]["txt"], $col);
91  }
92 
93  if (!$this->has_schedule) {
94  $this->addColumn($this->lng->txt("available"));
95  }
96 
97  $this->addColumn($this->lng->txt("actions"));
98 
99  $this->setEnableHeader(true);
100  $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, $a_parent_cmd));
101  $this->setRowTemplate("tpl.booking_object_row.html", "Modules/BookingManager");
102  $this->process_class = $DIC->bookingManager()
103  ->internal()
104  ->gui()
105  ->process()
106  ->getProcessClass($a_pool_has_schedule);
107 
108  $this->initFilter();
109  $this->getItems();
110  }
111 
116  {
117  // #16827
118  return $this->record_gui;
119  }
120 
121  public function initFilter(): void
122  {
123  $lng = $this->lng;
124 
125  // title/description
127  "title",
129  false,
130  $lng->txt("title") . "/" . $lng->txt("description")
131  );
132  if ($title !== null) {
133  $this->filter["title"] = $title->getValue();
134  }
135 
136  // #18651
137  if ($this->has_schedule) {
138  // booking period
139  $period = $this->addFilterItemByMetaType(
140  "period",
142  false,
143  $lng->txt("book_period")
144  );
145  if ($period !== null) {
146  $this->filter["period"] = $period->getValue();
147  }
148  }
149  }
150 
151  public function getItems(): void
152  {
153  $ilUser = $this->user;
154 
155  $data = ilBookingObject::getList($this->pool_id, $this->filter["title"]);
156 
157 
158  // check schedule availability
159  if ($this->has_schedule) {
160  $now = time();
161  $limit = strtotime("+1year");
162  foreach ($data as $idx => $item) {
163  $schedule = new ilBookingSchedule($item["schedule_id"]);
164  $av_from = ($schedule->getAvailabilityFrom() && !$schedule->getAvailabilityFrom()->isNull())
165  ? $schedule->getAvailabilityFrom()->get(IL_CAL_UNIX)
166  : null;
167  $av_to = ($schedule->getAvailabilityTo() && !$schedule->getAvailabilityTo()->isNull())
168  ? strtotime($schedule->getAvailabilityTo()->get(IL_CAL_DATE) . " 23:59:59")
169  : null;
170  if (($av_from && $av_from > $limit)) {
171  unset($data[$idx]);
172  }
173  if ($av_from > $now) {
174  $data[$idx]["not_yet"] = ilDatePresentation::formatDate(new ilDate($av_from, IL_CAL_UNIX));
175  }
176  if ($av_to) {
177  // #18658
178  if (!ilBookingReservation::isObjectAvailableInPeriod($item["booking_object_id"], $schedule, $av_from, $av_to)) {
179  $this->lng->loadLanguageModule("dateplaner");
180  $data[$idx]["full_up"] = $this->lng->txt("cal_booked_out");
181  }
182  }
183  }
184  }
185 
186  foreach ($data as $idx => $item) {
187  $item_id = $item["booking_object_id"];
188 
189  // available for given period?
190  if (isset($this->filter["period"]["from"]) ||
191  isset($this->filter["period"]["to"])) {
192  $from = is_object($this->filter["period"]["from"])
193  ? strtotime($this->filter["period"]["from"]->get(IL_CAL_DATE) . " 00:00:00")
194  : null;
195  $to = is_object($this->filter["period"]["to"])
196  ? strtotime($this->filter["period"]["to"]->get(IL_CAL_DATE) . " 23:59:59")
197  : null;
198 
199  $bobj = new ilBookingObject($item_id);
200  $schedule = new ilBookingSchedule($bobj->getScheduleId());
201 
202  if (!ilBookingReservation::isObjectAvailableInPeriod($item_id, $schedule, $from, $to)) {
203  unset($data[$idx]);
204  continue;
205  }
206  }
207 
208  // cache reservations
209  $item_rsv = ilBookingReservation::getList(array($item_id), 1000, 0, array());
210  $this->reservations[$item_id] = $item_rsv["data"];
211  }
212 
213  if (!$this->has_schedule &&
214  $this->overall_limit) {
215  $this->current_bookings = 0;
216  foreach ($this->reservations as $obj_rsv) {
217  foreach ($obj_rsv as $item) {
218  if ($item["status"] != ilBookingReservation::STATUS_CANCELLED) {
219  if ($item["user_id"] == $ilUser->getId()) {
220  $this->current_bookings++;
221  }
222  }
223  }
224  }
225 
226  if ($this->current_bookings >= $this->overall_limit) {
227  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt("book_overall_limit_warning"));
228  }
229  }
230  if ($this->advmd) {
231  // advanced metadata
232  $this->record_gui = new ilAdvancedMDRecordGUI(
234  "book",
235  $this->pool_id,
236  "bobj"
237  );
238  $this->record_gui->setTableGUI($this);
239  $this->record_gui->parse();
240 
242  $this->ref_id,
243  "book",
244  "bobj",
245  [$this->pool_id],
246  "bobj",
247  $data,
248  "pool_id",
249  "booking_object_id",
250  $this->record_gui->getFilterElements()
251  );
252  }
253 
254  $this->setMaxCount(count($data));
255  $this->setData($data);
256  }
257 
258  public function numericOrdering(string $a_field): bool
259  {
260  if (str_starts_with($a_field, "md_")) {
261  $md_id = (int) substr($a_field, 3);
262  if ($this->advmd[$md_id]["type"] === ilAdvancedMDFieldDefinition::TYPE_DATE) {
263  return true;
264  }
265  }
266  return false;
267  }
268 
269  public function getSelectableColumns(): array
270  {
271  $cols = array();
272 
273  $cols["description"] = array(
274  "txt" => $this->lng->txt("description"),
275  "default" => true
276  );
277 
278  foreach ($this->advmd as $field) {
279  $cols["advmd" . $field["id"]] = array(
280  "txt" => $field["title"],
281  "default" => false
282  );
283  }
284 
285  return $cols;
286  }
287 
288  protected function fillRow(array $a_set): void
289  {
290  $lng = $this->lng;
291  $ilCtrl = $this->ctrl;
292  $ilUser = $this->user;
293 
294  $has_booking = false;
295  $booking_possible = true;
296  $assign_possible = true;
297  $has_reservations = false;
298 
299  $selected = $this->getSelectedColumns();
300 
301  $this->tpl->setVariable("TXT_TITLE", $a_set["title"]);
302 
303  if (in_array("description", $selected, true)) {
304  $this->tpl->setVariable("TXT_DESC", nl2br($a_set["description"] ?? ""));
305  }
306 
307  if (isset($a_set["full_up"])) {
308  $this->tpl->setVariable("NOT_YET", $a_set["full_up"]);
309  $booking_possible = false;
310  $assign_possible = false;
311  } elseif (isset($a_set["not_yet"])) {
312  $this->tpl->setVariable("NOT_YET", $a_set["not_yet"]);
313  }
314 
315  if (!$this->has_schedule) {
316  $cnt = 0;
317  foreach ($this->reservations[$a_set["booking_object_id"]] as $item) {
318  if ($item["status"] != ilBookingReservation::STATUS_CANCELLED) {
319  $cnt++;
320 
321  if ($item["user_id"] == $ilUser->getId()) {
322  $has_booking = true;
323  }
324 
325  $has_reservations = true;
326  }
327  }
328 
329  $this->tpl->setVariable("VALUE_AVAIL", $a_set["nr_items"] - $cnt);
330  $this->tpl->setVariable("VALUE_AVAIL_ALL", $a_set["nr_items"]);
331 
332  if ($a_set["nr_items"] <= $cnt || ($this->overall_limit && $this->current_bookings && $this->current_bookings >= $this->overall_limit)) {
333  $booking_possible = false;
334  }
335  if ($has_booking) {
336  $booking_possible = false;
337  }
338  if ($a_set["nr_items"] <= $cnt) {
339  $assign_possible = false;
340  }
341  } elseif (!$this->may_edit) {
342  foreach ($this->reservations[$a_set["booking_object_id"]] as $item) {
343  if ($item["status"] != ilBookingReservation::STATUS_CANCELLED &&
344  $item["user_id"] == $ilUser->getId()) {
345  $has_booking = true;
346  }
347  }
348  }
349 
350  //Actions
351  $items = array();
352 
353  $ilCtrl->setParameter($this->parent_obj, 'object_id', $a_set['booking_object_id']);
354 
355  if ($booking_possible) {
356  if (isset($this->filter['period']['from'])) {
357  $ilCtrl->setParameter($this->parent_obj, 'sseed', $this->filter['period']['from']->get(IL_CAL_DATE));
358  }
359 
360  $items[] = $this->ui_factory->button()->shy(
361  $lng->txt('book_book'),
362  $ilCtrl->getLinkTargetByClass($this->process_class, 'book')
363  );
364 
365  $ilCtrl->setParameter($this->parent_obj, 'sseed', '');
366  }
367 
368  if ($has_booking || $this->may_edit) {
369  if (trim($a_set['post_text'] ?? "") || $a_set['post_file']) {
370  $items[] = $this->ui_factory->button()->shy(
371  $lng->txt('book_post_booking_information'),
372  $ilCtrl->getLinkTargetByClass($this->process_class, 'displayPostInfo')
373  );
374  }
375  }
376 
377  // #16663
378  if (!$this->has_schedule && $has_booking) {
379  $ilCtrl->setParameterByClass("ilbookingreservationsgui", 'object_id', $a_set['booking_object_id']);
380  $items[] = $this->ui_factory->button()->shy($lng->txt('book_set_cancel'), $ilCtrl->getLinkTargetByClass("ilbookingreservationsgui", 'rsvConfirmCancelUser'));
381  $ilCtrl->setParameterByClass("ilbookingreservationsgui", 'object_id', "");
382  }
383 
384  if ($this->may_edit || $has_booking) {
385  $ilCtrl->setParameterByClass('ilBookingReservationsGUI', 'object_id', $a_set['booking_object_id']);
386  $items[] = $this->ui_factory->button()->shy(
387  $lng->txt('book_log'),
388  $ilCtrl->getLinkTargetByClass('ilBookingReservationsGUI', 'log')
389  );
390  $ilCtrl->setParameterByClass('ilBookingReservationsGUI', 'object_id', '');
391  }
392 
393  if ($this->may_assign && $assign_possible) {
394  // note: this call is currently super expensive
395  // see #26388, it has been performed even for users without edit permissions before
396  // now the call has been moved here, but still this needs improvement
397  // EDIT: deactivated for now due to performance reasons
398  //if (!empty(ilBookingParticipant::getAssignableParticipants($a_set["booking_object_id"]))) {
399  if (isset($this->filter['period']['from'])) {
400  $ilCtrl->setParameterByClass(
401  $this->process_class,
402  'sseed',
403  $this->filter['period']['from']->get(IL_CAL_DATE)
404  );
405  }
406 
407  $items[] = $this->ui_factory->button()->shy(
408  $lng->txt('book_assign_participant'),
409  $ilCtrl->getLinkTargetByClass($this->process_class, 'assignParticipants')
410  );
411 
412  $ilCtrl->setParameterByClass($this->process_class, 'sseed', '');
413  //}
414  }
415 
416  if ($a_set['info_file']) {
417  $items[] = $this->ui_factory->button()->shy($lng->txt('book_download_info'), $ilCtrl->getLinkTarget($this->parent_obj, 'deliverInfo'));
418  }
419 
420  if ($this->may_edit) {
421  $items[] = $this->ui_factory->button()->shy($lng->txt('edit'), $ilCtrl->getLinkTarget($this->parent_obj, 'edit'));
422 
423  // #10890
424  if (!$has_reservations) {
425  $items[] = $this->ui_factory->button()->shy($lng->txt('delete'), $ilCtrl->getLinkTarget($this->parent_obj, 'confirmDelete'));
426  }
427  }
428 
429  if ($this->advmd) {
430  foreach ($this->advmd as $item) {
431  $advmd_id = (int) $item["id"];
432 
433  if (!in_array("advmd" . $advmd_id, $selected)) {
434  continue;
435  }
436 
437  $val = " ";
438  $key = "md_" . $advmd_id . "_presentation";
439  if (isset($a_set[$key])) {
440  $pb = $a_set[$key]->getList();
441  if ($pb) {
442  $val = $pb;
443  }
444  }
445 
446  $this->tpl->setCurrentBlock("advmd_bl");
447  $this->tpl->setVariable("ADVMD_VAL", $val);
448  $this->tpl->parseCurrentBlock();
449  }
450  }
451 
452  if (count($items)) {
453  $actions_dropdown = $this->ui_factory->dropdown()->standard($items)->withLabel($this->lng->txt('actions'));
454  $this->tpl->setVariable("ACTION_DROPDOWN", $this->ui_renderer->render($actions_dropdown));
455  }
456  }
457 }
setData(array $a_data)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
setFormAction(string $a_form_action, bool $a_multipart=false)
__construct(object $a_parent_obj, string $a_parent_cmd, int $a_ref_id, int $a_pool_id, bool $a_pool_has_schedule, ?int $a_pool_overall_limit, bool $active_management=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
static getAdvancedMDFields(int $a_ref_id)
const IL_CAL_UNIX
static getList(array $a_object_ids, int $a_limit=10, int $a_offset=0, array $filter=[])
List all reservations.
ilLanguage $lng
setId(string $a_val)
global $DIC
Definition: feed.php:28
getAdvMDRecordGUI()
needed for advmd filter handling
addFilterItemByMetaType(string $id, int $type=self::FILTER_TEXT, bool $a_optional=false, string $caption="")
Add filter by standard type.
__construct(VocabulariesInterface $vocabularies)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
const IL_CAL_DATE
static queryForRecords(int $adv_rec_obj_ref_id, string $adv_rec_obj_type, string $adv_rec_obj_subtype, array $a_obj_id, string $a_subtype, array $a_records, string $a_obj_id_key, string $a_obj_subid_key, array $a_amet_filter=null)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
static isObjectAvailableInPeriod(int $a_obj_id, ilBookingSchedule $a_schedule, ?int $a_from, ?int $a_to)
static getList(int $a_pool_id, string $a_title=null)
Get list of booking objects.
setEnableHeader(bool $a_enableheader)
setMaxCount(int $a_max_count)
set max.