### Eclipse Workspace Patch 1.0
#P moodle
Index: lib/db/upgrade.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/upgrade.php,v
retrieving revision 1.121
diff -u -r1.121 upgrade.php
--- lib/db/upgrade.php	31 Aug 2007 19:27:04 -0000	1.121
+++ lib/db/upgrade.php	5 Sep 2007 09:28:13 -0000
@@ -1761,24 +1761,6 @@
         $result = $result && add_field($table, $field);
     }    
 
-/*
-    /// drop old gradebook tables
-    if ($result && $oldversion < 2007072209) {
-        $tables = array('grade_category',
-                        'grade_item',
-                        'grade_letter',
-                        'grade_preferences',
-                        'grade_exceptions');
-
-        foreach ($tables as $table) {
-            $table = new XMLDBTable($table);
-            if (table_exists($table)) {
-                drop_table($table);
-            }
-        }
-    }
-*/
-
     if ($result && $oldversion < 2007082700) {
 
     /// Define field timemodified to be added to tag_instance
@@ -1991,6 +1973,52 @@
 
     }
 
+    if ($result && $oldversion < 2007090501) {
+
+    /// Define field aggregatesubcats to be added to grade_categories
+        $table = new XMLDBTable('grade_categories');
+        $field = new XMLDBField('aggregatesubcats');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'aggregateoutcomes');
+
+    /// Launch add field aggregatesubcats
+        $result = $result && add_field($table, $field);
+
+    /// Define field aggregatesubcats to be added to grade_categories_history
+        $table = new XMLDBTable('grade_categories_history');
+        $field = new XMLDBField('aggregatesubcats');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'aggregateoutcomes');
+
+    /// Launch add field aggregatesubcats
+        $result = $result && add_field($table, $field);
+    }
+
+    if ($result && $oldversion < 2007090502) {
+        // upgrade path in grade_categrories table - now using slash on both ends
+        $concat = sql_concat('path', "'/'");
+        $sql = "UPDATE {$CFG->prefix}grade_categories SET path = $concat WHERE path NOT LIKE '/%/'";
+        execute_sql($sql, true);
+    }
+
+
+
+/*
+    /// drop old gradebook tables
+    if ($result && $oldversion < xxxxxxxx) {
+        $tables = array('grade_category',
+                        'grade_item',
+                        'grade_letter',
+                        'grade_preferences',
+                        'grade_exceptions');
+
+        foreach ($tables as $table) {
+            $table = new XMLDBTable($table);
+            if (table_exists($table)) {
+                drop_table($table);
+            }
+        }
+    }
+*/
+
     return $result;
 }
 
Index: lib/db/install.xml
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/db/install.xml,v
retrieving revision 1.113
diff -u -r1.113 install.xml
--- lib/db/install.xml	31 Aug 2007 19:27:04 -0000	1.113
+++ lib/db/install.xml	5 Sep 2007 09:28:09 -0000
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20070829" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20070905" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
@@ -1292,8 +1292,9 @@
         <FIELD NAME="aggregation" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="A constant pointing to one of the predefined aggregation strategies (none, mean,median,sum, etc)" PREVIOUS="fullname" NEXT="keephigh"/>
         <FIELD NAME="keephigh" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Keep only the X highest items" PREVIOUS="aggregation" NEXT="droplow"/>
         <FIELD NAME="droplow" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Drop the X lowest items" PREVIOUS="keephigh" NEXT="aggregateoutcomes"/>
-        <FIELD NAME="aggregateoutcomes" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Aggregate outcomes" PREVIOUS="droplow" NEXT="timecreated"/>
-        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="aggregateoutcomes" NEXT="timemodified"/>
+        <FIELD NAME="aggregateoutcomes" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Aggregate outcomes" PREVIOUS="droplow" NEXT="aggregatesubcats"/>
+        <FIELD NAME="aggregatesubcats" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="ignore subcategories in aggregation" PREVIOUS="aggregateoutcomes" NEXT="timecreated"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="aggregatesubcats" NEXT="timemodified"/>
         <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
       </FIELDS>
       <KEYS>
