tags:

views:

59

answers:

0

Hey guys, I have been working on a solution/algorithm for a function to calculate a grade based on the grade type (Homework, Exam, Project, etc.). I've successfully implemented the function that gets the information I need to make the calculation, but I'm having trouble constructing the grade calculation function that takes all the grades in a table and calculates them based on the grade type.


I have two tables built for this function; grades and grade_type

*grades*
id | grade_type | cid (the id of the class that the grade is for) | grade 

*grade_type*
id | assignment_type (same as grade_type) | cid | percentage

As I said, I'm able to get all of the data I need from those two tables, but I'm having trouble building the function that calculates them.

So far here's what I have:


/**************************************************************************************/
// Function to calculate the grades, based on class and assignment type

function calculateGradeByClass($cid)
{
 $grade = getGradesByType($cid);

 // If the array list is not empty, get all the elements from the array and calculate the grade
 if(!empty($grade))
 {
  $hwTotal = 0; $hwCount = 0; $hwGrade = 0;
  $eTotal = 0; $eCount = 0;
  $hwTotal = 0; $hwcount = 0;


  foreach($grade as $value)
  {    
    if($value['grade_type'] == 'Homework')
    { 
     $hwGrade = (($value['grade']  *                                                 $value['percentage'])/100);
     $hwTotal += $hwGrade;

    }
    if($value['grade_type'] == 'Exam')
    { 
     $eGrade = (($value['grade'] *                                                 $value['percentage'])/100);
     $eTotal += $eGrade;
    }      
  }
  echo $hwTotal . ", ";  
 }
}

This is all Ive been able to come up with within a 2 hour period, so I thought I'd turn to the community to possibly help me out. If you have any suggestions, please let me know. Thanks.