]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10202 merge raw and final grade tables
authorskodak <skodak>
Wed, 20 Jun 2007 23:06:29 +0000 (23:06 +0000)
committerskodak <skodak>
Wed, 20 Jun 2007 23:06:29 +0000 (23:06 +0000)
40 files changed:
backup/backuplib.php
backup/restorelib.php
grade/export/lib.php
grade/export/ods/grade_export_ods.php
grade/export/txt/grade_export_txt.php
grade/export/xls/grade_export_xls.php
grade/export/xml/grade_export_xml.php
grade/import/csv/index.php
grade/import/lib.php
grade/import/xml/index.php
grade/report/user/index.php
lib/db/install.xml
lib/db/upgrade.php
lib/grade/grade_calculation.php
lib/grade/grade_category.php
lib/grade/grade_grades.php [moved from lib/grade/grade_grades_raw.php with 51% similarity]
lib/grade/grade_grades_final.php [deleted file]
lib/grade/grade_grades_text.php
lib/grade/grade_history.php
lib/grade/grade_item.php
lib/grade/grade_tree.php
lib/gradelib.php
lib/simpletest/fixtures/gradetest.php
lib/simpletest/grade/simpletest/testgradecalculation.php
lib/simpletest/grade/simpletest/testgradecategory.php
lib/simpletest/grade/simpletest/testgradefinal.php [deleted file]
lib/simpletest/grade/simpletest/testgradegrades.php [new file with mode: 0755]
lib/simpletest/grade/simpletest/testgradeitem.php
lib/simpletest/grade/simpletest/testgraderaw.php [deleted file]
lib/simpletest/grade/simpletest/testgradetext.php
lib/simpletest/grade/simpletest/testgradetree.php
lib/simpletest/testgradelib.php
mod/assignment/lib.php
mod/data/lib.php
mod/forum/lib.php
mod/glossary/lib.php
mod/lesson/index.php
mod/lesson/lib.php
mod/quiz/lib.php
version.php

index 0e5ff1839e89b70e44e1d2bba92cc03776881899..a3aac290a5b80cf57e6841f14a440a7485bfe794 100644 (file)
                 fwrite ($bf,full_tag("MULTFACTOR",5,false,$grade_item->multfactor));
                 fwrite ($bf,full_tag("PLUSFACTOR",5,false,$grade_item->plusfactor));
                 fwrite ($bf,full_tag("HIDDEN",5,false,$grade_item->hidden));
+                fwrite ($bf,full_tag("LOCKED",5,false,$grade_item->locked));
+                fwrite ($bf,full_tag("LOCKTIME",5,false,$grade_item->locktime));
 
                 // back up the other stuff here                
                 $status = backup_gradebook_calculations_info($bf,$preferences,$grade_item->id);              
-                $status = backup_gradebook_grades_raw_info($bf,$preferences,$grade_item->id);
-                $status = backup_gradebook_grades_final_info($bf,$preferences,$grade_item->id);
+                $status = backup_gradebook_grades_info($bf,$preferences,$grade_item->id);
                 $status = backup_gradebook_grades_history_info($bf,$preferences,$grade_item->id);
                 $status = backup_gradebook_grades_text_info($bf,$preferences,$grade_item->id);
 
         return $status;
     }
 
-    function backup_gradebook_grades_raw_info($bf,$preferences, $itemid) {
+    function backup_gradebook_grades_info($bf,$preferences, $itemid) {
 
         global $CFG;
 
         $status = true;
         
         // find all calculations belonging to this item
-        if ($raws = get_records('grade_grades_raw', 'itemid', $itemid)) {
-            fwrite ($bf,start_tag("GRADE_GRADES_RAW",5,true));
+        if ($raws = get_records('grade_grades', 'itemid', $itemid)) {
+            fwrite ($bf,start_tag("GRADE_GRADES",5,true));
             foreach ($raws as $raw) {
-                fwrite ($bf,start_tag("GRADE_RAW",6,true));
+                fwrite ($bf,start_tag("GRADE",6,true));
                 fwrite ($bf,full_tag("ID",7,false,$raw->id));
                 fwrite ($bf,full_tag("USERID",7,false,$raw->userid));
-                fwrite ($bf,full_tag("GRADEVALUE",7,false,$raw->gradevalue));
-                fwrite ($bf,full_tag("GRADEMAX",7,false,$raw->grademax));
-                fwrite ($bf,full_tag("GRADEMIN",7,false,$raw->grademin));
-                fwrite ($bf,full_tag("SCALEID",7,false,$raw->scaleid));
+                fwrite ($bf,full_tag("RAWGRADE",7,false,$raw->rawgrade));
+                fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$raw->rawgrademax));
+                fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$raw->rawgrademin));
+                fwrite ($bf,full_tag("RAWSCALEID",7,false,$raw->rawscaleid));
                 fwrite ($bf,full_tag("USERMODIFIED",7,false,$raw->usermodified));
-                fwrite ($bf,end_tag("GRADE_RAW",6,true));
-            }  
-            $stauts = fwrite ($bf,end_tag("GRADE_GRADES_RAW",5,true));
-        }
-        return $status;
-    }
-
-    function backup_gradebook_grades_final_info($bf, $preferences, $itemid) {
-
-        global $CFG;
-
-        $status = true;
-        
-        // find all calculations belonging to this item
-        if ($finals = get_records('grade_grades_final', 'itemid', $itemid)) {
-            fwrite ($bf,start_tag("GRADE_GRADES_FINAL",5,true));
-            foreach ($finals as $final) {
-                fwrite ($bf,start_tag("GRADE_FINAL",6,true));
-                fwrite ($bf,full_tag("ID",7,false,$final->id));
-                fwrite ($bf,full_tag("USERID",7,false,$final->userid));
-                fwrite ($bf,full_tag("GRADEVALUE",7,false,$final->gradevalue));
+                fwrite ($bf,full_tag("FINALGRADE",7,false,$raw->finalgrade));
                 fwrite ($bf,full_tag("HIDDEN",7,false,$final->hidden));
                 fwrite ($bf,full_tag("LOCKED",7,false,$final->locked));
+                fwrite ($bf,full_tag("LOCKTIME",7,false,$final->locktime));
                 fwrite ($bf,full_tag("EXPORTED",7,false,$final->exported));
-                fwrite ($bf,full_tag("USERMODIFIED",7,false,$final->usermodified));
-                fwrite ($bf,end_tag("GRADE_FINAL",6,true));
+                fwrite ($bf,end_tag("GRADE_RAW",6,true));
             }  
-            $stauts = fwrite ($bf,end_tag("GRADE_GRADES_FINAL",5,true));
+            $stauts = fwrite ($bf,end_tag("GRADE_GRADES",5,true));
         }
         return $status;
     }
index d0b20f14ebbe34363f069d98469943118802021a..e3cb459aa5223034891d5b07b3405907d082e271 100644 (file)
                             $dbrec->multfactor = backup_todb($info['GRADE_ITEM']['#']['MULTFACTOR']['0']['#']);
                             $dbrec->plusfactor = backup_todb($info['GRADE_ITEM']['#']['PLUSFACTOR']['0']['#']);
                             $dbrec->hidden = backup_todb($info['GRADE_ITEM']['#']['HIDDEN']['0']['#']);                                                                      
+                            $dbrec->locked = backup_todb($info['GRADE_ITEM']['#']['LOCKED']['0']['#']);                                                                      
+                            $dbrec->locktime = backup_todb($info['GRADE_ITEM']['#']['LOCKTIME']['0']['#']);                                                                      
                             
                             /// if thesse 5 all match then we know this item is already in db
 
                             $itemid = insert_record('grade_items',$dbrec);
                             
                             /// now, restore grade_calculations, grade_raw, grade_final, grade_text, and grade_history
-                            if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_RAW']['0']['#']) && ($raws = $info['GRADE_ITEM']['#']['GRADE_GRADES_RAW']['0']['#']['GRADE_RAW'])) {
+                            if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']) && ($grades = $info['GRADE_ITEM']['#']['GRADE_GRADES']['0']['#']['GRADE'])) {
                                 //Iterate over items
-                                for($i = 0; $i < sizeof($raws); $i++) {
-                                    $ite_info = $raws[$i];
+                                for($i = 0; $i < sizeof($grades); $i++) {
+                                    $ite_info = $grades[$i];
                                     //traverse_xmlize($ite_info);
 //Debug
                                     //print_object ($GLOBALS['traverse_array']);                                                  //Debug
                                     //$GLOBALS['traverse_array']="";                                                              //Debug
                                     //Now build the GRADE_ITEM record structure
-                                    $raw->itemid       = $itemid;
+                                    $grade = new object();
+                                    $grade->itemid       = $itemid;
                                     $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));            
-                                    $raw->userid = $user->new_id;
-                                    $raw->gradevalue = backup_todb($ite_info['#']['GRADEVALUE']['0']['#']);
-                                    $raw->grademax = backup_todb($ite_info['#']['GRADEMAX']['0']['#']);
-                                    $raw->grademin = backup_todb($ite_info['#']['GRADEMIN']['0']['#']);
+                                    $grade->userid = $user->new_id;
+                                    $grade->rawgrade = backup_todb($ite_info['#']['RAWGRADE']['0']['#']);
+                                    $grade->rawgrademax = backup_todb($ite_info['#']['RAWGRADEMAX']['0']['#']);
+                                    $grade->rawgrademin = backup_todb($ite_info['#']['RAWGRADEMIN']['0']['#']);
                                     // need to find scaleid
-                                
-                                    if ($ite_info['#']['SCALEID']['0']['#']) {
-                                        $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($ite_info['#']['SCALEID']['0']['#']));
-                                        $raw->scaleid = $scale->new_id;
+                                    if ($ite_info['#']['RAWSCALEID']['0']['#']) {
+                                        $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($ite_info['#']['RAWSCALEID']['0']['#']));
+                                        $grade->rawscaleid = $scale->new_id;
                                     }
+                                    $grade->finalgrade = backup_todb($ite_info['#']['FINALGRADE']['0']['#']);
+                                    $grade->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
+                                    $grade->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
+                                    $grade->locktime = backup_todb($ite_info['#']['LOCKTIME']['0']['#']);
+                                    $grade->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
                                 
-                                    insert_record('grade_grades_raw', $raw);
+                                    insert_record('grade_grades', $grade);
                                     
                                     $counter++;
                                     if ($counter % 20 == 0) {
                                     }
                                 }
                             }
-                            
-                            /// processing grade_grades_final   
-                            if (!empty($info['GRADE_ITEM']['#']['GRADE_GRADES_FINAL']['0']['#']) && ($finals = $info['GRADE_ITEM']['#']['GRADE_GRADES_FINAL']['0']['#']['GRADE_FINAL'])) {
-                                //Iterate over items
-                                for($i = 0; $i < sizeof($finals); $i++) {
-                                    $ite_info = $finals[$i];
-                                    //traverse_xmlize($ite_info);                                                                 //Debug
-                                    //print_object ($GLOBALS['traverse_array']);                                                  //Debug
-                                    //$GLOBALS['traverse_array']="";                                                              //Debug
-                                    $final->itemid       = $itemid;
-                                    $user = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERID']['0']['#']));            
-                                    $final->userid = $user->new_id;
-                                    $final->gradevalue = backup_todb($ite_info['#']['GRADEVALUE']['0']['#']);
-                                    $final->hidden = backup_todb($ite_info['#']['HIDDEN']['0']['#']);
-                                    $final->locked = backup_todb($ite_info['#']['LOCKED']['0']['#']);
-                                    $final->exported = backup_todb($ite_info['#']['EXPORTED']['0']['#']);
-                                    
-                                    $modifier = backup_getid($restore->backup_unique_code,"user", backup_todb($ite_info['#']['USERMODIFIED']['0']['#']));
-                                    $final->usermodified = $modifier->new_id;
-                                
-                                    insert_record('grade_grades_final', $final);
-                                    
-                                    $counter++;
-                                    if ($counter % 20 == 0) {
-                                        if (!defined('RESTORE_SILENTLY')) {
-                                            echo ".";
-                                            if ($counter % 400 == 0) {
-                                                echo "<br />";
-                                            }
-                                        }
-                                        backup_flush(300);
-                                    }
-                                }
-                            }    
                                
                             /// processing grade_calculations
                             if (!empty($info['GRADE_ITEM']['#']['GRADE_CALCULATIONS']['0']['#']) && ($calcs = $info['GRADE_ITEM']['#']['GRADE_CALCULATIONS']['0']['#']['GRADE_CALCULATION'])) {
index 791b4f3dbc0709473f718bc55109ce96c6e3a355..f1e4f7096c299a2ad4ecc67ccc4a3bdaa9ef1b54 100755 (executable)
@@ -155,8 +155,8 @@ class grade_export {
                                 $studentgrade = $itemgrades[$student->id];
                             }
                             
-                            if (!empty($studentgrade->gradevalue)) {
-                                $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->gradevalue;                                    
+                            if (!empty($studentgrade->finalgrade)) {
+                                $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->finalgrade;                                    
                             } else {
                                 $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = "";
                                 $this->gradeshtml[$student->id][$gradeitem->id] = "";
index 590f25ed8e4a5e3df157652d8b5450676a36f34c..b0d1301ebebf8f09f29b484a50147927bd88bd19 100755 (executable)
@@ -106,17 +106,17 @@ class grade_export_ods extends grade_export {
                     }                                   
                     
                     /// if export flag needs to be set
-                    /// construct the grade_grades_final object and update timestamp if CFG flag is set
+                    /// construct the grade_grades object and update timestamp if CFG flag is set
                 
                     if ($export) {
-                        unset($params);
+                        $params= new object();
                         $params->itemid = $gradeitemid;
                         $params->userid = $studentid;
                 
-                        $grade_grades_final = new grade_grades_final($params);
-                        $grade_grades_final->exported = time();
+                        $grade_grades = new grade_grades($params);
+                        $grade_grades->exported = time();
                         // update the time stamp;
-                        $grade_grades_final->update();
+                        $grade_grades->update();
                     }
                 }
                 $myxls->write_number($i,$j,$this->totals[$student->id]);
index 7655b40ffb3300373d051b0058fbc3a831a814cd..6339aac34b9935d016b651680f6ac5acdda12e85 100755 (executable)
@@ -97,17 +97,17 @@ class grade_export_txt extends grade_export {
                 }                
                 
                 /// if export flag needs to be set
-                /// construct the grade_grades_final object and update timestamp if CFG flag is set
+                /// construct the grade_grades object and update timestamp if CFG flag is set
 
                 if ($export) {
-                    unset($params);
+                    $params = new object();
                     $params->itemid = $gradeitemid;
                     $params->userid = $studentid;
                 
-                    $grade_grades_final = new grade_grades_final($params);
-                    $grade_grades_final->exported = time();
+                    $grade_grades = new grade_grades($params);
+                    $grade_grades->exported = time();
                     // update the time stamp;
-                    $grade_grades_final->update();
+                    $grade_grades->update();
                 }
             }
             echo "{$this->separator}".$this->totals[$student->id];
index 92fea23bc6062fa60398e4bc6df1321a759e9a87..dcbd01f7f5947496e3b34ac813fe65e93f16e86e 100755 (executable)
@@ -105,17 +105,17 @@ class grade_export_xls extends grade_export {
                     }
 
                     /// if export flag needs to be set
-                    /// construct the grade_grades_final object and update timestamp if CFG flag is set
+                    /// construct the grade_grades object and update timestamp if CFG flag is set
                 
                     if ($export) {
-                        unset($params);
+                        $params = new object();
                         $params->itemid = $gradeitemid;
                         $params->userid = $studentid;
                 
-                        $grade_grades_final = new grade_grades_final($params);
-                        $grade_grades_final->exported = time();
+                        $grade_grades = new grade_grades($params);
+                        $grade_grades->exported = time();
                         // update the time stamp;
-                        $grade_grades_final->update();
+                        $grade_grades->update();
                     }
                 }
                 $myxls->write_number($i,$j,$this->totals[$student->id]);
