engelsystem/includes/model/ShiftEntry_model.php

252 lines
5.5 KiB
PHP
Raw Normal View History

2013-12-09 17:10:07 +01:00
<?php
use Engelsystem\Database\DB;
/**
* Returns an array with the attributes of shift entries.
* FIXME! Needs entity object.
2017-01-03 03:22:48 +01:00
*
* @return array
*/
2017-01-02 03:57:23 +01:00
function ShiftEntry_new()
{
return [
2017-01-02 15:43:36 +01:00
'id' => null,
'SID' => null,
'TID' => null,
'UID' => null,
'Comment' => null,
'freeloaded_comment' => null,
'freeloaded' => false
];
}
2014-09-28 15:01:02 +02:00
/**
* Counts all freeloaded shifts.
2017-01-03 03:22:48 +01:00
*
* @return int
2014-09-28 15:01:02 +02:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_freeleaded_count()
{
$result = DB::select('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1');
$result = array_shift($result);
if (!is_array($result)) {
return 0;
}
return (int)array_shift($result);
2014-09-28 15:01:02 +02:00
}
2014-12-07 17:07:19 +01:00
/**
* List users subsribed to a given shift.
2017-01-03 03:22:48 +01:00
*
* @param int $shift_id
* @return array
2014-12-07 17:07:19 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_by_shift($shift_id)
{
2017-01-21 19:37:42 +01:00
return DB::select('
SELECT
`User`.`Nick`,
`User`.`email`,
`User`.`email_shiftinfo`,
`User`.`Sprache`,
`User`.`Gekommen`,
`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`
2017-01-21 19:37:42 +01:00
WHERE `ShiftEntry`.`SID` = ?',
[$shift_id]
);
}
2014-12-07 16:45:09 +01:00
/**
* Create a new shift entry.
*
2017-01-03 03:22:48 +01:00
* @param array $shift_entry
* @return bool
2014-12-07 16:45:09 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntry_create($shift_entry)
{
mail_shift_assign(User($shift_entry['UID']), Shift($shift_entry['SID']));
return DB::insert('
INSERT INTO `ShiftEntry` (
`SID`,
`TID`,
`UID`,
`Comment`,
`freeload_comment`,
`freeloaded`
)
VALUES(?, ?, ?, ?, ?, ?)
',
[
$shift_entry['SID'],
$shift_entry['TID'],
$shift_entry['UID'],
$shift_entry['Comment'],
$shift_entry['freeload_comment'],
$shift_entry['freeloaded'],
]
);
2014-12-07 16:45:09 +01:00
}
2014-12-07 17:13:14 +01:00
/**
* Update a shift entry.
2017-01-03 03:22:48 +01:00
*
* @param array $shift_entry
* @return bool
2014-12-07 17:13:14 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntry_update($shift_entry)
{
DB::update('
UPDATE `ShiftEntry`
SET
`Comment` = ?,
`freeload_comment` = ?,
`freeloaded` = ?
WHERE `id` = ?',
[
$shift_entry['Comment'],
$shift_entry['freeload_comment'],
$shift_entry['freeloaded'],
$shift_entry['id']
]
);
return (DB::getStm()->errorCode() == '00000');
2014-12-07 17:13:14 +01:00
}
2014-12-07 20:29:01 +01:00
/**
* Get a shift entry.
2017-01-03 03:22:48 +01:00
*
* @param int $shift_entry_id
* @return array|false|null
2014-12-07 20:29:01 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntry($shift_entry_id)
{
$shift_entry = DB::select('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]);
if (DB::getStm()->errorCode() != '00000') {
2017-01-02 03:57:23 +01:00
return false;
}
if (empty($shift_entry)) {
2017-01-02 03:57:23 +01:00
return null;
}
return $shift_entry[0];
2014-12-07 20:29:01 +01:00
}
2014-12-07 17:07:19 +01:00
/**
* Delete a shift entry.
2017-01-03 03:22:48 +01:00
*
* @param int $shift_entry_id
* @return bool
2014-12-07 17:07:19 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntry_delete($shift_entry_id)
{
$shift_entry = ShiftEntry($shift_entry_id);
mail_shift_removed(User($shift_entry['UID']), Shift($shift_entry['SID']));
return DB::delete('DELETE FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]);
2014-12-07 17:07:19 +01:00
}
2014-08-23 01:55:18 +02:00
/**
* Returns next (or current) shifts of given user.
2014-12-07 16:45:09 +01:00
*
2017-01-03 03:22:48 +01:00
* @param array $user
* @return array
2014-08-23 01:55:18 +02:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_upcoming_for_user($user)
{
return DB::select('
SELECT *
FROM `ShiftEntry`
JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`)
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
WHERE `ShiftEntry`.`UID` = ?
AND `Shifts`.`end` > ?
ORDER BY `Shifts`.`end`
',
[
$user['UID'],
time(),
]
);
2014-08-23 01:55:18 +02:00
}
/**
* Returns shifts completed by the given user.
*
2017-01-03 03:22:48 +01:00
* @param array $user
* @return array
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_finished_by_user($user)
{
return DB::select('
SELECT *
FROM `ShiftEntry`
JOIN `Shifts` ON (`Shifts`.`SID` = `ShiftEntry`.`SID`)
JOIN `ShiftTypes` ON `ShiftTypes`.`id` = `Shifts`.`shifttype_id`
WHERE `ShiftEntry`.`UID` = ?
AND `Shifts`.`end` < ?
AND `ShiftEntry`.`freeloaded` = 0
ORDER BY `Shifts`.`end`
',
[
$user['UID'],
time(),
]
);
}
2013-12-09 17:10:07 +01:00
/**
* Returns all shift entries in given shift for given angeltype.
2014-08-23 01:55:18 +02:00
*
2017-01-02 03:57:23 +01:00
* @param int $shift_id
* @param int $angeltype_id
* @return array
2013-12-09 17:10:07 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id)
{
$result = DB::select('
SELECT *
FROM `ShiftEntry`
WHERE `SID` = ?
AND `TID` = ?
',
[
$shift_id,
$angeltype_id,
]
);
if (DB::getStm()->errorCode() != '00000') {
2017-01-03 14:12:17 +01:00
engelsystem_error('Unable to load shift entries.');
2017-01-02 03:57:23 +01:00
}
return $result;
2013-12-09 17:10:07 +01:00
}
2013-12-27 18:45:27 +01:00
/**
* Returns all freeloaded shifts for given user.
2017-01-03 03:22:48 +01:00
*
* @param array $user
* @return array
2013-12-27 18:45:27 +01:00
*/
2017-01-02 03:57:23 +01:00
function ShiftEntries_freeloaded_by_user($user)
{
return DB::select('
SELECT *
FROM `ShiftEntry`
WHERE `freeloaded` = 1
AND `UID` = ?
',
[
$user['UID']
]
);
2013-12-27 18:45:27 +01:00
}