Created the log functionality #6
@ -10,7 +10,7 @@
 | 
			
		||||
        <ul>
 | 
			
		||||
            <li><a href="search.html" class="btn">Search</a></li>
 | 
			
		||||
            <li><a href="addVehicle.html" class="btn">Add new vehicle</a></li>
 | 
			
		||||
            <li><a href="newReport.html" class="btn">Create new report</a></li>
 | 
			
		||||
            <li><a href="reports.html" class="btn">Reports</a></li>
 | 
			
		||||
            <li class="admin"><a href="newUser.html" class="btn">Create new user</a></li>
 | 
			
		||||
            <li class="admin"><a href="addFine.html" class="btn active">Add Fines</a></li>
 | 
			
		||||
            <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>
 | 
			
		||||
 | 
			
		||||
@ -16,6 +16,11 @@ if (isset($_SESSION["username"]) && isset($_SESSION["admin"]))
 | 
			
		||||
    $stmt->bindParam(":incident", $incident);
 | 
			
		||||
    $stmt->execute();
 | 
			
		||||
 | 
			
		||||
    $logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Added a fine', :username, NOW())";
 | 
			
		||||
    $logStmt = $conn->prepare($logSQL);
 | 
			
		||||
    $logStmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
    $logStmt->execute();
 | 
			
		||||
 | 
			
		||||
    echo json_encode(array("message" => "ok"));
 | 
			
		||||
}
 | 
			
		||||
else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"]))
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@
 | 
			
		||||
        <ul>
 | 
			
		||||
            <li><a href="search.html" class="btn">Search</a></li>
 | 
			
		||||
            <li><a href="addVehicle.html" class="btn active">Add new vehicle</a></li>
 | 
			
		||||
            <li><a href="newReport.html" class="btn">Create new report</a></li>
 | 
			
		||||
            <li><a href="reports.html" class="btn">Reports</a></li>
 | 
			
		||||
            <li class="admin"><a href="newUser.html" class="btn">Create new user</a></li>
 | 
			
		||||
            <li class="admin"><a href="addFine.html" class="btn">Add Fines</a></li>
 | 
			
		||||
            <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>
 | 
			
		||||
 | 
			
		||||
@ -34,6 +34,11 @@ if (isset($_SESSION["username"]))
 | 
			
		||||
        $stmt->execute();
 | 
			
		||||
        echo json_encode(array("message" => "Vehicle added successfully and assigned to existing owner"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Add vehicle', :username, NOW())";
 | 
			
		||||
    $logStmt = $conn->prepare($logSQL);
 | 
			
		||||
    $logStmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
    $logStmt->execute();
 | 
			
		||||
}
 | 
			
		||||
else
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@
 | 
			
		||||
        <ul>
 | 
			
		||||
            <li><a href="search.html" class="btn">Search</a></li>
 | 
			
		||||
            <li><a href="addVehicle.html" class="btn">Add new vehicle</a></li>
 | 
			
		||||
            <li><a href="newReport.html" class="btn">Create new report</a></li>
 | 
			
		||||
            <li><a href="reports.html" class="btn">Reports</a></li>
 | 
			
		||||
            <li class="admin"><a href="newUser.html" class="btn">Create new user</a></li>
 | 
			
		||||
            <li class="admin"><a href="addFine.html" class="btn">Add Fines</a></li>
 | 
			
		||||
            <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>
 | 
			
		||||
 | 
			
		||||
@ -12,7 +12,12 @@ if (isset($_SESSION["username"]))
 | 
			
		||||
    $stmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
    if ($stmt->execute())
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Change password', :username, NOW())";
 | 
			
		||||
        $logStmt = $conn->prepare($logSQL);
 | 
			
		||||
        $logStmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
        echo json_encode(array("message" => "ok"));
 | 
			
		||||
        $logStmt->execute();
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
@ -10,4 +10,8 @@ if (isset($_SESSION["username"]))
 | 
			
		||||
    $stmt->execute();
 | 
			
		||||
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
 | 
			
		||||
    echo json_encode(array("message" => "ok", "owners" => $result));
 | 
			
		||||
}
 | 
			
		||||
