Skip to content

Commit

Permalink
Merge pull request #27 from Lairdd1989/Add_Submission_Lateness_Function
Browse files Browse the repository at this point in the history
Lateness Function
  • Loading branch information
Lairdd1989 authored Dec 11, 2019
2 parents 7863f4e + 155087b commit 7d0600e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
17 changes: 17 additions & 0 deletions grades/grade_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,20 @@ function calculateGradeSecondSubmission($mark) {
}
return false;
}

// this converts the two string dates into timestamps, finds the difference in seconds, and divides by 864000 to convert seconds into days
function calculateLateness($hand_in_date, $deadline) {
$lateness = ((strtotime($hand_in_date) - strtotime($deadline)) / 86400);
return $lateness;
}

function applyLatePenalty($grade_id, $lateness) {
if ($lateness > 5) {
// BF is a 'bad fail', assigned if a submission is more than 5 days late
$grade_id = getGradeID("BF");
return $grade_id;
} else {
$late_grade_id = $grade_id - $lateness;
return $late_grade_id;
}
}
2 changes: 1 addition & 1 deletion submissions/create_submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$mark = null;
}
$hand_in_date = $_POST['hand_in_date'];
if ($_POST['second_submission']) {
if (isset($_POST['second_submission'])) {
$second_submission = true;
} else {
$second_submission = false;
Expand Down
15 changes: 15 additions & 0 deletions submissions/submission_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
if (php_sapi_name() == "cli") {
require_once getcwd().'../database/dbconnection.php';
require_once getcwd().'../grades/grade_functions.php';
require_once getcwd().'../courseworks/coursework_functions.php';
} else {
require_once $_SERVER['DOCUMENT_ROOT'].'/database/dbconnection.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/grades/grade_functions.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/courseworks/coursework_functions.php';
}

function getAllSubmissions() {
Expand All @@ -21,11 +23,24 @@ function getAllSubmissions() {
function createSubmission($coursework_id, $student_id, $mark, $hand_in_date, $second_submission) {
$grade_value = calculateGrade($mark, $second_submission);
$grade_id = getGradeId($grade_value);
$related_coursework = getCourseworkById($coursework_id);
if (submissionIsLate($hand_in_date, $related_coursework['deadline'])) {
$lateness = calculateLateness($hand_in_date, $related_coursework['deadline']);
$grade_id = applyLatePenalty($grade_id, $lateness);
}
$query = "INSERT INTO submissions (coursework_id, student_id, mark, hand_in_date, second_submission, grade_id)
VALUES (?, ?, ?, ?, ?, ?)";
DB::run($query, [$coursework_id, $student_id, $mark, $hand_in_date, $second_submission, $grade_id]);
}

function submissionIsLate($hand_in_date, $deadline) {
if (strtotime($hand_in_date) > strtotime($deadline)) {
return true;
} else {
return false;
}
}

function getSubmissionById($submission_id) {
$query = "SELECT submission_id, sub.coursework_id, stu.student_id, mark, hand_in_date, g.grade, second_submission, stu.name as student_name, c.name
FROM submissions sub
Expand Down
16 changes: 16 additions & 0 deletions tests/GradesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,20 @@ public function testGetGradeByGradeId() {
$this->assertEquals("BF", getGrade(1));
}

public function testCalculateLateness() {
$this->assertEquals(1, calculateLateness("2019-12-13", "2019-12-12"));
$this->assertEquals(2, calculateLateness("2019-12-14", "2019-12-12"));
$this->assertEquals(3, calculateLateness("2019-12-15", "2019-12-12"));
$this->assertEquals(4, calculateLateness("2019-12-16", "2019-12-12"));
$this->assertEquals(5, calculateLateness("2019-12-17", "2019-12-12"));
}

public function testApplyLatePenalty() {
$this->assertEquals(1, applyLatePenalty(17, 6));
$this->assertEquals(12, applyLatePenalty(17, 5));
$this->assertEquals(13, applyLatePenalty(17, 4));
$this->assertEquals(14, applyLatePenalty(17, 3));
$this->assertEquals(15, applyLatePenalty(17, 2));
$this->assertEquals(16, applyLatePenalty(17, 1));
}
}
5 changes: 5 additions & 0 deletions tests/SubmissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public function testDeleteSubmissionById() {
$this->assertEquals(0, $recordExists);
}

public function testSubmissionIsLate() {
$this->assertTrue(submissionIsLate("2019-12-13", "2019-12-12"));
$this->assertFalse(submissionIsLate("2019-12-12", "2019-12-13"));
}

/**
* Remove the test submission, coursework and student from the db.
* This is run after every test method.
Expand Down

0 comments on commit 7d0600e

Please sign in to comment.