summaryrefslogtreecommitdiff
path: root/includes/view/ShiftCalendarRenderer.php
blob: ab13a5f366c2e9b074207b0fd2adce715272241c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php

namespace Engelsystem;

class ShiftCalendarRenderer {

  /**
   * 15m * 60s/m = 900s
   */
  const MINUTES_PER_ROW = 900;

  const EMPTY_CELL = '<td class="empty"></td>';

  private $shifts;

  private $shiftsFilter;

  public function __construct($shifts, ShiftsFilter $shiftsFilter) {
    $this->shifts = $shifts;
    $this->shiftsFilter = $shiftsFilter;
  }

  public function render() {
    $rooms = $this->rooms();
    
    $first_block_start_time = $this->calcFirstBlockStartTime();
    $blocks_per_slot = $this->calcBlocksPerSlot($first_block_start_time);
    
    $slotSizes = $this->calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot);
    
    return $this->renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot);
  }

  private function renderTableHead($rooms, $slotSizes) {
    $shifts_table = '<thead><tr><th>' . _("Time") . '</th>';
    foreach ($rooms as $room_id => $room_name) {
      $colspan = $slotSizes[$room_id];
      $shifts_table .= "<th" . (($colspan > 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([
          'RID' => $room_id,
          'Name' => $room_name 
      ]) . "</th>\n";
    }
    $shifts_table .= "</tr></thead>";
    return $shifts_table;
  }

  private function initTableBody($slotSizes, $first_block_start_time, $blocks_per_slot) {
    // Slot sizes plus 1 for the time
    $columns_needed = array_sum($slotSizes) + 1;
    $table_line = array_fill(0, $columns_needed, ShiftCalendarRenderer::EMPTY_CELL);
    $table = array_fill(0, $blocks_per_slot, $table_line);
    
    for ($block = 0; $block < $blocks_per_slot; $block ++) {
      $thistime = $first_block_start_time + ($block * ShiftCalendarRenderer::MINUTES_PER_ROW);
      if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $this->shiftsFilter->getEndTime() - $this->shiftsFilter->getStartTime() > 24 * 60 * 60) {
        $table[$block][0] = '<th class="row-day">' . date('Y-m-d<b\r />H:i', $thistime) . '</th>';
      } elseif ($thistime % (60 * 60) == 0) {
        $table[$block][0] = '<th class="row-hour">' . date('H:i', $thistime) . '</th>';
      } else {
        $table[$block][0] = '<th class="empty"></th>';
      }
    }
    
    return $table;
  }

  private function calcRoomSlots($rooms, $slotSizes) {
    $result = [];
    $slot = 1; // 1 for the time
    foreach (array_keys($rooms) as $room_id) {
      $result[$room_id] = $slot;
      $slot += $slotSizes[$room_id];
    }
    
    return $result;
  }

  private function collides() {
    // TODO
    return false;
  }

  private function renderShift($shift) {
    global $privileges;
    
    $collides = $this->collides();
    $is_free = false;
    $shifts_row = '';
    $header_buttons = "";
    if (in_array('admin_shifts', $privileges)) {
      $header_buttons = '<div class="pull-right">' . table_buttons([
          button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'),
          button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') 
      ]) . '</div>';
    }
    $info_text = "";
    if ($shift['title'] != '') {
      $info_text = glyph('info-sign') . $shift['title'] . '<br>';
    }
    
    $angeltypes = NeededAngelTypes_by_shift($shift['SID']);
    foreach ($angeltypes as $angeltype) {
      $entry_list = [];
      $freeloader = 0;
      foreach ($angeltype['shift_entries'] as $entry) {
        $style = '';
        if ($entry['freeloaded']) {
          $freeloader ++;
          $style = " text-decoration: line-through;";
        }
        if (in_array('user_shifts_admin', $privileges)) {
          $entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . ' ' . table_buttons([
              button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') 
          ]) . '</span>';
        } else {
          $entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . "</span>";
        }
      }
      if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) {
        $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries']));
        // is the shift still running or alternatively is the user shift admin?
        $user_may_join_shift = true;
        
        // you cannot join if user alread joined a parallel or this shift
        $user_may_join_shift &= ! $collides;
        
        // you cannot join if user is not of this angel type
        $user_may_join_shift &= isset($angeltype['user_id']);
        
        // you cannot join if you are not confirmed
        if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) {
          $user_may_join_shift &= isset($angeltype['confirm_user_id']);
        }
        
        // you can only join if the shift is in future or running
        $user_may_join_shift &= time() < $shift['start'];
        
        // User shift admins may join anybody in every shift
        $user_may_join_shift |= in_array('user_shifts_admin', $privileges);
        if ($user_may_join_shift) {
          $entry_list[] = '<a href="' . page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'] . '">' . $inner_text . '</a> ' . button(page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs');
        } else {
          if (time() > $shift['start']) {
            $entry_list[] = $inner_text . ' (' . _('ended') . ')';
          } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) {
            $entry_list[] = $inner_text . glyph('lock');
          } elseif ($angeltype['restricted'] == 1) {
            $entry_list[] = $inner_text;
          } elseif ($collides) {
            $entry_list[] = $inner_text;
          } else {
            $entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs');
          }
        }
        
        unset($inner_text);
        $is_free = true;
      }
      
      $shifts_row .= '<li class="list-group-item">';
      $shifts_row .= '<strong>' . AngelType_name_render($angeltype) . ':</strong> ';
      $shifts_row .= join(", ", $entry_list);
      $shifts_row .= '</li>';
    }
    if (in_array('user_shifts_admin', $privileges)) {
      $shifts_row .= '<li class="list-group-item">' . button(page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '</li>';
    }
    if ($shifts_row != '') {
      $shifts_row = '<ul class="list-group">' . $shifts_row . '</ul>';
    }
    if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) {
      $class = 'primary';
    } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) {
      $class = 'default';
    } elseif ($is_free) {
      $class = 'danger';
    } else {
      $class = 'success';
    }
    
    $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW);
    if ($blocks < 1) {
      $blocks = 1;
    }
    return [
        $blocks,
        '<td class="shift" rowspan="' . $blocks . '">' . div('panel panel-' . $class, [
            div('panel-heading', [
                date('H:i', $shift['start']),
                '&dash;',
                date('H:i', $shift['end']),
                '&mdash;',
                ShiftType($shift['shifttype_id'])['name'],
                $header_buttons 
            ]),
            div('panel-body', [
                $info_text,
                Room_name_render([
                    'RID' => $shift['RID'],
                    'Name' => $shift['room_name'] 
                ]) 
            ]),
            $shifts_row 
        ]) . '</td>' 
    ];
  }

  private function renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) {
    $table = $this->initTableBody($slotSizes, $first_block_start_time, $blocks_per_slot);
    
    $room_slots = $this->calcRoomSlots($rooms, $slotSizes);
    
    foreach ($this->shifts as $shift) {
      list($blocks, $shift_content) = $this->renderShift($shift);
      $start_block = floor(($shift['start'] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW);
      $slot = $room_slots[$shift['RID']];
      while ($table[$start_block][$slot] != ShiftCalendarRenderer::EMPTY_CELL) {
        $slot ++;
      }
      $table[$start_block][$slot] = $shift_content;
      for ($block = 1; $block < $blocks; $block ++) {
        $table[$start_block + $block][$slot] = '';
      }
    }
    
    $result = '<tbody>';
    foreach ($table as $table_line) {
      $result .= '<tr>' . join('', $table_line) . '</tr>';
    }
    $result .= '</tbody>';
    return $result;
  }

  private function renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) {
    return div('shifts-table', [
        '<table id="shifts" class="table scrollable">',
        $this->renderTableHead($rooms, $slotSizes),
        $this->renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot),
        '</table>' 
    ]);
  }

  /**
   * Calculates the slots for each room that appears in the shifts
   */
  private function rooms() {
    $rooms = [];
    foreach ($this->shifts as $shift) {
      if (! isset($rooms[$shift['RID']])) {
        $rooms[$shift['RID']] = $shift['room_name'];
      }
    }
    return $rooms;
  }

  private function calcFirstBlockStartTime() {
    $start_time = $this->shiftsFilter->getEndTime();
    foreach ($this->shifts as $shift) {
      if ($shift['start'] < $start_time) {
        $start_time = $shift['start'];
      }
    }
    return ShiftCalendarRenderer::MINUTES_PER_ROW * floor(($start_time - 60 * 60) / ShiftCalendarRenderer::MINUTES_PER_ROW);
  }

  private function calcBlocksPerSlot($first_block_start_time) {
    return ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW);
  }

  private function calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot) {
    $parallel_blocks = [];
    
    // initialize $block array
    foreach (array_keys($rooms) as $room_id) {
      $parallel_blocks[$room_id] = array_fill(0, $blocks_per_slot, 0);
    }
    
    // calculate number of parallel shifts in each timeslot for each room
    foreach ($this->shifts as $shift) {
      $room_id = $shift["RID"];
      $shift_blocks = ($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW;
      $firstblock = floor(($shift["start"] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW);
      for ($block = $firstblock; $block < $shift_blocks + $firstblock && $block < $blocks_per_slot; $block ++) {
        $parallel_blocks[$room_id][$block] ++;
      }
    }
    
    return array_map('max', $parallel_blocks);
  }
}

?>