summaryrefslogtreecommitdiff
path: root/includes/model/NeededAngelTypes_model.php
blob: d8de5e692ef6dc634e52daafdf5dba99050c6767 (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
<?php

use Engelsystem\Database\DB;

/**
 * Entity needed angeltypes describes how many angels of given type are needed for a shift or in a room.
 */

/**
 * Insert a new needed angel type.
 *
 * @param int $shift_id     The shift. Can be null, but then a room_id must be given.
 * @param int $angeltype_id The angeltype
 * @param int $room_id      The room. Can be null, but then a shift_id must be given.
 * @param int $count        How many angels are needed?
 * @return int|false
 */
function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count)
{
    DB::insert('
          INSERT INTO `NeededAngelTypes` ( `shift_id`, `angel_type_id`, `room_id`, `count`)
           VALUES (?, ?, ?, ?)
        ',
        [
            $shift_id,
            $angeltype_id,
            $room_id,
            $count,
        ]);

    return DB::getPdo()->lastInsertId();
}

/**
 * Deletes all needed angel types from given shift.
 *
 * @param int $shift_id id of the shift
 */
function NeededAngelTypes_delete_by_shift($shift_id)
{
    DB::delete('DELETE FROM `NeededAngelTypes` WHERE `shift_id` = ?', [$shift_id]);
}

/**
 * Deletes all needed angel types from given room.
 *
 * @param int $room_id id of the room
 */
function NeededAngelTypes_delete_by_room($room_id)
{
    DB::delete(
        'DELETE FROM `NeededAngelTypes` WHERE `room_id` = ?',
        [$room_id]
    );
}

/**
 * Returns all needed angeltypes and already taken needs.
 *
 * @param int $shiftId id of shift
 * @return array
 */
function NeededAngelTypes_by_shift($shiftId)
{
    $needed_angeltypes_source = DB::select('
        SELECT `NeededAngelTypes`.*, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted`, `AngelTypes`.`no_self_signup`
        FROM `NeededAngelTypes`
        JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id`
        WHERE `shift_id` = ?
        AND `count` > 0
        ORDER BY `room_id` DESC',
        [$shiftId]
    );

    // Use settings from room
    if (count($needed_angeltypes_source) == 0) {
        $needed_angeltypes_source = DB::select('
        SELECT `NeededAngelTypes`.*, `AngelTypes`.`name`, `AngelTypes`.`restricted`
        FROM `NeededAngelTypes`
        JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id`
        JOIN `Shifts` ON `Shifts`.`RID` = `NeededAngelTypes`.`room_id`
        WHERE `Shifts`.`SID` = ?
        AND `count` > 0
        ORDER BY `room_id` DESC
        ', [$shiftId]);
    }

    $shift_entries = ShiftEntries_by_shift($shiftId);
    $needed_angeltypes = [];
    foreach ($needed_angeltypes_source as $angeltype) {
        $angeltype['shift_entries'] = [];
        $angeltype['taken'] = 0;
        foreach ($shift_entries as $shift_entry) {
            if ($shift_entry['TID'] == $angeltype['angel_type_id'] && $shift_entry['freeloaded'] == 0) {
                $angeltype['taken']++;
                $angeltype['shift_entries'][] = $shift_entry;
            }
        }

        $needed_angeltypes[] = $angeltype;
    }

    return $needed_angeltypes;
}