ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BookingDataProvider.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use ilUtil;
29use ilObjUser;
30use ilObject;
31use ilLink;
32use DateTimeImmutable;
33use ilArrayUtil;
38use ilDate;
39use ilDateTime;
40
42{
43 private int $user_id;
44
45 private array $data = [];
46 private StaticURL\Services $static_url_service;
47 private \ILIAS\UI\Factory $ui_factory;
48
51
52 public function __construct(int $user_id, string $vm_period, string $vm_status)
53 {
54 global $DIC;
55
56 $this->user_id = $user_id;
57 $this->vm_status = $vm_status;
58 $this->vm_period = $vm_period;
59 $this->static_url_service = $DIC['static_url'];
60 $this->ui_factory = $DIC->ui()->factory();
61 $this->read();
62 }
63
64 public function getUserId(): int
65 {
66 return $this->user_id;
67 }
68
69 protected function read(): void
70 {
71 $data = [];
72 $counter = 0;
73 foreach (ilConsultationHourAppointments::getAppointments($this->getUserId()) as $appointment) {
74 $booking = new ilBookingEntry($appointment->getContextId());
75 $booked_user_ids = $booking->getCurrentBookings($appointment->getEntryId());
76 if ($this->isFiltered($appointment, $booking, $booked_user_ids)) {
77 continue;
78 }
79
80 $row = [];
81 $row['id'] = (string) $appointment->getEntryId();
82 $row['booking_start'] = (new \DateTimeImmutable())->setTimestamp($appointment->getStart()->getUnixTime());
83 $row['booking_duration'] =
84 (int) ($appointment->getEnd()->getUnixTime() - $appointment->getStart()->getUnixTime()) / 60;
85 $row['booking_title'] = $appointment->getTitle();
86
87 $obj_ids = array_map(
88 'intval',
89 ilUtil::_sortIds($booking->getTargetObjIds(), 'object_data', 'title', 'obj_id')
90 );
91 // locations
92 $locations = [];
93 foreach ($obj_ids as $obj_id) {
94 $link = null;
95 foreach (ilObject::_getAllReferences($obj_id) as $ref_id) {
96 $link = $this->static_url_service->builder()->build(
97 ilObject::_lookupType($obj_id),
99 );
100 $locations[] = $this->ui_factory->link()->standard(
101 ilObject::_lookupTitle($obj_id),
102 (string) $link
103 );
104 break;
105 }
106 }
107 $row['booking_location'] = $this->ui_factory->listing()->unordered($locations);
108
109 // booking users
110 $booked_user_ids = array_map('intval', ilUtil::_sortIds($booked_user_ids, 'usr_data', 'lastname', 'usr_id'));
111 $bookings = [];
112 $comments = [];
113 foreach ($booked_user_ids as $booked_user_id) {
114 $fullname = ilObjUser::_lookupFullname($booked_user_id);
115 $link = \ilUserUtil::getProfileLink($booked_user_id);
116 $ui_link = $this->ui_factory->link()->standard(
117 $fullname,
118 $link ? $link : '#'
119 );
120 if ($link === '') {
121 $ui_link = $ui_link->withDisabled(true);
122 }
123 $bookings[] = $ui_link;
124
126 $appointment->getEntryId(),
127 $booked_user_id
128 );
129 if (trim($comment) !== '') {
130 $ui_link = $this->ui_factory->link()
131 ->standard(
132 $fullname . ': "' . $comment . '"',
133 '#'
134 )
135 ->withDisabled(true);
136 $comments[] = $ui_link;
137 }
138
139 }
140 $row['booking_participant'] = $this->ui_factory->listing()->unordered($bookings);
141 $row['booking_comment'] = $this->ui_factory->listing()->unordered($comments);
142 $data[$counter++] = $row;
143 }
144 $this->data = $data;
145 }
146
147 public function isFiltered(ilCalendarEntry $entry, ilBookingEntry $booking, array $booked_users): bool
148 {
149 if (
152 ) {
153 return false;
154 }
155 $now = new ilDate(time(), IL_CAL_UNIX);
156 if ($this->vm_period === ilConsultationHoursGUI::VIEW_MODE_PERIOD_UPCOMING) {
157 if (ilDateTime::_before($entry->getStart(), $now, IL_CAL_DAY)) {
158 return true;
159 }
160 }
161 if ($this->vm_period === ilConsultationHoursGUI::VIEW_MODE_PERIOD_PAST) {
162 if (ilDateTime::_after($entry->getStart(), $now, IL_CAL_DAY)) {
163 return true;
164 }
165 }
166 if ($this->vm_status === ilConsultationHoursGUI::VIEW_MODE_STATUS_OPEN) {
167 if (count($booked_users) >= $booking->getNumberOfBookings()) {
168 return true;
169 }
170 }
171 if ($this->vm_status === ilConsultationHoursGUI::VIEW_MODE_STATUS_BOOKED) {
172 if (count($booked_users) < $booking->getNumberOfBookings()) {
173 return true;
174 }
175 }
176 return false;
177 }
178
179 public function getData(): array
180 {
181 return $this->data;
182 }
183
184 public function sortData(Order $order): array
185 {
186 $data = $this->getData();
187 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
188 usort($data, fn($a, $b) => $a[$order_field] <=> $b[$order_field]);
189 if ($order_direction === 'DESC') {
190 $data = array_reverse($data);
191 }
192 return $data;
193 }
194
195 public function limitData(Range $range, Order $order): array
196 {
197 return array_slice($this->sortData($order), $range->getStart(), $range->getLength());
198 }
199
200
201
202}
$comment
Definition: buildRTE.php:72
isFiltered(ilCalendarEntry $entry, ilBookingEntry $booking, array $booked_users)
__construct(int $user_id, string $vm_period, string $vm_status)
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
const IL_CAL_UNIX
const IL_CAL_DAY
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Booking definition.
static lookupBookingMessage(int $a_entry_id, int $a_usr_id)
Model for a calendar entry.
getStart()
Get start of date period.
static getAppointments(int $a_user_id)
Get all appointments.
Consultation hours editor.
Class for date presentation.
@classDescription Date and time handling
static _after(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
Class for single dates.
User class.
static _lookupFullname(int $a_user_id)
Class ilObject Basic functions for all objects.
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupTitle(int $obj_id)
static getProfileLink(int $a_usr_id)
Get link to personal profile Return empty string in case of not public profile.
Util class various functions, usage as namespace.
static _sortIds(array $a_ids, string $a_table, string $a_field, string $a_id_name)
Function that sorts ids by a given table field using WHERE IN E.g: __sort(array(6,...
$ref_id
Definition: ltiauth.php:66
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26
$counter
$comments