From ccbb0eac64cdbb64359bc5d5f007effee8cda56a Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Sun, 5 Jan 2025 21:24:55 +0100 Subject: [PATCH] Added: Stats in dashboard --- app/controllers/DashboardController.php | 26 +++++++++ app/models/Refuel.php | 55 +++++++++++++++++++ app/models/Vehicle.php | 18 +++++- app/views/dashboard/index.php | 73 +++++++++++++++---------- core/Database.php | 1 + public/css/dashboard.css | 2 +- 6 files changed, 143 insertions(+), 32 deletions(-) diff --git a/app/controllers/DashboardController.php b/app/controllers/DashboardController.php index 2066317..9a097ad 100644 --- a/app/controllers/DashboardController.php +++ b/app/controllers/DashboardController.php @@ -4,9 +4,35 @@ class DashboardController extends Controller { $vehicle = new Vehicle(); $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); + $default_car = $vehicle->getDefaultVehicle($_SESSION['user']['id']); + + $refuel = new Refuel(); + $data = [ + "date" => [], + "price" => [], + ]; + $raw_data = $refuel->latest_data($default_car['id'], 5); + foreach($raw_data as $one) { + array_push($data['date'], date('d. m.', strtotime($one['created_at']))); + array_push($data['price'], $one['price_per_liter']); + } + + $latest_record = [ + 'name', + 'liters', + 'price_per_liter', + 'total_price', + 'created_at' + ]; + + $latest_record = $refuel->latest_one($_SESSION['user']['id'])[0]; + $this->view('dashboard/index', [ 'title' => 'Dashboard', 'vehicles' => $vehicles, + 'date_price_data' => $data, + 'default_car' => $default_car, + 'latest_record' => $latest_record, ]); } diff --git a/app/models/Refuel.php b/app/models/Refuel.php index e5cbc47..5029936 100644 --- a/app/models/Refuel.php +++ b/app/models/Refuel.php @@ -34,4 +34,59 @@ class Refuel { return $e->getMessage(); } } + + public function latest_data($vehicle_id, $record_count) { + try { + $stmt = $this->db->prepare(" + SELECT `liters`, `price_per_liter`, `total_price`, `created_at` + FROM `refueling_records` + WHERE `vehicle_id` = ? + ORDER BY created_at DESC + LIMIT ?; + "); + + $stmt->bind_param("ii", $vehicle_id, $record_count); + if ($stmt->execute()) { + $result = $stmt->get_result(); + $data = $result->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return array_reverse($data); + } else { + return "Error: " . $stmt->error; + } + } catch (mysqli_sql_exception $e) { + return $e->getMessage(); + } + } + + public function latest_one($user_id, $record_count = 1) { + try { + $stmt = $this->db->prepare(" + SELECT + `r`.`vehicle_id`, + `v`.`name` AS `vehicle_name`, + `r`.`liters`, + `r`.`price_per_liter`, + `r`.`total_price`, + `r`.`created_at` + FROM `refueling_records` AS `r` + JOIN `vehicles` AS `v` ON `r`.`vehicle_id` = `v`.`id` + WHERE `r`.`user_id` = ? + ORDER BY `r`.`created_at` DESC + LIMIT ?; + "); + + $stmt->bind_param("ii", $user_id, $record_count); + if ($stmt->execute()) { + $result = $stmt->get_result(); + $data = $result->fetch_all(MYSQLI_ASSOC); + $stmt->close(); + return array_reverse($data); + } else { + return "Error: " . $stmt->error; + } + } catch (mysqli_sql_exception $e) { + return $e->getMessage(); + } + } } diff --git a/app/models/Vehicle.php b/app/models/Vehicle.php index 42dd2f1..3832345 100644 --- a/app/models/Vehicle.php +++ b/app/models/Vehicle.php @@ -33,9 +33,9 @@ class Vehicle { } } - public function getVehiclesByUser($userId) { + public function getVehiclesByUser($user_id) { $stmt = $this->db->prepare("SELECT id, name, registration_plate, fuel_type, note, created_at FROM vehicles WHERE user_id = ?"); - $stmt->bind_param("i", $userId); + $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); @@ -46,4 +46,18 @@ class Vehicle { return $vehicles; } + + public function getDefaultVehicle($user_id) { + $stmt = $this->db->prepare(" + SELECT id, name, registration_plate, fuel_type, note, is_default + FROM vehicles + WHERE user_id = ? AND is_default = TRUE + LIMIT 1 + "); + $stmt->bind_param("i", $user_id); + $stmt->execute(); + $result = $stmt->get_result(); + + return $result->fetch_assoc(); + } } diff --git a/app/views/dashboard/index.php b/app/views/dashboard/index.php index 559b53e..4d91474 100644 --- a/app/views/dashboard/index.php +++ b/app/views/dashboard/index.php @@ -7,41 +7,56 @@ List all vehicles
-
-

Upcoming

-
- Habit Title -

Frequency

-

Reward points

+
+

Latest fuel record

+
+
+ Car: +

+ Liters: +

liters

+ Price per liter: +

,-/liter

+ Total price: +

,-

-
-

Recent

-
- Habit Title -

Frequency

-

Reward points

-
-
- -
-

Missed

-
- Habit Title -

Frequency

-

Reward points

-
+
+

Default car

+
+ Car +

+ Registration plate +

+ Fuel type +

+ Note +

-

Graph of History

-
- -
-

Streak

-

You're current streak is 123 days

-

Good job!

+

Chart of Gas price

+
+

+
+ + diff --git a/core/Database.php b/core/Database.php index c1d51ae..277b140 100644 --- a/core/Database.php +++ b/core/Database.php @@ -79,6 +79,7 @@ class Database { registration_plate VARCHAR(50) NOT NULL UNIQUE, fuel_type ENUM('Diesel', 'Gasoline 95', 'Gasoline 98', 'Premium Diesel', 'Premium Gasoline 95', 'Premium Gasoline 98', 'Other') NOT NULL, note VARCHAR(150) NULL, + is_default BOOLEAN DEFAULT FALSE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB; diff --git a/public/css/dashboard.css b/public/css/dashboard.css index a6aa7ec..e1106bd 100644 --- a/public/css/dashboard.css +++ b/public/css/dashboard.css @@ -16,6 +16,6 @@ background-color: var(--clr-secondary); border-radius: var(--border-radious); border: var(--borderWidth-thin) solid var(--clr-border); - min-width: 17rem; + width: 18rem; padding: 1rem; }