Skip to content

Instantly share code, notes, and snippets.

@surferxo3
Last active October 30, 2016 18:18
Show Gist options
  • Save surferxo3/353e63a013813f61b780f5b7099b2c0b to your computer and use it in GitHub Desktop.
Save surferxo3/353e63a013813f61b780f5b7099b2c0b to your computer and use it in GitHub Desktop.
Script to calculate O and A levels Equivalence.
<?php
/*#############################
* Developer: Mohammad Sharaf Ali
* Designation: Web Developer
* Version: 1.0
*/#############################
##################### CONSTANTS #####################
// grade equivalence constants
const IBCC_GRADES = array(
'A*' => 90,
'A' => 85,
'B' => 75,
'C' => 65,
'D' => 55,
'E' => 45);
const IBCC_MATRIC_TOTAL = 900;
const IBCC_INTERMEDIATE_TOTAL = 1100;
##################### HELPER METHODS #####################
function notEmptyString($str) {
return $str !== '';
}
function getEquivalent($grade) {
$percent = 0;
if (array_key_exists($grade, IBCC_GRADES)) {
$percent = IBCC_GRADES[$grade];
}
return $percent;
}
function significatDigits($num, $sep = '.', $sig = 2) {
$cut = substr($num, 0, ((strpos($num, $sep) + 1) + $sig));
return $cut;
}
function printResult($str) {
echo '<pre>';
echo $str;
echo '</pre>';
}
##################### MAIN #####################
if (isset($_POST['submit'])) {
$OLevelSum = 0;
$OLevelTotal = 0;
$ALevelSum = 0;
$ALevelTotal = 0;
$OLevelGrades = isset($_POST['grades_o']) ? $_POST['grades_o'] : array();
$ALevelGrades = isset($_POST['grades_a']) ? $_POST['grades_a'] : array();
if (!empty($OLevelGrades)) { // calculate Matric Equivalence Certificate
$OLevelGrades = explode(',', $_POST['grades_o']); // validation when no comma separated
$OLevelGrades = array_map('trim', $OLevelGrades);
$OLevelGrades = array_filter($OLevelGrades, 'notEmptyString');
foreach ($OLevelGrades as $OLevelGrade) {
list($subject, $grade) = explode('-', $OLevelGrade); // validation when no hyphen separated
$equivalentGrade = getEquivalent(strtoupper($grade));
$OLevelSum += $subject * $equivalentGrade;
if ($equivalentGrade > 0) {
$OLevelTotal += $subject;
}
}
$equivalentMarks = ceil(($OLevelSum / ($OLevelTotal * 100)) * IBCC_MATRIC_TOTAL);
$equivalentPercent = ($equivalentMarks / IBCC_MATRIC_TOTAL) * 100;
$toScreen = 'Matric Marks obtained:';
$toScreen .= '<br />';
$toScreen .= 'In number <strong>'. $equivalentMarks. '</strong> out of <strong>'. IBCC_MATRIC_TOTAL. '</strong>';
$toScreen .= '<br />';
$toScreen .= 'In percentage <strong>'. significatDigits($equivalentPercent). '%</strong>';
printResult($toScreen);
if (!empty($ALevelGrades)) { // calculate Intermediate Equivalence Certificate
$ALevelGrades = explode(',', $_POST['grades_a']); // validation when no comma separated
$ALevelGrades = array_map('trim', $ALevelGrades);
$ALevelGrades = array_filter($ALevelGrades, 'notEmptyString');
foreach ($ALevelGrades as $ALevelGrade) {
list($subject, $grade) = explode('-', $ALevelGrade); // validation when no hyphen separated
$equivalentGrade = getEquivalent(strtoupper($grade));
$ALevelSum += $subject * $equivalentGrade;
if ($equivalentGrade > 0) {
$ALevelTotal += $subject;
}
}
$equivalentMarks = ceil((($OLevelSum + $ALevelSum) / (($OLevelTotal + $ALevelTotal) * 100)) * IBCC_INTERMEDIATE_TOTAL);
$equivalentPercent = ($equivalentMarks / IBCC_INTERMEDIATE_TOTAL) * 100;
$toScreen = 'Intermediate Marks obtained:';
$toScreen .= '<br />';
$toScreen .= 'In number <strong>'. $equivalentMarks. '</strong> out of <strong>'. IBCC_INTERMEDIATE_TOTAL. '</strong>';
$toScreen .= '<br />';
$toScreen .= 'In percentage <strong>'. significatDigits($equivalentPercent). '%</strong>';
printResult($toScreen);
}
}
}
?>
<html>
<head>
<title>Script for calculating O and A Level's Equivalence</title>
</head>
<body>
<h4>O and A Level Equivalence Calculator</h4>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" id="grades_o" name="grades_o" required autofocus tabindex="1" placeholder="Enter grades as 3-A, 2-B, 1-C, 1-D, 1-E" />
<br /><br />
<input type="text" id="grades_a" name="grades_a" tabindex="2" placeholder="Enter grades as 1-A*, 2-B" />
<br /><br />
<input type="submit" id="submit" name="submit" value="CALCULATE" tabindex="3" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment