summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-12-12 21:57:57 +0100
committermsquare <msquare@notrademark.de>2017-12-12 21:57:57 +0100
commitff94df53d69234ae2625462a76926e131ade05b7 (patch)
treeb5ee27bd8461a06a2b7e082faa48c6fa66fc560f /includes
parentd5631297dc42c06b1695fdb6f65d2b6a4b04c633 (diff)
finish basic public dashboard
Diffstat (limited to 'includes')
-rw-r--r--includes/controller/public_dashboard_controller.php66
-rw-r--r--includes/includes.php1
-rw-r--r--includes/model/Shifts_model.php23
-rw-r--r--includes/model/Stats.php94
-rw-r--r--includes/sys_template.php25
-rw-r--r--includes/view/PublicDashboard_view.php69
6 files changed, 203 insertions, 75 deletions
diff --git a/includes/controller/public_dashboard_controller.php b/includes/controller/public_dashboard_controller.php
index dc81ba2c..bf909bed 100644
--- a/includes/controller/public_dashboard_controller.php
+++ b/includes/controller/public_dashboard_controller.php
@@ -1,70 +1,22 @@
<?php
-use Engelsystem\Database\Db;
/**
* Loads all data for the public dashboard
*/
function public_dashboard_controller()
{
- $stats = [];
-
- $now = time();
- $in3hours = $now + 3 * 60 * 60;
- $result = Db::selectOne("
- SELECT SUM(
- (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
- - (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
- ) as `count`
- FROM `Shifts`
- WHERE ((`end` > ? AND `end` < ?) OR (`start` > ? AND `start` < ?))", [
- $now,
- $in3hours,
- $now,
- $in3hours
- ]);
- $stats['needed-3-hours'] = $result['count'];
-
- $night_start = parse_date('Y-m-d H:i', date('Y-m-d', time() + 12 * 60 * 60) . ' 02:00');
- $night_end = $night_start + 6 * 60 * 60;
- $result = Db::selectOne("
- SELECT SUM(
- (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
- - (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
- ) as `count`
- FROM `Shifts`
- WHERE ((`end` > ? AND `end` < ?) OR (`start` > ? AND `start` < ?))", [
- $night_start,
- $night_end,
- $night_start,
- $night_end
- ]);
- $stats['needed-night'] = $result['count'];
-
- $result = Db::selectOne("
- SELECT SUM(
- (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
- ) as `count`
- FROM `Shifts`
- WHERE (`end` >= ? AND `start` <= ?)", [
- time(),
- time()
- ]);
- $stats['angels-working'] = $result['count'];
+ $stats = [
+ 'needed-3-hours' => stats_angels_needed_three_hours(),
+ 'needed-night' => stats_angels_needed_for_nightshifts(),
+ 'angels-working' => stats_currently_working(),
+ 'hours-to-work' => stats_hours_to_work()
+ ];
- $result = Db::selectOne("
- SELECT ROUND(SUM(
- (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
- * (`Shifts`.`end` - `Shifts`.`start`)/3600
- )) as `count`
- FROM `Shifts`
- WHERE `end` >= ?", [
- time()
- ]);
- $stats['hours-to-work'] = $result['count'];
+ $free_shifts = Shifts_free(time(), time() + 12 * 60 * 60);
return [
- 'Engelsystem Public Dashboard',
- public_dashboard_view($stats)
+ _('Engelsystem Public Dashboard'),
+ public_dashboard_view($stats, $free_shifts)
];
}
?> \ No newline at end of file
diff --git a/includes/includes.php b/includes/includes.php
index c533cfe8..b6bd41cf 100644
--- a/includes/includes.php
+++ b/includes/includes.php
@@ -22,6 +22,7 @@ $includeFiles = [
__DIR__ . '/../includes/model/ShiftsFilter.php',
__DIR__ . '/../includes/model/ShiftSignupState.php',
__DIR__ . '/../includes/model/ShiftTypes_model.php',
+ __DIR__ . '/../includes/model/Stats.php',
__DIR__ . '/../includes/model/UserAngelTypes_model.php',
__DIR__ . '/../includes/model/UserDriverLicenses_model.php',
__DIR__ . '/../includes/model/UserGroups_model.php',
diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php
index 1fc7fe89..b0d82a5b 100644
--- a/includes/model/Shifts_model.php
+++ b/includes/model/Shifts_model.php
@@ -27,6 +27,29 @@ function Shifts_by_angeltype($angeltype) {
}
/**
+ * Returns every shift with needed angels in the given time range.
+ */
+function Shifts_free($start, $end)
+{
+ $shifts = Db::select("
+ SELECT *
+ FROM `Shifts`
+ WHERE (`end` > ? AND `start` < ?)
+ AND (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
+ > (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
+ ORDER BY `start`
+ ", [
+ $start,
+ $end
+ ]);
+ $free_shifts = [];
+ foreach ($shifts as $shift) {
+ $free_shifts[] = Shift($shift['SID']);
+ }
+ return $free_shifts;
+}
+
+/**
* Returns all shifts with a PSID (from frab import)
*/
function Shifts_from_frab() {
diff --git a/includes/model/Stats.php b/includes/model/Stats.php
new file mode 100644
index 00000000..5618cdc6
--- /dev/null
+++ b/includes/model/Stats.php
@@ -0,0 +1,94 @@
+<?php
+use Engelsystem\Database\Db;
+
+/**
+ * Returns the number of angels currently working.
+ */
+function stats_currently_working()
+{
+ $result = Db::selectOne("
+ SELECT SUM(
+ (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
+ ) as `count`
+ FROM `Shifts`
+ WHERE (`end` >= ? AND `start` <= ?)", [
+ time(),
+ time()
+ ]);
+ if (empty($result['count'])) {
+ return '-';
+ }
+ return $result['count'];
+}
+
+/**
+ * Return the number of hours still to work.
+ */
+function stats_hours_to_work()
+{
+ $result = Db::selectOne("
+ SELECT ROUND(SUM(
+ (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
+ * (`Shifts`.`end` - `Shifts`.`start`)/3600
+ )) as `count`
+ FROM `Shifts`
+ WHERE `end` >= ?", [
+ time()
+ ]);
+ if (empty($result['count'])) {
+ return '-';
+ }
+ return $result['count'];
+}
+
+/**
+ * Returns the number of needed angels in the next 3 hours
+ */
+function stats_angels_needed_three_hours()
+{
+ $now = time();
+ $in3hours = $now + 3 * 60 * 60;
+ $result = Db::selectOne("
+ SELECT SUM(
+ (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
+ - (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
+ ) as `count`
+ FROM `Shifts`
+ WHERE ((`end` > ? AND `end` < ?) OR (`start` > ? AND `start` < ?))", [
+ $now,
+ $in3hours,
+ $now,
+ $in3hours
+ ]);
+ if (empty($result['count'])) {
+ return '-';
+ }
+ return $result['count'];
+}
+
+/**
+ * Returns the number of needed angels for nightshifts (between 2 and 8)
+ */
+function stats_angels_needed_for_nightshifts()
+{
+ $night_start = parse_date('Y-m-d H:i', date('Y-m-d', time() + 12 * 60 * 60) . ' 02:00');
+ $night_end = $night_start + 6 * 60 * 60;
+ $result = Db::selectOne("
+ SELECT SUM(
+ (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
+ - (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
+ ) as `count`
+ FROM `Shifts`
+ WHERE ((`end` > ? AND `end` < ?) OR (`start` > ? AND `start` < ?))", [
+ $night_start,
+ $night_end,
+ $night_start,
+ $night_end
+ ]);
+ if (empty($result['count'])) {
+ return '-';
+ }
+ return $result['count'];
+}
+
+?> \ No newline at end of file
diff --git a/includes/sys_template.php b/includes/sys_template.php
index 819b7d1c..b5846e9b 100644
--- a/includes/sys_template.php
+++ b/includes/sys_template.php
@@ -1,6 +1,31 @@
<?php
/**
+ * Render a stat for dashborad (big number with label).
+ * If no style given, style is danger if number > 0, and success if number == 0.
+ *
+ * @param string $label
+ * @param string $number
+ * @param string $style default, warning, danger or success. Optional.
+ */
+function stats($label, $number, $style = null)
+{
+ if(empty($style)) {
+ if($number > 0) {
+ $style = 'danger';
+ } else {
+ $style = 'success';
+ }
+ }
+ return div('stats stats-' . $style, [
+ $label,
+ div('number', [
+ $number
+ ])
+ ]);
+}
+
+/**
* Renders tabs from the array. Array key is tab name, array value is tab content.
*
* @param array $tabs
diff --git a/includes/view/PublicDashboard_view.php b/includes/view/PublicDashboard_view.php
index 6fa40ed4..7b15c7dd 100644
--- a/includes/view/PublicDashboard_view.php
+++ b/includes/view/PublicDashboard_view.php
@@ -3,29 +3,62 @@
/**
* Public dashboard (formerly known as angel news hub)
*/
-function public_dashboard_view($stats)
+function public_dashboard_view($stats, $free_shifts)
{
+ $shift_panels = [];
+ foreach ($free_shifts as $shift) {
+ $shift_panels[] = public_dashborad_shift_render($shift);
+ }
return page([
- div('first', [
- div('col-xs-3 text-center', [
- _('Angels needed in the next 3 hrs'),
- heading($stats['needed-3-hours'], 1)
- ]),
- div('col-xs-3 text-center', [
- _('Angels needed for nightshifts'),
- heading($stats['needed-night'], 1)
- ]),
- div('col-xs-3 text-center', [
- _('Angels currently working'),
- heading($stats['angels-working'], 1)
- ]),
- div('col-xs-3 text-center', [
- _('Hours to be worked'),
- heading($stats['hours-to-work'], 1)
- ]),
+ div('first container-fluid', [
+ stats(_('Angels needed in the next 3 hrs'), $stats['needed-3-hours']),
+ stats(_('Angels needed for nightshifts'), $stats['needed-night']),
+ stats(_('Angels currently working'), $stats['angels-working'], 'default'),
+ stats(_('Hours to be worked'), $stats['hours-to-work'], 'default'),
'<script>$(function(){setTimeout(function(){window.location.reload();}, 60000)})</script>'
+ ]),
+ div('container-fluid first', [
+ heading(_('Needed angels:'), 1),
+ join($shift_panels)
])
]);
}
+/**
+ * Renders a single shift panel for a dashboard shift with needed angels
+ */
+function public_dashborad_shift_render($shift)
+{
+ $style = 'default';
+ if (time() + 3 * 60 * 60 > $shift['start']) {
+ $style = 'warning';
+ }
+ if (time() > $shift['start']) {
+ $style = 'danger';
+ }
+
+ $panel_body = glyph('time') . date('H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']);
+
+ $panel_body .= '<br>' . glyph('tasks') . ShiftType($shift['shifttype_id'])['name'];
+ if (! empty($shift['title'])) {
+ $panel_body .= ' (' . $shift['title'] . ')';
+ }
+
+ $panel_body .= '<br>' . glyph('map-marker') . Room($shift['RID'])['Name'];
+
+ foreach ($shift['NeedAngels'] as $needed_angels) {
+ $need = $needed_angels['count'] - $needed_angels['taken'];
+ if ($need > 0) {
+ $panel_body .= '<br>' . glyph('user') . $need . ' &times; ' . AngelType($needed_angels['TID'])['name'];
+ }
+ }
+
+ $panel_body = '<a href="' . shift_link($shift) . '">' . $panel_body . '</a>';
+
+ return div('panel panel-' . $style . ' col-xs-3', [
+ div('panel-body', [
+ heading($panel_body, 4)
+ ])
+ ]);
+}
?> \ No newline at end of file