Add statistics feature with bookings tracking functionality

Introduce a scalable statistics architecture including `statistics` class, interface, and related implementations. Added support for tracking new bookings with routes and data handling through `bookings_s` and `statistic_t`. Integrated the feature into the application using autoloaders and response mechanisms.
This commit is contained in:
Jepp9350
2025-01-31 12:32:22 +01:00
parent ca52d3b248
commit e09eedb010
6 changed files with 226 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace classes;
use interfaces\statistics_i;
use statistics\bookings_s;
class statistics implements statistics_i
{
/**
* @inheritDoc
*/
public function bookings(): bookings_s
{
return new bookings_s();
}
}
+8
View File
@@ -28,6 +28,13 @@ foreach ( glob(WD . '/traits/*.php') as $trait ) {
require_once $trait;
}
/**
* Load all statistics
*/
foreach ( glob(WD . '/statistics/*.php') as $statistic ) {
require_once $statistic;
}
/** Load all Classes */
require_once 'classes/encrypt.php';
require_once 'classes/authentication.php';
@@ -43,6 +50,7 @@ require_once 'classes/slack.php';
require_once 'classes/wordpress_bookings_remote.php';
require_once 'classes/language_packs.php';
require_once 'classes/economic.php';
require_once 'classes/statistics.php';
/**
* Modules
@@ -0,0 +1,14 @@
<?php
namespace interfaces;
use statistics\bookings_s;
interface statistics_i
{
/**
* Get the statistics for bookings
* @return bookings_s
*/
public function bookings(): bookings_s;
}
@@ -0,0 +1,48 @@
<?php
namespace routes;
use classes\authentication;
use classes\response;
use classes\statistics;
use objects\logs_o;
use traits\route_t;
class statisticsRoute
{
use route_t;
public function run(): void
{
/** Statistics >> Bookings >> New bookings */
$this->get('/statistics/bookings/new', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_bookings_new');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'NEW_BOOKINGS', 'User requested new bookings');
$statics = new statistics();
// Return the list of bookings
$response->success(
$statics
->bookings()
->get_new_bookings()
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'NEW_BOOKINGS', 'User requested new bookings');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}
@@ -0,0 +1,36 @@
<?php
namespace statistics;
use traits\statistic_t;
class bookings_s
{
use statistic_t;
public function get_new_bookings(): self
{
// Get the departments
$departments_o = new \objects\departments_o();
// Get the bookings
$bookings_o = new \objects\bookings_o();
// Get the bookings
$bookings = $bookings_o->getFields(['id', 'department', 'date']);
// Get the departments
$departments = $departments_o->getFields(['id', 'name']);
// Add the labels (months)
$this->labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
// Add the labels, and the data sets
foreach ( $departments as $department ) {
$tmp_department_monthly = [];
foreach ( $this->labels as $label ) {
$tmp_department_monthly[$label] = 0;
}
$this->add_data_set($department['name'], $tmp_department_monthly, ['backgroundColor' => '#FF0000']);
}
return $this;
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
namespace traits;
use statistics\bookings_s;
trait statistic_t
{
public array $data_sets = [];
public array $labels = [];
/**
* The start date of the date range
* This is set by the set_date_range method, and is used to filter the data.
* All dates are in the format 'YYYY-MM-DD'
* If the start date is not set, the default is fetched from self::get_default_start_date()
* @var string
*/
public string $start;
/**
* The end date of the date range
* This is set by the set_date_range method, and is used to filter the data.
* All dates are in the format 'YYYY-MM-DD'
* If the end date is not set, the default is fetched from self::get_default_end_date()
* @var string
*/
public string $end;
/**
* Set the date range for the statistics
*/
public function set_date_range(string $start, string $end): self
{
$this->start = $start;
$this->end = $end;
return $this;
}
/**
* Get the default start date for the statistics
*/
public function get_default_start_date(): string
{
return date('Y-m-d', strtotime('-1 month'));
}
/**
* Get the default end date for the statistics
*/
public function get_default_end_date(): string
{
return date('Y-m-d');
}
/**
* Add a label to the statistics
* @param string $label The label to add to the statistics (e.g. 'January')
* @return bookings_s|statistic_t
*/
public function add_label(string $label): self
{
$this->labels[] = $label;
return $this;
}
/**
* Add a data set to the statistics
* @param string $label The label for the data set (e.g. 'January')
* @param array $data_set The data set to add to the statistics (e.g. [1, 2, 3, 4])
* @param array $options The options for the data set (e.g. ['backgroundColor' => '#FF0000'])
* @return bookings_s|statistic_t
*/
public function add_data_set(string $label, array $data_set, array $options = []): self
{
// Apply the default options
$options = array_merge([
'label' => 'Dataset ' . (count($this->data_sets) + 1),
'backgroundColor' => '#FF0000',
], $options);
// Add the data set
$this->data_sets[] = [
'label' => $label,
'backgroundColor' => $options['backgroundColor'],
'data' => $data_set
];
return $this;
}
/**
* Get the response for the statistics
* @return array
*/
public function get_response(): array
{
return [
'labels' => $this->labels,
'datasets' => $this->data_sets
];
}
}