ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.WeekGridGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
19 
24 {
25  protected string $form_action;
26  protected int $week_start;
27  protected \ilDate $seed;
28  protected string $seed_str;
29  protected int $time_format;
30  protected \ilLanguage $lng;
31  protected int $day_end;
32  protected int $day_start;
33  protected string $title;
34  protected array $entries;
35 
40  public function __construct(
41  array $entries = [],
42  \ilDate $seed = null,
43  int $day_start = 8,
44  int $day_end = 19,
45  int $time_format = \ilCalendarSettings::TIME_FORMAT_24,
46  int $week_start = \ilCalendarSettings::WEEK_START_MONDAY
47  ) {
48  global $DIC;
49 
50  $this->title = "Test";
51  $this->form_action = "#";
52  $this->lng = $DIC->language();
53  $this->day_start = $day_start;
54  $this->day_end = $day_end;
55  $this->time_format = $time_format;
56  $this->seed = $seed ?? new \ilDate(time(), IL_CAL_UNIX);
57  $this->week_start = $week_start;
58  $this->entries = $entries;
59  }
60 
61  protected function getHoursOfDay(): array
62  {
63  $hours = array();
64  $sep = "<br>-<br>";
65  for ($i = $this->day_start;$i <= $this->day_end;$i++) {
66  $caption = "";
67  $start = sprintf('%02d:00', $i);
68  if ($this->day_start > 0 && $i === $this->day_start) {
69  $start = sprintf('%02d:00', 0);
70  $end = sprintf('%02d:00', $i + 1);
71  } else {
72  $end = sprintf('%02d:00', $i + 1);
73  }
74  if ($this->day_end < 23 && $i === $this->day_end) {
75  $end = sprintf('%02d:00', 23 + 1);
76  }
77  switch ($this->time_format) {
78  case \ilCalendarSettings::TIME_FORMAT_12:
79  if ($this->day_start > 0 && $i === $this->day_start) {
80  $caption = date('h a', mktime(0, 0, 0, 1, 1, 2000)) . $sep;
81  }
82  $caption .= date('h a', mktime($i, 0, 0, 1, 1, 2000));
83  if ($this->day_end < 23 && $i === $this->day_end) {
84  $caption .= $sep . date('h a', mktime(23, 0, 0, 1, 1, 2000));
85  }
86  break;
87 
88  default:
89  if ($this->day_start > 0 && $i === $this->day_start) {
90  $caption = sprintf('%02d:00', 0) . $sep;
91  }
92  $caption .= sprintf('%02d:00', $i);
93  if ($this->day_end < 23 && $i === $this->day_end) {
94  $caption .= $sep . sprintf('%02d:00', 23);
95  }
96  break;
97  }
98  $hours[$i] = [
99  "caption" => $caption,
100  "start" => $start,
101  "end" => $end
102  ];
103  }
104  return $hours;
105  }
106 
112  protected function buildCellData(): array
113  {
114  $morning_aggr = $this->day_start;
115  $evening_aggr = $this->day_end;
116  $hours = $this->getHoursOfDay();
117  $week_start = $this->week_start;
119  $cells = [];
120  $week = 0;
121  foreach (\ilCalendarUtil::_buildWeekDayList($this->seed, $week_start)->get() as $date) {
122  foreach ($hours as $hour => $data) {
123  $start = new \ilDateTime($date->get(IL_CAL_DATE) . " " . $data["start"] . ":00", IL_CAL_DATETIME);
124  $end = new \ilDateTime($date->get(IL_CAL_DATE) . " " . $data["end"] . ":00", IL_CAL_DATETIME);
125  $data["start_ts"] = $start->get(IL_CAL_UNIX);
126  $data["end_ts"] = $end->get(IL_CAL_UNIX);
127  $data["entries"] = $this->getEntriesForCell($data["start_ts"], $data["end_ts"]);
128  $cells[$week][$hour] = $data;
129  // store how much slots are max. to be displayed in parallel per day
130  $cells[$week]["col_span"] = max(count($data["entries"]), $cells[$week]["col_span"] ?? 1);
131  }
132  $week++;
133  }
134  return $cells;
135  }
136 
137  public function render(): string
138  {
139  $mytpl = new \ilTemplate(
140  'tpl.week_grid.html',
141  true,
142  true,
143  'components/ILIAS/BookingManager/BookingProcess'
144  );
145 
146  $cells = $this->buildCellData();
147 
148 
149  $weekday_list = \ilCalendarUtil::_buildWeekDayList($this->seed, $this->week_start)->get();
150  $start = current($weekday_list);
151  $end = end($weekday_list);
152  $mytpl->setVariable("TXT_OBJECT", $this->lng->txt('week') . ' ' . $this->seed->get(IL_CAL_FKT_DATE, 'W') .
153  ", " . \ilDatePresentation::formatDate($start) . " - " .
155 
156  $mytpl->setVariable('TXT_TITLE', $this->lng->txt('book_reservation_title'));
157  $mytpl->setVariable('TIME', $this->lng->txt('time'));
158 
159 
160  $day_of_week = 0;
161  reset($weekday_list);
162  foreach ($weekday_list as $date) {
163  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', 'UTC');
164  $mytpl->setCurrentBlock('weekdays');
165  $mytpl->setVariable('TXT_WEEKDAY', \ilCalendarUtil::_numericDayToString((int) $date_info['wday']));
166  $mytpl->setVariable('COL_SPAN', $cells[$day_of_week]["col_span"]);
167  $mytpl->setVariable('WIDTH', "12");
168  $mytpl->setVariable('TXT_DATE', $date_info['mday'] . ' ' . \ilCalendarUtil::_numericMonthToString($date_info['mon']));
169  $mytpl->parseCurrentBlock();
170  $day_of_week++;
171  }
172 
173  $hours = $this->getHoursOfDay();
174 
175  foreach ($hours as $hour => $days) {
176  $caption = $days["caption"];
177  $day_of_week = 0;
178  foreach (\ilCalendarUtil::_buildWeekDayList($this->seed, $this->week_start)->get() as $date) {
179  $data = $cells[$day_of_week][$hour];
180  $total_tds = $cells[$day_of_week]["col_span"];
181  foreach ($data["entries"] as $e) {
182  // starting in cell? show it
184  if ($e->getStart() >= $data["start_ts"] && $e->getStart() < $data["end_ts"]) {
185  $mytpl->setCurrentBlock('dates');
186  $mytpl->setVariable('CONTENT', $e->getHTML());
187  $row_span = max(1, ceil(($e->getEnd() - $data["end_ts"]) / 3600) + 1);
188  $mytpl->setVariable('ROW_SPAN', $row_span);
189  $mytpl->parseCurrentBlock();
190  }
191  $total_tds--;
192  }
193  while ($total_tds > 0) {
194  $mytpl->setCurrentBlock('dates');
195  $mytpl->setVariable('CONTENT', "&nbsp;");
196  $mytpl->parseCurrentBlock();
197  $total_tds--;
198  }
199  $day_of_week++;
200  }
201 
202  $mytpl->setCurrentBlock('slots');
203  $mytpl->setVariable('TXT_HOUR', $caption);
204  $mytpl->parseCurrentBlock();
205  }
206  //\ilPropertyFormGUI::initJavascript();
207  return $mytpl->get();
208  }
209 
214  protected function getEntriesForCell(int $start_ts, int $end_ts): array
215  {
216  return array_filter($this->entries, function ($e) use ($start_ts, $end_ts) {
218  return ($e->getStart() < $end_ts && $e->getEnd() > $start_ts);
219  });
220  }
221 
222  protected function renderCell(array $data)
223  {
224  return "&nbsp;";
225  }
226 }
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, ilObjUser $user=null,)
const IL_CAL_DATETIME
static _buildWeekDayList(ilDate $a_day, int $a_weekstart)
build week day list public
static _numericMonthToString(int $a_month, bool $a_long=true, ilLanguage $lng=null)
numeric month to string
const IL_CAL_UNIX
global $DIC
Definition: shib_login.php:25
const IL_CAL_FKT_DATE
__construct(array $entries=[], \ilDate $seed=null, int $day_start=8, int $day_end=19, int $time_format=\ilCalendarSettings::TIME_FORMAT_24, int $week_start=\ilCalendarSettings::WEEK_START_MONDAY)
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
static _numericDayToString(int $a_day, bool $a_long=true, ilLanguage $lng=null)