Added: Stats in dashboard
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build and Deploy Zola Website / build_and_deploy (push) Successful in 12s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build and Deploy Zola Website / build_and_deploy (push) Successful in 12s
				
			This commit is contained in:
		@@ -4,9 +4,35 @@ class DashboardController extends Controller {
 | 
				
			|||||||
        $vehicle = new Vehicle();
 | 
					        $vehicle = new Vehicle();
 | 
				
			||||||
        $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
 | 
					        $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', [
 | 
					        $this->view('dashboard/index', [
 | 
				
			||||||
            'title' => 'Dashboard',
 | 
					            'title' => 'Dashboard',
 | 
				
			||||||
            'vehicles' => $vehicles,
 | 
					            'vehicles' => $vehicles,
 | 
				
			||||||
 | 
					            'date_price_data' => $data,
 | 
				
			||||||
 | 
					            'default_car' => $default_car,
 | 
				
			||||||
 | 
					            'latest_record' => $latest_record,
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,4 +34,59 @@ class Refuel {
 | 
				
			|||||||
            return $e->getMessage();
 | 
					            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();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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 = $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();
 | 
					        $stmt->execute();
 | 
				
			||||||
        $result = $stmt->get_result();
 | 
					        $result = $stmt->get_result();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -46,4 +46,18 @@ class Vehicle {
 | 
				
			|||||||
        
 | 
					        
 | 
				
			||||||
        return $vehicles;
 | 
					        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();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,41 +7,56 @@
 | 
				
			|||||||
        <a href="/vehicles" class="btn-primary">List all vehicles</a>
 | 
					        <a href="/vehicles" class="btn-primary">List all vehicles</a>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <div class="card-wrapper">
 | 
					    <div class="card-wrapper">
 | 
				
			||||||
        <section class="card upcoming">
 | 
					        <section class="card latest">
 | 
				
			||||||
            <h2>Upcoming</h2>
 | 
					            <h2>Latest fuel record</h2>
 | 
				
			||||||
            <div class="habit">
 | 
					            <hr>
 | 
				
			||||||
                <b>Habit Title</b>
 | 
					            <div>
 | 
				
			||||||
                <p>Frequency</p>
 | 
					                <b>Car:</b>
 | 
				
			||||||
                <p>Reward points</p>
 | 
					                <p><?= $data['latest_record']['vehicle_name'] ?></p>
 | 
				
			||||||
 | 
					                <b>Liters:</b>
 | 
				
			||||||
 | 
					                <p><?= $data['latest_record']['liters'] ?> liters</p>
 | 
				
			||||||
 | 
					                <b>Price per liter:</b>
 | 
				
			||||||
 | 
					                <p><?= $data['latest_record']['price_per_liter'] ?>,-/liter</p>
 | 
				
			||||||
 | 
					                <b>Total price:</b>
 | 
				
			||||||
 | 
					                <p><?= $data['latest_record']['total_price'] ?>,-</p>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
        </section>
 | 
					        </section>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <section class="card recent">
 | 
					        <section class="card">
 | 
				
			||||||
            <h2>Recent</h2>
 | 
					            <h2>Default car</h2>
 | 
				
			||||||
            <div class="habit">
 | 
					            <hr>
 | 
				
			||||||
                <b>Habit Title</b>
 | 
					            <b>Car</b>
 | 
				
			||||||
                <p>Frequency</p>
 | 
					            <p><?= $data['default_car']['name'] ?></p>
 | 
				
			||||||
                <p>Reward points</p>
 | 
					            <b>Registration plate</b>
 | 
				
			||||||
            </div>
 | 
					            <p><?= $data['default_car']['registration_plate'] ?></p>
 | 
				
			||||||
        </section>
 | 
					            <b>Fuel type</b>
 | 
				
			||||||
 | 
					            <p><?= $data['default_car']['fuel_type'] ?></p>
 | 
				
			||||||
        <section class="card missed">
 | 
					            <b>Note</b>
 | 
				
			||||||
            <h2>Missed</h2>
 | 
					            <p><?= $data['default_car']['note'] ?></p>
 | 
				
			||||||
            <div class="habit">
 | 
					 | 
				
			||||||
                <b>Habit Title</b>
 | 
					 | 
				
			||||||
                <p>Frequency</p>
 | 
					 | 
				
			||||||
                <p>Reward points</p>
 | 
					 | 
				
			||||||
            </div>
 | 
					 | 
				
			||||||
        </section>
 | 
					        </section>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <section class="card history-graph">
 | 
					        <section class="card history-graph">
 | 
				
			||||||
            <h2>Graph of History</h2>
 | 
					            <h2>Chart of Gas price</h2>
 | 
				
			||||||
        </section>
 | 
					            <hr>
 | 
				
			||||||
 | 
					            <p><?= $data['default_car']['name'] . " | " . $data['default_car']['registration_plate']?></p>
 | 
				
			||||||
        <section class="card streak">
 | 
					            <canvas id="chart-gas-price"></canvas>
 | 
				
			||||||
            <h2>Streak</h2>
 | 
					 | 
				
			||||||
            <p>You're current streak is 123 days</p>
 | 
					 | 
				
			||||||
            <p>Good job!</p>
 | 
					 | 
				
			||||||
        </section>
 | 
					        </section>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
</section>
 | 
					</section>
 | 
				
			||||||
 | 
					    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
 | 
				
			||||||
 | 
					    <script>
 | 
				
			||||||
 | 
					    const ctx = document.getElementById('chart-gas-price');
 | 
				
			||||||
 | 
					    const data = <?= json_encode($data['date_price_data']); ?>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    new Chart(ctx, {
 | 
				
			||||||
 | 
					        type: 'line',
 | 
				
			||||||
 | 
					        data: {
 | 
				
			||||||
 | 
					            labels: [...data['date']],
 | 
				
			||||||
 | 
					            datasets: [{
 | 
				
			||||||
 | 
					                label: 'Gas price history',
 | 
				
			||||||
 | 
					                data: data['price'],
 | 
				
			||||||
 | 
					                borderWidth: 1,
 | 
				
			||||||
 | 
					            }]
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					</script>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -79,6 +79,7 @@ class Database {
 | 
				
			|||||||
                registration_plate VARCHAR(50) NOT NULL UNIQUE,
 | 
					                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,
 | 
					                fuel_type ENUM('Diesel', 'Gasoline 95', 'Gasoline 98', 'Premium Diesel', 'Premium Gasoline 95', 'Premium Gasoline 98', 'Other') NOT NULL,
 | 
				
			||||||
                note VARCHAR(150) NULL,
 | 
					                note VARCHAR(150) NULL,
 | 
				
			||||||
 | 
					                is_default BOOLEAN DEFAULT FALSE NOT NULL,
 | 
				
			||||||
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 | 
					                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 | 
				
			||||||
                FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
 | 
					                FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
 | 
				
			||||||
            ) ENGINE=InnoDB;
 | 
					            ) ENGINE=InnoDB;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,6 @@
 | 
				
			|||||||
  background-color: var(--clr-secondary);
 | 
					  background-color: var(--clr-secondary);
 | 
				
			||||||
  border-radius: var(--border-radious);
 | 
					  border-radius: var(--border-radious);
 | 
				
			||||||
  border: var(--borderWidth-thin) solid var(--clr-border);
 | 
					  border: var(--borderWidth-thin) solid var(--clr-border);
 | 
				
			||||||
  min-width: 17rem;
 | 
					  width: 18rem;
 | 
				
			||||||
  padding: 1rem;
 | 
					  padding: 1rem;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user