@@ -1435,7 +1436,8 @@
         <FIELD NAME="aggregation" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="A constant pointing to one of the predefined aggregation strategies (none, mean,median,sum, etc)" PREVIOUS="fullname" NEXT="keephigh"/>
         <FIELD NAME="keephigh" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Keep only the X highest items" PREVIOUS="aggregation" NEXT="droplow"/>
         <FIELD NAME="droplow" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Drop the X lowest items" PREVIOUS="keephigh" NEXT="aggregateoutcomes"/>
-        <FIELD NAME="aggregateoutcomes" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Aggregate outcomes" PREVIOUS="droplow"/>
+        <FIELD NAME="aggregateoutcomes" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="Aggregate outcomes" PREVIOUS="droplow" NEXT="aggregatesubcats"/>
+        <FIELD NAME="aggregatesubcats" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="ignore subcategories in aggregation" PREVIOUS="aggregateoutcomes"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="primary key of the table, please edit me" NEXT="oldid"/>
Index: version.php
===================================================================
RCS file: /cvsroot/moodle/moodle/version.php,v
retrieving revision 1.533
diff -u -r1.533 version.php
--- version.php	31 Aug 2007 18:42:54 -0000	1.533
+++ version.php	5 Sep 2007 09:28:05 -0000
@@ -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 = 2007083101;  // YYYYMMDD = date
+    $version = 2007090502;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '1.9 Beta +';   // Human-friendly version name
Index: lib/grade/grade_category.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/grade/grade_category.php,v
retrieving revision 1.90
diff -u -r1.90 grade_category.php
--- lib/grade/grade_category.php	31 Aug 2007 22:42:10 -0000	1.90
+++ lib/grade/grade_category.php	5 Sep 2007 09:28:14 -0000
@@ -63,7 +63,7 @@
     var $depth = 0;
 
     /**
-     * Shows the hierarchical path for this category as /1/2/3 (like course_categories), the last number being
+     * Shows the hierarchical path for this category as /1/2/3/ (like course_categories), the last number being
      * this category's autoincrement ID number.
      * @var string $path
      */
@@ -95,11 +95,17 @@
 
     /**
      * Aggregate outcomes together with normal items
-     * @$aggregateoutcomes
+     * @var int $aggregateoutcomes
      */
     var $aggregateoutcomes = 0;
 
     /**
+     * Ignore subcategories when aggregating
+     * @var int $aggregatesubcats
+     */
+    var $aggregatesubcats = 0;
+
+    /**
      * Array of grade_items or grade_categories nested exactly 1 level below this category
      * @var array $children
      */
@@ -136,10 +142,10 @@
      */
     function build_path($grade_category) {
         if (empty($grade_category->parent)) {
-            return '/'.$grade_category->id;
+            return '/'.$grade_category->id.'/';
         } else {
             $parent = get_record('grade_categories', 'id', $grade_category->parent);
-            return grade_category::build_path($parent).'/'.$grade_category->id;
+            return grade_category::build_path($parent).$grade_category->id.'/';
         }
     }
 
@@ -177,7 +183,7 @@
         // force recalculation of path;
         if (empty($this->path)) {
             $this->path  = grade_category::build_path($this);
-            $this->depth = substr_count($this->path, '/');
+            $this->depth = substr_count($this->path, '/') - 1;
         }
 
 
@@ -318,8 +324,9 @@
         $keephighdiff    = $db_item->keephigh          != $this->keephigh;
         $droplowdiff     = $db_item->droplow           != $this->droplow;
         $aggoutcomesdiff = $db_item->aggregateoutcomes != $this->aggregateoutcomes;
