MDL-9506 Added grademax extrapolation from scale info for grade_item, in insert(...
authornicolasconnault <nicolasconnault>
Thu, 24 May 2007 03:32:20 +0000 (03:32 +0000)
committernicolasconnault <nicolasconnault>
Thu, 24 May 2007 03:32:20 +0000 (03:32 +0000)
lib/grade/grade_calculation.php
lib/grade/grade_grades_final.php
lib/grade/grade_item.php
lib/grade/grade_tree.php

index 1508dc89a2188cc16960a95ef3f9de260c1082fd..e3b75fb1b472478e5373a40671c4ccb90b8b57cd 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.com                                            //
 //                                                                       //
-// Copyright (C) 2001-2003  Martin Dougiamas  http://dougiamas.com       //
+// Copyright (C) 2001-2007  Martin Dougiamas  http://dougiamas.com       //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
 // it under the terms of the GNU General Public License as published by  //
index 18221ce1e428b04f5a9e170fbb8d01f9ed1a8990..713113ffd064d42d04b5fa76ab1a178acc96b8d6 100644 (file)
@@ -150,6 +150,20 @@ class grade_grades_final extends grade_object {
             return false;
         }
     } 
+
+    /**
+     * Extends grade_object->insert() to prevent a final grade from being inserted if it already exists
+     * (If a final grade already has the same userid/itemid key combination). It will update it instead.
+     *
+     */
+    function insert() {
+        $exists = count_records('grade_grades_final', 'userid', $this->userid, 'itemid', $this->itemid);
+        if ($exists) {
+            return $this->update();
+        } else {
+            return parent::insert();
+        }
+    }
 }
 
 ?>
index 51a9dcf3f01ca296aae74b689d657c4216050d04..06e4aedd346ce3892869fb8a031d171d4cc4b402 100644 (file)
@@ -352,6 +352,15 @@ class grade_item extends grade_object {
             $this->outcomeid = $this->outcome->id;
         }
         
+        // Retrieve scale and infer grademax from it
+        if (!empty($this->scaleid)) {
+            $this->load_scale();
+            $this->scale->load_items();
+            $this->grademax = count ($this->scale->scale_items);
+            $this->grademin = 0;
+            $this->gradetype = GRADE_TYPE_SCALE;
+        }
+        
         if (!empty($this->scale->id)) {
             $this->scaleid = $this->scale->id;
         }
@@ -450,6 +459,15 @@ class grade_item extends grade_object {
      * @return int ID of the new grade_item record.
      */
     function insert() {
+        // Retrieve scale and infer grademax from it
+        if (!empty($this->scaleid)) {
+            $this->load_scale();
+            $this->scale->load_items();
+            $this->grademax = count ($this->scale->scale_items);
+            $this->grademin = 0;
+            $this->gradetype = GRADE_TYPE_SCALE;
+        }
+        
         $result = parent::insert();
 
         // Notify parent category of need to update. Note that a grade_item may not have a categoryid.
@@ -554,6 +572,7 @@ class grade_item extends grade_object {
             $final_grade->gradevalue = $this->adjust_grade($raw_grade);
             $final_grade->itemid = $this->id;
             $final_grade->userid = $raw_grade->userid;
+            
             if ($final_grade->gradevalue > $this->grademax) {
                 debugging("FINAL GRADE EXCEEDED grademax FIRST");
                 return false;
index 8f642cd244329415c0441a91ccfec569cf4714a8..5a26316945db282a620e7a9573fb9489e3e13e43 100644 (file)
 //                                                                       //
 ///////////////////////////////////////////////////////////////////////////
 
+require_once $CFG->libdir . '/grade/grade_category.php';
+require_once $CFG->libdir . '/grade/grade_item.php';
+require_once $CFG->libdir . '/grade/grade_grades_final.php';
+require_once $CFG->libdir . '/grade/grade_grades_raw.php';
+
 /**
  * This class represents a complete tree of categories, grade_items and final grades,
  * organises as an array primarily, but which can also be converted to other formats.
@@ -91,7 +96,9 @@ class grade_tree {
             $this->tree_array = $this->get_tree($include_grades);
         }
         
-        $this->first_sortorder = key($this->tree_array);
+        if (!empty($this->tree_array)) {
+            $this->first_sortorder = key($this->tree_array);
+        }
     }
 
     /**
@@ -508,15 +515,23 @@ class grade_tree {
         // Get ordered list of grade_items (not category type)
         $query = "SELECT * FROM $items_table WHERE itemtype <> 'category' $itemconstraint ORDER BY sortorder";
         $grade_items = get_records_sql($query);
-
+        
+        if (empty($grade_items)) {
+            return null;
+        }
+        
         // For every grade_item that doesn't have a parent category, create category fillers
         foreach ($grade_items as $itemid => $item) {
             if (empty($item->categoryid)) {
                 $item = new grade_item($item);
-                $fillers[$item->sortorder] = $item;
+                if (empty($item->sortorder)) {
+                    $fillers[] = $item;
+                } else {
+                    $fillers[$item->sortorder] = $item;
+                }
             }
         }
-
+        
         // Get all top categories
         $query = "SELECT $category_table.*, sortorder FROM $category_table, $items_table 
                   WHERE iteminstance = $category_table.id $catconstraint ORDER BY sortorder";
@@ -524,7 +539,10 @@ class grade_tree {
         $topcats = get_records_sql($query);
         
         if (empty($topcats)) {
-            return null;
+            $topcats = $grade_items;
+            $topcats[0] = new stdClass();
+            $topcats[0]->sortorder = 0;
+            $topcats[0]->courseid = $this->courseid;
         }
         
         // If any of these categories has grade_items as children, create a topcategory filler with colspan=count(children)