154 lines
6.4 KiB
PHP
154 lines
6.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in and is an admin
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'admin') {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
|
|
$page_title = "Run Organ Matching Algorithm";
|
|
$pdo = db();
|
|
$match_message = '';
|
|
|
|
// Matching Logic
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['run_matching'])) {
|
|
try {
|
|
// Fetch all approved donors and recipients
|
|
$donors = $pdo->query("SELECT * FROM donors WHERE status = 'approved'")->fetchAll(PDO::FETCH_ASSOC);
|
|
$recipients = $pdo->query("SELECT * FROM recipients")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$matches_found = 0;
|
|
|
|
foreach ($recipients as $recipient) {
|
|
foreach ($donors as $donor) {
|
|
// Simple matching logic: blood type and organ must match
|
|
// And donor must have the organ listed
|
|
if ($recipient['blood_type'] === $donor['blood_type'] &&
|
|
strpos($donor['organs'], $recipient['organ']) !== false) {
|
|
|
|
// Check if this match already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM matches WHERE donor_id = ? AND recipient_id = ?");
|
|
$stmt->execute([$donor['id'], $recipient['id']]);
|
|
if (!$stmt->fetch()) {
|
|
// Insert new match as 'pending'
|
|
$insert_stmt = $pdo->prepare("INSERT INTO matches (donor_id, recipient_id, status) VALUES (?, ?, 'pending')");
|
|
$insert_stmt->execute([$donor['id'], $recipient['id']]);
|
|
$matches_found++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($matches_found > 0) {
|
|
$match_message = "Found $matches_found new potential matches! They are now pending approval.";
|
|
} else {
|
|
$match_message = "No new matches found at this time.";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
$match_message = "Database error during matching: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// Fetch current matches
|
|
$matches = $pdo->query("
|
|
SELECT m.id, d.name as donor_name, r.name as recipient_name, h.name as hospital_name, m.status
|
|
FROM matches m
|
|
JOIN donors d ON m.donor_id = d.id
|
|
JOIN recipients r ON m.recipient_id = r.id
|
|
JOIN hospitals h ON r.hospital_id = h.id
|
|
ORDER BY m.created_at DESC
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($page_title) ?> - Organ Donation</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<header class="header bg-primary text-white text-center py-4">
|
|
<div class="container">
|
|
<h1 class="display-4">Organ Donation Management</h1>
|
|
<nav class="nav justify-content-center">
|
|
<a class="nav-link text-white" href="dashboard.php">Dashboard</a>
|
|
<a class="nav-link text-white" href="verify.php">Verify Donors/Hospitals</a>
|
|
<a class="nav-link text-white active" href="run_matching.php">Run Matching</a>
|
|
<a class="nav-link text-white" href="logout.php">Logout</a>
|
|
</nav>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container my-5">
|
|
<div class="text-center mb-5">
|
|
<h2>Run Matching Algorithm</h2>
|
|
<p>Click the button below to find potential matches between approved donors and recipients.</p>
|
|
<form action="run_matching.php" method="POST">
|
|
<button type="submit" name="run_matching" class="btn btn-lg btn-success">Find Matches</button>
|
|
</form>
|
|
<?php if ($match_message): ?>
|
|
<div class="alert alert-info mt-4"><?= htmlspecialchars($match_message) ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
<h3 class="text-center mb-4">Current Matches</h3>
|
|
<?php if (empty($matches)): ?>
|
|
<p class="text-center">No matches found yet.</p>
|
|
<?php else: ?>
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Donor</th>
|
|
<th>Recipient</th>
|
|
<th>Hospital</th>
|
|
<th>Status</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($matches as $match): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($match['donor_name']) ?></td>
|
|
<td><?= htmlspecialchars($match['recipient_name']) ?></td>
|
|
<td><?= htmlspecialchars($match['hospital_name']) ?></td>
|
|
<td>
|
|
<span class="badge bg-<?= $match['status'] === 'approved' ? 'success' : ($match['status'] === 'rejected' ? 'danger' : 'warning') ?>">
|
|
<?= htmlspecialchars(ucfirst($match['status'])) ?>
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<?php if($match['status'] === 'pending'): ?>
|
|
<form action="run_matching.php" method="POST" class="d-inline">
|
|
<input type="hidden" name="match_id" value="<?= $match['id'] ?>">
|
|
<button type="submit" name="action" value="approve_match" class="btn btn-sm btn-outline-success">Approve</button>
|
|
<button type="submit" name="action" value="reject_match" class="btn btn-sm btn-outline-danger">Reject</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
|
|
|
|
</main>
|
|
|
|
<footer class="footer bg-light text-center py-3 mt-5">
|
|
<div class="container">
|
|
<p class="mb-0">© <?= date("Y") ?> Organ Donation Management System. All Rights Reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|