admin privilage
This commit is contained in:
parent
b2bd22e909
commit
cf1ff1d401
115
admin_dashboard.php
Normal file
115
admin_dashboard.php
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
// Check if user is logged in and is an admin
|
||||||
|
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
|
||||||
|
header("location: login.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$db = db();
|
||||||
|
|
||||||
|
// Fetch all submissions with user information
|
||||||
|
$stmt = $db->prepare("
|
||||||
|
SELECT
|
||||||
|
s.id,
|
||||||
|
s.item_type,
|
||||||
|
s.quantity,
|
||||||
|
s.points_awarded,
|
||||||
|
s.submission_date,
|
||||||
|
u.name as user_name,
|
||||||
|
u.email as user_email
|
||||||
|
FROM
|
||||||
|
waste_submissions s
|
||||||
|
JOIN
|
||||||
|
users u ON s.user_id = u.id
|
||||||
|
ORDER BY
|
||||||
|
s.submission_date DESC
|
||||||
|
");
|
||||||
|
$stmt->execute();
|
||||||
|
$all_submissions = $stmt->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>Admin Dashboard - E-Waste Reclaimer</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
||||||
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a class="navbar-brand" href="index.php"><i class="fas fa-recycle me-2"></i>E-Waste Reclaimer (Admin)</a>
|
||||||
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||||
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div class="collapse navbar-collapse" id="navbarNav">
|
||||||
|
<ul class="navbar-nav ms-auto">
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="dashboard.php">My Dashboard</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link active" href="admin_dashboard.php">Admin Dashboard</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="logout.php">Logout</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<main class="container my-5">
|
||||||
|
<h1 class="mb-4">Admin Dashboard: All Submissions</h1>
|
||||||
|
|
||||||
|
<div class="card shadow-sm">
|
||||||
|
<div class="card-header">
|
||||||
|
<h5 class="mb-0">All User Submissions</h5>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<?php if (empty($all_submissions)): ?>
|
||||||
|
<p class="text-center">No submissions have been made by any user yet.</p>
|
||||||
|
<?php else: ?>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-hover">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Date</th>
|
||||||
|
<th>User</th>
|
||||||
|
<th>Email</th>
|
||||||
|
<th>Item Type</th>
|
||||||
|
<th>Quantity</th>
|
||||||
|
<th>Points</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($all_submissions as $submission): ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo date("F j, Y, g:i a", strtotime($submission['submission_date'])); ?></td>
|
||||||
|
<td><?php echo htmlspecialchars($submission['user_name']); ?></td>
|
||||||
|
<td><?php echo htmlspecialchars($submission['user_email']); ?></td>
|
||||||
|
<td><?php echo htmlspecialchars($submission['item_type']); ?></td>
|
||||||
|
<td><?php echo $submission['quantity']; ?></td>
|
||||||
|
<td><span class="badge bg-success"><?php echo $submission['points_awarded']; ?></span></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer class="bg-dark text-white text-center p-3 mt-auto">
|
||||||
|
<p class="mb-0">© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
127
dashboard.php
127
dashboard.php
@ -7,6 +7,28 @@ if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
require_once 'db/config.php';
|
require_once 'db/config.php';
|
||||||
|
$db = db();
|
||||||
|
|
||||||
|
// Fetch user data
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
$stmt = $db->prepare("SELECT name, email, points, created_at, role FROM users WHERE id = :id");
|
||||||
|
$stmt->bindParam(':id', $user_id, PDO::PARAM_INT);
|
||||||
|
$stmt->execute();
|
||||||
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
if (!$user) {
|
||||||
|
// Handle user not found, though unlikely if session is set
|
||||||
|
session_destroy();
|
||||||
|
header("location: login.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch user submissions
|
||||||
|
$stmt_submissions = $db->prepare("SELECT item_type, quantity, points_awarded, submission_date FROM waste_submissions WHERE user_id = :user_id ORDER BY submission_date DESC");
|
||||||
|
$stmt_submissions->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
||||||
|
$stmt_submissions->execute();
|
||||||
|
$submissions = $stmt_submissions->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
@ -33,6 +55,11 @@ require_once 'db/config.php';
|
|||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link active" href="dashboard.php">Dashboard</a>
|
<a class="nav-link active" href="dashboard.php">Dashboard</a>
|
||||||
</li>
|
</li>
|
||||||
|
<?php if ($user['role'] === 'admin'): ?>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="admin_dashboard.php">Admin</a>
|
||||||
|
</li>
|
||||||
|
<?php endif; ?>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="logout.php">Logout</a>
|
<a class="nav-link" href="logout.php">Logout</a>
|
||||||
</li>
|
</li>
|
||||||
@ -43,17 +70,31 @@ require_once 'db/config.php';
|
|||||||
|
|
||||||
<main class="container my-5">
|
<main class="container my-5">
|
||||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||||
<h1>Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!</h1>
|
<h1 class="mb-0">Welcome, <?php echo htmlspecialchars($user['name']); ?>!</h1>
|
||||||
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#submitWasteModal">
|
||||||
|
<i class="fas fa-plus-circle me-2"></i>Submit E-Waste
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="lead mb-5">This is your dashboard. From here you can submit e-waste, track your points, and view your submission history.</p>
|
<?php if (isset($_SESSION['success_message'])): ?>
|
||||||
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||||
|
<?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php if (isset($_SESSION['error_message'])): ?>
|
||||||
|
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
||||||
|
<?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-6 mb-4">
|
<div class="col-md-6 mb-4">
|
||||||
<div class="card text-center h-100 shadow-sm">
|
<div class="card text-center h-100 shadow-sm">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title">Your Points</h5>
|
<h5 class="card-title">Your Points</h5>
|
||||||
<p class="display-4 text-success fw-bold">0</p>
|
<p class="display-4 text-success fw-bold"><?php echo $user['points']; ?></p>
|
||||||
<p class="card-text">Keep recycling to earn more!</p>
|
<p class="card-text">Keep recycling to earn more!</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -61,15 +102,89 @@ require_once 'db/config.php';
|
|||||||
<div class="col-md-6 mb-4">
|
<div class="col-md-6 mb-4">
|
||||||
<div class="card text-center h-100 shadow-sm">
|
<div class="card text-center h-100 shadow-sm">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title">Your Submissions</h5>
|
<h5 class="card-title">Total Submissions</h5>
|
||||||
<p class="display-4 fw-bold">0</p>
|
<p class="display-4 fw-bold"><?php echo count($submissions); ?></p>
|
||||||
<a href="#" class="btn btn-primary">Submit New E-Waste</a>
|
<p class="card-text">Thank you for your contribution!</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="card mt-4 shadow-sm">
|
||||||
|
<div class="card-header">
|
||||||
|
<h5 class="mb-0">Your Submission History</h5>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<?php if (empty($submissions)): ?>
|
||||||
|
<p class="text-center">You haven't made any submissions yet. Click the "Submit E-Waste" button to get started!</p>
|
||||||
|
<?php else: ?>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-hover">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Date</th>
|
||||||
|
<th>Item Type</th>
|
||||||
|
<th>Quantity</th>
|
||||||
|
<th>Points Awarded</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($submissions as $submission): ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo date("F j, Y, g:i a", strtotime($submission['submission_date'])); ?></td>
|
||||||
|
<td><?php echo htmlspecialchars($submission['item_type']); ?></td>
|
||||||
|
<td><?php echo $submission['quantity']; ?></td>
|
||||||
|
<td><span class="badge bg-success"><?php echo $submission['points_awarded']; ?></span></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<!-- Submit Waste Modal -->
|
||||||
|
<div class="modal fade" id="submitWasteModal" tabindex="-1" aria-labelledby="submitWasteModalLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="submitWasteModalLabel">Submit E-Waste for Recycling</h5>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<form action="submit_waste.php" method="post">
|
||||||
|
<div class="modal-body">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="item_type" class="form-label">What are you recycling?</label>
|
||||||
|
<select class="form-select" id="item_type" name="item_type" required>
|
||||||
|
<option value="" disabled selected>Select an item type...</option>
|
||||||
|
<option value="Smartphone">Smartphone</option>
|
||||||
|
<option value="Laptop">Laptop</option>
|
||||||
|
<option value="Tablet">Tablet</option>
|
||||||
|
<option value="Desktop Computer">Desktop Computer</option>
|
||||||
|
<option value="Monitor">Monitor</option>
|
||||||
|
<option value="Printer">Printer</option>
|
||||||
|
<option value="Battery">Battery</option>
|
||||||
|
<option value="Cables & Chargers">Cables & Chargers</option>
|
||||||
|
<option value="Other">Other</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="quantity" class="form-label">How many items?</label>
|
||||||
|
<input type="number" class="form-control" id="quantity" name="quantity" min="1" required>
|
||||||
|
</div>
|
||||||
|
<div class="alert alert-info">You'll earn <strong>10 points</strong> for each item you submit.</div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Submit for Points</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<footer class="bg-dark text-white text-center p-3 mt-auto">
|
<footer class="bg-dark text-white text-center p-3 mt-auto">
|
||||||
<p class="mb-0">© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
<p class="mb-0">© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|||||||
@ -36,6 +36,16 @@ function db() {
|
|||||||
points INT DEFAULT 0,
|
points INT DEFAULT 0,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);");
|
);");
|
||||||
|
|
||||||
|
$pdo->exec("CREATE TABLE IF NOT EXISTS waste_submissions (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
user_id INT NOT NULL,
|
||||||
|
item_type VARCHAR(100) NOT NULL,
|
||||||
|
quantity INT NOT NULL,
|
||||||
|
points_awarded INT NOT NULL,
|
||||||
|
submission_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);");
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
error_log('Database setup failed: ' . $e->getMessage());
|
error_log('Database setup failed: ' . $e->getMessage());
|
||||||
// You could display a generic error page here instead of dying
|
// You could display a generic error page here instead of dying
|
||||||
|
|||||||
53
submit_waste.php
Normal file
53
submit_waste.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||||
|
header("location: login.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||||
|
$item_type = trim($_POST["item_type"]);
|
||||||
|
$quantity = trim($_POST["quantity"]);
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
|
||||||
|
if (empty($item_type) || empty($quantity) || !is_numeric($quantity) || $quantity <= 0) {
|
||||||
|
$_SESSION['error_message'] = "Please enter a valid item and quantity.";
|
||||||
|
header("location: dashboard.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Basic points system: 10 points per item
|
||||||
|
$points_awarded = $quantity * 10;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db = db();
|
||||||
|
|
||||||
|
// Insert submission
|
||||||
|
$sql = "INSERT INTO waste_submissions (user_id, item_type, quantity, points_awarded) VALUES (:user_id, :item_type, :quantity, :points_awarded)";
|
||||||
|
$stmt = $db->prepare($sql);
|
||||||
|
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
||||||
|
$stmt->bindParam(':item_type', $item_type, PDO::PARAM_STR);
|
||||||
|
$stmt->bindParam(':quantity', $quantity, PDO::PARAM_INT);
|
||||||
|
$stmt->bindParam(':points_awarded', $points_awarded, PDO::PARAM_INT);
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
// Update user points
|
||||||
|
$sql_update_points = "UPDATE users SET points = points + :points_awarded WHERE id = :user_id";
|
||||||
|
$stmt_update_points = $db->prepare($sql_update_points);
|
||||||
|
$stmt_update_points->bindParam(':points_awarded', $points_awarded, PDO::PARAM_INT);
|
||||||
|
$stmt_update_points->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
||||||
|
$stmt_update_points->execute();
|
||||||
|
|
||||||
|
$_SESSION['success_message'] = "E-waste submitted successfully! You earned " . $points_awarded . " points.";
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$_SESSION['error_message'] = "Oops! Something went wrong. Please try again later.";
|
||||||
|
error_log("E-waste submission failed: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
header("location: dashboard.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
?>
|
||||||
Loading…
x
Reference in New Issue
Block a user