index a07cacdea463306e0668879a2fa8f5a1dded571d..51425f0d1814e6600184032625839745a42c8ecf 100755 (executable)
@@ -66,6 +66,7 @@ class grade_export_xml extends grade_export {
                 // state can be new, or regrade
                 // require comparing of timestamps in db
                 
+                $params = new object();
                 $params->idnumber = $idnumber;
                 // get the grade item
                 $gradeitem = new grade_item($params);
@@ -73,16 +74,16 @@ class grade_export_xml extends grade_export {
                 // we are trying to figure out if this is a new grade, or a regraded grade
                 // only relevant if this grade for this user is already exported
                     
-                // get the grade_grades_final for this user
-                unset($params);
+                // get the grade_grades for this user
+                $params = new object();
                 $params->itemid = $gradeitem->id;
                 $params->userid = $studentid;
                 
-                $grade_grades_final = new grade_grades_final($params);
+                $grade_grades = new grade_grades($params);
                     
                 // if exported, check grade_history, if modified after export, set state to regrade
-                if (!empty($grade_grades_final->exported)) {
-                    if (record_exists_select('grade_history', 'itemid = '.$gradeitem->id.' AND userid = '.$studentid.' AND timemodified > '.$grade_grades_final->exported)) {
+                if (!empty($grade_grades->exported)) {
+                    if (record_exists_select('grade_history', 'itemid = '.$gradeitem->id.' AND userid = '.$studentid.' AND timemodified > '.$grade_grades->exported)) {
                         $status = 'regrade';  
                     } else {
                         $status = 'new';  
@@ -105,9 +106,9 @@ class grade_export_xml extends grade_export {
 
                 // timestamp this if needed
                 if ($export) {
-                    $grade_grades_final->exported = time();
+                    $grade_grades->exported = time();
                     // update the time stamp;
-                    $grade_grades_final->update();
+                    $grade_grades->update();
                 }
             }
         }
index baf480b765190cf05a1c23c1ad742c0353169d95..08fbc4353d9f65c7a744ff6118717eea79c8bda3 100755 (executable)
@@ -143,7 +143,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
                         } 
                         unset($newgrade);
                         $newgrade -> newgradeitem = $newgradeitems[$key];
-                        $newgrade -> gradevalue = $value;                        
+                        $newgrade -> rawgrade = $value;                        
                         $newgrades[] = $newgrade;
                         
                         // if not, put it in                        
@@ -174,7 +174,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
                             
                             unset($newgrade);
                             $newgrade -> itemid = $gradeitem->id;
-                            $newgrade -> gradevalue = $value;                            
+                            $newgrade -> rawgrade = $value;                            
                             $newgrades[] = $newgrade;
                         } // otherwise, we ignore this column altogether 
                           // because user has chosen to ignore them (e.g. institution, address etc)
index 1eaec1956ac6f86eb7346f47572e505e502ea8a0..50db97f2344b052554f134bd31808ecdcc12ea47 100755 (executable)
@@ -36,7 +36,7 @@ function grade_import_commit($courseid, $importcode) {
                     
                     $g = new object();
                     $g -> userid = $grade->userid;
-                    $g -> gradevalue = $grade->gradevalue;                    
+                    $g -> rawgrade = $grade->rawgrade;                    
                     $studentgrades[] = $g ;  
 
                 }
@@ -96,7 +96,7 @@ function grade_import_commit($courseid, $importcode) {
                     
                     $g = new object();
                     $g -> userid = $grade->userid;
-                    $g -> gradevalue = $grade->gradevalue;                    
+                    $g -> rawgrade = $grade->rawgrade;                    
                     $studentgrades[] = $g ;  
 
                 }
index 170a16357eb83ba9e523d8226c0614b4167bfb47..3e37a94e691b117ae7d5458330f3815ec7ea6afe 100755 (executable)
@@ -87,7 +87,7 @@ if ( $formdata = $mform->get_data()) {
 
             if (isset($result['#']['score'][0]['#'])) {
                 $newgrade -> itemid = $gradeitem->id;
-                $newgrade -> gradevalue = $result['#']['score'][0]['#'];
+                $newgrade -> rawgrade = $result['#']['score'][0]['#'];
                 $newgrade-> userid = $result['#']['student'][0]['#'];
                 $newgrades[] = $newgrade;
             }
@@ -108,7 +108,7 @@ if ( $formdata = $mform->get_data()) {
                 }
           
                 // check grade value is a numeric grade
-                if (!is_numeric($newgrade->gradevalue)) {
+                if (!is_numeric($newgrade->rawgrade)) {
                     $status = false;
                     import_cleanup($importcode);
                     notify(get_string('badgrade', 'grades'));
index 7c67cc3056723628f09af2351e55821be30167d3..bac1b4643f7654641ab13185b9f2fa09bdce079b 100644 (file)
@@ -59,8 +59,8 @@ if ($gradetree = new grade_tree($courseid)) {
 
         $params->itemid = $gradeitem->id;
         $params->userid = $userid;
-        $grade_grades_final = new grade_grades_final($params);
-        $grade_text = $grade_grades_final->load_text();
+        $grade_grades = new grade_grades($params);
+        $grade_text = $grade_grades->load_text();
 
         /// prints mod icon if available
         if ($gradeitem->itemtype == 'mod') {
@@ -81,26 +81,25 @@ if ($gradetree = new grade_tree($courseid)) {
         }
     
         /// prints the grade 
-        $data[] = $grade_grades_final->gradevalue;
+        $data[] = $grade_grades->finalgrade;
     
         /// prints percentage
    
         if ($gradeitem->gradetype == 1) {
             // processing numeric grade
-            if ($grade_grades_final->gradevalue) {
-                $percentage = $grade_grades_final->gradevalue / $gradeitem->grademax * 100 .'%';
+            if ($grade_grades->finalgrade) {
             } else {
                 $percentage = '-';
             }
             $gradetotal += $gradeitem->grademax;
-            $gradesum += $grade_grades_final->gradevalue;
+            $gradesum += $grade_grades->finalgrade;
         } else if ($gradeitem->gradetype == 2) {
             // processing scale grade
             $scale = get_record('scale', 'id', $gradeitem->scaleid);
             $scalevals = explode(",", $scale->scale);
-            $percentage = ($grade_grades_final->gradevalue -1) / count($scalevals);        
+            $percentage = ($grade_grades->finalgrade -1) / count($scalevals);        
             $gradesum += count($scalevals);
-            $gradetotal += $grade_grades_final->gradevalue;
+            $gradetotal += $grade_grades->finalgrade;
         } else {
             // text grade
             $percentage = '-';  
index cd11c8a7ec3c73df9504f31b68b0c69486311cf6..841978717b219ab2d4376552f2d0a3e2aa44d452 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20070605" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20070619" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
         <FIELD NAME="plusfactor" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="Add this to all grades" PREVIOUS="multfactor" NEXT="sortorder"/>
         <FIELD NAME="sortorder" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Sorting order of the columns" PREVIOUS="plusfactor" NEXT="hidden"/>
         <FIELD NAME="hidden" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 is hidden, &amp;gt; 1 is a date to hide until (prevents viewing)" PREVIOUS="sortorder" NEXT="locked"/>
-        <FIELD NAME="locked" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 is locked, &amp;gt; 1 is a date to lock until (prevents update)" PREVIOUS="hidden" NEXT="deleted"/>
-        <FIELD NAME="deleted" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 means the associated module instance has been deleted" PREVIOUS="locked" NEXT="needsupdate"/>
+        <FIELD NAME="locked" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 is locked, &amp;gt; 1 is a date to lock until (prevents update)" PREVIOUS="hidden" NEXT="locktime"/>
+        <FIELD NAME="locktime" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="lock all final grades after this date" PREVIOUS="locked" NEXT="deleted"/>
+        <FIELD NAME="deleted" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 means the associated module instance has been deleted" PREVIOUS="locktime" NEXT="needsupdate"/>
         <FIELD NAME="needsupdate" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="If this flag is set, then the whole column will be recalculated" PREVIOUS="deleted" NEXT="timecreated"/>
         <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The first time this grade_item was created" PREVIOUS="needsupdate" NEXT="timemodified"/>
         <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The last time this grade_item was modified" PREVIOUS="timecreated"/>
         <KEY NAME="parent" TYPE="foreign" FIELDS="parent" REFTABLE="grade_categories" REFFIELDS="id" PREVIOUS="courseid"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="grade_calculations" COMMENT="This table describes the calculated grade_items in more details." PREVIOUS="grade_categories" NEXT="grade_grades_final">
+    <TABLE NAME="grade_calculations" COMMENT="This table describes the calculated grade_items in more details." PREVIOUS="grade_categories" NEXT="grade_grades">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="itemid"/>
         <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The grade_item this relates to" PREVIOUS="id" NEXT="calculation"/>
         <KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id" PREVIOUS="itemid"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="grade_grades_final" COMMENT="This table keeps individual grades for each user and each item/category– they have undergone all scaling and other calculations, and are ready for display. This table is effectively a cache and values are rebuilt whenever source values change. The gradevalue or gradescale values are all normalised to the max/min or scaleid as defined in the grade_item table." PREVIOUS="grade_calculations" NEXT="grade_grades_raw">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="itemid"/>
-        <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The associated grade_item these grades belong to" PREVIOUS="id" NEXT="userid"/>
-        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The user who this grade is for" PREVIOUS="itemid" NEXT="gradevalue"/>
-        <FIELD NAME="gradevalue" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If the grade is a float value (or has been converted to one)" PREVIOUS="userid" NEXT="hidden"/>
-        <FIELD NAME="hidden" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 is hidden, &amp;gt; 1 is a date to hide until" PREVIOUS="gradevalue" NEXT="locked"/>
-        <FIELD NAME="locked" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="1 is locked, &amp;gt;1 is a date to lock after" PREVIOUS="hidden" NEXT="exported"/>
-        <FIELD NAME="exported" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="0 is not exported, &amp;gt; 1 is the last exported date" PREVIOUS="locked" NEXT="timecreated"/>
-        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the time this grade was first created" PREVIOUS="exported" NEXT="usermodified"/>
-        <FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the userid of the person who last modified this grade" PREVIOUS="timecreated" NEXT="timemodified"/>
-        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the time this grade was last modified" PREVIOUS="usermodified"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="primary key of the table, please edit me" NEXT="itemid"/>
-        <KEY NAME="itemid" TYPE="foreign" FIELDS="itemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="primary" NEXT="userid"/>
-        <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id" PREVIOUS="itemid" NEXT="usermodified"/>
-        <KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id" PREVIOUS="userid"/>
-      </KEYS>
-    </TABLE>
-    <TABLE NAME="grade_grades_raw" COMMENT="grade_grades_raw  This table keeps individual grades for each user and each item, exactly as imported or submitted by modules. The grademax/min and scaleid are stored here to record the values at the time the grade was stored, because teachers might change this for an activity! All the results are normalised/resampled for the grade_grades_final table." PREVIOUS="grade_grades_final" NEXT="grade_grades_text">
+    <TABLE NAME="grade_grades" COMMENT="grade_grades  This table keeps individual grades for each user and each item, exactly as imported or submitted by modules. The rawgrademax/min and rawscaleid are stored here to record the values at the time the grade was stored, because teachers might change this for an activity! All the results are normalised/resampled for the final grade value." PREVIOUS="grade_calculations" NEXT="grade_grades_text">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="itemid"/>
         <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The item this grade belongs to" PREVIOUS="id" NEXT="userid"/>
-        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The user who this grade is for" PREVIOUS="itemid" NEXT="gradevalue"/>
-        <FIELD NAME="gradevalue" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If the grade is a float value (or has been converted to one)" PREVIOUS="userid" NEXT="grademax"/>
-        <FIELD NAME="grademax" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="100" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="The maximum allowable grade when this was created" PREVIOUS="gradevalue" NEXT="grademin"/>
-        <FIELD NAME="grademin" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="The minimum allowable grade when this was created" PREVIOUS="grademax" NEXT="scaleid"/>
-        <FIELD NAME="scaleid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="If this grade is based on a scale, which one was it?" PREVIOUS="grademin" NEXT="timecreated"/>
-        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the time this grade was first created" PREVIOUS="scaleid" NEXT="timemodified"/>
-        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the time this grade was last modified" PREVIOUS="timecreated" NEXT="usermodified"/>
-        <FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the userid of the person who last modified this grade" PREVIOUS="timemodified"/>
+        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="The user who this grade is for" PREVIOUS="itemid" NEXT="rawgrade"/>
+        <FIELD NAME="rawgrade" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="If the grade is a float value (or has been converted to one)" PREVIOUS="userid" NEXT="rawgrademax"/>
+        <FIELD NAME="rawgrademax" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="100" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="The maximum allowable grade when this was created" PREVIOUS="rawgrade" NEXT="rawgrademin"/>
+        <FIELD NAME="rawgrademin" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="The minimum allowable grade when this was created" PREVIOUS="rawgrademax" NEXT="rawscaleid"/>
+        <FIELD NAME="rawscaleid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="If this grade is based on a scale, which one was it?" PREVIOUS="rawgrademin" NEXT="usermodified"/>
+        <FIELD NAME="usermodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the userid of the person who last modified this grade" PREVIOUS="rawscaleid" NEXT="finalgrade"/>
+        <FIELD NAME="finalgrade" TYPE="number" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="The final grade (cached) after all calculations are made" PREVIOUS="usermodified" NEXT="hidden"/>
+        <FIELD NAME="hidden" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="show 0, hide 1 or hide until date" PREVIOUS="finalgrade" NEXT="locked"/>
+        <FIELD NAME="locked" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="not locked 0, locked from date" PREVIOUS="hidden" NEXT="locktime"/>
+        <FIELD NAME="locktime" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="automatic locking of final grade, 0 means none, date otherwise" PREVIOUS="locked" NEXT="exported"/>
+        <FIELD NAME="exported" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="date of last grade export, 0 if none" PREVIOUS="locktime" NEXT="timecreated"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the time this grade was first created" PREVIOUS="exported" NEXT="timemodified"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="the time this grade was last modified" PREVIOUS="timecreated"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="primary key of the table, please edit me" NEXT="itemid"/>
         <KEY NAME="itemid" TYPE="foreign" FIELDS="itemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="primary" NEXT="userid"/>
-        <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id" PREVIOUS="itemid" NEXT="scaleid"/>
-        <KEY NAME="scaleid" TYPE="foreign" FIELDS="scaleid" REFTABLE="scale" REFFIELDS="id" PREVIOUS="userid" NEXT="usermodified"/>
-        <KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id" PREVIOUS="scaleid"/>
+        <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id" PREVIOUS="itemid" NEXT="rawscaleid"/>
+        <KEY NAME="rawscaleid" TYPE="foreign" FIELDS="rawscaleid" REFTABLE="scale" REFFIELDS="id" PREVIOUS="userid" NEXT="usermodified"/>
+        <KEY NAME="usermodified" TYPE="foreign" FIELDS="usermodified" REFTABLE="user" REFFIELDS="id" PREVIOUS="rawscaleid"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="grade_grades_text" COMMENT="This table keeps additional textual information about each individual grade, whether it be automatically generated from the module or entered manually by the teacher. It's here separate from the all-numeric grade_grades for database efficiency reasons." PREVIOUS="grade_grades_raw" NEXT="grade_outcomes">
+    <TABLE NAME="grade_grades_text" COMMENT="This table keeps additional textual information about each individual grade, whether it be automatically generated from the module or entered manually by the teacher. It's here separate from the all-numeric grade_grades for database efficiency reasons." PREVIOUS="grade_grades" NEXT="grade_outcomes">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="itemid"/>
         <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="item id" PREVIOUS="id" NEXT="userid"/>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="itemid"/>
         <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="if set, this points to existing grade_items id" PREVIOUS="id" NEXT="newgradeitem"/>
         <FIELD NAME="newgradeitem" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="if set, points to the id of grade_import_newitem" PREVIOUS="itemid" NEXT="userid"/>
-        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="newgradeitem" NEXT="gradevalue"/>
-        <FIELD NAME="gradevalue" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0.0" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="raw grade value" PREVIOUS="userid" NEXT="import_code"/>
-        <FIELD NAME="import_code" TYPE="int" LENGTH="12" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="similar to backup_code, a unique batch code for identifying one batch of imports" PREVIOUS="gradevalue"/>
+        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="newgradeitem" NEXT="rawgrade"/>
+        <FIELD NAME="rawgrade" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0.0" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="raw grade value" PREVIOUS="userid" NEXT="import_code"/>
+        <FIELD NAME="import_code" TYPE="int" LENGTH="12" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="similar to backup_code, a unique batch code for identifying one batch of imports" PREVIOUS="rawgrade"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="primary key of the table, please edit me" NEXT="itemid"/>
index 3669ba0e3e2a758242df46a736e9f9b075868118..dc980b767b5f804571ef7d6a7688694b01426a6f 100644 (file)
@@ -884,75 +884,62 @@ function xmldb_main_upgrade($oldversion=0) {
     /// Launch create table for grade_calculations
         $result = $result && create_table($table);
         
-    /// Define table grade_grades_raw to be created
-        $table = new XMLDBTable('grade_grades_raw');
-
-    /// Adding fields to table grade_grades_raw
-        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
-        $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-        $table->addFieldInfo('gradevalue', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
-        $table->addFieldInfo('gradescale', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
-        $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
-        $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
-        $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-        $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-        $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-        $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-
-    /// Adding keys to table grade_grades_raw
-        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
-        $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
-        $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
-        $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
-        $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
 
-    /// Launch create table for grade_grades_raw
-        $result = $result && create_table($table);
-        
-    /// Define table grade_grades_final to be created
-        $table = new XMLDBTable('grade_grades_final');
+    /// Define table grade_grades to be created
+        $table = new XMLDBTable('grade_grades');
 
-    /// Adding fields to table grade_grades_final
+    /// Adding fields to table grade_grades
         $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
         $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
         $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-        $table->addFieldInfo('gradevalue', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
-        $table->addFieldInfo('gradescale', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
-        $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
+        $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
+        $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
+        $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
+        $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+        $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+        $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
+        $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+        $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+        $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
         $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
         $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
         $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-        $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
 
-    /// Adding keys to table grade_grades_final
+    /// Adding keys to table grade_grades
         $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
         $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
         $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
+        $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
         $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
 
-    /// Launch create table for grade_grades_final
+    /// Launch create table for grade_grades
         $result = $result && create_table($table);
         
+
     /// Define table grade_grades_text to be created
         $table = new XMLDBTable('grade_grades_text');
 
     /// Adding fields to table grade_grades_text
         $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
-        $table->addFieldInfo('gradesid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
+        $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
+        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
         $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
+        $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
         $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
+        $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
         $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
         $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
         $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
 
     /// Adding keys to table grade_grades_text
         $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
-        $table->addKeyInfo('gradesid', XMLDB_KEY_FOREIGN, array('gradesid'), 'grade_grades_raw', array('id'));
         $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
+        $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_item', array('id'));
+        $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
 
     /// Launch create table for grade_grades_text
         $result = $result && create_table($table);
+
         
    /// Define table grade_outcomes to be created
         $table = new XMLDBTable('grade_outcomes');
@@ -1072,58 +1059,10 @@ function xmldb_main_upgrade($oldversion=0) {
     /// Launch add field path
         $result = $result && add_field($table, $field);
      
-    /// Define field gradevalue to be dropped from grade_grades_final
-        $table = new XMLDBTable('grade_grades_final');
-        $field = new XMLDBField('gradevalue');
-
-    /// Launch drop field gradevalue
-        $result = $result && drop_field($table, $field);
-    
-    /// Define field gradescale to be dropped from grade_grades_final
-        $table = new XMLDBTable('grade_grades_final');
-        $field = new XMLDBField('gradescale');
-
-    /// Launch drop field gradescale
-        $result = $result && drop_field($table, $field);
-    
-    /// Define field locked to be added to grade_grades_final
-        $table = new XMLDBTable('grade_grades_final');
-        $field = new XMLDBField('locked');
-        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'hidden');
-
-    /// Launch add field locked
-        $result = $result && add_field($table, $field);
     } 
-    
-    if ($result && $oldversion < 2007043000) {
-
-    /// Define field gradevalue to be added to grade_grades_final
-        $table = new XMLDBTable('grade_grades_final');
-        $field = new XMLDBField('gradevalue');
-        $field->setAttributes(XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null, 'userid');
-
-    /// Launch add field gradevalue
-        $result = $result && add_field($table, $field);
-        
-    /// Define field gradescale to be added to grade_grades_final
-        $table = new XMLDBTable('grade_grades_final');
-        $field = new XMLDBField('gradescale');
-        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null, 'gradevalue');
-
-    /// Launch add field gradescale
-        $result = $result && add_field($table, $field);
-    }
 
     if ($result && $oldversion < 2007043001) {
 
-    /// Define field informationformat to be added to grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $field = new XMLDBField('informationformat');
-        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0', 'information');
-
-    /// Launch add field informationformat
-        $result = $result && add_field($table, $field);
-        
     /// Define field schedule to be added to events_handlers
         $table = new XMLDBTable('events_handlers');
         $field = new XMLDBField('schedule');
@@ -1170,87 +1109,6 @@ function xmldb_main_upgrade($oldversion=0) {
     /// Launch drop field parent
         $result = $result && drop_field($table, $field);
     }
-    if ($result && $oldversion < 2007050400) {
-
-    /// Define field feedbackformat to be added to grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $field = new XMLDBField('feedbackformat');
-        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'feedback');
-
-    /// Launch add field feedbackformat
-        $result = $result && add_field($table, $field);
-    }
-    
-    
-    if ($result && $oldversion < 2007050900) {
-
-    /// Define field gradescale to be dropped from grade_grades_raw
-        $table = new XMLDBTable('grade_grades_raw');
-        $field = new XMLDBField('gradescale');
-
-    /// Launch drop field gradescale
-        $result = $result && drop_field($table, $field);
-        
-        
-    /// Define field gradescale to be dropped from grade_grades_final
-        $table = new XMLDBTable('grade_grades_final');
-        $field = new XMLDBField('gradescale');
-
-    /// Launch drop field gradescale
-        $result = $result && drop_field($table, $field);
-    }
-    
-    /// fixing the problem of grade_grades_text can't be referenced directly from grade_grades_final
-    if ($result && $oldversion < 2007050901) {
-
-    /// Define key gradesid (foreign) to be dropped form grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $key = new XMLDBKey('gradesid');
-        $key->setAttributes(XMLDB_KEY_FOREIGN, array('gradesid'), 'grade_grades_raw', array('id'));
-
-    /// Launch drop key gradesid
-        $result = $result && drop_key($table, $key);
-        
-    /// Define field gradesid to be dropped from grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $field = new XMLDBField('gradesid');
-
-    /// Launch drop field gradesid
-        $result = $result && drop_field($table, $field);
-        
-    /// Define field itemid to be added to grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $field = new XMLDBField('itemid');
-        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'id');
-
-    /// Launch add field itemid
-        $result = $result && add_field($table, $field);
-        
-    /// Define field userid to be added to grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $field = new XMLDBField('userid');
-        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'itemid');
-
-    /// Launch add field userid
-        $result = $result && add_field($table, $field);
-        
-    /// Define key itemid (foreign) to be added to grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $key = new XMLDBKey('itemid');
-        $key->setAttributes(XMLDB_KEY_FOREIGN, array('itemid'), 'grade_item', array('id'));
-
-    /// Launch add key itemid
-        $result = $result && add_key($table, $key);
-                
-    /// Define key userid (foreign) to be added to grade_grades_text
-        $table = new XMLDBTable('grade_grades_text');
-        $key = new XMLDBKey('userid');
-        $key->setAttributes(XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
-
-    /// Launch add key userid
-        $result = $result && add_key($table, $key);
-    
-    }
 
     if ($result && $oldversion < 2007051100) {
 
@@ -1356,18 +1214,120 @@ function xmldb_main_upgrade($oldversion=0) {
         $result = $result && change_field_default($table, $field);
     }
     
-    /// new tables for import buffers
-    if ($result && $oldversion < 2007060502) {
-    
+
+/// merge raw and final grade tables
+    if ($result && $oldversion < 2007062007) {
+        // it should be ok to frop following tables so early in development cycle ;-)
+        // the grades can be fetched again from modules anyway
+
+        $table = new XMLDBTable('grade_grades_final');
+        if (table_exists($table)) {
+            drop_table($table);
+        }
+
+        $table = new XMLDBTable('grade_grades_raw');
+        if (table_exists($table)) {
+            drop_table($table);
+        }
+
+        $table = new XMLDBTable('grade_grades_text');
+        $field = new XMLDBField('gradesid');
+
+        if (field_exists($table, $field)) {
+            drop_table($table);
+
+        /// Adding fields to table grade_grades_text
+            $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+            $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
+            $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
+            $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
+            $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null);
+            $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+            $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
+            $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null);
+
+        /// Adding keys to table grade_grades_text
+            $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
+            $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
+            $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_item', array('id'));
+            $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
+
+        /// Launch create table for grade_grades_text
+            $result = $result && create_table($table);
+        }
+
+        $table = new XMLDBTable('grade_grades');
+        if (!table_exists($table)) {
+        /// Adding fields to table grade_grades
+            $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
+            $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
+            $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
+            $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
+            $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
+            $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+            $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+            $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
+            $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+            $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+
+        /// Adding keys to table grade_grades
+            $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
+            $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
+            $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
+            $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
+            $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
+
+        /// Launch create table for grade_grades
+            $result = $result && create_table($table);
+        }
+
+        $table  = new XMLDBTable('grade_items');
+        $field = new XMLDBField('locktime');
+
+        if (!field_exists($table, $field)) {
+            $locktime->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'locked');
+        /// Launch add field locktime
+            $result = $result && add_field($table, $field);
+        }
+
+        /// Remove the obsoleted unitttests tables - they will be recreated automatically
+        $tables = array('grade_categories',
+                        'scale',
+                        'grade_items',
+                        'grade_calculations',
+                        'grade_grades_raw',
+                        'grade_grades_final',
+                        'grade_grades_text',
+                        'grade_outcomes',
+                        'grade_history');
+
+        foreach ($tables as $table) {
+            $table = new XMLDBTable('unittest_'.$table);
+            if (table_exists($table)) {
+                drop_table($table);
+            }
+
+        }
+
     /// Define table grade_import_values to be created
         $table = new XMLDBTable('grade_import_values');
+        if (table_exists($table)) {
+            drop_table($table);
+        }
 
     /// Adding fields to table grade_import_values
         $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
         $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
         $table->addFieldInfo('newgradeitem', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
         $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-        $table->addFieldInfo('gradevalue', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0.0');
+        $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0.0');
         $table->addFieldInfo('import_code', XMLDB_TYPE_INTEGER, '12', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
 
     /// Adding keys to table grade_import_values
@@ -1377,9 +1337,12 @@ function xmldb_main_upgrade($oldversion=0) {
 
     /// Launch create table for grade_import_values
         $result = $result && create_table($table);
-        
+
     /// Define table grade_import_newitem to be created
         $table = new XMLDBTable('grade_import_newitem');
+        if (table_exists($table)) {
+            drop_table($table);
+        }
 
     /// Adding fields to table grade_import_newitem
         $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
@@ -1391,10 +1354,11 @@ function xmldb_main_upgrade($oldversion=0) {
 
     /// Launch create table for grade_import_newitem
         $result = $result && create_table($table);
+
     }
-    
-    
-    return $result; 
+
+
+    return $result;
 }
 
 ?>
index ac2b342797f86c2160ca2334a388fbb9530c79a1..8443bd4c631d1f6235126764966527a9d140d014 100644 (file)
@@ -28,6 +28,10 @@ require_once('grade_object.php');
 /**
  * A calculation string used to compute the value displayed by a grade_item.
  * There can be only one grade_calculation per grade_item (one-to-one).
+ *
+ * Calculation formula may use final grades of other grade items (giXXX
+ * where XXX is the id number of grade item). The result is stored in
+ * finalgrade field. The rawgrade is not used at all.
  */
 class grade_calculation extends grade_object {
     /**
@@ -40,7 +44,7 @@ class grade_calculation extends grade_object {
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields', 'formula', 'useditems', 'grade_item');
+    var $nonfields = array('table', 'nonfields', 'formula', 'grade_item');
 
     /**
      * A reference to the grade_item this calculation belongs to.
@@ -61,26 +65,24 @@ class grade_calculation extends grade_object {
     var $usermodified;
 
     /**
-     * Calculation formula object
-     */
-    var $formula;
-
-    /**
-     * List of other items this calculation depends on
+     * Grade item object
      */
-    var $useditems;
+    var $grade_item;
 
     /**
-     * Grade item object
+     * Math evaluation object
      */
-    var $grade_item;
+    var $formula;
 
     /**
-     * Get associated grade_item object
-     * @return object
+     * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access.
+     * @return object grade_item.
      */
-    function get_grade_item() {
-        return grade_item::fetch('id', $this->itemid);
+    function load_grade_item() {
+        if (empty($this->grade_item) && !empty($this->itemid)) {
+            $this->grade_item = grade_item::fetch('id', $this->itemid);
+        }
+        return $this->grade_item;
     }
 
     /**
@@ -98,62 +100,66 @@ class grade_calculation extends grade_object {
         }
 
         // init grade_item
-        $this->grade_item = $this->get_grade_item();
+        $this->load_grade_item();
 
-        //init used items
-        $this->useditems = $this->dependson();
+        //get used items
+        $useditems = $this->dependson();
 
         // init maths library
         $this->formula = new calc_formula($this->calculation);
 
-        // where to look for final grades
-        $gis = implode(',', array_merge($this->useditems, array($this->itemid)));
+        // where to look for final grades?
+        // this itemid is added so that we use only one query for source and final grades
+        $gis = implode(',', array_merge($useditems, array($this->itemid)));
 
-        $sql = "SELECT f.*
-                  FROM {$CFG->prefix}grade_grades_final f, {$CFG->prefix}grade_items gi
-                 WHERE gi.id = f.itemid AND gi.courseid={$this->grade_item->courseid} AND gi.id IN ($gis)
-              ORDER BY f.userid";
+        $sql = "SELECT g.*
+                  FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items gi
+                 WHERE gi.id = g.itemid AND gi.courseid={$this->grade_item->courseid} AND gi.id IN ($gis)
+              ORDER BY g.userid";
 
         $return = true;
 
+        // group the grades by userid and use formula on the group
         if ($rs = get_recordset_sql($sql)) {
             if ($rs->RecordCount() > 0) {
                 $prevuser = 0;
                 $grades   = array();
                 $final    = null;
-                while ($subfinal = rs_fetch_next_record($rs)) {
-                    if ($subfinal->userid != $prevuser) {
-                        if (!$this->_use_formula($prevuser, $grades, $final)) {
+                while ($used = rs_fetch_next_record($rs)) {
+                    if ($used->userid != $prevuser) {
+                        if (!$this->use_formula($prevuser, $grades, $useditems, $final)) {
                             $return = false;
                         }
-                        $prevuser = $subfinal->userid;
+                        $prevuser = $used->userid;
                         $grades   = array();
                         $final    = null;
                     }
-                    if ($subfinal->itemid == $this->grade_item->id) {
-                        $final = grade_grades_final::fetch('id', $subfinal->id);
+                    if ($used->itemid == $this->grade_item->id) {
+                        $final = new grade_grades($used, false); // fetching from db is not needed
                     }
-                    $grades['gi'.$subfinal->itemid] = $subfinal->gradevalue;
+                    $grades['gi'.$used->itemid] = $used->finalgrade;
                 }
-                if (!$this->_use_formula($prevuser, $grades, $final)) {
+                if (!$this->use_formula($prevuser, $grades, $useditems, $final)) {
                     $return = false;
                 }
             }
         }
 
+        //TODO: we could return array of errors here
         return $return;
     }
 
     /**
      * internal function - does the final grade calculation
      */
-    function _use_formula($userid, $params, $final) {
+    function use_formula($userid, $params, $useditems, $final) {
         if (empty($userid)) {
             return true;
         }
 
-        // add missing final grade values - use 0
-        foreach($this->useditems as $gi) {
+        // add missing final grade values
+        // not graded (null) is counted as 0 - the spreadsheet way
+        foreach($useditems as $gi) {
             if (!array_key_exists('gi'.$gi, $params)) {
                 $params['gi'.$gi] = 0;
             } else {
@@ -164,32 +170,41 @@ class grade_calculation extends grade_object {
         // can not use own final grade during calculation
         unset($params['gi'.$this->grade_item->id]);
 
+
         // do the calculation
         $this->formula->set_params($params);
         $result = $this->formula->evaluate();
 
 
-        // insert final grade if needed
+        // insert final grade - will be needed anyway later
         if (empty($final)) {
-            $final = new grade_grades_final(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
+            $final = new grade_grades(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
             $final->insert();
         }
 
         // store the result
         if ($result === false) {
-            $final->gradevalue = null;
-            $final->update();
+            // error during calculation
+            if (!is_null($final->finalgrade) or !is_null($final->rawgrade)) {
+                $final->finalgrade = null;
+                $final->rawgrade   = null;
+                $final->update();
+            }
             return false;
 
         } else {
             // normalize
             $result = bounded_number($this->grade_item->grademin, $result, $this->grade_item->grademax);
             if ($this->grade_item->gradetype == GRADE_TYPE_SCALE) {
-                $result = round($result+0.00001); // round upwards
+                $result = round($result+0.00001); // round scales upwards
             }
 
-            $final->gradevalue = $result;
-            $final->update();
+            // store only if final grade changed, remove raw grade because we do not need it
+            if ($final->finalgrade != $result or !is_null($final->rawgrade)) {
+                $final->finalgrade = $result;
+                $final->rawgrade   = null;
+                $final->update();
+            }
             return true;
         }
     }
@@ -199,8 +214,11 @@ class grade_calculation extends grade_object {
      * @return array of grade_item ids this one depends on
      */
     function dependson() {
-        preg_match_all('/gi([0-9]+)/i', $this->calculation, $matches);
-        return ($matches[1]);
+        if (preg_match_all('/gi([0-9]+)/i', $this->calculation, $matches)) {
+            return ($matches[1]);
+        } else {
+            return array();
+        }
     }
 
     /**
index 41e491dfaa0837e288a2c28f4c545be53ce143bb..2bb1b96067c4fd9eb3db8d494649c299e67958c6 100644 (file)
@@ -36,7 +36,7 @@ class grade_category extends grade_object {
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields', 'children', 'all_children');
+    var $nonfields = array('table', 'nonfields', 'children', 'all_children', 'grade_item', 'parent_category');
 
     /**
      * The course this category belongs to.
@@ -327,104 +327,115 @@ class grade_category extends grade_object {
     }
 
     /**
-     * Generates and saves raw_grades, based on this category's immediate children, then uses the
-     * associated grade_item to generate matching final grades. These immediate children must first have their own
-     * raw and final grades, which means that ultimately we must get grade_items as children. The category's aggregation
-     * method is used to generate these raw grades, which can then be used by the category's associated grade_item
-     * to apply calculations to and generate final grades.
+     * Generates and saves raw_grades in associated category grade item.
+     * These immediate children must alrady have their own final grades.
+     * The category's aggregation method is used to generate raw grades.
+     *
+     * Please note that category grade is either calculated or aggregated - not both at the same time.
+     *
+     * This method must be used ONLY from grade_item::update_final_grades(),
+     * because the calculation must be done in correct order!
      *
-     * This function must be use ONLY from grade_item::update_final_grade(),
-     * because the calculation must be done in correct order!!
-
      * Steps to follow:
-     *  1. Get final grades from immediate children (if the children are categories, get the final grades from their grade_item)
+     *  1. Get final grades from immediate children
      *  3. Aggregate these grades
-     *  4. Save them under $this->grade_item->grade_grades_raw
+     *  4. Save them in raw grades of associated category grade item
      */
     function generate_grades() {
         global $CFG;
 
-        $grade_item = $this->get_grade_item();
-        if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
-            $grade_item->load_scale();
-        }
+        $this->load_grade_item();
+        $this->grade_item->load_scale();
 
-        $dependson = $grade_item->dependson();
+        // find grde items of immediate children (category or grade items)
+        $dependson = $this->grade_item->dependson();
         $items = array();
 
         foreach($dependson as $dep) {
             $items[$dep] = grade_item::fetch('id', $dep);
         }
 
-        // where to look for final grades
-        $gis = implode(',', array_merge($dependson, array($grade_item->id)));
+        // where to look for final grades - include or grade item too
+        $gis = implode(',', array_merge($dependson, array($this->grade_item->id)));
 
-        $sql = "SELECT f.*
-                  FROM {$CFG->prefix}grade_grades_final f, {$CFG->prefix}grade_items gi
-                 WHERE gi.id = f.itemid AND gi.courseid={$grade_item->courseid} AND gi.id IN ($gis)
-              ORDER BY f.userid";
+        $sql = "SELECT g.*
+                  FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items gi
+                 WHERE gi.id = g.itemid AND gi.courseid={$this->grade_item->courseid} AND gi.id IN ($gis)
+              ORDER BY g.userid";
 
+        // group the results by userid and aggregate the grades in this group
         if ($rs = get_recordset_sql($sql)) {
             if ($rs->RecordCount() > 0) {
                 $prevuser = 0;
                 $grades   = array();
-                while ($subfinal = rs_fetch_next_record($rs)) {
-                    if ($subfinal->userid != $prevuser) {
-                        $this->aggregate_grades($prevuser, $items, $grades, $grade_item, $dependson);
-                        $prevuser = $subfinal->userid;
+                $final    = null;
+                while ($used = rs_fetch_next_record($rs)) {
+                    if ($used->userid != $prevuser) {
+                        $this->aggregate_grades($prevuser, $items, $grades, $dependson, $final);
+                        $prevuser = $used->userid;
                         $grades   = array();
+                        $final    = null;
+                    }
+                    if ($used->itemid == $this->grade_item->id) {
+                        $final = new grade_grades($used, false);
                     }
-                    $grades[$subfinal->itemid] = $subfinal->gradevalue;
+                    $grades[$used->itemid] = $used->finalgrade;
                 }
-                $this->aggregate_grades($prevuser, $items, $grades, $grade_item, $dependson);
+                $this->aggregate_grades($prevuser, $items, $grades, $dependson, $final);
             }
         }
 
-        //TODO: set grade to null for raw grades that do not have corresponding final grade
-        //      using left join
-
         return true;
     }
 
     /**
-     * internal function for vategory grades aggregation
+     * internal function for category grades aggregation
      */
-    function aggregate_grades($userid, $items, $grades, &$grade_item, $dependson) {
+    function aggregate_grades($userid, $items, $grades, $dependson, $final) {
         if (empty($userid)) {
+            //ignore first run
             return;
         }
 
-        // remove the final item we used to get all existing final grades of this category
-        unset($grades[$grade_item->id]);
+        // no circular references allowed
+        unset($grades[$this->grade_item->id]);
 
-        if (empty($grades) or empty($items) or ($grade_item->gradetype != GRADE_TYPE_VALUE and $grade_item->gradetype != GRADE_TYPE_SCALE)) {
-            // no grading
-            if ($raw = grade_grades_raw::fetch('itemid', $grade_item->id, 'userid', $userid)) {
-                $raw->gradevalue = null;
-                $raw->update();
-            }
+        // insert final grade - it will needed anyway later
+        if (empty($final)) {
+            $final = new grade_grades(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
+            $final->insert();
+        }
+
+        // if no grades calculation possible or grading not allowed clear both final and raw
+        if (empty($grades) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
+            $final->finalgrade = null;
+            $final->rawgrade   = null;
+            $final->update();
             return;
         }
 
         // normalize the grades first - all will have value 0...1
+        // ungraded items are not used in aggreagation
         foreach ($grades as $k=>$v) {
             if (is_null($v)) {
                 // null means no grade
                 unset($grades[$k]);
                 continue;
             }
-            $grades[$k] = standardise_score($v, $items[$k]->grademin, $items[$k]->grademax, 0, 1);
+            $grades[$k] = grade_grades::standardise_score($v, $items[$k]->grademin, $items[$k]->grademax, 0, 1);
         }
 
-        //sort and limit
+        //limit and sort
         $this->apply_limit_rules($grades);
         sort($grades, SORT_NUMERIC);
 
+        // let's see we have still enough grades to do any statisctics
         if (count($grades) == 0) {
-            // no grading yet
-            if ($raw = grade_grades_raw::fetch('itemid', $grade_item->id, 'userid', $userid)) {
-                $raw->gradevalue = null;
-                $raw->update();
+            // not enough attempts yet
+            if (!is_null($final->finalgrade) or !is_null($final->rawgrade)) {
+                $final->finalgrade = null;
+                $final->rawgrade   = null;
+                $final->update();
             }
             return;
         }
@@ -435,23 +446,24 @@ class grade_category extends grade_object {
                 $halfpoint = intval($num / 2);
 
                 if($num % 2 == 0) {
-                    $gradevalue = ($grades[ceil($halfpoint)] + $grades[floor($halfpoint)]) / 2;
+                    $rawgrade = ($grades[ceil($halfpoint)] + $grades[floor($halfpoint)]) / 2;
                 } else {
-                    $gradevalue = $grades[$halfpoint];
+                    $rawgrade = $grades[$halfpoint];
                 }
                 break;
+
             case GRADE_AGGREGATE_MIN:
-                $gradevalue = reset($grades);
+                $rawgrade = reset($grades);
                 break;
 
             case GRADE_AGGREGATE_MAX:
-                $gradevalue = array_pop($grades);
+                $rawgrade = array_pop($grades);
                 break;
 
             case GRADE_AGGREGATE_MEAN_ALL:    // Arithmetic average of all grade items including even NULLs; NULL grade caunted as minimum
                 $num = count($dependson);     // you can calculate sum from this one if you multiply it with count($this->dependson() ;-)
                 $sum = array_sum($grades);
-                $gradevalue = $sum / $num;
+                $rawgrade = $sum / $num;
                 break;
 
             case GRADE_AGGREGATE_MODE:       // the most common value, the highest one if multimode
@@ -460,35 +472,28 @@ class grade_category extends grade_object {
                 $top = reset($freq);               // highest frequency count
                 $modes = array_keys($freq, $top);  // search for all modes (have the same highest count)
                 rsort($modes, SORT_NUMERIC);       // get highes mode
-                $gradevalue = reset($modes);
+                $rawgrade = reset($modes);
 
             case GRADE_AGGREGATE_MEAN_GRADED: // Arithmetic average of all final grades, unfinished are not calculated
+            default:
                 $num = count($grades);
                 $sum = array_sum($grades);
-                $gradevalue = $sum / $num;
-            default:
+                $rawgrade = $sum / $num;
                 break;
         }
 
-        $raw = new grade_grades_raw(array('itemid'=>$grade_item->id, 'userid'=>$userid));
-        $raw->gradevalue = $gradevalue;
-        $raw->gradetype  = $grade_item->gradetype;
-        $raw->scaleid    = $grade_item->scaleid;
-        $raw->grademin   = 0;
-        $raw->grademax   = 1;
-
-        // recalculate the gradevalue bvack to requested range
-        $raw->gradevalue = $grade_item->adjust_grade($raw);
+        // recalculate the rawgrade back to requested range
+        $rawgrade = $this->grade_item->adjust_grade($rawgrade, 0, 1);
 
-        $raw->grademin   = $grade_item->grademin;
-        $raw->grademax   = $grade_item->grademax;
-
-        if ($raw->id) {
-            $raw->update();
-        } else {
-            $raw->insert();
-        }
+        // prepare update of new raw grade
+        $final->rawgrade    = $rawgrade;
+        $final->finalgrade  = null;
+        $final->rawgrademin = $this->grade_item->grademin;
+        $final->rawgrademax = $this->grade_item->grademax;
+        $final->rawscaleid  = $this->grade_item->scaleid;
 
+        // TODO - add some checks to prevent updates when not needed
+        $final->update();
     }
 
     /**
@@ -676,7 +681,9 @@ class grade_category extends grade_object {
      * @return object Grade_item
      */
     function load_grade_item() {
-        $this->grade_item = $this->get_grade_item();
+        if (empty($this->grade_item)) {
+            $this->grade_item = $this->get_grade_item();
+        }
         return $this->grade_item;
     }
 
@@ -703,7 +710,8 @@ class grade_category extends grade_object {
 
         } else {
             debugging("Found more than one grade_item attached to category id:".$this->id);
-            return false;
+            // return first one
+            $grade_item = reset($grade_items);
         }
 
         return $grade_item;
similarity index 51%
rename from lib/grade/grade_grades_raw.php
rename to lib/grade/grade_grades.php
index 63fdee81679b2738b31ca03226aabb131954d4f2..fd78ce717fb1b564bde9935f891c42d143b77f5a 100644 (file)
 
 require_once('grade_object.php');
 
-class grade_grades_raw extends grade_object {
+class grade_grades extends grade_object {
 
     /**
      * The DB table.
      * @var string $table
      */
-    var $table = 'grade_grades_raw';
+    var $table = 'grade_grades';
 
     /**
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields', 'scale', 'grade_grades_text', 'grade_item', 'feedback', 'feedbackformat', 'information', 'informationformat');
+    var $nonfields = array('table', 'nonfields', 'grade_grades_text', 'grade_item');
 
     /**
-     * The id of the grade_item this raw grade belongs to.
+     * The id of the grade_item this grade belongs to.
      * @var int $itemid
      */
     var $itemid;
@@ -52,40 +52,34 @@ class grade_grades_raw extends grade_object {
     var $grade_item;
 
     /**
-     * The id of the user this raw grade belongs to.
+     * The id of the user this grade belongs to.
      * @var int $userid
      */
     var $userid;
 
     /**
      * The grade value of this raw grade, if such was provided by the module.
-     * @var float $gradevalue
+     * @var float $rawgrade
      */
-    var $gradevalue;
+    var $rawgrade;
 
     /**
      * The maximum allowable grade when this grade was created.
-     * @var float $grademax
+     * @var float $rawgrademax
      */
-    var $grademax;
+    var $rawgrademax;
 
     /**
      * The minimum allowable grade when this grade was created.
-     * @var float $grademin
+     * @var float $rawgrademin
      */
-    var $grademin;
+    var $rawgrademin;
 
     /**
      * id of the scale, if this grade is based on a scale.
-     * @var int $scaleid
+     * @var int $rawscaleid
      */
-    var $scaleid;
-
-    /**
-     * A grade_scale object (referenced by $this->scaleid).
-     * @var object $scale
-     */
-    var $scale;
+    var $rawscaleid;
 
     /**
      * The userid of the person who last modified this grade.
@@ -96,71 +90,58 @@ class grade_grades_raw extends grade_object {
     /**
      * Additional textual information about this grade. It can be automatically generated
      * from the module or entered manually by the teacher. This is kept in its own table
-     * for efficiency reasons, so it is encapsulated in its own object, and included in this raw grade object.
+     * for efficiency reasons, so it is encapsulated in its own object, and included in this grade object.
      * @var object $grade_grades_text
      */
     var $grade_grades_text;
 
+    /**
+     * The final value of this grade.
+     * @var float $finalgrade
+     */
+    var $finalgrade;
+
+    /**
+     * 0 if visible, 1 always hidden or date not visible until
+     * @var float $hidden
+     */
+    var $hidden;
+
+    /**
+     * 0 not locked, date when the item was locked
+     * @var float locked
+     */
+    var $locked;
+
+    /**
+     * 0 no automatic locking, date when to lock the grade automatically
+     * @var float $locktime
+     */
+    var $locktime;
+
+    /**
+     * Exported flag
+     * @var boolean $exported
+     */
+    var $exported;
+
     /**
      * Constructor. Extends the basic functionality defined in grade_object.
      * @param array $params Can also be a standard object.
      * @param boolean $fetch Wether or not to fetch the corresponding row from the DB.
      */
-    function grade_grades_raw($params=NULL, $fetch=true) {
+    function grade_grades($params=NULL, $fetch=true) {
         $this->grade_object($params, $fetch);
     }
 
-    /**
-     * Instantiates a grade_scale object whose data is retrieved from the DB,
-     * if this raw grade's scaleid variable is set.
-     * @return object grade_scale
-     */
-    function load_scale() {
-        if (!empty($this->scaleid)) {
-            $this->scale = grade_scale::fetch('id', $this->scaleid);
-            $this->scale->load_items();
-        }
-        return $this->scale;
-    }
-
     /**
      * Loads the grade_grades_text object linked to this grade (through the intersection of itemid and userid), and
      * saves it as a class variable for this final object.
-     * If not already set initializes $this->feedback, $this->feedbackformat, $this->inforamtion, $this->informationformat.
      * @return object
      */
     function load_text() {
-        if (empty($this->grade_grades_text)) {
-            if ($this->grade_grades_text = grade_grades_text::fetch('itemid', $this->itemid, 'userid', $this->userid)) {
-                // set defaul $this properties if not already there
-                if (!array_key_exists('feedback', $this)) {
-                    $this->feedback = $this->grade_grades_text->feedback;
-                }
-                if (!array_key_exists('feedbackformat', $this)) {
-                    $this->feedbackformat = $this->grade_grades_text->feedbackformat;
-                }
-                if (!array_key_exists('information', $this)) {
-                    $this->information = $this->grade_grades_text->information;
-                }
-                if (!array_key_exists('informationformat', $this)) {
-                    $this->informationformat = $this->grade_grades_text->informationformat;
-                }
-
-            } else {
-                // set defaul $this properties if not already there
-                if (!array_key_exists('feedback', $this)) {
-                    $this->feedback = null;
-                }
-                if (!array_key_exists('feedbackformat', $this)) {
-                    $this->feedbackformat = FORMAT_MOODLE;
-                }
-                if (!array_key_exists('information', $this)) {
-                    $this->information = null;
-                }
-                if (!array_key_exists('informationformat', $this)) {
-                    $this->informationformat = FORMAT_MOODLE;
-                }
-            }
+        if (empty($this->grade_grades_text->id)) {
+            $this->grade_grades_text = grade_grades_text::fetch('itemid', $this->itemid, 'userid', $this->userid);
         }
 
         return $this->grade_grades_text;
@@ -178,7 +159,7 @@ class grade_grades_raw extends grade_object {
     }
 
     /**
-     * Finds and returns a grade_grades_raw object based on 1-3 field values.
+     * Finds and returns a grade_grades object based on 1-3 field values.
      * @static
      * @param string $field1
      * @param string $value1
@@ -190,15 +171,15 @@ class grade_grades_raw extends grade_object {
      * @return object grade_category object or false if none found.
      */
     function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
-        if ($object = get_record('grade_grades_raw', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
-            if (isset($this) && get_class($this) == 'grade_grades_raw') {
+        if ($object = get_record('grade_grades', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
+            if (isset($this) && get_class($this) == 'grade_grades') {
                 foreach ($object as $param => $value) {
                     $this->$param = $value;
                 }
 
                 return $this;
             } else {
-                $object = new grade_grades_raw($object);
+                $object = new grade_grades($object);
                 return $object;
             }
         } else {
@@ -216,7 +197,7 @@ class grade_grades_raw extends grade_object {
     function update_information($information, $informationformat) {
         $this->load_text();
 
-        if (empty($this->grade_grades_text)) {
+        if (empty($this->grade_grades_text->id)) {
             $this->grade_grades_text = new grade_grades_text();
 
             $this->grade_grades_text->itemid            = $this->itemid;
@@ -249,7 +230,7 @@ class grade_grades_raw extends grade_object {
     function update_feedback($feedback, $feedbackformat) {
         $this->load_text();
 
-        if (empty($this->grade_grades_text)) {
+        if (empty($this->grade_grades_text->id)) {
             $this->grade_grades_text = new grade_grades_text();
 
             $this->grade_grades_text->itemid         = $this->itemid;
@@ -273,108 +254,23 @@ class grade_grades_raw extends grade_object {
     }
 
     /**
-     * In addition to the normal updating set up in grade_object, this object also records
-     * its pre-update value and its new value in the grade_history table. The grade_item
-     * object is also flagged for update.
+     * Given a float value situated between a source minimum and a source maximum, converts it to the
+     * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
+     * for the formula :-)
      *
-     * @param float $newgrade The new gradevalue of this object, false if no change
-     * @param string $howmodified What caused the modification? manual/module/import/cron...
-     * @param string $note A note attached to this modification.
-     * @return boolean Success or Failure.
-     */
-    function update($newgrade, $howmodified='manual', $note=NULL) {
-        global $USER;
-
-        if (!empty($this->scale->id)) {
-            $this->scaleid = $this->scale->id;
-            $this->grademin = 0;
-            $this->scale->load_items();
-            $this->grademax = count($this->scale->scale_items) - 1;
-        }
-
-        $trackhistory = false;
-
-        if ($newgrade != $this->gradevalue) {
-            $trackhistory = true;
-            $oldgrade = $this->gradevalue;
-            $this->gradevalue = $newgrade;
-        }
-
-        $result = parent::update();
-
-        // Update grade_grades_text if specified
-        if ($result) {
-            // we do this to prevent some db queries in load_text()
-            if (array_key_exists('feedback', $this) or array_key_exists('feedbackformat', $this)
-             or array_key_exists('information', $this) or array_key_exists('informationformat', $this)) {
-                $this->load_text();
-                $result = $this->update_feedback($this->feedback, $this->feedbackformat);
-                $result = $result && $this->update_information($this->information, $this->informationformat);
-            }
-        }
-
-        if ($result && $trackhistory) {
-            // TODO Handle history recording error, such as displaying a notice, but still return true
-            grade_history::insert_change($this, $oldgrade, $howmodified, $note);
-
-            // Notify parent grade_item of need to update
-            $this->load_grade_item();
-            $result = $this->grade_item->flag_for_update();
-        }
-
-        if ($result) {
-            return true;
-        } else {
-            debugging("Could not update a raw grade in the database.");
-            return false;
-        }
-    }
-
-    /**
-     * In addition to perform parent::insert(), this infers the grademax from the scale if such is given, and
-     * sets grademin to 0 if scale is given.
-     * @return int ID of the new grades_grade_raw 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) - 1;
-            $this->grademin = 0;
-        }
-
-        $result = parent::insert();
-
-        // Notify parent grade_item of need to update
-        $this->load_grade_item();
-        $result = $result && $this->grade_item->flag_for_update();
-
-        // Update grade_grades_text if specified
-        if ($result) {
-            // we do this to prevent some db queries in load_text()
-            if (array_key_exists('feedback', $this) or array_key_exists('feedbackformat', $this)
-             or array_key_exists('information', $this) or array_key_exists('informationformat', $this)) {
-                $this->load_text();
-                $result = $this->update_feedback($this->feedback, $this->feedbackformat);
-                $result = $result && $this->update_information($this->information, $this->informationformat);
-            }
-        }
-
-        return $result && $this->grade_item->flag_for_update();
-    }
-
-    /**
-     * If parent::delete() is successful, send flag_for_update message to parent grade_item.
-     * @return boolean Success or failure.
+     * @static
+     * @param float $rawgrade
+     * @param float $source_min
+     * @param float $source_max
+     * @param float $target_min
+     * @param float $target_max
+     * @return float Converted value
      */
-    function delete() {
-        $result = parent::delete();
-        if ($result) {
-            $this->load_grade_item();
-            return $this->grade_item->flag_for_update();
-        }
-        return $result;
+    function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) {
+        $factor = ($rawgrade - $source_min) / ($source_max - $source_min);
+        $diff = $target_max - $target_min;
+        $standardised_value = $factor * $diff + $target_min;
+        return $standardised_value;
     }
 }
 
diff --git a/lib/grade/grade_grades_final.php b/lib/grade/grade_grades_final.php
deleted file mode 100644 (file)
index cfe6efa..0000000
+++ /dev/null
@@ -1,172 +0,0 @@
-<?php // $Id$
-
-///////////////////////////////////////////////////////////////////////////
-//                                                                       //
-// NOTICE OF COPYRIGHT                                                   //
-//                                                                       //
-// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
-//          http://moodle.com                                            //
-//                                                                       //
-// Copyright (C) 2001-2003  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  //
-// the Free Software Foundation; either version 2 of the License, or     //
-// (at your option) any later version.                                   //
-//                                                                       //
-// This program is distributed in the hope that it will be useful,       //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
-// GNU General Public License for more details:                          //
-//                                                                       //
-//          http://www.gnu.org/copyleft/gpl.html                         //
-//                                                                       //
-///////////////////////////////////////////////////////////////////////////
-
-require_once('grade_object.php');
-
-class grade_grades_final extends grade_object {
-    /**
-     * The DB table.
-     * @var string $table
-     */
-    var $table = 'grade_grades_final';
-    
-    /**
-     * Array of class variables that are not part of the DB table fields
-     * @var array $nonfields
-     */
-    var $nonfields = array('nonfields', 'table', 'grade_grades_text', 'grade_item');
-    
-    /**
-     * The id of the grade_item this final grade belongs to.
-     * @var int $itemid
-     */
-    var $itemid;
-
-    /**
-     * The grade_item object referenced by $this->itemid.
-     * @var object $grade_item
-     */
-    var $grade_item;
-    
-    /**
-     * The id of the user this final grade belongs to.
-     * @var int $userid
-     */
-    var $userid;
-    
-    /**
-     * The value of the grade.
-     * @var float $gradevalue
-     */
-    var $gradevalue;
-    
-    /**
-     * Date until which to hide this grade_final. If null, 0 or false, grade_item is not hidden. Hiding prevents viewing.
-     * @var int $hidden
-     */
-    var $hidden;
-    
-    /**
-     * Date until which to lock this grade_final. If null, 0 or false, grade_item is not locked. Locking prevents updating.
-     * @var int $locked
-     */
-    var $locked = false;
-    
-    /**
-     * 0 if not exported, > 1 is the last exported date.
-     * @var int $exported
-     */
-    var $exported;
-
-    /**
-     * The userid of the person who last modified this grade.
-     * @var int $usermodified
-     */
-    var $usermodified;
-
-    /**
-     * The grade_grades_text object linked to this grade through itemid and userid.
-     * @var object $grade_grades_text
-     */
-    var $grade_grades_text;
-
-    /**
-     * Constructor. Extends the basic functionality defined in grade_object.
-     * @param array $params Can also be a standard object.
-     * @param boolean $fetch Wether or not to fetch the corresponding row from the DB.
-     */
-    function grade_grades_final($params=NULL, $fetch=true) {
-        $this->grade_object($params, $fetch);
-    }
-
-    /**
-     * Loads the grade_grades_text object linked to this grade (through the intersection of itemid and userid), and
-     * saves it as a class variable for this final object.
-     * @return object
-     */
-    function load_text() {
-        if (empty($this->grade_grades_text)) {
-            return $this->grade_grades_text = grade_grades_text::fetch('itemid', $this->itemid, 'userid', $this->userid);
-        } 
-    }
-
-    /**
-     * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access.
-     * @return object grade_item.
-     */
-    function load_grade_item() {
-        if (empty($this->grade_item) && !empty($this->itemid)) {
-            $this->grade_item = grade_item::fetch('id', $this->itemid);
-        }
-        return $this->grade_item;
-    }
-
-    /**
-     * Finds and returns a grade_grades_final object based on 1-3 field values.
-     *
-     * @param string $field1
-     * @param string $value1
-     * @param string $field2
-     * @param string $value2
-     * @param string $field3
-     * @param string $value3
-     * @param string $fields
-     * @return object grade_category object or false if none found.
-     */
-    function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { 
-        if ($object = get_record('grade_grades_final', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
-            if (isset($this) && get_class($this) == 'grade_grades_final') {
-                foreach ($object as $param => $value) {
-                    $this->$param = $value;
-                }
-                return $this;
-            } else {
-                $object = new grade_grades_final($object);
-                return $object;
-            }
-        } else {
-            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) {
-            // Retrieve id number from DB
-            $record = get_record('grade_grades_final', 'userid', $this->userid, 'itemid', $this->itemid); 
-            $this->id = $record->id;
-            return $this->update();
-        } else {
-            return parent::insert();
-        }
-    }
-}
-
-?>
index 0f215264e193845b686084a56a5112798d5fba14..ca88398b7f39b62d6bea71b9be4240e05e315553 100644 (file)
@@ -40,8 +40,8 @@ class grade_grades_text extends grade_object {
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields');
-    
+    var $nonfields = array('table', 'nonfields', 'grade_item');
+
     /**
      * The grade_item.id this text refers to.
      * @var int $itemid
@@ -89,7 +89,7 @@ class grade_grades_text extends grade_object {
      * @var int $usermodified
      */
     var $usermodified;
-    
+
     /**
      * Finds and returns a grade_text object based on 1-3 field values.
      *
@@ -103,7 +103,7 @@ class grade_grades_text extends grade_object {
      * @param string $fields
      * @return object grade_text object or false if none found.
      */
-    function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { 
+    function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
         if ($grade_text = get_record('grade_grades_text', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
             if (isset($this) && get_class($this) == 'grade_grades_text') {
                 foreach ($grade_text as $param => $value) {
index 56fb5f669bea1429928525c7b404b3d7068db387..bb5400da1b20ed12004e6adb26cdd93f659336d1 100644 (file)
@@ -35,25 +35,25 @@ class grade_history extends grade_object {
      * @var string $table
      */
     var $table = 'grade_history';
-    
+
     /**
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
     var $nonfields = array('table', 'nonfields');
-  
+
     /**
      * The grade_item whose raw grade is being changed.
      * @var int $itemid
      */
     var $itemid;
-    
+
     /**
      * The user whose raw grade is being changed.
      * @var int $userid
      */
     var $userid;
-    
+
     /**
      * The value of the grade before the change.
      * @var float $oldgrade
@@ -71,13 +71,13 @@ class grade_history extends grade_object {
      * @var string $note
      */
     var $note;
-    
+
     /**
      * How the grade was modified ('manual', 'module', 'import' etc...).
      * @var string $howmodified
      */
     var $howmodified;
-    
+
     /**
      * Finds and returns a grade_history object based on 1-3 field values.
      *
@@ -90,7 +90,7 @@ class grade_history extends grade_object {
      * @param string $fields
      * @return object grade_history object or false if none found.
      */
-    function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { 
+    function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") {
         if ($grade_history = get_record('grade_history', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) {
             if (isset($this) && get_class($this) == 'grade_history') {
                 foreach ($grade_history as $param => $value) {
@@ -104,10 +104,10 @@ class grade_history extends grade_object {
         } else {
             return false;
         }
-    } 
+    }
 
     /**
-     * Given a grade_grades_raw object and some other parameters, records the 
+     * Given a info about changed raw grade and some other parameters, records the
      * change of grade value for this object, and associated data.
      * @static
      * @param object $grade_raw
@@ -116,19 +116,19 @@ class grade_history extends grade_object {
      * @param string $howmodified
      * @return boolean Success or Failure
      */
-    function insert_change($grade_raw, $oldgrade, $howmodified='manual', $note=NULL) {
+    function insert_change($userid, $itemid, $newgrade, $oldgrade, $howmodified='manual', $note=NULL) {
         global $USER;
         $history = new grade_history();
-        $history->itemid = $grade_raw->itemid;
-        $history->userid = $grade_raw->userid;
-        $history->oldgrade = $oldgrade;
-        $history->newgrade = $grade_raw->gradevalue;
-        $history->note = $note;
-        $history->howmodified = $howmodified;
+        $history->itemid       = $itemid;
+        $history->userid       = $userid;
+        $history->oldgrade     = $oldgrade;
+        $history->newgrade     = $newgrade;
+        $history->note         = $note;
+        $history->howmodified  = $howmodified;
         $history->timemodified = time();
         $history->usermodified = $USER->id;
 
         return $history->insert();
-    } 
+    }
 }
 ?>
index 46c74194c15b4b5b4ec6ba1cded495898316da45..dcdef93f009e77985c6a68e2463ebaadccead636 100644 (file)
@@ -24,7 +24,7 @@
 ///////////////////////////////////////////////////////////////////////////
 
 require_once('grade_object.php');
-global $db;
+
 /**
  * Class representing a grade item. It is responsible for handling its DB representation,
  * modifying and returning its metadata.
@@ -40,7 +40,7 @@ class grade_item extends grade_object {
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields', 'calculation', 'grade_grades_raw', 'grade_grades_final', 'scale', 'category', 'outcome');
+    var $nonfields = array('table', 'nonfields', 'calculation', 'scale', 'category', 'outcome');
 
     /**
      * The course this grade_item belongs to.
@@ -204,18 +204,6 @@ class grade_item extends grade_object {
      */
     var $calculation;
 
-    /**
-     * Array of grade_grades_raw objects linked to this grade_item. They are indexed by userid.
-     * @var array $grade_grades_raw
-     */
-    var $grade_grades_raw = array();
-
-    /**
-     * Array of grade_grades_final objects linked to this grade_item. They are indexed by userid.
-     * @var array $grade_grades_final
-     */
-    var $grade_grades_final = array();
-
     /**
      * Constructor. Extends the basic functionality defined in grade_object.
      * @param array $params Can also be a standard object.
@@ -439,6 +427,8 @@ class grade_item extends grade_object {
      * @return boolean Locked state
      */
     function is_locked($userid=NULL) {
+        // TODO: rewrite the item check
+
         if ($this->locked || empty($userid)) {
             return $this->locked; // This could be true or false (false only if no $userid given)
         } else {
@@ -454,33 +444,9 @@ class grade_item extends grade_object {
      * @return int Number of final grades changed, or false if error occurred during update.
      */
     function toggle_locking($update_final=false, $new_state=NULL) {
-        $this->locked = !$this->locked;
-
-        if (!empty($new_state)) {
-            $this->locked = $new_state;
-        }
-
-        if (!$this->update()) {
-            debugging("Could not update this grade_item's locked state in the database.");
-            return false;
-        }
-
-        $count = 0;
-
-        if ($update_final) {
-            $this->load_final();
-            foreach ($this->grade_grades_final as $id => $final) {
-                $final->locked = $this->locked;
-                if (!$final->update()) {
-                    debugging("Could not update this grade_item's final grade's locked state in the database.");
-                    return false;
-                }
-                $count++;
-            }
-            $this->load_final();
-        }
+        // TODO: implement new locking
 
-        return $count;
+        return 0;
     }
 
     /**
@@ -490,47 +456,22 @@ class grade_item extends grade_object {
      * @return int Number of final grades changed, or false if error occurred during update.
      */
     function toggle_hiding($update_final=false, $new_state=NULL) {
-        $this->hidden = !$this->hidden;
-
-        if (!empty($new_state)) {
-            $this->hidden = $new_state;
-        }
-
-        if (!$this->update()) {
-            debugging("Could not update this grade_item's hidden state in the database.");
-            return false;
-        }
+        //TODO: implement new hiding
 
-        $count = 0;
-
-        if ($update_final) {
-            $this->load_final();
-            foreach ($this->grade_grades_final as $id => $final) {
-                $final->hidden = $this->hidden;
-                if (!$final->update()) {
-                    debugging("Could not update this grade_item's final grade's hidden state in the database.");
-                    return false;
-                }
-                $count++;
-            }
-            $this->load_final();
-        }
-
-        return $count;
+        return 0;
     }
 
 
     /**
-     * Performs the necessary calculations on the grades_final referenced by this grade_item,
-     * and stores the results in grade_grades_final. Also resets the needsupdate flag
-     * once successfully performed.
+     * Performs the necessary calculations on the grades_final referenced by this grade_item.
+     * Also resets the needsupdate flag once successfully performed.
      *
      * This function must be use ONLY from lib/gradeslib.php/grade_update_final_grades(),
      * because the calculation must be done in correct order!!
      *
      * @return boolean true if ok, array of errors otherwise
      */
-    function update_final_grade() {
+    function update_final_grades() {
         global $CFG;
 
         $errors = array();
@@ -552,42 +493,37 @@ class grade_item extends grade_object {
             }
         }
 
-        $sql = "SELECT r.*, f.userid AS fuserid, f.id AS fid, f.itemid AS fitemid, f.gradevalue AS fgradevalue
-                  FROM {$CFG->prefix}grade_grades_raw r
-                       LEFT JOIN {$CFG->prefix}grade_grades_final f ON f.itemid=r.itemid AND f.userid=r.userid
-                 WHERE r.itemid={$this->id}";
-
-        if ($rs = get_recordset_sql($sql)) {
+        // TODO: add locking support
+        if ($rs = get_recordset('grade_grades', 'itemid', $this->id)) {
             if ($rs->RecordCount() > 0) {
                 while ($grade = rs_fetch_next_record($rs)) {
-                    if (!empty($errors) or is_null($grade->gradevalue)) {
+                    if (!empty($errors) or is_null($grade->rawgrade)) {
                         // unset existing final grade when no raw present or error
-                        $final = grade_grades_final::fetch('id', $grade->fid);
-                        $final->gradevalue = NULL;
-                        $final->update();
-                        continue;
-                    }
-
-                    $finalvalue = $this->adjust_grade($grade);
-
-                    if (is_null($grade->fid)) {
-                        // final grade does not exist yet
-                        $final = new grade_grades_final(array('itemid'=>$grade->itemid, 'userid'=>$grade->userid, 'gradevalue'=>$finalvalue, false));
-                        $final->insert();
-
-                    } else if ($finalvalue != $grade->fgradevalue) {
-                        // let's update the final grade
-                        $final = grade_grades_final::fetch('id', $grade->fid);
-                        $final->gradevalue = $finalvalue;
-                        $final->update();
+                        if (!is_null($grade->finalgrade)) {
+                            $g = new object();
+                            $g->id         = $grade->id;
+                            $g->finalgrade = null;
+                            if (!update_record('grade_grades', $g)) {
+                                $errors[] = "Could not remove final grade for grade item:".$this->id;
+                            }
+                        }
+
+                    } else {
+                        $finalgrade = $this->adjust_grade($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
+
+                        if ($finalgrade != $grade->finalgrade) {
+                            $g = new object();
+                            $g->id         = $grade->id;
+                            $g->finalgrade = $finalgrade;
+                            if (!update_record('grade_grades', $g)) {
+                                $errors[] = "Could not update final grade for grade item:".$this->id;
+                            }
+                        }
                     }
                 }
             }
         }
 
-        //TODO: set grade to null for final grades that do not have corresponding raw grade
-        //      using left join SQL update only
-
         if (!empty($errors)) {
             $this->flag_for_update();
             return $errors;
@@ -602,11 +538,13 @@ class grade_item extends grade_object {
     /**
      * Given a float grade value or integer grade scale, applies a number of adjustment based on
      * grade_item variables and returns the result.
-     * @param object $rawgrade The raw grade.
+     * @param object $rawgrade The raw grade value.
      * @return mixed
      */
-    function adjust_grade($rawgrade) {
-        $gradevalue = $rawgrade->gradevalue;
+    function adjust_grade($rawgrade, $rawmin, $rawmax) {
+        if (is_null($rawgrade)) {
+            return null;
+        }
 
         if ($this->gradetype == GRADE_TYPE_VALUE) { // Dealing with numerical grade
 
@@ -620,21 +558,21 @@ class grade_item extends grade_object {
 
             // Standardise score to the new grade range
             // NOTE: this is not compatible with current assignment grading
-            if ($rawgrade->grademin != $this->grademin or $rawgrade->grademax != $this->grademax) { 
-                $gradevalue = standardise_score($gradevalue, $rawgrade->grademin, $rawgrade->grademax, $this->grademin, $this->grademax);
+            if ($rawmin != $this->grademin or $rawmax != $this->grademax) {
+                $rawgrade = grade_grades::standardise_score($rawgrade, $rawmin, $rawmax, $this->grademin, $this->grademax);
             }
 
             // Apply other grade_item factors
-            $gradevalue *= $this->multfactor;
-            $gradevalue += $this->plusfactor;
+            $rawgrade *= $this->multfactor;
+            $rawgrade += $this->plusfactor;
 
-            return bounded_number($this->grademin, $gradevalue, $this->grademax);
+            return bounded_number($this->grademin, $rawgrade, $this->grademax);
 
         } else if($this->gradetype == GRADE_TYPE_SCALE) { // Dealing with a scale value
             if (empty($this->scale)) {
                 $this->load_scale();
             }
-            
+
             if ($this->grademax < 0) {
                 return null; // scale not present - no grade
             }
@@ -645,11 +583,11 @@ class grade_item extends grade_object {
 
             // Convert scale if needed
             // NOTE: this is not compatible with current assignment grading
-            if ($rawgrade->grademax != $this->grademax and $rawgrade->grademax > 0) {
-                $gradevalue = standardise_score($gradevalue, $rawgrade->grademin, $rawgrade->grademax, $this->grademin, $this->grademax);
+            if ($rawmin != $this->grademin or $rawmax != $this->grademax) {
+                $rawgrade = grade_grades::standardise_score($rawgrade, $rawmin, $rawmax, $this->grademin, $this->grademax);
             }
 
-            return (int)bounded_number(0, round($gradevalue+0.00001), $this->grademax);
+            return (int)bounded_number(0, round($rawgrade+0.00001), $this->grademax);
 
 
         } else if ($this->gradetype == GRADE_TYPE_TEXT or $this->gradetype == GRADE_TYPE_NONE) { // no value
@@ -667,7 +605,7 @@ class grade_item extends grade_object {
     /**
      * Sets this grade_item's needsupdate to true. Also looks at parent category, if any, and calls
      * its flag_for_update() method.
-     * This is triggered whenever any change in any grade_raw may cause grade_finals
+     * This is triggered whenever any change in any raw grade may cause grade_finals
      * for this grade_item to require an update. The flag needs to be propagated up all
      * levels until it reaches the top category. This is then used to determine whether or not
      * to regenerate the raw and final grades for each category grade_item.
@@ -703,6 +641,10 @@ class grade_item extends grade_object {
      * @return object grade_scale
      */
     function load_scale() {
+        if ($this->gradetype != GRADE_TYPE_SCALE) {
+            $this->scaleid = null;
+        }
+
         if (!empty($this->scaleid)) {
             $this->scale = grade_scale::fetch('id', $this->scaleid);
             $this->scale->load_items();
@@ -727,45 +669,6 @@ class grade_item extends grade_object {
         return $this->outcome;
     }
 
-    /**
-     * Loads all the grade_grades_raw objects for this grade_item from the DB into grade_item::$grade_grades_raw array.
-     * @return array grade_grades_raw objects
-     */
-    function load_raw() {
-        $grade_raw_array = get_records('grade_grades_raw', 'itemid', $this->id);
-
-        if (empty($grade_raw_array)) {
-            return null;
-        }
-
-        foreach ($grade_raw_array as $r) {
-            $this->grade_grades_raw[$r->userid] = new grade_grades_raw($r);
-        }
-        return $this->grade_grades_raw;
-    }
-
-    /**
-     * Loads all the grade_grades_final objects for this grade_item from the DB into grade_item::$grade_grades_final array.
-     * @return array grade_grades_final objects
-     */
-    function load_final() {
-        global $CFG;
-
-        $this->grade_grades_final = array();
-
-        $grade_final_array = get_records('grade_grades_final', 'itemid', $this->id);
-
-        if (empty($grade_final_array)) {
-            return array();
-        }
-
-        foreach ($grade_final_array as $f) {
-            $this->grade_grades_final[$f->userid] = new grade_grades_final($f);
-        }
-
-        return fullclone($this->grade_grades_final);
-    }
-
     /**
     * Returns the grade_category object this grade_item belongs to (if any).
     * This category object may be the parent (referenced by categoryid) or the associated category
@@ -836,7 +739,7 @@ class grade_item extends grade_object {
                 $grade_calculation->calculation = $formula;
                 if ($grade_calculation->update()) {
                     $this->flag_for_update();
-                    return true;                    
+                    return true;
                 } else {
                     $this->calculation = null; // remove cache
                     debugging("Could not save the calculation in the database for this grade_item.");
@@ -859,44 +762,27 @@ class grade_item extends grade_object {
         }
     }
 
-    /**
-     * Returns the raw values for this grade item (as imported by module or other source).
-     * @param int $userid Optional: to retrieve a single raw grade
-     * @return mixed An array of all raw_grades (stdClass objects) for this grade_item, or a single raw_grade.
-     */
-    function get_raw($userid=NULL) {
-        if (empty($this->grade_grades_raw)) {
-            $this->load_raw();
-        }
-
-        $grade_raw_array = null;
-        if (!empty($userid)) {
-            $r = get_record('grade_grades_raw', 'itemid', $this->id, 'userid', $userid);
-            $grade_raw_array[$r->userid] = new grade_grades_raw($r);
-        } else {
-            $grade_raw_array = $this->grade_grades_raw;
-        }
-        return $grade_raw_array;
-    }
-
     /**
      * Returns the final values for this grade item (as imported by module or other source).
      * @param int $userid Optional: to retrieve a single final grade
      * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
      */
     function get_final($userid=NULL) {
-        if (empty($this->grade_grades_final)) {
-            $this->load_final();
-        }
-
-        if (empty($userid)) {
-            return $this->grade_grades_final;
+        if ($userid) {
+            if ($user = get_record('grade_grades', 'itemid', $this->id, 'userid', $userid)) {
+                return $user;
+            }
 
         } else {
-            if (array_key_exists($userid, $this->grade_grades_final)) {
-                return $this->grade_grades_final[$userid];
+            if ($grades = get_records('grade_grades', 'itemid', $this->id)) {
+                //TODO: speed up with better SQL
+                $result = array();
+                foreach ($grades as $grade) {
+                    $result[$grade->userid] = $grade;
+                }
+                return $result;
             } else {
-                return new grade_grades_final(array('itemid'=>$this->itemid, 'gradevalue'=>NULL, 'userid'=>$userid));
+                return array();
             }
         }
     }
@@ -1045,5 +931,69 @@ class grade_item extends grade_object {
             return array();
         }
     }
+
+    /**
+     * Updates raw grade value for given user, this is a only way to update raw
+     * grades from external source (module, gradebook, import, etc.),
+     * because it logs the change in history table and deals with final grade recalculation.
+     *
+     * The only exception is category grade item which stores the raw grades directly.
+     * Calculated grades do not use raw grades at all, the rawgrade changes there are not logged too.
+     *
+     * @param int $userid the graded user
+     * @param float $rawgrade value of raw grade
+     * @param string $howmodified modification source
+     * @param string $note optional note
+     * @param string $feedback teachjers feedback
+     * @param int $feedbackformat
+     * @return boolean true if ok, false if error
+     */
+    function update_raw($userid, $rawgrade=false, $howmodified='manual', $note=NULL, $feedback=false, $feedbackformat=FORMAT_MOODLE) {
+        $grade = new grade_grades(array('itemid'=>$this->id, 'userid'=>$userid));
+
+        //TODO: add locking checks here - prevent update if item or individaul grade locked
+        //TODO: if grade tree does not need to be recalculated, try to update all users grades in course and flag_for_update only if failed
+
+        // fist copy current grademin/max and scale
+        $grade->rawgrademin = $this->grademin;
+        $grade->rawgrademax = $this->grademax;
+        $grade->rawscaleid  = $this->scaleid;
+
+        if ($rawgrade !== false) {
+            // change of grade value requested
+            if (empty($grade->id)) {
+                $oldgrade = null;
+                $grade->rawgrade = $rawgrade;
+                $result = $grade->insert();
+
+            } else {
+                $oldgrade = $grade->rawgrade;
+                $grade->rawgrade = $rawgrade;
+                $result = $grade->update();
+            }
+        }
+
+        // do we have comment from teacher?
+        if ($result and $feedback !== false) {
+            if (empty($grade->id)) {
+                // create new grade
+                $oldgrade = null;
+                $result = $grade->insert();
+            }
+            $result = $result && $grade->update_feedback($feedback, $feedbackformat);
+        }
+
+        // TODO Handle history recording error, such as displaying a notice, but still return true
+        grade_history::insert_change($userid, $this->id, $grade->rawgrade, $oldgrade, $howmodified, $note);
+
+        // This grade item needs update
+        $this->flag_for_update();
+
+        if ($result) {
+            return $grade;
+        } else {
+            return false;
+        }
+    }
 }
 ?>
index 8f1874f7f9449ee47988da06299cd8f9261bf80e..2ac10fb2779de26678b4e2cca6e2d7c77aac98f4 100644 (file)
@@ -25,8 +25,7 @@
 
 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';
+require_once $CFG->libdir . '/grade/grade_grades.php';
 
 /**
  * This class represents a complete tree of categories, grade_items and final grades,
@@ -316,8 +315,8 @@ class grade_tree {
         
         // If the object is a grade_item, but the final_grades index isn't yet loaded, make the switch now. Same for grade_category and children
         if ($new_element_class == 'grade_item' && !$has_final_grades && $this->include_grades) {
-            $new_element->element['final_grades'] = $new_element->element['object']->load_final();
-            unset($new_element->element['object']->grade_grades_final);
+            $new_element->element['final_grades'] = $new_element->element['object']->get_final();
+
         } elseif ($new_element_class == 'grade_category' && empty($new_element->element['children']) && $new_element->element['object']->has_children()) {
             $new_element->element['children'] = $new_element->element['object']->get_children(1);
             unset($new_element->element['object']->children);
@@ -580,7 +579,7 @@ class grade_tree {
                     $finals = array();
 
                     if ($this->include_grades) {
-                        $final = new grade_grades_final();
+                        $final = new grade_grades();
                         $final->itemid = $element['object']->id;
                         $finals = $final->fetch_all_using_this();
                     }
@@ -592,7 +591,6 @@ class grade_tree {
                 $element['children'] = $itemtree;
             } elseif (get_class($filler_object) == 'grade_item' && $this->include_grades) {
                 $final_grades = $filler_object->get_final();
-                unset($filler_object->grade_grades_final);
                 $element['final_grades'] = $final_grades;
             }
 
@@ -658,7 +656,7 @@ class grade_tree {
                 } else {
                     if (!empty($level1['finalgrades'])) {
                         foreach ($level1['finalgrades'] as $final_grade) {
-                            $this->grades[$final_grade->userid][$final_grade->itemid] = $final_grade->gradevalue;
+                            $this->grades[$final_grade->userid][$final_grade->itemid] = $final_grade->finalgrade;
                         } 
                     }
                 }
@@ -745,7 +743,7 @@ class grade_tree {
                     $finals = array();
 
                     if ($this->include_grades) {
-                        $final = new grade_grades_final();
+                        $final = new grade_grades();
                         $final->itemid = $itemid;
                         $finals = $final->fetch_all_using_this();
                     }
@@ -799,10 +797,10 @@ class grade_tree {
             $finals = array();
             if ($this->include_grades) {
                 if (get_class($object) == 'grade_item') {
-                    $finals = $object->load_final();
+                    $finals = $object->get_final();
                 } else {
                     $item_object = new grade_item($object, false);
-                    $finals = $object->load_final();
+                    $finals = $object->get_final();
                 }
             }
 
@@ -818,11 +816,11 @@ class grade_tree {
                 
                 if ($this->include_grades) {
                     if (get_class($item) == 'grade_item') {
-                        $finals = $item->load_final();
+                        $finals = $item->get_final();
                     } else {
                         $item_object = new grade_item($item, false);
-                        if (method_exists($item, 'load_final')) {
-                            $finals = $item->load_final();
+                        if (method_exists($item, 'get_final')) {
+                            $finals = $item->get_final();
                         }
                     }
                 }
index 616927b89da867831c23f30944c55ee8c6f58a42..02845b1f5b3576cda1086afbd49d7dfdd621e88c 100644 (file)
@@ -60,8 +60,7 @@ define('GRADE_UPDATE_ITEM_LOCKED', 4);
 require_once($CFG->libdir . '/grade/grade_category.php');
 require_once($CFG->libdir . '/grade/grade_item.php');
 require_once($CFG->libdir . '/grade/grade_calculation.php');
-require_once($CFG->libdir . '/grade/grade_grades_raw.php');
-require_once($CFG->libdir . '/grade/grade_grades_final.php');
+require_once($CFG->libdir . '/grade/grade_grades.php');
 require_once($CFG->libdir . '/grade/grade_scale.php');
 require_once($CFG->libdir . '/grade/grade_outcome.php');
 require_once($CFG->libdir . '/grade/grade_history.php');
@@ -72,7 +71,7 @@ require_once($CFG->libdir . '/grade/grade_tree.php');
 
 /**
  * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
- * gradevalue and feedback with format are optional. gradevalue NULL means 'Not graded', missing property
+ * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
  * or key means do not change existing.
  *
  * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
@@ -190,54 +189,38 @@ function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance,
             $failed = true;
             debugging('Invalid userid in grade submitted');
             continue;
+        } else {
+            $userid = $grade['userid'];
         }
 
-        // get the raw grade if it exist
-        $rawgrade = new grade_grades_raw(array('itemid'=>$grade_item->id, 'userid'=>$grade['userid']));
-        $rawgrade->grade_item = &$grade_item; // we already have it, so let's use it
-
-        // store these to keep track of original grade item settings
-        $rawgrade->grademax = $grade_item->grademax;
-        $rawgrade->grademin = $grade_item->grademin;
-        $rawgrade->scaleid  = $grade_item->scaleid;
+        $rawgrade     = false;
+        $feedback       = false;
+        $feedbackformat = FORMAT_MOODLE;
+        
+        if (array_key_exists('rawgrade', $grade)) {
+            $rawgrade = $grade['rawgrade'];
+        }
 
         if (array_key_exists('feedback', $grade)) {
-            $rawgrade->feedback = $grade['feedback'];
-            if (isset($grade['feedbackformat'])) {
-                $rawgrade->feedbackformat = $grade['feedbackformat'];
-            } else {
-                $rawgrade->feedbackformat = FORMAT_MOODLE;
-            }
+            $feedback = $grade['feedback'];
         }
 
-        $result = true;
-        if ($rawgrade->id) {
-            if (array_key_exists('gradevalue', $grade)) {
-                $result = $rawgrade->update($grade['gradevalue'], $source);
-            } else {
-                $result = $rawgrade->update($rawgrade->gradevalue, $source);
-            }
-
-        } else {
-            if (array_key_exists('gradevalue', $grade)) {
-                $rawgrade->gradevalue = $grade['gradevalue'];
-            } else {
-                $rawgrade->gradevalue = null;
-            }
-            $result = $rawgrade->insert();
+        if (array_key_exists('feedbackformat', $grade)) {
+            $feedbackformat = $grade['feedbackformat'];
         }
 
-        if (!$result) {
+        // update or insert the grade
+        $grade = $grade_item->update_raw($userid, $rawgrade, $source, null, $feedback, $feedbackformat);
+
+        if (!$grade) {
             $failed = true;
             debugging('Grade not updated');
             continue;
         }
 
-        // load existing text annotation
-        $rawgrade->load_text();
-
         // trigger grade_updated event notification
         $eventdata = new object();
+
         $eventdata->source            = $source;
         $eventdata->itemid            = $grade_item->id;
         $eventdata->courseid          = $grade_item->courseid;
@@ -246,12 +229,16 @@ function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance,
         $eventdata->iteminstance      = $grade_item->iteminstance;
         $eventdata->itemnumber        = $grade_item->itemnumber;
         $eventdata->idnumber          = $grade_item->idnumber;
-        $eventdata->userid            = $rawgrade->userid;
-        $eventdata->gradevalue        = $rawgrade->gradevalue;
-        $eventdata->feedback          = $rawgrade->feedback;
-        $eventdata->feedbackformat    = (int)$rawgrade->feedbackformat;
-        $eventdata->information       = $rawgrade->information;
-        $eventdata->informationformat = (int)$rawgrade->informationformat;
+        $eventdata->userid            = $grade->userid;
+        $eventdata->rawgrade        = $grade->rawgrade;
+
+        // load existing text annotation
+        if ($grade_text = $grade->load_text()) {
+            $eventdata->feedback          = $grade_text->feedback;
+            $eventdata->feedbackformat    = $grade_text->feedbackformat;
+            $eventdata->information       = $grade_text->information;
+            $eventdata->informationformat = $grade_text->informationformat;
+        }
 
         events_trigger('grade_updated', $eventdata);
     }
@@ -331,30 +318,7 @@ function grade_get_items($courseid, $itemtype=NULL, $itemmodule=NULL, $iteminsta
 }
 
 /**
-* For a given set of items, create a category to group them together (if one doesn't yet exist).
-* Modules may want to do this when they are created. However, the ultimate control is in the gradebook interface itself.
-*
-* @param int $courseid
-* @param string $fullname The name of the new category
-* @param array $items An array of grade_items to group under the new category
-* @param string $aggregation
-* @return mixed New grade_category id if successful
-*/
-/*
-// TODO: this should be obsoleted by grade_update() or removed completely - modules must not use any IDs or grade_item objects directly!
-function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN) {
-    $grade_category = new grade_category(compact('courseid', 'fullname', 'items', 'aggregation'));
-
-    if (empty($grade_category->id)) {
-        return $grade_category->insert();
-    } else {
-        return $grade_category->id;
-    }
-}
-*/
-
-/**
- * Updates all grade_grades_final in course.
+ * Updates all final grades in course.
  *
  * @param int $courseid
  * @return boolean true if ok, array of errors if problems found
@@ -389,7 +353,7 @@ function grade_update_final_grades($courseid) {
         foreach ($grade_items as $gid=>$gitem) {
             $grade_item =& $grade_items[$gid];
             if ($grade_item->needsupdate) {
-                $result = $grade_item->update_final_grade();
+                $result = $grade_item->update_final_grades();
                 if ($result !== true) {
                     $errors = array_merge($errors, $result);
                 }
@@ -454,7 +418,7 @@ function grade_update_final_grades($courseid) {
 
             //oki - let's update, calculate or aggregate :-)
             if ($doupdate) {
-                $result = $grade_item->update_final_grade();
+                $result = $grade_item->update_final_grades();
                 if ($result !== true) {
                     $errors = array_merge($errors, $result);
                 } else {
@@ -486,7 +450,7 @@ function grade_update_final_grades($courseid) {
  */
 function grade_grab_legacy_grades() {
 
-    global $CFG, $db;
+    global $CFG;
 
     if (!$mods = get_list_of_plugins('mod') ) {
         error('No modules installed!');
@@ -533,6 +497,48 @@ function grade_grab_legacy_grades() {
     }
 }
 
+/**
+ * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
+ */
+function grade_grab_grades() {
+
+    global $CFG;
+
+    if (!$mods = get_list_of_plugins('mod') ) {
+        error('No modules installed!');
+    }
+
+    foreach ($mods as $mod) {
+
+        if ($mod == 'NEWMODULE') {   // Someone has unzipped the template, ignore it
+            continue;
+        }
+
+        if (!$module = get_record('modules', 'name', $mod)) {
+            //not installed
+            continue;
+        }
+
+        if (!$module->visible) {
+            //disabled module
+            continue;
+        }
+
+        $fullmod = $CFG->dirroot.'/mod/'.$mod;
+
+        // include the module lib once
+        if (file_exists($fullmod.'/lib.php')) {
+            include_once($fullmod.'/lib.php');
+            // look for modname_grades() function - old gradebook pulling function
+            // if present sync the grades with new grading system
+            $gradefunc = $mod.'_update_grades';
+            if (function_exists($gradefunc)) {
+                $gradefunc();
+            }
+        }
+    }
+}
+
 /**
  * Force full update of module grades in central gradebook - works for both legacy and converted activities.
  * @param object $modinstance object with extra cmidnumber and modname property
@@ -579,16 +585,16 @@ function grade_update_mod_grades($modinstance) {
 
                 if ($usergrade == '-') {
                     // no grade
-                    $grade->gradevalue = null;
+                    $grade->rawgrade = null;
 
                 } else if ($scaleid) {
                     // scale in use, words used
                     $gradescale = explode(",", $scale->scale);
-                    $grade->gradevalue = array_search($usergrade, $gradescale) + 1;
+                    $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
 
                 } else {
                     // good old numeric value
-                    $grade->gradevalue = $usergrade;
+                    $grade->rawgrade = $usergrade;
                 }
                 $grades[] = $grade;
             }
@@ -672,33 +678,6 @@ function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
     return $grade_item;
 }
 
-/**
- * Given a float value situated between a source minimum and a source maximum, converts it to the
- * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
- * for the formula :-)
- * @param float $gradevalue
- * @param float $source_min
- * @param float $source_max
- * @param float $target_min
- * @param float $target_max
- * @return float Converted value
- */
-function standardise_score($gradevalue, $source_min, $source_max, $target_min, $target_max, $debug=false) {
-    $factor = ($gradevalue - $source_min) / ($source_max - $source_min);
-    $diff = $target_max - $target_min;
-    $standardised_value = $factor * $diff + $target_min;
-    if ($debug) {
-        echo 'standardise_score debug info: (lib/gradelib.php)';
-        print_object(array('gradevalue' => $gradevalue,
-                           'source_min' => $source_min,
-                           'source_max' => $source_max,
-                           'target_min' => $target_min,
-                           'target_max' => $target_max,
-                           'result'     => $standardised_value));
-    }
-    return $standardised_value;
-}
-
 /**
  * This function is used to migrade old date and settings from old gradebook into new grading system.
  *
index 8bb411ce3c1898ca5887c834a095545879284749..3995279dc95bb9c0e613b5cc737d0b510130d797 100644 (file)
@@ -55,8 +55,7 @@ class grade_test extends UnitTestCase {
                         'scale',
                         'grade_items',
                         'grade_calculations',
-                        'grade_grades_raw',
-                        'grade_grades_final',
+                        'grade_grades',
                         'grade_grades_text',
                         'grade_outcomes',
                         'grade_history');
@@ -64,8 +63,7 @@ class grade_test extends UnitTestCase {
     var $grade_items = array();
     var $grade_categories = array();
     var $grade_calculations = array();
-    var $grade_grades_raw = array();
-    var $grade_grades_final = array();
+    var $grade_grades = array();
     var $grade_grades_text = array();
     var $grade_outcomes = array();
     var $grade_history = array();
@@ -121,6 +119,7 @@ class grade_test extends UnitTestCase {
             $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
@@ -259,52 +258,35 @@ class grade_test extends UnitTestCase {
             $result = $result && create_table($table, true, false);
         }
 
-        /// Define table grade_grades_final to be created
-        $table = new XMLDBTable('grade_grades_final');
+        /// Define table grade_grades to be created
+        $table = new XMLDBTable('grade_grades');
 
         if ($result && !table_exists($table)) {
 
             $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
             $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
             $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-            $table->addFieldInfo('gradevalue', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
-            $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
+            $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
+            $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+            $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+            $table->addFieldInfo('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
+            $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-            $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
             $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
+    
+        /// Adding keys to table grade_grades
             $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
             $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
             $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
+            $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id'));
             $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
 
-            /// Launch create table for grade_grades_final
-            $result = $result && create_table($table, true, false);
-        }
-
-        /// Define table grade_grades_raw to be created
-        $table = new XMLDBTable('grade_grades_raw');
-
-        if ($result && !table_exists($table)) {
-
-            $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
-            $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-            $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-            $table->addFieldInfo('gradevalue', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
-            $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100');
-            $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0');
-            $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-            $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-            $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-            $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-            $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
-            $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id'));
-            $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
-            $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id'));
-            $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
-
-            /// Launch create table for grade_grades_raw
+            /// Launch create table for grade_grades
             $result = $result && create_table($table, true, false);
         }
 
@@ -733,397 +715,228 @@ class grade_test extends UnitTestCase {
     }
 
     /**
-     * Load grade_grades_raw data into the database, and adds the corresponding objects to this class' variable.
+     * Load grade_grades data into the database, and adds the corresponding objects to this class' variable.
      */
-    function load_grade_grades_raw() {
+    function load_grade_grades() {
         // Grades for grade_item 1
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[0]->id;
-        $grade_raw->userid = 1;
-        $grade_raw->gradevalue = 15; // too small
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[0]->id;
+        $grade->userid = 1;
+        $grade->rawgrade = 15; // too small
+        $grade->finalgrade = 30;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[0] = $grade;
         }
         
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[0]->id;
-        $grade_raw->userid = 2;
-        $grade_raw->gradevalue = 40;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[0]->id;
+        $grade->userid = 2;
+        $grade->rawgrade = 40;
+        $grade->finalgrade = 40;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[1] = $grade;
         }
         
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[0]->id;
-        $grade_raw->userid = 3;
-        $grade_raw->gradevalue = 170; // too big
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[0]->id;
+        $grade->userid = 3;
+        $grade->rawgrade = 170; // too big
+        $grade->finalgrade = 110;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[2] = $grade;
         }
 
+
         // No raw grades for grade_item 2 - it is calculated
 
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[1]->id;
+        $grade->userid = 1;
+        $grade->finalgrade = 60;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+        $grade->locked = true; 
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[3] = $grade;
+        } 
+        
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[1]->id;
+        $grade->userid = 2;
+        $grade->finalgrade = 70;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+        $grade->locked = true; 
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[4] = $grade;
+        }
+
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[1]->id;
+        $grade->userid = 3;
+        $grade->finalgrade = 100;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+        $grade->locked = false; 
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[5] = $grade;
+        } 
 
-        // Grades for grade_item 3
 
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[2]->id;
-        $grade_raw->userid = 1;
-        $grade_raw->gradevalue = 2;
-        $grade_raw->scaleid = $this->scale[3]->id;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
+        // Grades for grade_item 3
 
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[2]->id;
+        $grade->userid = 1;
+        $grade->rawgrade = 2;
+        $grade->finalgrade = 6;
+        $grade->scaleid = $this->scale[3]->id;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[6] = $grade;
         }
         
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[2]->id;
-        $grade_raw->userid = 2;
-        $grade_raw->gradevalue = 3;
-        $grade_raw->scaleid = $this->scale[3]->id;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[2]->id;
+        $grade->userid = 2;
+        $grade->rawgrade = 3;
+        $grade->finalgrade = 2;
+        $grade->scaleid = $this->scale[3]->id;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
         
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[2]->id;
-        $grade_raw->userid = 3;
-        $grade_raw->gradevalue = 1;
-        $grade_raw->scaleid = $this->scale[3]->id;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[2]->id;
+        $grade->userid = 3;
+        $grade->rawgrade = 1;
+        $grade->finalgrade = 3;
+        $grade->scaleid = $this->scale[3]->id;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
         
         // Grades for grade_item 7
 
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[6]->id;
-        $grade_raw->userid = 1;
-        $grade_raw->gradevalue = 97;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[6]->id;
+        $grade->userid = 1;
+        $grade->rawgrade = 97;
+        $grade->finalgrade = 69;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
 
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
         
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[6]->id;
-        $grade_raw->userid = 2;
-        $grade_raw->gradevalue = 49;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[6]->id;
+        $grade->userid = 2;
+        $grade->rawgrade = 49;
+        $grade->finalgrade = 87;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
         
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[6]->id;
-        $grade_raw->userid = 3;
-        $grade_raw->gradevalue = 67;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[6]->id;
+        $grade->userid = 3;
+        $grade->rawgrade = 67;
+        $grade->finalgrade = 94;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
 
         // Grades for grade_item 8
 
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[7]->id;
-        $grade_raw->userid = 2;
-        $grade_raw->gradevalue = 3;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[7]->id;
+        $grade->userid = 2;
+        $grade->rawgrade = 3;
+        $grade->finalgrade = 3;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
 
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
     
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[7]->id;
-        $grade_raw->userid = 3;
-        $grade_raw->gradevalue = 6;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[7]->id;
+        $grade->userid = 3;
+        $grade->rawgrade = 6;
+        $grade->finalgrade = 6;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
   
         // Grades for grade_item 9
 
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[8]->id;
-        $grade_raw->userid = 1;
-        $grade_raw->gradevalue = 20;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
-        }
-        
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[8]->id;
-        $grade_raw->userid = 2;
-        $grade_raw->gradevalue = 50;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
-        }
-        
-        $grade_raw = new stdClass();
-        $grade_raw->itemid = $this->grade_items[7]->id;
-        $grade_raw->userid = 3;
-        $grade_raw->gradevalue = 100;
-        $grade_raw->timecreated = mktime();
-        $grade_raw->timemodified = mktime();
-
-        if ($grade_raw->id = insert_record('grade_grades_raw', $grade_raw)) {
-            $this->grade_grades_raw[] = $grade_raw;
-        }
-    }
-
-    /**
-     * Load grade_grades_final data into the database, and adds the corresponding objects to this class' variable.
-     */
-    function load_grade_grades_final() {
-        // Grades for grade_item 1
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[0]->id;
-        $grade_final->userid = 1;
-        $grade_final->gradevalue = 30;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[8]->id;
+        $grade->userid = 1;
+        $grade->rawgrade = 20;
+        $grade->finalgrade = 20;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
 
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[0] = $grade_final;
-        } 
-        
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[0]->id;
-        $grade_final->userid = 2;
-        $grade_final->gradevalue = 40;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[1] = $grade_final;
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[0]->id;
-        $grade_final->userid = 3;
-        $grade_final->gradevalue = 110;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = false; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[2] = $grade_final;
-        } 
-        
-        // Grades for grade_item 2
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[1]->id;
-        $grade_final->userid = 1;
-        $grade_final->gradevalue = 60;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[3] = $grade_final;
-        } 
         
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[1]->id;
-        $grade_final->userid = 2;
-        $grade_final->gradevalue = 70;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[4] = $grade_final;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[8]->id;
+        $grade->userid = 2;
+        $grade->rawgrade = 50;
+        $grade->finalgrade = 50;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[1]->id;
-        $grade_final->userid = 3;
-        $grade_final->gradevalue = 100;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = false; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[5] = $grade_final;
-        } 
         
-        // Grades for grade_item 3
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[2]->id;
-        $grade_final->userid = 1;
-        $grade_final->gradevalue = 6;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
-        
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[2]->id;
-        $grade_final->userid = 2;
-        $grade_final->gradevalue = 2;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
+        $grade = new stdClass();
+        $grade->itemid = $this->grade_items[7]->id;
+        $grade->userid = 3;
+        $grade->rawgrade = 100;
+        $grade->finalgrade = 100;
+        $grade->timecreated = mktime();
+        $grade->timemodified = mktime();
+
+        if ($grade->id = insert_record('grade_grades', $grade)) {
+            $this->grade_grades[] = $grade;
         }
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[2]->id;
-        $grade_final->userid = 3;
-        $grade_final->gradevalue = 3;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = false; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
-        
-        // Grades for grade_item 7 (orphan item)
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[6]->id;
-        $grade_final->userid = 1;
-        $grade_final->gradevalue = 69;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
-        
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[6]->id;
-        $grade_final->userid = 2;
-        $grade_final->gradevalue = 87;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        }
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[6]->id;
-        $grade_final->userid = 3;
-        $grade_final->gradevalue = 94;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = false; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
-
-        // Grades for grade_item 8
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[7]->id;
-        $grade_final->userid = 2;
-        $grade_final->gradevalue = 3;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        }
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[7]->id;
-        $grade_final->userid = 3;
-        $grade_final->gradevalue = 6;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = false; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
-
-        // Grades for grade_item 9
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[8]->id;
-        $grade_final->userid = 1;
-        $grade_final->gradevalue = 20;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
-        
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[8]->id;
-        $grade_final->userid = 2;
-        $grade_final->gradevalue = 50;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = true; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        }
-
-        $grade_final = new stdClass();
-        $grade_final->itemid = $this->grade_items[8]->id;
-        $grade_final->userid = 3;
-        $grade_final->gradevalue = 100;
-        $grade_final->timecreated = mktime();
-        $grade_final->timemodified = mktime();
-        $grade_final->locked = false; 
-
-        if ($grade_final->id = insert_record('grade_grades_final', $grade_final)) {
-            $this->grade_grades_final[] = $grade_final;
-        } 
     }
     
     /**
@@ -1132,8 +945,8 @@ class grade_test extends UnitTestCase {
     function load_grade_grades_text() {
         $grade_grades_text = new stdClass();
 
-        $grade_grades_text->itemid = $this->grade_grades_raw[0]->itemid;
-        $grade_grades_text->userid = $this->grade_grades_raw[0]->userid;
+        $grade_grades_text->itemid = $this->grade_grades[0]->itemid;
+        $grade_grades_text->userid = $this->grade_grades[0]->userid;
         $grade_grades_text->information = 'Thumbs down';
         $grade_grades_text->informationformat = FORMAT_PLAIN;
         $grade_grades_text->feedback = 'Good, but not good enough..';
index 626233239c0563642217a58594be19343d9a2e9c..5832d8ffeec56cfdaf1df58934565cfa6ddc5408 100644 (file)
@@ -53,7 +53,7 @@ class grade_calculation_test extends grade_test {
     function test_grade_calculation_insert() {
         $grade_calculation = new grade_calculation();
         $this->assertTrue(method_exists($grade_calculation, 'insert'));
-        
+
         $grade_calculation->itemid = $this->grade_items[0]->id;
         $grade_calculation->calculation = '=MEAN(1, 2)';
 
@@ -70,51 +70,51 @@ class grade_calculation_test extends grade_test {
     function test_grade_calculation_update() {
         $grade_calculation = new grade_calculation($this->grade_calculations[0]);
         $this->assertTrue(method_exists($grade_calculation, 'update'));
-        $grade_calculation->calculation = '=MEAN(1, 2)';        
+        $grade_calculation->calculation = '=MEAN(1, 2)';
         $this->assertTrue($grade_calculation->update());
         $calculation = get_field('grade_calculations', 'calculation', 'id', $this->grade_calculations[0]->id);
-        $this->assertEqual($grade_calculation->calculation, $calculation); 
+        $this->assertEqual($grade_calculation->calculation, $calculation);
     }
 
     function test_grade_calculation_delete() {
         $grade_calculation = new grade_calculation($this->grade_calculations[0]);
         $this->assertTrue(method_exists($grade_calculation, 'delete'));
-        
+
         $this->assertTrue($grade_calculation->delete());
-        $this->assertFalse(get_record('grade_calculations', 'id', $grade_calculation->id)); 
+        $this->assertFalse(get_record('grade_calculations', 'id', $grade_calculation->id));
     }
 
     function test_grade_calculation_fetch() {
-        $grade_calculation = new grade_calculation(); 
+        $grade_calculation = new grade_calculation();
         $this->assertTrue(method_exists($grade_calculation, 'fetch'));
 
         $grade_calculation = grade_calculation::fetch('id', $this->grade_calculations[0]->id);
         $this->assertEqual($this->grade_calculations[0]->id, $grade_calculation->id);
-        $this->assertEqual($this->grade_calculations[0]->calculation, $grade_calculation->calculation); 
-    } 
+        $this->assertEqual($this->grade_calculations[0]->calculation, $grade_calculation->calculation);
+    }
 
     function test_grade_calculation_compute() {
-        $grade_calculation = new grade_calculation($this->grade_calculations[0]); 
+        $grade_calculation = new grade_calculation($this->grade_calculations[0]);
         $this->assertTrue(method_exists($grade_calculation, 'compute'));
 
-        $grade_item = $grade_calculation->get_grade_item();
+        $grade_item = $grade_calculation->load_grade_item();
 
-        $grade_grades_final = grade_grades_final::fetch('id', $this->grade_grades_final[3]->id);
-        $grade_grades_final->delete();
-        $grade_grades_final = grade_grades_final::fetch('id', $this->grade_grades_final[4]->id);
-        $grade_grades_final->delete();
-        $grade_grades_final = grade_grades_final::fetch('id', $this->grade_grades_final[5]->id);
-        $grade_grades_final->delete();
+        $grade_grades = grade_grades::fetch('id', $this->grade_grades[3]->id);
+        $grade_grades->delete();
+        $grade_grades = grade_grades::fetch('id', $this->grade_grades[4]->id);
+        $grade_grades->delete();
+        $grade_grades = grade_grades::fetch('id', $this->grade_grades[5]->id);
+        $grade_grades->delete();
 
         $grade_calculation->compute();
 
-        $grade_grades_final = grade_grades_final::fetch('userid', $this->grade_grades_final[3]->userid, 'itemid', $this->grade_grades_final[3]->itemid);
-        $this->assertEqual($this->grade_grades_final[3]->gradevalue, $grade_grades_final->gradevalue); 
-        $grade_grades_final = grade_grades_final::fetch('userid', $this->grade_grades_final[4]->userid, 'itemid', $this->grade_grades_final[4]->itemid);
-        $this->assertEqual($this->grade_grades_final[4]->gradevalue, $grade_grades_final->gradevalue); 
-        $grade_grades_final = grade_grades_final::fetch('userid', $this->grade_grades_final[5]->userid, 'itemid', $this->grade_grades_final[5]->itemid);
-        $this->assertEqual($this->grade_grades_final[5]->gradevalue, $grade_grades_final->gradevalue); 
-    } 
+        $grade_grades = grade_grades::fetch('userid', $this->grade_grades[3]->userid, 'itemid', $this->grade_grades[3]->itemid);
+        $this->assertEqual($this->grade_grades[3]->finalgrade, $grade_grades->finalgrade);
+        $grade_grades = grade_grades::fetch('userid', $this->grade_grades[4]->userid, 'itemid', $this->grade_grades[4]->itemid);
+        $this->assertEqual($this->grade_grades[4]->finalgrade, $grade_grades->finalgrade);
+        $grade_grades = grade_grades::fetch('userid', $this->grade_grades[5]->userid, 'itemid', $this->grade_grades[5]->itemid);
+        $this->assertEqual($this->grade_grades[5]->finalgrade, $grade_grades->finalgrade);
+    }
 
-} 
+}
 ?>
index 37690ed6bb66a59404f22109b3e0083faa08268e..37c81f25523c7669e37fad82b422e0a7b92dfd61 100755 (executable)
@@ -170,17 +170,17 @@ class grade_category_test extends grade_test {
         $this->assertTrue(method_exists($category, 'generate_grades'));
         $category->load_grade_item();
 
-        $raw_grades = get_records('grade_grades_raw', 'itemid', $category->grade_item->id);
-        $this->assertFalse($raw_grades);
+        $grades = get_records('grade_grades', 'itemid', $category->grade_item->id);
+        $this->assertFalse($grades);
 
         $category->generate_grades();
-        $raw_grades = get_records('grade_grades_raw', 'itemid', $category->grade_item->id);
-        $this->assertEqual(3, count($raw_grades));
+        $grades = get_records('grade_grades', 'itemid', $category->grade_item->id);
+        $this->assertEqual(3, count($grades));
 
         $rawvalues = array();
-        foreach ($raw_grades as $grade) {
-            $this->assertWithinMargin($grade->gradevalue, $grade->grademin, $grade->grademax);
-            $rawvalues[] = (int)$grade->gradevalue;
+        foreach ($grades as $grade) {
+            $this->assertWithinMargin($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
+            $rawvalues[] = (int)$grade->rawgrade;
         }
         sort($rawvalues);
         // calculated mean results
@@ -194,15 +194,15 @@ class grade_category_test extends grade_test {
     }
     
     function generate_random_raw_grade($item, $userid) {
-        $raw_grade = new grade_grades_raw();
-        $raw_grade->itemid = $item->id;
-        $raw_grade->userid = $userid;
-        $raw_grade->grademin = 0;
-        $raw_grade->grademax = 1;
+        $grade = new grade_grades();
+        $grade->itemid = $item->id;
+        $grade->userid = $userid;
+        $grade->grademin = 0;
+        $grade->grademax = 1;
         $valuetype = "grade$item->gradetype";
-        $raw_grade->gradevalue = rand(0, 1000) / 1000;
-        $raw_grade->insert();
-        return $raw_grade->gradevalue;
+        $grade->rawgrade = rand(0, 1000) / 1000;
+        $grade->insert();
+        return $grade->rawgrade;
     } 
 
     function test_grade_category_set_as_parent() {
diff --git a/lib/simpletest/grade/simpletest/testgradefinal.php b/lib/simpletest/grade/simpletest/testgradefinal.php
deleted file mode 100644 (file)
index 3488aaa..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-<?php // $Id$
-
-///////////////////////////////////////////////////////////////////////////
-//                                                                       //
-// NOTICE OF COPYRIGHT                                                   //
-//                                                                       //
-// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
-//          http://moodle.org                                            //
-//                                                                       //
-// Copyright (C) 1999-2004  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  //
-// the Free Software Foundation; either version 2 of the License, or     //
-// (at your option) any later version.                                   //
-//                                                                       //
-// This program is distributed in the hope that it will be useful,       //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
-// GNU General Public License for more details:                          //
-//                                                                       //
-//          http://www.gnu.org/copyleft/gpl.html                         //
-//                                                                       //
-///////////////////////////////////////////////////////////////////////////
-
-/**
- * Unit tests for grade_final object.
- *
- * @author nicolas@moodle.com
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package moodlecore
- */
-
-if (!defined('MOODLE_INTERNAL')) {
-    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
-}
-
-require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
-
-class grade_final_test extends grade_test {
-
-    function test_grade_grades_final_construct() {
-        $params = new stdClass();
-
-        $params->itemid = $this->grade_items[0]->id;
-        $params->userid = 1;
-        $params->gradevalue = 88;
-
-        $grade_grades_final = new grade_grades_final($params, false);
-        $this->assertEqual($params->itemid, $grade_grades_final->itemid);
-        $this->assertEqual($params->gradevalue, $grade_grades_final->gradevalue);
-    }
-
-    function test_grade_grades_final_insert() {
-        $grade_grades_final = new grade_grades_final();
-        $this->assertTrue(method_exists($grade_grades_final, 'insert'));
-        
-        $grade_grades_final->itemid = $this->grade_items[0]->id;
-        $grade_grades_final->userid = 4;
-        $grade_grades_final->gradevalue = 88;
-
-        $this->assertTrue($grade_grades_final->insert()); 
-
-        $last_grade_grades_final = end($this->grade_grades_final);
-
-        $this->assertEqual($last_grade_grades_final->id + 1, $grade_grades_final->id);
-        $this->assertFalse(empty($grade_grades_final->timecreated));
-        $this->assertFalse(empty($grade_grades_final->timemodified));
-    }
-
-    function test_grade_grades_final_update() {
-        $grade_grades_final = new grade_grades_final($this->grade_grades_final[0]);
-        $this->assertTrue(method_exists($grade_grades_final, 'update'));
-        $grade_grades_final->gradevalue = 89;        
-        $this->assertTrue($grade_grades_final->update());
-        $gradevalue = get_field('grade_grades_final', 'gradevalue', 'id', $this->grade_grades_final[0]->id);
-        $this->assertEqual($grade_grades_final->gradevalue, $gradevalue); 
-    }
-
-    function test_grade_grades_final_delete() {
-        $grade_grades_final = new grade_grades_final($this->grade_grades_final[0]);
-        $this->assertTrue(method_exists($grade_grades_final, 'delete'));
-        
-        $this->assertTrue($grade_grades_final->delete());
-        $this->assertFalse(get_record('grade_grades_final', 'id', $grade_grades_final->id)); 
-    }
-
-    function test_grade_grades_final_fetch() {
-        $grade_grades_final = new grade_grades_final(); 
-        $this->assertTrue(method_exists($grade_grades_final, 'fetch'));
-
-        $grade_grades_final = grade_grades_final::fetch('id', $this->grade_grades_final[0]->id);
-        $this->assertEqual($this->grade_grades_final[0]->id, $grade_grades_final->id);
-        $this->assertEqual($this->grade_grades_final[0]->gradevalue, $grade_grades_final->gradevalue); 
-    } 
-
-    function test_grade_grades_final_load_grade_item() {
-        $grade_grades_final = new grade_grades_final($this->grade_grades_final[0]);
-        $this->assertTrue(method_exists($grade_grades_final, 'load_grade_item'));
-        $this->assertNull($grade_grades_final->grade_item);
-        $this->assertTrue($grade_grades_final->itemid);
-        $this->assertNotNull($grade_grades_final->load_grade_item());
-        $this->assertNotNull($grade_grades_final->grade_item);
-        $this->assertEqual($this->grade_items[0]->id, $grade_grades_final->grade_item->id);
-    }
-} 
-?>
diff --git a/lib/simpletest/grade/simpletest/testgradegrades.php b/lib/simpletest/grade/simpletest/testgradegrades.php
new file mode 100755 (executable)
index 0000000..52bc066
--- /dev/null
@@ -0,0 +1,128 @@
+<?php // $Id$
+
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.org                                            //
+//                                                                       //
+// Copyright (C) 1999-2004  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  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+/**
+ * Unit tests for grade_raw object.
+ *
+ * @author nicolas@moodle.com
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package moodlecore
+ */
+
+global $CFG;if (!defined('MOODLE_INTERNAL')) {
+    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
+}
+
+require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
+
+class grade_grades_test extends grade_test {
+
+    function test_grade_grades_construct() {
+        $params = new stdClass();
+
+        $params->itemid = $this->grade_items[0]->id;
+        $params->userid = 1;
+        $params->rawgrade = 88;
+        $params->rawgrademax = 110;
+        $params->rawgrademin = 18;
+
+        $grade_grades = new grade_grades($params, false);
+        $this->assertEqual($params->itemid, $grade_grades->itemid);
+        $this->assertEqual($params->rawgrade, $grade_grades->rawgrade);
+    }
+
+    function test_grade_grades_insert() {
+        $grade_grades = new grade_grades();
+        $this->assertTrue(method_exists($grade_grades, 'insert'));
+
+        $grade_grades->itemid = $this->grade_items[0]->id;
+        $grade_grades->userid = 1;
+        $grade_grades->rawgrade = 88;
+        $grade_grades->rawgrademax = 110;
+        $grade_grades->rawgrademin = 18;
+
+        // Check the grade_item's needsupdate variable first
+        $grade_grades->load_grade_item();
+        $this->assertFalse($grade_grades->grade_item->needsupdate);
+
+        $grade_grades->insert();
+
+        $last_grade_grades = end($this->grade_grades);
+
+        $this->assertEqual($grade_grades->id, $last_grade_grades->id + 1);
+        $this->assertFalse(empty($grade_grades->timecreated));
+        $this->assertFalse(empty($grade_grades->timemodified));
+    }
+
+    function test_grade_grades_update() {
+        $grade_grades = new grade_grades($this->grade_grades[0]);
+        $this->assertTrue(method_exists($grade_grades, 'update'));
+    }
+
+    function test_grade_grades_fetch() {
+        $grade_grades = new grade_grades();
+        $this->assertTrue(method_exists($grade_grades, 'fetch'));
+
+        $grade_grades = grade_grades::fetch('id', $this->grade_grades[0]->id);
+        $this->assertEqual($this->grade_grades[0]->id, $grade_grades->id);
+        $this->assertEqual($this->grade_grades[0]->rawgrade, $grade_grades->rawgrade);
+    }
+
+    function test_grade_raw_update_feedback() {
+
+    }
+
+    function test_grade_raw_update_information() {
+
+    }
+
+    function test_grade_load_text() {
+        $grade_grades = new grade_grades($this->grade_grades[0]);
+        $this->assertTrue(method_exists($grade_grades, 'load_text'));
+        $this->assertNull($grade_grades->grade_grades_text);
+        $this->assertNotNull($grade_grades->load_text());
+        $this->assertNotNull($grade_grades->grade_grades_text);
+        $this->assertEqual($this->grade_grades_text[0]->id, $grade_grades->grade_grades_text->id);
+    }
+
+    function test_grade_grades_load_grade_item() {
+        $grade_grades = new grade_grades($this->grade_grades[0]);
+        $this->assertTrue(method_exists($grade_grades, 'load_grade_item'));
+        $this->assertNull($grade_grades->grade_item);
+        $this->assertTrue($grade_grades->itemid);
+        $this->assertNotNull($grade_grades->load_grade_item());
+        $this->assertNotNull($grade_grades->grade_item);
+        $this->assertEqual($this->grade_items[0]->id, $grade_grades->grade_item->id);
+    }
+
+
+    function test_grade_grades_standardise_score() {
+        $this->assertEqual(4, round(grade_grades::standardise_score(6, 0, 7, 0, 5)));
+        $this->assertEqual(40, grade_grades::standardise_score(50, 30, 80, 0, 100));
+    }
+
+
+}
+?>
index 84b535a6197ce369b228f157887143120558c81b..a1513027baa20046cb382fe2d1204c8cef2e14d1 100755 (executable)
@@ -206,30 +206,6 @@ class grade_item_test extends grade_test {
         $first_grade_item = reset($grade_items);\r
         $this->assertEqual($this->grade_items[0]->id, $first_grade_item->id);\r
     }\r
-    \r
-    /**\r
-     * Retrieve all raw scores for a given grade_item.\r
-     */\r
-    function test_grade_item_get_all_raws() {\r
-        $grade_item = new grade_item($this->grade_items[0]);\r
-        $this->assertTrue(method_exists($grade_item, 'get_raw'));\r
-        \r
-        $raw_grades = $grade_item->get_raw();\r
-        $this->assertEqual(3, count($raw_grades));\r
-    }\r
-\r
-    /**\r
-     * Retrieve the raw score for a specific userid.\r
-     */\r
-    function test_grade_item_get_raw() { \r
-        $grade_item = new grade_item($this->grade_items[0]);\r
-        $this->assertTrue(method_exists($grade_item, 'get_raw'));\r
-        \r
-        $raw_grades = $grade_item->get_raw($this->userid);\r
-        $raw_grade = current($raw_grades);\r
-        $this->assertEqual(1, count($raw_grades));        \r
-        $this->assertEqual($this->grade_grades_raw[0]->gradevalue, $raw_grade->gradevalue);\r
-    }\r
 \r
     \r
     /**\r
@@ -251,7 +227,7 @@ class grade_item_test extends grade_test {
         $grade_item = new grade_item($this->grade_items[0]);\r
         $this->assertTrue(method_exists($grade_item, 'get_final'));\r
         $final_grade = $grade_item->get_final($this->userid);\r
-        $this->assertEqual($this->grade_grades_final[0]->gradevalue, $final_grade->gradevalue);\r
+        $this->assertEqual($this->grade_grades[0]->finalgrade, $final_grade->finalgrade);\r
     }\r
 \r
     function test_grade_item_get_calculation() {\r
@@ -263,16 +239,16 @@ class grade_item_test extends grade_test {
     }\r
 \r
     function test_grade_item_set_calculation() {\r
-        $grade_item = new grade_item($this->grade_items[1]);\r
+/*        $grade_item = new grade_item($this->grade_items[1]);\r
         $this->assertTrue(method_exists($grade_item, 'set_calculation'));\r
         $this->assertTrue(method_exists($grade_item, 'get_calculation'));\r
         \r
-        $calculation = 'SUM([unittestgradeitem1], [unittestgradeitem3])';\r
+        $calculation = '=SUM([unittestgradeitem1], [unittestgradeitem3])';\r
         $grade_item->set_calculation($calculation);\r
         $new_calculation = $grade_item->get_calculation();\r
 \r
         $this->assertEqual($calculation, $new_calculation->calculation);\r
-    }\r
+*/    }\r
 \r
     function test_grade_item_get_category() {\r
         $grade_item = new grade_item($this->grade_items[0]);\r
@@ -287,48 +263,8 @@ class grade_item_test extends grade_test {
      */\r
     function test_grade_item_update_final_grades() {\r
         $grade_item = new grade_item($this->grade_items[0]);\r
-        $this->assertTrue(method_exists($grade_item, 'update_final_grade'));\r
-        $this->assertEqual(true, $grade_item->update_final_grade()); \r
-    }\r
-\r
-    /**\r
-     * Test loading of raw and final items into grade_item.\r
-     */\r
-    function test_grade_item_load() {\r
-        $grade_item = new grade_item($this->grade_items[0]);\r
-        $this->assertTrue(method_exists($grade_item, 'load_final'));\r
-        $this->assertTrue(method_exists($grade_item, 'load_raw'));\r
-\r
-        // Check that final and raw items are not yet loaded\r
-        $this->assertTrue(empty($grade_item->grade_grades_final));\r
-        $this->assertTrue(empty($grade_item->grade_grades_raw));\r
-        \r
-        // Load raw and final grades\r
-        $grade_item->load_final();\r
-        $grade_item->load_raw();\r
-        \r
-        // Check that final and raw grades are now loaded\r
-        $this->assertFalse(empty($grade_item->grade_grades_final));\r
-        $this->assertFalse(empty($grade_item->grade_grades_raw));\r
-        $this->assertEqual($this->grade_grades_final[0]->gradevalue, $grade_item->grade_grades_final[1]->gradevalue);\r
-        $this->assertEqual($this->grade_grades_raw[0]->gradevalue, $grade_item->grade_grades_raw[1]->gradevalue);\r
-    }\r
-\r
-    /**\r
-     * Test loading final items, generating fake values to replace missing grades\r
-     */\r
-    function test_grade_item_load_fake_final() {\r
-        $grade_item = new grade_item($this->grade_items[0]);\r
-        $this->assertTrue(method_exists($grade_item, 'load_final'));\r
-\r
-        // Delete one of the final grades\r
-        $final_grade = new grade_grades_final($this->grade_grades_final[0]);\r
-        $final_grade->delete();\r
-        unset($this->grade_grades_final[0]);\r
-\r
-        // Load normal final grades\r
-        $final_grades = $grade_item->load_final();\r
-        $this->assertEqual(2, count($final_grades));\r
+        $this->assertTrue(method_exists($grade_item, 'update_final_grades'));\r
+        $this->assertEqual(true, $grade_item->update_final_grades()); \r
     }\r
     \r
     /**\r
@@ -339,7 +275,7 @@ class grade_item_test extends grade_test {
         $this->assertTrue(method_exists($grade_item, 'adjust_grade'));\r
         $grade_raw = new stdClass();\r
 \r
-        $grade_raw->gradevalue = 40;\r
+        $grade_raw->rawgrade = 40;\r
         $grade_raw->grademax = 100;\r
         $grade_raw->grademin = 0;\r
         \r
@@ -351,17 +287,17 @@ class grade_item_test extends grade_test {
         $original_grade_raw  = clone($grade_raw);\r
         $original_grade_item = clone($grade_item);\r
 \r
-        $this->assertEqual(20, $grade_item->adjust_grade($grade_raw)); \r
+        $this->assertEqual(20, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)); \r
         \r
         // Try a larger maximum grade\r
         $grade_item->grademax = 150;\r
         $grade_item->grademin = 0;\r
-        $this->assertEqual(60, $grade_item->adjust_grade($grade_raw)); \r
+        $this->assertEqual(60, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)); \r
 \r
         // Try larger minimum grade\r
         $grade_item->grademin = 50;\r
 \r
-        $this->assertEqual(90, $grade_item->adjust_grade($grade_raw)); \r
+        $this->assertEqual(90, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)); \r
 \r
         // Rescaling from a small scale (0-50) to a larger scale (0-100)\r
         $grade_raw->grademax = 50;\r
@@ -369,13 +305,13 @@ class grade_item_test extends grade_test {
         $grade_item->grademax = 100;\r
         $grade_item->grademin = 0;\r
 \r
-        $this->assertEqual(80, $grade_item->adjust_grade($grade_raw)); \r
+        $this->assertEqual(80, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)); \r
 \r
         // Rescaling from a small scale (0-50) to a larger scale with offset (40-100)\r
         $grade_item->grademax = 100;\r
         $grade_item->grademin = 40;\r
 \r
-        $this->assertEqual(88, $grade_item->adjust_grade($grade_raw)); \r
+        $this->assertEqual(88, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)); \r
 \r
         // Try multfactor and plusfactor\r
         $grade_raw = clone($original_grade_raw);\r
@@ -383,7 +319,7 @@ class grade_item_test extends grade_test {
         $grade_item->multfactor = 1.23;\r
         $grade_item->plusfactor = 3;\r
 \r
-        $this->assertEqual(27.6, $grade_item->adjust_grade($grade_raw)); \r
+        $this->assertEqual(27.6, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)); \r
 \r
         // Try multfactor below 0 and a negative plusfactor\r
         $grade_raw = clone($original_grade_raw);\r
@@ -391,11 +327,11 @@ class grade_item_test extends grade_test {
         $grade_item->multfactor = 0.23;\r
         $grade_item->plusfactor = -3;\r
 \r
-        $this->assertEqual(round(1.6), round($grade_item->adjust_grade($grade_raw))); \r
+        $this->assertEqual(round(1.6), round($grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax))); \r
     }\r
 \r
     function test_grade_item_adjust_scale_grade() {\r
-        // Load grade item and its scale\r
+/*        // Load grade item and its scale\r
         $grade_item = new grade_item(array('scaleid' => $this->scale[1]->id), false);\r
         $grade_item->gradetype = GRADE_TYPE_SCALE;\r
         $grade_item->insert();\r
@@ -403,8 +339,8 @@ class grade_item_test extends grade_test {
         $this->assertEqual('Very Good', $grade_item->scale->scale_items[1]);\r
 \r
         // Load raw grade and its scale\r
-        $grade_raw = new grade_grades_raw(array('scaleid' => $this->scale[0]->id), false);\r
-        $grade_raw->gradevalue = 4;\r
+        $grade_raw = new grade_grades(array('scaleid' => $this->scale[0]->id), false);\r
+        $grade_raw->rawgrade = 4;\r
         $grade_raw->itemid = $grade_item->id;\r
         $grade_raw->userid = 1;\r
         $grade_raw->insert();\r
@@ -413,26 +349,26 @@ class grade_item_test extends grade_test {
 \r
         // Test grade_item::adjust_scale\r
         $this->assertEqual(3, $grade_item->adjust_grade($grade_raw));\r
-        $grade_raw->gradevalue = 6;\r
+        $grade_raw->rawgrade = 6;\r
         $this->assertEqual(4, $grade_item->adjust_grade($grade_raw));\r
-    }\r
+*/    }\r
 \r
     function test_grade_item_toggle_locking() {\r
-        $grade_item = new grade_item($this->grade_items[0]);\r
+/*        $grade_item = new grade_item($this->grade_items[0]);\r
         $this->assertTrue(method_exists($grade_item, 'toggle_locking'));\r
 \r
         $this->assertFalse($grade_item->locked);\r
         $this->assertEqual(0, $grade_item->toggle_locking());\r
         $this->assertTrue($grade_item->locked);\r
         $grade_item->load_final();\r
-        $this->assertFalse($grade_item->grade_grades_final[1]->locked);\r
+        $this->assertFalse($grade_item->grade_grades[1]->locked);\r
         \r
         $grade_item->locked = false;\r
         $this->assertEqual(3, $grade_item->toggle_locking(true));\r
         $this->assertTrue($grade_item->locked);\r
-        $this->assertTrue($grade_item->grade_grades_final[1]->locked);\r
-        $this->assertTrue($grade_item->grade_grades_final[2]->locked);\r
-        $this->assertTrue($grade_item->grade_grades_final[3]->locked);\r
+        $this->assertTrue($grade_item->grade_grades[1]->locked);\r
+        $this->assertTrue($grade_item->grade_grades[2]->locked);\r
+        $this->assertTrue($grade_item->grade_grades[3]->locked);\r
     }\r
 \r
     function test_grade_item_toggle_hiding() {\r
@@ -443,15 +379,15 @@ class grade_item_test extends grade_test {
         $this->assertEqual(0, $grade_item->toggle_hiding());\r
         $this->assertTrue($grade_item->hidden);\r
         $grade_item->load_final();\r
-        $this->assertFalse($grade_item->grade_grades_final[1]->hidden);\r
+        $this->assertFalse($grade_item->grade_grades[1]->hidden);\r
         \r
         $grade_item->hidden = false;\r
         $this->assertEqual(3, $grade_item->toggle_hiding(true));\r
         $this->assertTrue($grade_item->hidden);\r
-        $this->assertTrue($grade_item->grade_grades_final[1]->hidden);\r
-        $this->assertTrue($grade_item->grade_grades_final[2]->hidden);\r
-        $this->assertTrue($grade_item->grade_grades_final[3]->hidden);\r
-    } \r
+        $this->assertTrue($grade_item->grade_grades[1]->hidden);\r
+        $this->assertTrue($grade_item->grade_grades[2]->hidden);\r
+        $this->assertTrue($grade_item->grade_grades[3]->hidden);\r
+*/    } \r
 \r
     function test_float_keys() {\r
     }\r
diff --git a/lib/simpletest/grade/simpletest/testgraderaw.php b/lib/simpletest/grade/simpletest/testgraderaw.php
deleted file mode 100755 (executable)
index cd6bf2b..0000000
+++ /dev/null
@@ -1,191 +0,0 @@
-<?php // $Id$
-
-///////////////////////////////////////////////////////////////////////////
-//                                                                       //
-// NOTICE OF COPYRIGHT                                                   //
-//                                                                       //
-// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
-//          http://moodle.org                                            //
-//                                                                       //
-// Copyright (C) 1999-2004  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  //
-// the Free Software Foundation; either version 2 of the License, or     //
-// (at your option) any later version.                                   //
-//                                                                       //
-// This program is distributed in the hope that it will be useful,       //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
-// GNU General Public License for more details:                          //
-//                                                                       //
-//          http://www.gnu.org/copyleft/gpl.html                         //
-//                                                                       //
-///////////////////////////////////////////////////////////////////////////
-
-/**
- * Unit tests for grade_raw object.
- *
- * @author nicolas@moodle.com
- * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
- * @package moodlecore
- */
-
-global $CFG;if (!defined('MOODLE_INTERNAL')) {
-    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
-}
-
-require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
-
-class grade_raw_test extends grade_test {
-
-    function test_grade_grades_raw_construct() {
-        $params = new stdClass();
-
-        $params->itemid = $this->grade_items[0]->id;
-        $params->userid = 1;
-        $params->gradevalue = 88;
-        $params->grademax = 110;
-        $params->grademin = 18;
-
-        $grade_grades_raw = new grade_grades_raw($params, false);
-        $this->assertEqual($params->itemid, $grade_grades_raw->itemid);
-        $this->assertEqual($params->gradevalue, $grade_grades_raw->gradevalue);
-    }
-
-    function test_grade_grades_raw_insert() {
-        $grade_grades_raw = new grade_grades_raw();
-        $this->assertTrue(method_exists($grade_grades_raw, 'insert'));
-        
-        $grade_grades_raw->itemid = $this->grade_items[0]->id;
-        $grade_grades_raw->userid = 1;
-        $grade_grades_raw->gradevalue = 88;
-        $grade_grades_raw->grademax = 110;
-        $grade_grades_raw->grademin = 18;
-
-        // Check the grade_item's needsupdate variable first
-        $grade_grades_raw->load_grade_item(); 
-        $this->assertFalse($grade_grades_raw->grade_item->needsupdate);
-
-        $grade_grades_raw->insert();
-
-        // Now check the needsupdate variable, it should have been set to true
-        $this->assertTrue($grade_grades_raw->grade_item->needsupdate);
-        
-        $last_grade_grades_raw = end($this->grade_grades_raw);
-
-        $this->assertEqual($grade_grades_raw->id, $last_grade_grades_raw->id + 1);
-        $this->assertFalse(empty($grade_grades_raw->timecreated));
-        $this->assertFalse(empty($grade_grades_raw->timemodified));
-
-        // try a scale raw grade
-        $grade_grades_raw = new grade_grades_raw();
-        
-        $grade_grades_raw->itemid = $this->grade_items[0]->id;
-        $grade_grades_raw->userid = 1;
-        $grade_grades_raw->gradevalue = 6;
-        $grade_grades_raw->scaleid = $this->scale[0]->id;
-
-        $grade_grades_raw->insert();
-
-        $this->assertEqual(6, $grade_grades_raw->grademax);
-        $this->assertEqual(0, $grade_grades_raw->grademin); 
-    }
-
-    function test_grade_grades_raw_update() {
-        $grade_grades_raw = new grade_grades_raw($this->grade_grades_raw[0]);
-        $this->assertTrue(method_exists($grade_grades_raw, 'update'));
-        
-        // Check the grade_item's needsupdate variable first
-        $grade_grades_raw->load_grade_item();
-        $this->assertFalse($grade_grades_raw->grade_item->needsupdate);
-        $this->assertTrue($grade_grades_raw->update(89));
-        $gradevalue = get_field('grade_grades_raw', 'gradevalue', 'id', $this->grade_grades_raw[0]->id);
-        $this->assertEqual($grade_grades_raw->gradevalue, $gradevalue); 
-
-        // Now check the needsupdate variable, it should have been set to true
-        $this->assertTrue($grade_grades_raw->grade_item->needsupdate);
-    }
-
-    function test_grade_grades_raw_delete() {
-        $grade_grades_raw = new grade_grades_raw($this->grade_grades_raw[0]);
-        $this->assertTrue(method_exists($grade_grades_raw, 'delete'));
-        
-        // Check the grade_item's needsupdate variable first
-        $grade_grades_raw->load_grade_item(); 
-        $this->assertFalse($grade_grades_raw->grade_item->needsupdate);
-
-        $this->assertTrue($grade_grades_raw->delete());
-        $this->assertFalse(get_record('grade_grades_raw', 'id', $grade_grades_raw->id)); 
-        
-        // Now check the needsupdate variable, it should have been set to true
-        $this->assertTrue($grade_grades_raw->grade_item->needsupdate);
-    }
-
-    function test_grade_grades_raw_fetch() {
-        $grade_grades_raw = new grade_grades_raw(); 
-        $this->assertTrue(method_exists($grade_grades_raw, 'fetch'));
-
-        $grade_grades_raw = grade_grades_raw::fetch('id', $this->grade_grades_raw[0]->id);
-        $this->assertEqual($this->grade_grades_raw[0]->id, $grade_grades_raw->id);
-        $this->assertEqual($this->grade_grades_raw[0]->gradevalue, $grade_grades_raw->gradevalue); 
-    } 
-    
-    /**
-     * Make sure that an update of a grade_raw object also updates the history table.
-     */
-    function test_grade_raw_update_history() {
-        $grade_raw = new grade_grades_raw($this->grade_grades_raw[0]);
-        $oldgrade = $grade_raw->gradevalue;
-        $newgrade = 88;
-        $howmodified = 'manual';
-        $note = 'unittest editing grade manually';
-        $grade_raw->update($newgrade, $howmodified, $note);
-        
-        // Get last entry in the history log and check its attributes
-        $results = get_records('grade_history', 'itemid', $grade_raw->itemid, 'id DESC', '*', 0, 1);
-        $history_log = current($results);
-        $this->assertEqual($grade_raw->userid, $history_log->userid);
-        $this->assertEqual($oldgrade, $history_log->oldgrade);
-        $this->assertEqual($newgrade, $history_log->newgrade);
-        $this->assertEqual($howmodified, $history_log->howmodified);
-        $this->assertEqual($note, $history_log->note);
-    }
-    
-    function test_grade_raw_update_feedback() {
-
-    }
-
-    function test_grade_raw_update_information() {
-
-    }
-
-    function test_grade_raw_load_text() {
-        $grade_grades_raw = new grade_grades_raw($this->grade_grades_raw[0]);
-        $this->assertTrue(method_exists($grade_grades_raw, 'load_text'));
-        $this->assertNull($grade_grades_raw->grade_grades_text);
-        $this->assertFalse(array_key_exists('feedback', $grade_grades_raw));
-        $this->assertFalse(array_key_exists('feedbackformat', $grade_grades_raw));
-        $this->assertFalse(array_key_exists('information', $grade_grades_raw));
-        $this->assertFalse(array_key_exists('informationformat', $grade_grades_raw));
-        $this->assertNotNull($grade_grades_raw->load_text());
-        $this->assertNotNull($grade_grades_raw->grade_grades_text);
-        $this->assertTrue(array_key_exists('feedback', $grade_grades_raw));
-        $this->assertTrue(array_key_exists('feedbackformat', $grade_grades_raw));
-        $this->assertTrue(array_key_exists('information', $grade_grades_raw));
-        $this->assertTrue(array_key_exists('informationformat', $grade_grades_raw));
-        $this->assertEqual($this->grade_grades_text[0]->id, $grade_grades_raw->grade_grades_text->id); 
-    }
-
-    function test_grade_grades_raw_load_grade_item() {
-        $grade_grades_raw = new grade_grades_raw($this->grade_grades_raw[0]);
-        $this->assertTrue(method_exists($grade_grades_raw, 'load_grade_item'));
-        $this->assertNull($grade_grades_raw->grade_item);
-        $this->assertTrue($grade_grades_raw->itemid);
-        $this->assertNotNull($grade_grades_raw->load_grade_item());
-        $this->assertNotNull($grade_grades_raw->grade_item);
-        $this->assertEqual($this->grade_items[0]->id, $grade_grades_raw->grade_item->id);
-    }
-
-} 
-?>
index e22f40aacfbde5ca9b11ec63b9bf329203f03895..53ff92e6d4ee7ad81c35d53a2c4477bb891a43cb 100755 (executable)
@@ -42,8 +42,8 @@ class grade_text_test extends grade_test {
     function test_grade_grades_text_construct() {
         $params = new stdClass();
 
-        $params->itemid = $this->grade_grades_raw[0]->itemid;
-        $params->userid = $this->grade_grades_raw[0]->userid;
+        $params->itemid = $this->grade_grades[0]->itemid;
+        $params->userid = $this->grade_grades[0]->userid;
         $params->information = 'Thumbs down';
         $params->informationformat = FORMAT_PLAIN;
         $params->feedback = 'Good, but not good enough..';
@@ -62,8 +62,8 @@ class grade_text_test extends grade_test {
         $grade_grades_text = new grade_grades_text();
         $this->assertTrue(method_exists($grade_grades_text, 'insert'));
         
-        $grade_grades_text->itemid = $this->grade_grades_raw[0]->itemid;
-        $grade_grades_text->userid = $this->grade_grades_raw[0]->userid;
+        $grade_grades_text->itemid = $this->grade_grades[0]->itemid;
+        $grade_grades_text->userid = $this->grade_grades[0]->userid;
         $grade_grades_text->information = 'Thumbs down';
         $grade_grades_text->informationformat = FORMAT_PLAIN;
         $grade_grades_text->feedback = 'Good, but not good enough..';
index 3e09b8584eb20ad6c2fdb0e8e241d5cf1c5b2123..a3b43840332ab46de5ea2c4148b49d0de740c311 100644 (file)
@@ -262,7 +262,7 @@ class grade_tree_test extends grade_test {
         $this->assertFalse(empty($tree->tree_array[1]['children'][2]['children'][1]['object']->itemname));
         $this->assertEqual($this->grade_items[2]->itemname, $tree->tree_array[1]['children'][2]['children'][1]['object']->itemname);
         $this->assertFalse(empty($tree->tree_array[1]['children'][2]['children'][1]['final_grades'][1]));
-        $this->assertEqual($this->grade_grades_final[6]->gradevalue, $tree->tree_array[1]['children'][2]['children'][1]['final_grades'][1]->gradevalue);
+        $this->assertEqual($this->grade_grades[6]->finalgrade, $tree->tree_array[1]['children'][2]['children'][1]['final_grades'][1]->finalgrade);
         
         // Check the need_insert array
         $this->assertEqual(1, count($tree->need_insert));
index 9ff1245176602eff3714fa9e1a56048cccb82258..ee01de03a2994c75443450e40cfedde6c94fac71 100644 (file)
@@ -45,10 +45,10 @@ require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
 class gradelib_test extends grade_test {
 
     function test_grade_get_items() {
-        if (get_class($this) == 'gradelib_test') { 
+        if (get_class($this) == 'gradelib_test') {
             $grade_items = grade_get_items($this->courseid);
 
-            $this->assertTrue(is_array($grade_items)); 
+            $this->assertTrue(is_array($grade_items));
             $this->assertEqual(count($grade_items), 10);
         }
     }
@@ -56,15 +56,15 @@ class gradelib_test extends grade_test {
 /*
 // obsolted function, should be replaced by grade_update() or removed completely
     function test_grade_create_category() {
-        if (get_class($this) == 'gradelib_test') { 
+        if (get_class($this) == 'gradelib_test') {
             $grade_category = new stdClass();
             $grade_category->timecreated = mktime();
             $grade_category->timemodified = mktime();
-        
+
             $items = array(new grade_item(), new grade_item());
-            
+
             $grade_category->id = grade_create_category($this->courseid, 'unittestcategory4', $items, GRADE_AGGREGATE_MEAN);
-            
+
             $last_grade_category = end($this->grade_categories);
             $this->assertEqual($grade_category->id, $last_grade_category->id + 1);
 
@@ -77,19 +77,13 @@ class gradelib_test extends grade_test {
     }
 */
     function test_grade_is_locked() {
-        if (get_class($this) == 'gradelib_test') { 
+        if (get_class($this) == 'gradelib_test') {
             $grade_item = $this->grade_items[0];
             $this->assertFalse(grade_is_locked($grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber));
             $grade_item = $this->grade_items[1];
-            $this->assertTrue(grade_is_locked($grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber)); 
+            $this->assertTrue(grade_is_locked($grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule, $grade_item->iteminstance, $grade_item->itemnumber));
         }
     }
-
-    function test_grade_standardise_score() {
-        $this->assertEqual(4, round(standardise_score(6, 0, 7, 0, 5)));
-        $this->assertEqual(40, standardise_score(50, 30, 80, 0, 100));
-    }
-
 }
 
 ?>
index cb6949180330c0591effbaae75c706990015ea08..59fcf0287a214f57361891cdfb56a0100ee18a04 100644 (file)
@@ -1833,7 +1833,7 @@ function assignment_get_user_grades($assignment, $userid=0) {
 
     $user = $userid ? "AND u.id = $userid" : "";
 
-    $sql = "SELECT u.id, u.id AS userid, s.grade AS gradevalue, s.submissioncomment AS feedback, s.format AS feedbackformat
+    $sql = "SELECT u.id, u.id AS userid, s.grade AS rawgrade, s.submissioncomment AS feedback, s.format AS feedbackformat
               FROM {$CFG->prefix}user u, {$CFG->prefix}assignment_submissions s
              WHERE u.id = s.userid AND s.assignment = $assignment->id
                    $user";
@@ -1856,8 +1856,8 @@ function assignment_update_grades($assignment=null, $userid=0, $nullifnone=true)
     if ($assignment != null) {
         if ($grades = assignment_get_user_grades($assignment, $userid)) {
             foreach($grades as $k=>$v) {
-                if ($v->gradevalue == -1) {
-                    $grades[$k]->gradevalue = null;
+                if ($v->rawgrade == -1) {
+                    $grades[$k]->rawgrade = null;
                 }
             }
             grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, $grades);
@@ -1971,10 +1971,10 @@ function assignment_grade_handler($eventdata) {
     $submission->teacher    = $USER->id;
     $submission->timemarked = time();
 
-    if (is_null($eventdata->gradevalue)) {
+    if (is_null($eventdata->rawgrade)) {
         $submission->grade  = -1;
     } else {
-        $submission->grade  = (int)$eventdata->gradevalue; // round it for now
+        $submission->grade  = (int)$eventdata->rawgrade; // round it for now
         if ($old->grade != $submission->grade) {
             $submission->mailed = 0;       // Make sure mail goes out (again, even)
         }
index c21de43d1eed3b2de5108b2881e9db4f3e16eae9..c6e95e503e685922912315f5f741d6bf04bb6950 100755 (executable)
@@ -742,7 +742,7 @@ function data_get_user_grades($data, $userid=0) {
 
     $user = $userid ? "AND u.id = $userid" : "";
 
-    $sql = "SELECT u.id, u.id AS userid, avg(drt.rating) AS gradevalue
+    $sql = "SELECT u.id, u.id AS userid, avg(drt.rating) AS rawgrade
               FROM {$CFG->prefix}user u, {$CFG->prefix}data_records dr,
                    {$CFG->prefix}data_ratings drt
              WHERE u.id = dr.userid AND dr.id = drt.recordid
@@ -773,7 +773,7 @@ function data_update_grades($data=null, $userid=0, $nullifnone=true) {
             $grade = new object();
             $grade->itemid     = $data->id;
             $grade->userid     = $userid;
-            $grade->gradevalue = NULL;
+            $grade->rawgrade = NULL;
             grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grade);
         }
 
index 743303f39e9eeaccfd91a6cb65d2cce84d0ac014..91f4d5b2279c10aa7c0e8dda977c99641c5f5874 100644 (file)
@@ -1111,7 +1111,7 @@ function forum_get_user_grades($forum, $userid=0) {
 
     $user = $userid ? "AND u.id = $userid" : "";
 
-    $sql = "SELECT u.id, u.id AS userid, avg(fr.rating) AS gradevalue
+    $sql = "SELECT u.id, u.id AS userid, avg(fr.rating) AS rawgrade
               FROM {$CFG->prefix}user u, {$CFG->prefix}forum_posts fp,
                    {$CFG->prefix}forum_ratings fr, {$CFG->prefix}forum_discussions fd
              WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id
@@ -1139,7 +1139,7 @@ function forum_update_grades($forum=null, $userid=0, $nullifnone=true) {
             $grade = new object();
             $grade->itemid     = $forum->id;
             $grade->userid     = $userid;
-            $grade->gradevalue = NULL;
+            $grade->rawgrade = NULL;
             grade_update('mod/forum', $data->course, 'mod', 'forum', $forum->id, 0, $grade);
         }
 
index 58380b026ad7a88f861725f32c6a73851a31f4ae..bfbce33816b90434e8fdebcdda2a7c3961ae3761 100644 (file)
@@ -321,7 +321,7 @@ function glossary_get_user_grades($glossary, $userid=0) {
 
     $user = $userid ? "AND u.id = $userid" : "";
 
-    $sql = "SELECT u.id, u.id AS userid, avg(gr.rating) AS gradevalue
+    $sql = "SELECT u.id, u.id AS userid, avg(gr.rating) AS rawgrade
               FROM {$CFG->prefix}user u, {$CFG->prefix}glossary_entries ge,
                    {$CFG->prefix}glossary_ratings gr
              WHERE u.id = ge.userid AND ge.id = gr.entryid
@@ -349,7 +349,7 @@ function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) {
             $grade = new object();
             $grade->itemid     = $glossary->id;
             $grade->userid     = $userid;
-            $grade->gradevalue = NULL;
+            $grade->rawgrade = NULL;
             grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade);
         }
 
index ea05cc7f82ee11f9fa469c456f8cc80a502067f8..a8c98d7c83842d639cebcc7430fc22f4d4cbbe94 100644 (file)
@@ -88,7 +88,7 @@
                 // it's a student, show their grade
                 $grade_value = 0;
                 if ($return = lesson_get_user_grades($lesson, $USER->id)) {
-                    $grade_value = $return[$USER->id]->gradevalue;
+                    $grade_value = $return[$USER->id]->rawgrade;
                 }
             }
             $table->data[] = array ($lesson->section, $link, $grade_value, $due);
index 781a90b622075a6888913b59691b9f4a03ccc524..978c4529d2c78e8542b56e572bca7ca747169d81 100644 (file)
@@ -412,13 +412,13 @@ function lesson_get_user_grades($lesson, $userid=0) {
 
     if ($lesson->retake) {
         if ($lesson->usemaxgrade) {
-            $sql = "SELECT u.id, u.id AS userid, MAX(g.grade) AS gradevalue
+            $sql = "SELECT u.id, u.id AS userid, MAX(g.grade) AS rawgrade
                       FROM {$CFG->prefix}user u, {$CFG->prefix}lesson_grades g
                      WHERE u.id = g.userid AND g.lessonid = $lesson->id
                            $user
                   GROUP BY u.id";
         } else {
-            $sql = "SELECT u.id, u.id AS userid, AVG(g.grade) AS gradevalue
+            $sql = "SELECT u.id, u.id AS userid, AVG(g.grade) AS rawgrade
                       FROM {$CFG->prefix}user u, {$CFG->prefix}lesson_grades g
                      WHERE u.id = g.userid AND g.lessonid = $lesson->id
                            $user
@@ -433,7 +433,7 @@ function lesson_get_user_grades($lesson, $userid=0) {
                              $fuser
                        GROUP BY uu.id";
 
-        $sql = "SELECT u.id, u.id AS userid, g.grade AS gradevalue
+        $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade
                   FROM {$CFG->prefix}user u, {$CFG->prefix}lesson_grades g, ($firstonly) f
                  WHERE u.id = g.userid AND g.lessonid = $lesson->id
                        AND g.id = f.firstcompleted AND g.userid=f.userid
@@ -463,7 +463,7 @@ function lesson_update_grades($lesson=null, $userid=0, $nullifnone=true) {
             $grade = new object();
             $grade->itemid     = $lesson->id;
             $grade->userid     = $userid;
-            $grade->gradevalue = NULL;
+            $grade->rawgrade = NULL;
             grade_update('mod/lesson', $lesson->course, 'mod', 'lesson', $lesson->id, 0, $grade);
         }
 
index b19c27b84533f0f6cb16b92e72e86abc97c14a3b..de3529e73f8f7a56dec8f27d7806c285317219d3 100644 (file)
@@ -238,7 +238,7 @@ function quiz_get_user_grades($quiz, $userid=0) {
 
     $user = $userid ? "AND u.id = $userid" : "";
 
-    $sql = "SELECT u.id, u.id AS userid, g.grade AS gradevalue
+    $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade
               FROM {$CFG->prefix}user u, {$CFG->prefix}quiz_grades g
              WHERE u.id = g.userid AND g.quiz = $quiz->id
                    $user";
@@ -266,7 +266,7 @@ function quiz_update_grades($quiz=null, $userid=0, $nullifnone=true) {
             $grade = new object();
             $grade->itemid     = $quiz->id;
             $grade->userid     = $userid;
-            $grade->gradevalue = NULL;
+            $grade->rawgrade = NULL;
             grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grade);
         }
 
index e24ad1b58f89b38703bb45da5485e6b67e90dc5b..c562ebeb33937ab94d420cad0ed3263616ffd010 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-   $version = 2007060502;  // YYYYMMDD = date
+   $version = 2007062007;  // YYYYMMDD = date
                            //       XY = increments within a single day
 
    $release = '1.9 dev';    // Human-friendly version name