engelsystem/includes/model/Stats.php

208 lines
6.7 KiB
PHP
Raw Normal View History

2017-12-12 21:57:57 +01:00
<?php
2017-12-25 23:12:52 +01:00
2017-12-12 21:57:57 +01:00
use Engelsystem\Database\Db;
/**
* Returns the number of angels currently working.
2017-12-25 23:12:52 +01:00
*
* @return int|string
2017-12-12 21:57:57 +01:00
*/
function stats_currently_working()
{
$result = Db::selectOne("
SELECT SUM(
(SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
2017-12-25 23:12:52 +01:00
) AS `count`
2017-12-12 21:57:57 +01:00
FROM `Shifts`
WHERE (`end` >= ? AND `start` <= ?)", [
time(),
time()
]);
2017-12-25 23:12:52 +01:00
2017-12-12 21:57:57 +01:00
if (empty($result['count'])) {
return '-';
}
2017-12-25 23:12:52 +01:00
2017-12-12 21:57:57 +01:00
return $result['count'];
}
/**
* Return the number of hours still to work.
2017-12-25 23:12:52 +01:00
*
* @return int|string
2017-12-12 21:57:57 +01:00
*/
function stats_hours_to_work()
{
$result = Db::selectOne("
2017-12-25 23:12:52 +01:00
SELECT ROUND(SUM(`count`)) AS `count` FROM (
SELECT
(SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
2017-12-25 23:12:52 +01:00
* (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` >= ?
AND s.shift_id IS NULL
2017-12-14 19:39:47 +01:00
UNION ALL
SELECT
(SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`room_id`=`Shifts`.`RID`)
2017-12-25 23:12:52 +01:00
* (`Shifts`.`end` - `Shifts`.`start`)/3600 AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` >= ?
AND NOT s.shift_id IS NULL
2017-12-25 23:12:52 +01:00
) AS `tmp`
", [
time(),
2017-12-12 21:57:57 +01:00
time()
]);
if (empty($result['count'])) {
return '-';
}
return $result['count'];
}
/**
* Returns the number of needed angels in the next 3 hours
2017-12-25 23:12:52 +01:00
*
* @return int|string
2017-12-12 21:57:57 +01:00
*/
function stats_angels_needed_three_hours()
{
$now = time();
$in3hours = $now + 3 * 60 * 60;
$result = Db::selectOne("
2017-12-25 23:12:52 +01:00
SELECT SUM(`count`) AS `count` FROM (
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
SELECT SUM(`count`)
FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
) - (
SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
AND s.shift_id IS NULL
2017-12-14 19:39:47 +01:00
UNION ALL
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
SELECT SUM(`count`)
FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
) - (
SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
AND NOT s.shift_id IS NULL
2017-12-25 23:12:52 +01:00
) AS `tmp`", [
$now,
$in3hours,
2017-12-12 21:57:57 +01:00
$now,
$in3hours
]);
if (empty($result['count'])) {
return '-';
}
return $result['count'];
}
/**
2018-09-17 12:33:15 +02:00
* Returns the number of needed angels for nightshifts (see config)
2017-12-25 23:12:52 +01:00
*
* @return int|string
2017-12-12 21:57:57 +01:00
*/
function stats_angels_needed_for_nightshifts()
{
2018-09-17 12:33:15 +02:00
$nightShiftsConfig = config('night_shifts');
$nightStartTime = $nightShiftsConfig['start'];
$nightEndTime = $nightShiftsConfig['end'];
2017-12-25 23:12:52 +01:00
$night_start = parse_date(
'Y-m-d H:i',
2018-09-17 12:33:15 +02:00
date('Y-m-d', time() + 12 * 60 * 60) . ' ' . $nightStartTime . ':00'
2017-12-25 23:12:52 +01:00
);
2018-09-17 12:33:15 +02:00
$night_end = $night_start + ($nightEndTime - $nightStartTime) * 60 * 60;
2017-12-12 21:57:57 +01:00
$result = Db::selectOne("
2017-12-25 23:12:52 +01:00
SELECT SUM(`count`) AS `count` FROM (
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
SELECT SUM(`count`)
FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
) - (
SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
AND s.shift_id IS NULL
2017-12-14 19:39:47 +01:00
UNION ALL
SELECT
2017-12-26 10:47:39 +01:00
GREATEST(0,
(
SELECT SUM(`count`)
FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
) - (
SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID`
2017-12-26 17:35:34 +01:00
WHERE `AngelTypes`.`show_on_dashboard`=TRUE
2017-12-26 10:47:39 +01:00
AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0
)
)
2017-12-25 23:12:52 +01:00
AS `count`
FROM `Shifts`
LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `end` > ? AND `start` < ?
AND NOT s.shift_id IS NULL
2017-12-25 23:12:52 +01:00
) AS `tmp`", [
$night_start,
$night_end,
2017-12-12 21:57:57 +01:00
$night_start,
$night_end
]);
if (empty($result['count'])) {
return '-';
}
return $result['count'];
}