MDL-46139 Grades: Change usedinaggregation column to 2 columns for status and weight.
authorDamyon Wiese <damyon@moodle.com>
Wed, 13 Aug 2014 08:22:36 +0000 (16:22 +0800)
committerAdrian Greeve <adrian@moodle.com>
Fri, 3 Oct 2014 05:48:55 +0000 (13:48 +0800)
grade/report/user/lib.php
lib/db/install.xml
lib/db/upgrade.php
lib/grade/grade_category.php
lib/grade/grade_grade.php

index 7135604..17846bf 100644 (file)
@@ -474,8 +474,8 @@ class grade_report_user extends grade_report {
                     $hint = $grade_grade->get_aggregation_hint($grade_object);
                     if ($hint) {
                         // This obliterates the weight because it provides a more informative description.
-                        if (intval($hint)) {
-                            $hint = format_float(intval($hint) / 100.0, 2) . ' %';
+                        if (is_numeric($hint)) {
+                            $hint = format_float($hint * 100.0, 2) . ' %';
                         }
                         $data['weight']['content'] = $hint;
                     }
@@ -616,7 +616,7 @@ class grade_report_user extends grade_report {
                         $hint = $grade_grade->get_aggregation_hint($grade_object);
                         if ($hint && is_numeric($hint)) {
                             $me = $grade_grade->grade_item;
-                            $percentoftotal = intval($hint) / 10000.0;
+                            $percentoftotal = $hint;
                             $validpercent = true;
                             $limit = 0;
                             $parent = null;
@@ -641,7 +641,7 @@ class grade_report_user extends grade_report {
                                     $validpercent = $parentgradeitem->is_course_item();
                                     continue;
                                 }
-                                $thispercent = intval($hint) / 10000.0;
+                                $thispercent = $hint;
                                 $percentoftotal *= $thispercent;
                                 $limit++;
                                 if ($limit > 20) {
index 10bd6f2..8102dd5 100755 (executable)
         <FIELD NAME="informationformat" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="format of information text"/>
         <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="the time this grade was first created"/>
         <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="the time this grade was last modified"/>
-        <FIELD NAME="usedinaggregation" TYPE="char" LENGTH="10" NOTNULL="true" DEFAULT="unknown" SEQUENCE="false" COMMENT="One of several values describing how this grade_grade was used when calculating the aggregation. Possible values are &quot;unknown&quot;, &quot;dropped&quot;, &quot;novalue&quot;, &quot;included&quot;"/>
+        <FIELD NAME="aggregationstatus" TYPE="char" LENGTH="10" NOTNULL="true" DEFAULT="unknown" SEQUENCE="false" COMMENT="One of several values describing how this grade_grade was used when calculating the aggregation. Possible values are &quot;unknown&quot;, &quot;dropped&quot;, &quot;novalue&quot;, &quot;used&quot;"/>
+        <FIELD NAME="aggregationweight" TYPE="number" LENGTH="10" NOTNULL="false" SEQUENCE="false" DECIMALS="5" COMMENT="If the aggregationstatus == 'included', then this is the percent this item contributed to the aggregation."/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
index 2e9866b..1d8c35c 100644 (file)
@@ -3719,21 +3719,6 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2014072400.01);
     }
 
-    if ($oldversion < 2014080700.00) {
-
-        // Define field usedinaggregation to be added to grade_grades.
-        $table = new xmldb_table('grade_grades');
-        $field = new xmldb_field('usedinaggregation', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'unknown', 'timemodified');
-
-        // Conditionally launch add field usedinaggregation.
-        if (!$dbman->field_exists($table, $field)) {
-            $dbman->add_field($table, $field);
-        }
-
-        // Main savepoint reached.
-        upgrade_main_savepoint(true, 2014080700.00);
-    }
-
     if ($oldversion < 2014080801.00) {
 
         // Define index behaviour (not unique) to be added to question_attempts.
@@ -3852,5 +3837,27 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2014100100.00);
     }
 
+    if ($oldversion < 2014082700.00) {
+
+        // Define field aggregationstatus to be added to grade_grades.
+        $table = new xmldb_table('grade_grades');
+        $field = new xmldb_field('aggregationstatus', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'unknown', 'timemodified');
+
+        // Conditionally launch add field aggregationstatus.
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+        $field = new xmldb_field('aggregationweight', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'aggregationstatus');
+
+        // Conditionally launch add field aggregationweight.
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2014082700.00);
+    }
+
     return true;
 }
