Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserverprogrammering/submissions/projekt-matkort-handler/classes/

CardBalance.php
FoodLog.php
Restaurant.php
User.php

CardBalance.php

112 lines ASCII Windows (CRLF)
<?php
Class CardBalance {

  private 
$conn;

  
// 2025/2026 schedule defining the periods and their starting amounts
  
private $periods = [
    [
'start' => '2025-08-20''amount' => 3290],
    [
'start' => '2025-11-03''amount' => 2380],
    [
'start' => '2026-01-08''amount' => 3920],
    [
'start' => '2026-04-13''amount' => 2660]
  ];

  function 
__construct ($conn) {
    
$this->conn $conn;
  }

  function 
getBalanceRecord($userId) {
    try {
      
$stmt $this->conn->prepare("SELECT * FROM card_balance WHERE user_id = ?");
      
$stmt->execute([$userId]);
      return 
$stmt->fetch(PDO::FETCH_ASSOC);
    } catch (
PDOException $e) {
      
error_log("Error in getBalanceRecord: " $e->getMessage());
      return 
null;
    }
  }

  function 
checkAndReset ($userId) {
    
$currentDate date('Y-m-d');
    
$activePeriod null;

    
// Find the latest period we're currently in based on today's date
    
foreach ($this->periods as $period) {
      if (
$currentDate >= $period['start']) {
        
$activePeriod $period;
      }
    }

    if (!
$activePeriod) return; // School year hasn't started

    
$record $this->getBalanceRecord($userId);

    if (!
$record) {
      
// No balance exists yet, so create it for the active period
      
$this->createBalance($userId$activePeriod['amount'], $activePeriod['start']);
    } else {
      
// Check if the balance needs to be wiped and reset for a newly started period
      
if ($record['last_reset_date'] < $activePeriod['start']) {
        
$this->resetBalance($userId$activePeriod['amount'], $activePeriod['start']);
      }
    }
  }

  function 
deduct ($userId$amount) {
    
// Before deducting, ensure we have processed any required date resets for the new active period
    
$this->checkAndReset($userId);
    
    try {
      
$stmt $this->conn->prepare("UPDATE card_balance SET amount = amount - ? WHERE user_id = ?");
      
$stmt->execute([$amount$userId]);
    } catch (
PDOException $e) {
      
error_log("Error in deduct: " $e->getMessage());
    }
  }

  function 
getBalanceAmount($userId) {
    
$this->checkAndReset($userId);
    
$record $this->getBalanceRecord($userId);
    return 
$record $record['amount'] : 0;
  }

  function 
getNextResetDate() {
    
$currentDate date('Y-m-d');
    foreach (
$this->periods as $period) {
      if (
$period['start'] > $currentDate) {
        return 
$period['start'];
      }
    }
    
// Check if we are in the last period but before the end of the school year
    
if ($currentDate <= '2026-06-10') {
        return 
'2026-06-11'// The day after the school year ends and the card turns invalid
    
}
    return 
null// The school year 2025/2026 timeline has finished
  
}

  function 
getPeriods() {
    return 
$this->periods;
  }

  function 
getDailyBudget($user_id) {
    return 
70// 70 kr per active school day
  
}

  function 
createBalance($user_id$amount$date) {
    try {
      
$stmt $this->conn->prepare("INSERT INTO card_balance (user_id, amount, last_reset_date) VALUES (?, ?, ?)");
      
$stmt->execute([$user_id$amount$date]);
    } catch (
PDOException $e) {
      
error_log("Error in createBalance: " $e->getMessage());
    }
  }

  function 
resetBalance($user_id$amount$date) {
    try {
      
$stmt $this->conn->prepare("UPDATE card_balance SET amount = ?, last_reset_date = ? WHERE user_id = ?");
      
$stmt->execute([$amount$date$user_id]);
    } catch (
PDOException $e) {
      
error_log("Error in resetBalance: " $e->getMessage());
    }
  }
}