summaryrefslogtreecommitdiff
path: root/includes/model
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model')
-rw-r--r--includes/model/ShiftEntry_model.php3
-rw-r--r--includes/model/ShiftTypes_model.php66
-rw-r--r--includes/model/Shifts_model.php98
3 files changed, 156 insertions, 11 deletions
diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php
index e1f0cd2e..5129f15a 100644
--- a/includes/model/ShiftEntry_model.php
+++ b/includes/model/ShiftEntry_model.php
@@ -76,7 +76,8 @@ function ShiftEntries_upcoming_for_user($user) {
return sql_select("
SELECT *
FROM `ShiftEntry`
- JOIN `Shifts` ON `Shifts`.`SID`=`ShiftEntry`.`SID`
+ 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`
diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php
new file mode 100644
index 00000000..7b502585
--- /dev/null
+++ b/includes/model/ShiftTypes_model.php
@@ -0,0 +1,66 @@
+<?php
+
+/**
+ * Delete a shift type.
+ * @param int $shifttype_id
+ */
+function ShiftType_delete($shifttype_id) {
+ return sql_query("DELETE FROM `ShiftTypes` WHERE `id`=" . sql_escape($shifttype_id));
+}
+
+/**
+ * Update a shift type.
+ *
+ * @param int $shifttype_id
+ * @param string $name
+ * @param int $angeltype_id
+ * @param string $description
+ */
+function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) {
+ return sql_query("UPDATE `ShiftTypes` SET
+ `name`='" . sql_escape($name) . "',
+ `angeltype_id`=" . sql_null($angeltype_id) . ",
+ `description`='" . sql_escape($description) . "'
+ WHERE `id`=" . sql_escape($shifttype_id));
+}
+
+/**
+ * Create a shift type.
+ *
+ * @param string $name
+ * @param int $angeltype_id
+ * @param string $description
+ * @return new shifttype id
+ */
+function ShiftType_create($name, $angeltype_id, $description) {
+ $result = sql_query("INSERT INTO `ShiftTypes` SET
+ `name`='" . sql_escape($name) . "',
+ `angeltype_id`=" . sql_null($angeltype_id) . ",
+ `description`='" . sql_escape($description) . "'");
+ if ($result === false)
+ return false;
+ return sql_id();
+}
+
+/**
+ * Get a shift type by id.
+ *
+ * @param int $shifttype_id
+ */
+function ShiftType($shifttype_id) {
+ $shifttype = sql_select("SELECT * FROM `ShiftTypes` WHERE `id`=" . sql_escape($shifttype_id));
+ if ($shifttype === false)
+ return false;
+ if ($shifttype == null)
+ return null;
+ return $shifttype[0];
+}
+
+/**
+ * Get all shift types.
+ */
+function ShiftTypes() {
+ return sql_select("SELECT * FROM `ShiftTypes` ORDER BY `name`");
+}
+
+?> \ No newline at end of file
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index 8530a0c8..37c772bf 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -1,6 +1,75 @@
<?php
/**
+ * Check if a shift collides with other shifts (in time).
+ * @param Shift $shift
+ * @param array<Shift> $shifts
+ */
+function Shift_collides($shift, $shifts) {
+ foreach ($shifts as $other_shift)
+ if ($shift['SID'] != $other_shift['SID'])
+ if (! ($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start']))
+ return true;
+ return false;
+}
+
+/**
+ * Check if an angel can sign up for given shift.
+ *
+ * @param Shift $shift
+ * @param AngelType $angeltype
+ * @param array<Shift> $user_shifts
+ */
+function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_shifts = null) {
+ global $user, $privileges;
+
+ if ($user_shifts == null) {
+ $user_shifts = Shifts_by_user($user);
+ if ($user_shifts === false)
+ engelsystem_error('Unable to load users shifts.');
+ }
+
+ $collides = Shift_collides($shift, $user_shifts);
+
+ if ($user_angeltype == null) {
+ $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
+ if ($user_angeltype === false)
+ engelsystem_error('Unable to load user angeltype.');
+ }
+
+ $signed_up = false;
+ foreach ($user_shifts as $user_shift)
+ if ($user_shift['SID'] == $shift['SID']) {
+ $signed_up = true;
+ break;
+ }
+
+ // 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 you already singed up for this shift
+ $user_may_join_shift &= ! $signed_up;
+
+ // you cannot join if user is not of this angel type
+ $user_may_join_shift &= $user_angeltype != null;
+
+ // you cannot join if you are not confirmed
+ if ($angeltype['restricted'] == 1 && $user_angeltype != null)
+ $user_may_join_shift &= isset($user_angeltype['confirm_user_id']);
+
+ // you can only join if the shift is in future
+ $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);
+
+ return $user_may_join_shift;
+}
+
+/**
* Delete a shift by its external id.
*/
function Shift_delete_by_psid($shift_psid) {
@@ -12,7 +81,7 @@ function Shift_delete_by_psid($shift_psid) {
*/
function Shift_delete($shift_id) {
mail_shift_delete(Shift($shift_id));
-
+
return sql_query("DELETE FROM `Shifts` WHERE `SID`=" . sql_escape($shift_id));
}
@@ -20,14 +89,15 @@ function Shift_delete($shift_id) {
* Update a shift.
*/
function Shift_update($shift) {
- $old_shift = Shift($shift['SID']);
+ $shift['name'] = ShiftType($shift['shifttype_id'])['name'];
mail_shift_change(Shift($shift['SID']), $shift);
-
+
return sql_query("UPDATE `Shifts` SET
+ `shifttype_id`=" . sql_escape($shift['shifttype_id']) . ",
`start`=" . sql_escape($shift['start']) . ",
`end`=" . sql_escape($shift['end']) . ",
`RID`=" . sql_escape($shift['RID']) . ",
- `name`=" . sql_null($shift['name']) . ",
+ `title`=" . sql_null($shift['title']) . ",
`URL`=" . sql_null($shift['URL']) . ",
`PSID`=" . sql_null($shift['PSID']) . "
WHERE `SID`=" . sql_escape($shift['SID']));
@@ -42,7 +112,7 @@ function Shift_update_by_psid($shift) {
return false;
if (count($shift_source) == 0)
return null;
- $shift['SID'] = $shift_source['SID'];
+ $shift['SID'] = $shift_source[0]['SID'];
return Shift_update($shift);
}
@@ -53,10 +123,11 @@ function Shift_update_by_psid($shift) {
*/
function Shift_create($shift) {
$result = sql_query("INSERT INTO `Shifts` SET
+ `shifttype_id`=" . sql_escape($shift['shifttype_id']) . ",
`start`=" . sql_escape($shift['start']) . ",
`end`=" . sql_escape($shift['end']) . ",
`RID`=" . sql_escape($shift['RID']) . ",
- `name`=" . sql_null($shift['name']) . ",
+ `title`=" . sql_null($shift['title']) . ",
`URL`=" . sql_null($shift['URL']) . ",
`PSID`=" . sql_null($shift['PSID']));
if ($result === false)
@@ -69,9 +140,10 @@ function Shift_create($shift) {
*/
function Shifts_by_user($user) {
return sql_select("
- SELECT *
+ SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.*, `Shifts`.*, `Room`.*
FROM `ShiftEntry`
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
+ JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
WHERE `UID`=" . sql_escape($user['UID']) . "
ORDER BY `start`
@@ -130,8 +202,12 @@ function Shifts_filtered() {
* ID
*/
function Shift($id) {
- $shifts_source = sql_select("SELECT * FROM `Shifts` WHERE `SID`=" . sql_escape($id) . " LIMIT 1");
- $shiftsEntry_source = sql_select("SELECT `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`=" . sql_escape($id));
+ $shifts_source = sql_select("
+ SELECT `Shifts`.*, `ShiftTypes`.`name`
+ FROM `Shifts`
+ JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
+ WHERE `SID`=" . sql_escape($id));
+ $shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`=" . sql_escape($id));
if ($shifts_source === false)
return false;
@@ -139,6 +215,7 @@ function Shift($id) {
$result = $shifts_source[0];
$result['ShiftEntry'] = $shiftsEntry_source;
+ $result['NeedAngels'] = [];
$temp = NeededAngelTypes_by_shift($id);
foreach ($temp as $e) {
@@ -160,8 +237,9 @@ function Shift($id) {
*/
function Shifts() {
$shifts_source = sql_select("
- SELECT `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name`
+ SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name`
FROM `Shifts`
+ JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID`
");
if ($shifts_source === false)