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

/**
 * Counts all freeloaded shifts.
 */
function ShiftEntries_freeleaded_count() {
  return sql_select_single_cell("SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1");
}

/**
 * List users subsribed to a given shift.
 */
function ShiftEntries_by_shift($shift_id) {
  return sql_select("
      SELECT `User`.`Nick`, `User`.`email`, `User`.`email_shiftinfo`, `User`.`Sprache`, `ShiftEntry`.`UID`, `ShiftEntry`.`TID`, `ShiftEntry`.`SID`, `AngelTypes`.`name` as `angel_type_name`, `ShiftEntry`.`Comment`, `ShiftEntry`.`freeloaded`
      FROM `ShiftEntry`
      JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID`
      JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id`
      WHERE `ShiftEntry`.`SID`='" . sql_escape($shift_id) . "'");
}

/**
 * Create a new shift entry.
 *
 * @param ShiftEntry $shift_entry          
 */
function ShiftEntry_create($shift_entry) {
  mail_shift_assign(User($shift_entry['UID']), Shift($shift_entry['SID']));
  return sql_query("INSERT INTO `ShiftEntry` SET
      `SID`='" . sql_escape($shift_entry['SID']) . "',
      `TID`='" . sql_escape($shift_entry['TID']) . "',
      `UID`='" . sql_escape($shift_entry['UID']) . "',
      `Comment`='" . sql_escape($shift_entry['Comment']) . "',
      `freeload_comment`='" . sql_escape($shift_entry['freeload_comment']) . "',
      `freeloaded`=" . sql_bool($shift_entry['freeloaded']));
}

/**
 * Update a shift entry.
 */
function ShiftEntry_update($shift_entry) {
  return sql_query("UPDATE `ShiftEntry` SET
      `Comment`='" . sql_escape($shift_entry['Comment']) . "',
      `freeload_comment`='" . sql_escape($shift_entry['freeload_comment']) . "',
      `freeloaded`=" . sql_bool($shift_entry['freeloaded']) . "
      WHERE `id`='" . sql_escape($shift_entry['id']) . "'");
}

/**
 * Get a shift entry.
 */
function ShiftEntry($shift_entry_id) {
  $shift_entry = sql_select("SELECT * FROM `ShiftEntry` WHERE `id`='" . sql_escape($shift_entry_id) . "'");
  if ($shift_entry === false)
    return false;
  if (count($shift_entry) == 0)
    return null;
  return $shift_entry[0];
}

/**
 * Delete a shift entry.
 */
function ShiftEntry_delete($shift_entry_id) {
  $shift_entry = ShiftEntry($shift_entry_id);
  mail_shift_removed(User($shift_entry['UID']), Shift($shift_entry['SID']));
  return sql_query("DELETE FROM `ShiftEntry` WHERE `id`='" . sql_escape($shift_entry_id) . "'");
}

/**
 * Returns next (or current) shifts of given user.
 *
 * @param User $user          
 */
function ShiftEntries_upcoming_for_user($user) {
  return sql_select("
      SELECT *
      FROM `ShiftEntry`
      JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`)
      JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
      WHERE `ShiftEntry`.`UID`=" . sql_escape($user['UID']) . "
      AND `Shifts`.`end` > " . sql_escape(time()) . "
      ORDER BY `Shifts`.`end`
      ");
}

/**
 * Returns all shift entries in given shift for given angeltype.
 *
 * @param int $shift_id          
 * @param int $angeltype_id          
 */
function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) {
  return sql_select("
      SELECT *
      FROM `ShiftEntry`
      WHERE `SID`=" . sql_escape($shift_id) . "
      AND `TID`=" . sql_escape($angeltype_id) . "
      ");
}

/**
 * Returns all freeloaded shifts for given user.
 */
function ShiftEntries_freeloaded_by_user($user) {
  return sql_select("SELECT *
      FROM `ShiftEntry`
      WHERE `freeloaded` = 1
      AND `UID`=" . sql_escape($user['UID']));
}

?>