index d8a4c66..143d3ae 100644 (file)
@@ -675,14 +675,22 @@ class grade_category extends grade_object {
         if (!empty($usedweights)) {
             // The usedweights items are updated individually to record the weights.
             foreach ($usedweights as $gradeitemid => $contribution) {
-                // Convert contribution to a 4 digit integer so there are no localization problems.
-                $contribution = intval($contribution * 10000);
                 $DB->set_field_select('grade_grades',
-                                      'usedinaggregation',
+                                      'aggregationweight',
                                       $contribution,
                                       "itemid = :itemid AND userid = :userid",
                                       array('itemid'=>$gradeitemid, 'userid'=>$userid));
             }
+
+            // Now set the status flag for all these weights.
+            list($itemsql, $itemlist) = $DB->get_in_or_equal(array_keys($usedweights), SQL_PARAMS_NAMED, 'g');
+            $itemlist['userid'] = $userid;
+
+            $DB->set_field_select('grade_grades',
+                                  'aggregationstatus',
+                                  'used',
+                                  "itemid $itemsql AND userid = :userid",
+                                  $itemlist);
         }
 
         // No value.
@@ -692,7 +700,7 @@ class grade_category extends grade_object {
             $itemlist['userid'] = $userid;
 
             $DB->set_field_select('grade_grades',
-                                  'usedinaggregation',
+                                  'aggregationstatus',
                                   'novalue',
                                   "itemid $itemsql AND userid = :userid",
                                   $itemlist);
@@ -705,7 +713,7 @@ class grade_category extends grade_object {
             $itemlist['userid'] = $userid;
 
             $DB->set_field_select('grade_grades',
-                                  'usedinaggregation',
+                                  'aggregationstatus',
                                   'dropped',
                                   "itemid $itemsql AND userid = :userid",
                                   $itemlist);
index 70d43d9..a2e6a6a 100644 (file)
@@ -50,7 +50,7 @@ class grade_grade extends grade_object {
     public $required_fields = array('id', 'itemid', 'userid', 'rawgrade', 'rawgrademax', 'rawgrademin',
                                  'rawscaleid', 'usermodified', 'finalgrade', 'hidden', 'locked',
                                  'locktime', 'exported', 'overridden', 'excluded', 'timecreated',
-                                 'timemodified', 'usedinaggregation');
+                                 'timemodified', 'aggregationstatus', 'aggregationweight');
 
     /**
      * Array of optional fields with default values (these should match db defaults)
@@ -161,11 +161,16 @@ class grade_grade extends grade_object {
     public $timemodified = null;
 
     /**
-     * Used in aggregation flag. Can be one of 'unknown', 'dropped', 'novalue' or a specific weighting.
-     * @var string $usedinaggregation
+     * Aggregation status flag. Can be one of 'unknown', 'dropped', 'novalue' or 'used'.
+     * @var string $aggregationstatus
      */
-    public $usedinaggregation = 'unknown';
+    public $aggregationstatus = 'unknown';
 
+    /**
+     * Aggregation weight is the specific weight used in the aggregation calculation for this grade.
+     * @var float $aggregationweight
+     */
+    public $aggregationweight = null;
 
     /**
      * Returns array of grades for given grade_item+users
@@ -292,23 +297,43 @@ class grade_grade extends grade_object {
         return $this->timecreated;
     }
 
+    /**
+     * Returns the weight this grade contributed to the aggregated grade
+     *
+     * @return float|null
+     */
+    public function get_aggregationweight() {
+        return $this->aggregationweight;
+    }
+
+    /**
+     * Set aggregationweight.
+     *
+     * @param float $aggregationweight
+     * @return void
+     */
+    public function set_aggregationweight($aggregationweight) {
+        $this->aggregationweight = $aggregationweight;
+        $this->update();
+    }
+
     /**
      * Returns the info on how this value was used in the aggregated grade
      *
      * @return string One of 'dropped', 'excluded', 'novalue' or a specific weighting
      */
-    public function get_usedinaggregation() {
-        return $this->usedinaggregation;
+    public function get_aggregationstatus() {
+        return $this->aggregationstatus;
     }
 
     /**
-     * Set usedinaggregation flag
+     * Set aggregationstatus flag
      *
-     * @param string $usedinaggregation
+     * @param string $aggregationstatus
      * @return void
      */
-    public function set_usedinaggregation($usedinaggregation) {
-        $this->usedinaggregation = $usedinaggregation;
+    public function set_aggregationstatus($aggregationstatus) {
+        $this->aggregationstatus = $aggregationstatus;
         $this->update();
     }
 
@@ -991,13 +1016,13 @@ class grade_grade extends grade_object {
 
         // Is it dropped?
         if ($hint == '') {
-            $aggr = $this->get_usedinaggregation();
+            $aggr = $this->get_aggregationstatus();
             if ($aggr == 'dropped') {
                 $hint = get_string('dropped', 'grades');
-            } else if ($aggr == 'novalue') {
-                $hint = '-';
+            } else if ($aggr == 'used') {
+                $hint = $this->get_aggregationweight();
             } else if ($aggr != 'unknown') {
-                $hint = $aggr;
+                $hint = '-';
             }
         }