<!DOCTYPE html>
<html lang="en">
<head>
<title>GradeBook</title>
</head>
<style>
body {
background-color: #CCCCCC;
font-size: 25px;
}
table {
color: black;
font-size: 25px;
}
</style>
<body>
<h1>GradeBook</h1>
<?php
session_start();
require_once('GradeBook.php');
// Check if the user is in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
header("Location: login.php");
exit();
}
// Get student table
$students = getStudents();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['editIndex'])) {
// Handle edit
$editIndex = $_POST['editIndex'];
displayEditForm($students[$editIndex], $editIndex);
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['delete'])) {
// Handle delete
Delete($students, $_POST['delete']);
} else {
// Handle add
handleForm($students);
}
// show table
showStudentTable($students);
?>
<h2>Add Student</h2>
<form method="post" action="index.php">
<label for="StudentName">Student Name</label>
<input type="Text" name="StudentName" required>
<label for="Grade"> Student Grade:</label>
<input type="number" name="Grade" required min="0" max="100">
<label for="StartDate"> Start Date:</label>
<input type="text" name="StartDate" required pattern="\d{2}/\d{2}/\d{2}" placeholder="MM/DD/YY">
<label for="EndDate"> End Date:</label>
<input type="text" name="EndDate" pattern="\d{2}/\d{2}/\d{2}" placeholder="MM/DD/YY">
<button type="submit">Add Student</button>
</form>
<form method="post" action="index.php">
<button type="submit" name="logout">Logout</button>
</form>
</body>
</html>
<?php
function displayEditForm($student, $editIndex)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['saveChanges'])) {
//edit but it doesnt work for some reason
Edit($students, $editIndex);
}
?>
<h2>Edit Student</h2>
<form method="post" action="">
<input type="hidden" name="editIndex" value="<?php echo $editIndex; ?>">
<label for="StudentName">Student Name</label>
<input type="text" name="StudentName" value="<?php echo $student['name']; ?>" required>
<label for="Grade"> Student Grade:</label>
<input type="number" name="Grade" value="<?php echo $student['grade']; ?>" required min="0" max="100">
<label for="StartDate"> Start Date:</label>
<input type="text" name="StartDate" value="<?php echo $student['StartDate']; ?>" required
pattern="\d{2}/\d{2}/\d{2}" placeholder="MM/DD/YY">
<label for="EndDate"> End Date:</label>
<input type="text" name="EndDate" value="<?php echo $student['EndDate']; ?>" pattern="\d{2}/\d{2}/\d{2}"
placeholder="MM/DD/YY">
<button type="submit" name="saveChanges">Save Changes</button>
</form>
<?php
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
// check username
if ($username === 'Teacher' && $password === 'password') {
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit();
} else {
echo '<p>Login failed. Please try again.</p>';
}
}
?>
<form method="post" action="">
<label for="username">Username:</label>
<input type="text" name="username" required>
<label for="password">Password:</label>
<input type="password" name="password" required>
<button type="submit">Login</button>
</form>
</body>
</html>
<?php
function getStudents() {
return [
['name'=> 'Joe Dean','grade'=> '100', 'StartDate'=> '08/10/23', 'EndDate'=> ''],
['name'=> 'Sam Deer','grade'=> '80', 'StartDate'=> '08/10/23', 'EndDate'=> ''],
['name'=> 'Alex Find','grade'=> '70', 'StartDate'=> '08/10/23', 'EndDate'=> ''],
['name'=> 'Jess Gram','grade'=> '60', 'StartDate'=> '08/10/23', 'EndDate'=> ''],
];
}
function adjustGrade($orginGrade) {
return $orginGrade * 1.1;
}
function ToLetter ($Grade) {
if ($Grade >= 90) {
return "A";
}elseif ($Grade >= 80) {
return "B";
}elseif ($Grade >= 70) {
return "C";
}elseif ($Grade >= 60) {
return "D";
}else {
return "F";
}
}
function handleForm(&$students) {
$name = $_POST['StudentName'];
$grade = $_POST['Grade'];
$StartDate = $_POST['StartDate'];
$EndDate = $_POST['EndDate'];
//update
$searchStudent = array_search($name,array_column($students,'name'));
if ($searchStudent !== false) {
$students[$searchStudent]['grade'] = $grade;
$students[$searchStudent]['StartDate'] = $StartDate;
$students[$searchStudent]['EndDate'] = $EndDate;
} else {
//Add student
$students[] =['name'=>$name,'grade'=>$grade, 'StartDate'=> $StartDate, 'EndDate'=> $EndDate];
}
}
function showStudentTable($students) {
echo '<h3>Student Information</h3>';
echo '<table border="1">';
echo '<tr><th>Name</th><th>Original Grade</th><th>Letter Grade</th><th>Adjusted Grade</th><th>Adjusted Letter Grade</th><th>Days in Class</th><th>Actions</th></tr>';
$totalGrades = 0;
foreach ($students as $index => $student) {
$originalGrade = $student['grade'];
$adjustedGrade = adjustGrade($originalGrade);
$daysInClass = calculateDaysInClass($student['StartDate'], $student['EndDate']);
echo '<tr>';
echo '<td>' . $student['name'] . '</td>';
echo '<td>' . $originalGrade . '</td>';
echo '<td>' . ToLetter($originalGrade) . '</td>';
echo '<td>' . $adjustedGrade . '</td>';
echo '<td>' . ToLetter($adjustedGrade) . '</td>';
// date or not
echo '<td>' . ($daysInClass === -1 ? 'N/A' : $daysInClass) . '</td>';
echo '<td>';
echo '<form method="post" action="index.php">';
echo '<input type="hidden" name="editIndex" value="' . $index . '">';
echo '<button type="submit">Edit</button>';
echo '</form>';
echo '<form method="post" action="index.php">';
echo '<input type="hidden" name="delete" value="' . $index . '">';
echo '<button type="submit">Delete</button>';
echo '</form>';
echo '</td>';
echo '</tr>';
$totalGrades += $originalGrade;
}
echo '</table>';
$totalStudents = count($students);
$averageGrade = $totalGrades / $totalStudents;
echo '<p>Total Students: ' . $totalStudents . '</p>';
echo '<p>Total Original Grades: ' . $totalGrades . '</p>';
echo '<p>Average Original Grade: ' . $averageGrade . '</p>';
}
function Edit(&$students, $editIndex) {
$name = $_POST['StudentName'];
$grade = $_POST['Grade'];
$startDate = $_POST['StartDate'];
$endDate = $_POST['EndDate'];
// Update student data
$students[$editIndex]['name'] = $name;
$students[$editIndex]['grade'] = $grade;
$students[$editIndex]['StartDate'] = $startDate;
$students[$editIndex]['EndDate'] = $endDate;
}
function Delete($students, $deleteIndex) {
//delete
if (isset($students[$deleteIndex])) {
unset($students[$deleteIndex]);
}
}
function calculateDaysInClass($startDate, $endDate) {
// If there is no end date
if (empty($endDate)) {
return -1;
}
// timestamps
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
// difference
$difference = $endTimestamp - $startTimestamp;
// difference to days
$daysInClass = floor($difference / (60 * 60 * 24));
return $daysInClass;
}
?>