+        $aggsubcatsdiff  = $db_item->aggregatesubcats  != $this->aggregatesubcats;
 
-        return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggoutcomesdiff);
+        return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggoutcomesdiff || $aggsubcatsdiff);
     }
 
     /**
Index: lib/grade/grade_item.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/grade/grade_item.php,v
retrieving revision 1.109
diff -u -r1.109 grade_item.php
--- lib/grade/grade_item.php	31 Aug 2007 22:42:08 -0000	1.109
+++ lib/grade/grade_item.php	5 Sep 2007 09:28:15 -0000
@@ -1152,20 +1152,33 @@
                 $outcomes_sql = "AND gi.outcomeid IS NULL";
             }
 
-            $sql = "SELECT gi.id
-                      FROM {$CFG->prefix}grade_items gi
-                     WHERE gi.categoryid = {$grade_category->id}
-                           AND (gi.gradetype = ".GRADE_TYPE_VALUE." OR gi.gradetype = ".GRADE_TYPE_SCALE.")
-                           $outcomes_sql
-
-                    UNION
-
-                    SELECT gi.id
-                      FROM {$CFG->prefix}grade_items gi, {$CFG->prefix}grade_categories gc
-                     WHERE (gi.itemtype = 'category' OR gi.itemtype = 'course') AND gi.iteminstance=gc.id
-                           AND gc.parent = {$grade_category->id}
-                           AND (gi.gradetype = ".GRADE_TYPE_VALUE." OR gi.gradetype = ".GRADE_TYPE_SCALE.")
-                           $outcomes_sql";
+            if ($grade_category->aggregatesubcats) {
+                // return all children excluding category items                
+                $sql = "SELECT gi.id
+                          FROM {$CFG->prefix}grade_items gi
+                         WHERE (gi.gradetype = ".GRADE_TYPE_VALUE." OR gi.gradetype = ".GRADE_TYPE_SCALE.")
+                               $outcomes_sql
+                               AND gi.categoryid IN (
+                                  SELECT gc.id
+                                    FROM {$CFG->prefix}grade_categories gc
+                                   WHERE gc.path LIKE '%/{$grade_category->id}/%')";
+
+            } else {
+                $sql = "SELECT gi.id
+                          FROM {$CFG->prefix}grade_items gi
+                         WHERE gi.categoryid = {$grade_category->id}
+                               AND (gi.gradetype = ".GRADE_TYPE_VALUE." OR gi.gradetype = ".GRADE_TYPE_SCALE.")
+                               $outcomes_sql
+    
+                        UNION
+    
+                        SELECT gi.id
+                          FROM {$CFG->prefix}grade_items gi, {$CFG->prefix}grade_categories gc
+                         WHERE (gi.itemtype = 'category' OR gi.itemtype = 'course') AND gi.iteminstance=gc.id
+                               AND gc.parent = {$grade_category->id}
+                               AND (gi.gradetype = ".GRADE_TYPE_VALUE." OR gi.gradetype = ".GRADE_TYPE_SCALE.")
+                               $outcomes_sql";
+            }
 
             if ($children = get_records_sql($sql)) {
                 return array_keys($children);
Index: grade/edit/tree/category_form.php
===================================================================
RCS file: /cvsroot/moodle/moodle/grade/edit/tree/category_form.php,v
retrieving revision 1.5
diff -u -r1.5 category_form.php
--- grade/edit/tree/category_form.php	9 Aug 2007 16:41:39 -0000	1.5
+++ grade/edit/tree/category_form.php	5 Sep 2007 09:28:05 -0000
@@ -36,6 +36,8 @@
                               false, true, false, get_string("aggregateoutcomeshelp", 'grades')));
         }
 
+        $mform->addElement('advcheckbox', 'aggregatesubcats', get_string('aggregatesubcats', 'grades'));
+
         $options = array();
         $options[0] = get_string('none');
         for ($i=1; $i<=20; $i++) {
