Created the log functionality

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
Rohit Pai 2022-12-16 12:19:38 +00:00
parent 3728701d77
commit 95b11f3496
14 changed files with 133 additions and 7 deletions

View File

@ -10,7 +10,7 @@
<ul> <ul>
<li><a href="search.html" class="btn">Search</a></li> <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="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="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="addFine.html" class="btn active">Add Fines</a></li>
<li class="admin"><a href="viewLog.html" class="btn">View log</a></li> <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>

View File

@ -16,6 +16,11 @@ if (isset($_SESSION["username"]) && isset($_SESSION["admin"]))
$stmt->bindParam(":incident", $incident); $stmt->bindParam(":incident", $incident);
$stmt->execute(); $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")); echo json_encode(array("message" => "ok"));
} }
else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"])) else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"]))

View File

@ -10,7 +10,7 @@
<ul> <ul>
<li><a href="search.html" class="btn">Search</a></li> <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="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="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="addFine.html" class="btn">Add Fines</a></li>
<li class="admin"><a href="viewLog.html" class="btn">View log</a></li> <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>

View File

@ -34,6 +34,11 @@ if (isset($_SESSION["username"]))
$stmt->execute(); $stmt->execute();
echo json_encode(array("message" => "Vehicle added successfully and assigned to existing owner")); 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 else
{ {

View File

@ -10,7 +10,7 @@
<ul> <ul>
<li><a href="search.html" class="btn">Search</a></li> <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="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="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="addFine.html" class="btn">Add Fines</a></li>
<li class="admin"><a href="viewLog.html" class="btn">View log</a></li> <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>

View File

@ -12,7 +12,12 @@ if (isset($_SESSION["username"]))
$stmt->bindParam(":username", $_SESSION["username"]); $stmt->bindParam(":username", $_SESSION["username"]);
if ($stmt->execute()) 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")); echo json_encode(array("message" => "ok"));
$logStmt->execute();
} }
else else
{ {

View File

@ -10,4 +10,8 @@ if (isset($_SESSION["username"]))
$stmt->execute(); $stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(array("message" => "ok", "owners" => $result)); echo json_encode(array("message" => "ok", "owners" => $result));
}
else
{
echo json_encode(array("message" => "Not logged in "));
} }

View File

@ -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;
}
}));
});

View File

@ -10,7 +10,7 @@
<ul> <ul>
<li><a href="search.html" class="btn">Search</a></li> <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="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="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="addFine.html" class="btn">Add Fines</a></li>
<li class="admin"><a href="viewLog.html" class="btn">View log</a></li> <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>

View File

@ -17,6 +17,13 @@ if (isset($_SESSION["username"]) && isset($_SESSION["admin"]))
$stmt->bindParam(":admin", $isAdmin); $stmt->bindParam(":admin", $isAdmin);
$stmt->execute(); $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)); echo json_encode(array("message" => "ok", "admin" => $admin));
} }
else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"])) else if (isset($_SESSION["username"]) && !isset($_SESSION["admin"]))

View File

@ -11,7 +11,7 @@
<ul> <ul>
<li><a href="search.html" class="btn active">Search</a></li> <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="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="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="addFine.html" class="btn">Add Fines</a></li>
<li class="admin"><a href="viewLog.html" class="btn">View log</a></li> <li class="admin"><a href="viewLog.html" class="btn">View log</a></li>

View File

@ -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"); $stmt = $conn->prepare("SELECT People_name, People_address, People_licence FROM People WHERE People_name LIKE :name OR People_licence = :searchField");
$name = "%" . $searchField . "%"; $name = "%" . $searchField . "%";
$stmt->bindParam(":name", $name); $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 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"); $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); $stmt->bindParam(":searchField", $searchField);
@ -36,4 +46,4 @@ if (isset($_SESSION["username"]))
else else
{ {
echo json_encode(array("message" => "Not logged in")); echo json_encode(array("message" => "Not logged in"));
} }

View File

@ -2,9 +2,41 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Title</title> <title>View Log</title>
<link rel="stylesheet" href="css/nav.css">
</head> </head>
<body> <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> </body>
</html> </html>

View File

@ -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 "));
}