Advertisement
Guest User

Untitled

a guest
May 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. <?php
  2.  
  3. /*#############################
  4. * Developer: Mohammad Sharaf Ali
  5. * Designation: Web Developer
  6. * Version: 1.0
  7. */#############################
  8.  
  9. ##################### CONSTANTS #####################
  10. // grade equivalence constants
  11. const IBCC_GRADES = array(
  12. 'A*' => 90,
  13. 'A' => 85,
  14. 'B' => 75,
  15. 'C' => 65,
  16. 'D' => 55,
  17. 'E' => 45);
  18.  
  19. const IBCC_MATRIC_TOTAL = 900;
  20. const IBCC_INTERMEDIATE_TOTAL = 1100;
  21.  
  22.  
  23. ##################### HELPER METHODS #####################
  24. function notEmptyString($str) {
  25. return $str !== '';
  26. }
  27.  
  28. function getEquivalent($grade) {
  29. $percent = 0;
  30.  
  31. if (array_key_exists($grade, IBCC_GRADES)) {
  32. $percent = IBCC_GRADES[$grade];
  33. }
  34.  
  35. return $percent;
  36. }
  37.  
  38. function significatDigits($num, $sep = '.', $sig = 2) {
  39. $cut = substr($num, 0, ((strpos($num, $sep) + 1) + $sig));
  40.  
  41. return $cut;
  42. }
  43.  
  44. function printResult($str) {
  45. echo '<pre>';
  46. echo $str;
  47. echo '</pre>';
  48. }
  49.  
  50.  
  51. ##################### MAIN #####################
  52. if (isset($_POST['submit'])) {
  53. $OLevelSum = 0;
  54. $OLevelTotal = 0;
  55. $ALevelSum = 0;
  56. $ALevelTotal = 0;
  57.  
  58. $OLevelGrades = isset($_POST['grades_o']) ? $_POST['grades_o'] : array();
  59. $ALevelGrades = isset($_POST['grades_a']) ? $_POST['grades_a'] : array();
  60.  
  61. if (!empty($OLevelGrades)) { // calculate Matric Equivalence Certificate
  62. $OLevelGrades = explode(',', $_POST['grades_o']); // validation when no comma separated
  63. $OLevelGrades = array_map('trim', $OLevelGrades);
  64. $OLevelGrades = array_filter($OLevelGrades, 'notEmptyString');
  65.  
  66. foreach ($OLevelGrades as $OLevelGrade) {
  67. list($subject, $grade) = explode('-', $OLevelGrade); // validation when no hyphen separated
  68.  
  69. $equivalentGrade = getEquivalent(strtoupper($grade));
  70. $OLevelSum += $subject * $equivalentGrade;
  71.  
  72. if ($equivalentGrade > 0) {
  73. $OLevelTotal += $subject;
  74. }
  75. }
  76.  
  77. $equivalentMarks = ceil(($OLevelSum / ($OLevelTotal * 100)) * IBCC_MATRIC_TOTAL);
  78. $equivalentPercent = ($equivalentMarks / IBCC_MATRIC_TOTAL) * 100;
  79.  
  80. $toScreen = 'Matric Marks obtained:';
  81. $toScreen .= '<br />';
  82. $toScreen .= 'In number <strong>'. $equivalentMarks. '</strong> out of <strong>'. IBCC_MATRIC_TOTAL. '</strong>';
  83. $toScreen .= '<br />';
  84. $toScreen .= 'In percentage <strong>'. significatDigits($equivalentPercent). '%</strong>';
  85.  
  86. printResult($toScreen);
  87.  
  88. if (!empty($ALevelGrades)) { // calculate Intermediate Equivalence Certificate
  89. $ALevelGrades = explode(',', $_POST['grades_a']); // validation when no comma separated
  90. $ALevelGrades = array_map('trim', $ALevelGrades);
  91. $ALevelGrades = array_filter($ALevelGrades, 'notEmptyString');
  92.  
  93. foreach ($ALevelGrades as $ALevelGrade) {
  94. list($subject, $grade) = explode('-', $ALevelGrade); // validation when no hyphen separated
  95.  
  96. $equivalentGrade = getEquivalent(strtoupper($grade));
  97. $ALevelSum += $subject * $equivalentGrade;
  98.  
  99. if ($equivalentGrade > 0) {
  100. $ALevelTotal += $subject;
  101. }
  102. }
  103.  
  104. $equivalentMarks = ceil((($OLevelSum + $ALevelSum) / (($OLevelTotal + $ALevelTotal) * 100)) * IBCC_INTERMEDIATE_TOTAL);
  105. $equivalentPercent = ($equivalentMarks / IBCC_INTERMEDIATE_TOTAL) * 100;
  106.  
  107. $toScreen = 'Intermediate Marks obtained:';
  108. $toScreen .= '<br />';
  109. $toScreen .= 'In number <strong>'. $equivalentMarks. '</strong> out of <strong>'. IBCC_INTERMEDIATE_TOTAL. '</strong>';
  110. $toScreen .= '<br />';
  111. $toScreen .= 'In percentage <strong>'. significatDigits($equivalentPercent). '%</strong>';
  112.  
  113. printResult($toScreen);
  114. }
  115. }
  116. }
  117.  
  118. ?>
  119.  
  120. <html>
  121. <head>
  122. <title>Script for calculating O and A Level's Equivalence</title>
  123. </head>
  124.  
  125. <body>
  126.  
  127. <h4>O and A Level Equivalence Calculator</h4>
  128.  
  129. <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
  130. <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" />
  131. <br /><br />
  132.  
  133. <input type="text" id="grades_a" name="grades_a" tabindex="2" placeholder="Enter grades as 1-A*, 2-B" />
  134. <br /><br />
  135.  
  136. <input type="submit" id="submit" name="submit" value="CALCULATE" tabindex="3" />
  137. </form>
  138.  
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement