if (num_rows($occurance) == 0) { cust_die("Invalid category ID."); } if (!isset($_POST['categoryweight']) or $_POST['categoryweight'] == "") { cust_die("You must submit the category's weight."); } if (strlen($_POST['categoryweight']) > 10) { cust_die("The category's weight must be 10 characters or fewer."); } if ($_POST['categoryweight'] > 100) { cust_die("The category's weight cannot exceed 100"); } $categoryweight = escape_string(htmlspecialchars($_POST['categoryweight'])); // make sure the categories' combined total doesn't exceed 100 $total = ""; $categories = return_categories($classid); $categories = explode(":::::", $categories); foreach ($categories as $part) { if ($part != "") { list($id, $category) = explode(":", $part); // we don't want to include the modified category twice if ($id != $categoryid) { // get the category's weight $weight = @query("SELECT `weight` FROM `categories` WHERE `ID`='{$id}' LIMIT 1") or die("Error checking the database."); while ($row = result($weight)) { $total += $row->weight; } } } } $total += $categoryweight;
/** * calculate a users's class average * * Takes category weights into effect; contact me if it's not working correctly. */ function class_average($user, $class, $the_grading_period) { $total_points_scored = 0; $total_points_possible = 0; // get the class's categories $categories = return_categories($class); $categories = explode(":::::", $categories); foreach ($categories as $the_part) { if ($the_part != "") { list($categoryid, $categoryname) = explode(":", $the_part); // get the category's weight $info = @query("SELECT `weight` FROM `categories` WHERE `ID`='{$categoryid}' LIMIT 1"); while ($row = result($info)) { $categoryweight = $row->weight; $total_category_scored = 0; $total_category_possible = 0; // get the assignments that belong to this category $points = @query("SELECT * FROM `grades` WHERE `student_ID`='{$user}' AND `class_id`='{$class}' AND `grading_period`='{$the_grading_period}' AND (`points_scored` != 'x' OR `points_scored` != 'X') AND `category`='{$categoryid}'") or die("Error."); while ($row2 = result($points)) { $total_category_scored += $row2->points_scored; $total_category_possible += $row2->points_possible; } // multiple the total category scores by the category's weight, and add to the overall total $total_category_scored = $total_category_scored * $categoryweight; $total_category_possible = $total_category_possible * $categoryweight; $total_points_scored += $total_category_scored; $total_points_possible += $total_category_possible; } } } if ($total_points_possible == 0 and $total_points_scored == 0) { $total_points_possible = 1; $total_points_scored = 1; } $average = $total_points_scored / $total_points_possible; // ..to make it a percent.. $average = $average * 100; // round to the nearest hundredths $average = round($average, 2); return $average; }