else
 | 
			
		||||
{
 | 
			
		||||
    echo json_encode(array("message" => "Not logged in "));
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
 | 
			
		||||
document.addEventListener("DOMContentLoaded", () =>
 | 
			
		||||
{
 | 
			
		||||
    fetch("isAdmin.php").then(res => res.json().then(json =>
 | 
			
		||||
    {
 | 
			
		||||
        if (json.message !== "ok")
 | 
			
		||||
        {
 | 
			
		||||
            window.location.href = "./search.html";
 | 
			
		||||
        }
 | 
			
		||||
    }));
 | 
			
		||||
 | 
			
		||||
    fetch("viewLog.php").then(res => res.json().then(json =>
 | 
			
		||||
    {
 | 
			
		||||
        if (json.message === "ok")
 | 
			
		||||
        {
 | 
			
		||||
            for (const key of Object.keys(json.logs[0]))
 | 
			
		||||
            {
 | 
			
		||||
                let header = key.substring(key.indexOf("_") + 1)
 | 
			
		||||
                header = header.charAt(0).toUpperCase() + header.slice(1);
 | 
			
		||||
                document.querySelector("#logTable thead tr").innerHTML += `<th>${header}</th>`;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let body = "";
 | 
			
		||||
            for (const row of json.logs)
 | 
			
		||||
            {
 | 
			
		||||
                body += "<tr>";
 | 
			
		||||
                for (const key of Object.keys(row))
 | 
			
		||||
                {
 | 
			
		||||
                    body += `<td>${row[key]}</td>`;
 | 
			
		||||
                }
 | 
			
		||||
                body += "</tr>";
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            document.querySelector("#logTable tbody").innerHTML = body;
 | 
			
		||||
        }
 | 
			
		||||
    }));
 | 
			
		||||
});
 | 
			
		||||
@ -10,7 +10,7 @@
 | 
			
		||||
        <ul>
 | 
			
		||||
            <li><a href="search.html" class="btn">Search</a></li>
 | 
			
		||||
            <li><a href="addVehicle.html" class="btn">Add new vehicle</a></li>
 | 
			
		||||
            <li><a href="newReport.html" class="btn">Create new report</a></li>
 | 
			
		||||
            <li><a href="reports.html" class="btn">Reports</a></li>
 | 
			
		||||
            <li class="admin"><a href="newUser.html" class="btn active">Create new user</a></li>
 | 
			
		||||
            <li class="admin"><a href="addFine.html" class="btn">Add Fines</a></li>
 | 
			
		||||
            <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>
 | 
			
		||||
 | 
			
		||||
@ -17,6 +17,13 @@ if (isset($_SESSION["username"]) && isset($_SESSION["admin"]))
 | 
			
		||||
    $stmt->bindParam(":admin", $isAdmin);
 | 
			
		||||
    $stmt->execute();
 | 
			
		||||
 | 
			
		||||
    $logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES (:type, :username, NOW())";
 | 
			
		||||
    $logStmt = $conn->prepare($logSQL);
 | 
			
		||||
    $type = !$admin ? "Add user" : "Add admin";
 | 
			
		||||
    $logStmt->bindParam(":type", $type);
 | 
			
		||||
    $logStmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
    $logStmt->execute();
 | 
			
		||||
 | 
			
		||||
    echo json_encode(array("message" => "ok", "admin" => $admin));
 | 
			
		||||
}
 | 
			
		||||
else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"]))
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@
 | 
			
		||||
        <ul>
 | 
			
		||||
            <li><a href="search.html" class="btn active">Search</a></li>
 | 
			
		||||
            <li><a href="addVehicle.html" class="btn">Add new vehicle</a></li>
 | 
			
		||||
            <li><a href="newReport.html" class="btn">Create new report</a></li>
 | 
			
		||||
            <li><a href="reports.html" class="btn">Reports</a></li>
 | 
			
		||||
            <li class="admin"><a href="newUser.html" class="btn">Create new user</a></li>
 | 
			
		||||
            <li class="admin"><a href="addFine.html" class="btn">Add Fines</a></li>
 | 
			
		||||
            <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>
 | 
			
		||||
 | 
			
		||||
@ -13,10 +13,20 @@ if (isset($_SESSION["username"]))
 | 
			
		||||
        $stmt = $conn->prepare("SELECT People_name, People_address, People_licence FROM People WHERE People_name LIKE :name OR People_licence = :searchField");
 | 
			
		||||
        $name = "%" . $searchField . "%";
 | 
			
		||||
        $stmt->bindParam(":name", $name);
 | 
			
		||||
 | 
			
		||||
        $logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Search by driving licence number', :username, NOW())";
 | 
			
		||||
        $logStmt = $conn->prepare($logSQL);
 | 
			
		||||
        $logStmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
        $logStmt->execute();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        $stmt = $conn->prepare("SELECT P.People_name, Vehicle_type, Vehicle_colour, Vehicle_licence FROM Vehicle LEFT JOIN Ownership O on Vehicle.Vehicle_ID = O.Vehicle_ID LEFT JOIN People P on O.People_ID = P.People_ID WHERE Vehicle_licence = :searchField");
 | 
			
		||||
 | 
			
		||||
        $logSQL = "INSERT INTO Logs (Logs_type, Users_username, Logs_date) VALUES ('Search by plate number', :username, NOW())";
 | 
			
		||||
        $logStmt = $conn->prepare($logSQL);
 | 
			
		||||
        $logStmt->bindParam(":username", $_SESSION["username"]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $stmt->bindParam(":searchField", $searchField);
 | 
			
		||||
@ -36,4 +46,4 @@ if (isset($_SESSION["username"]))
 | 
			
		||||
else
 | 
			
		||||
{
 | 
			
		||||
    echo json_encode(array("message" => "Not logged in"));
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -2,9 +2,41 @@
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <title>Title</title>
 | 
			
		||||
    <title>View Log</title>
 | 
			
		||||
    <link rel="stylesheet" href="css/nav.css">
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <nav>
 | 
			
		||||
        <ul>
 | 
			
		||||
            <li><a href="search.html" class="btn">Search</a></li>
 | 
			
		||||
            <li><a href="addVehicle.html" class="btn">Add new vehicle</a></li>
 | 
			
		||||
            <li><a href="reports.html" class="btn">Reports</a></li>
 | 
			
		||||
            <li class="admin"><a href="newUser.html" class="btn">Create new user</a></li>
 | 
			
		||||
            <li class="admin"><a href="addFine.html" class="btn">Add Fines</a></li>
 | 
			
		||||
            <li class="admin"><a href="viewLog.html" class="btn active">View log</a></li>
 | 
			
		||||
            <li><a href="changePassword.html" class="btn">Change password</a></li>
 | 
			
		||||
            <li><a id="logout" class="btn">Logout</a></li>
 | 
			
		||||
        </ul>
 | 
			
		||||
    </nav>
 | 
			
		||||
    <main id="search">
 | 
			
		||||
        <header id="title">
 | 
			
		||||
            <h1></h1>
 | 
			
		||||
        </header>
 | 
			
		||||
 | 
			
		||||
        <div class="content">
 | 
			
		||||
            <table id="logTable">
 | 
			
		||||
                <thead>
 | 
			
		||||
                    <tr>
 | 
			
		||||
 | 
			
		||||
                    </tr>
 | 
			
		||||
                </thead>
 | 
			
		||||
                <tbody></tbody>
 | 
			
		||||
            </table>
 | 
			
		||||
        </div>
 | 
			
		||||
    </main>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
<script src="js/checkUser.js"></script>
 | 
			
		||||
<script src="js/viewLog.js"></script>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
@ -0,0 +1,21 @@
 | 
			
		||||
<?php
 | 
			
		||||
session_start();
 | 
			
		||||
require_once 'config.php';
 | 
			
		||||
header('Content-Type: application/json');
 | 
			
		||||
 | 
			
		||||
if (isset($_SESSION["username"]) && isset($_SESSION["admin"]))
 | 
			
		||||
{
 | 
			
		||||
    $conn = dbConn();
 | 
			
		||||
    $stmt = $conn->prepare("SELECT Logs_type, Users_username, Logs_date FROM Logs");
 | 
			
		||||
    $stmt->execute();
 | 
			
		||||
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
 | 
			
		||||
    echo json_encode(array("message" => "ok", "logs" => $result));
 | 
			
		||||
}
 | 
			
		||||
else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"]))
 | 
			
		||||
{
 | 
			
		||||
    echo json_encode(array("message" => "not logged in as admin"));
 | 
			
		||||
}
 | 
			
		||||
else
 | 
			
		||||
{
 | 
			
		||||
    echo json_encode(array("message" => "Not logged in "));
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user