]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9629 Changed grade_item.gradetype default to 1, added GRADE_TYPE_NONE constant...
authornicolasconnault <nicolasconnault>
Tue, 5 Jun 2007 08:50:24 +0000 (08:50 +0000)
committernicolasconnault <nicolasconnault>
Tue, 5 Jun 2007 08:50:24 +0000 (08:50 +0000)
grade/report/grader/category.php
lang/en_utf8/grades.php
lib/db/install.xml
lib/db/upgrade.php
lib/grade/grade_category.php
lib/grade/grade_item.php
lib/grade/grade_tree.php
lib/gradelib.php
lib/simpletest/grade/simpletest/testgradecategory.php
version.php

index cb1c6f8cc7e1b82df8c5bb74fcb1ba561bfb1994..69213a2f53630999779060af6f0aad3855602fc7 100644 (file)
@@ -44,6 +44,8 @@ $param->categories    = optional_param('categories', 0, PARAM_INT);
 $param->element_type  = optional_param('element_type', 0, PARAM_ALPHA);
 $param->category_name = optional_param('category_name', 0, PARAM_ALPHA);
 
+print_object($param);
+
 $tree = new grade_tree($param->courseid);
 $select_source = false;
 
@@ -75,14 +77,13 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) {
         case 'edit':
             break;
         case 'delete':
-            if ($param->confirm == 1) { 
+            if ($param->confirm == 1) { // Perform the deletion
                 $tree->remove_element($param->target);
                 $tree->renumber();
                 $tree->update_db();
                 // Print result message
                 
-            } else {
-                // Print confirmation dialog
+            } else { // Print confirmation dialog
                 $strdeletecheckfull = get_string('deletecheck', '', $element->element['object']->get_name());
                 $linkyes = "category.php?target=$param->target&amp;action=delete&amp;confirm=1$tree->commonvars";
                 $linkno = "category.php?$tree->commonvars";
@@ -124,12 +125,11 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) {
             break;
     }
     unset($param->target);
-} elseif (!empty($param->element_type) && 
-          !empty($param->action) && 
-          $param->action == 'create' && 
-          confirm_sesskey() && 
-          !empty($param->category_name)) {
-    if ($param->element_type == 'items') {
+} elseif (!empty($param->element_type) && !empty($param->action) && $param->action == 'create' && confirm_sesskey()) {
+    if (empty($param->category_name)) {
+        notice(get_string('createcategoryerror', 'grades') . ': ' . get_string('nocategoryname', 'grades'));
+    } elseif ($param->element_type == 'items') {
+
         if (!empty($param->items)) {
             $category = new grade_category();
             $category->fullname = $param->category_name;
@@ -150,7 +150,7 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) {
             } 
 
         } else { // No items selected. Can't create a category over them...
-            notice("Couldn't create a category, you did not select any grade items."); 
+            notice(get_string('createcategoryerror', 'grades') . ': ' . get_string('noselecteditems', 'grades'));
         }
     } elseif ($param->element_type == 'categories') {
         if (!empty($param->categories)) {
@@ -172,7 +172,7 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) {
             }
 
         } else { // No categories selected. Can't create a category over them...
-            notice("Couldn't create a category, you did not select any sub-categories.");
+            notice(get_string('createcategoryerror', 'grades') . ': ' . get_string('noselectedcategories', 'grades'));
         }
 
     } else { // The element_type wasn't set properly, throw up an error
index 34ed69ee51799b81e1b6ed0faa0211cf08c12181..b281c91fe550124ef48a67711c9cc8003e619f06 100644 (file)
@@ -13,7 +13,8 @@ $string['categories'] = 'Categories';
 $string['category'] = 'Category';
 $string['categoryname'] = 'Category name';
 $string['choosecategory'] = 'Select Category';
-$string['createcategory'] = 'Create category';
+$string['createcategory'] = 'Create Category';
+$string['createcategoryerror'] = 'Could not create a new category';
 $string['creatinggradebooksettings'] = 'Creating Gradebook settings';
 $string['curveto'] = 'Curve To';
 $string['deletecategory'] = 'Delete Category';
@@ -75,6 +76,7 @@ $string['movingelement'] = 'Moving $a';
 $string['newcategory'] = 'New category';
 $string['no'] = 'No';
 $string['nocategories'] = 'Grade categories could not be added or found for this course';
+$string['nocategoryname'] = 'No category name was given.';
 $string['nocategoryview'] = 'No category to view by';
 $string['nogradeletters'] = 'No grade letters set';
 $string['nogradesreturned'] = 'No grades returned';
@@ -82,6 +84,8 @@ $string['nolettergrade'] = 'No letter grade for';
 $string['nomode'] = 'NA';
 $string['nonnumericweight'] = 'Received non-numeric value for';
 $string['nonweightedpct'] = 'non-weighted %%';
+$string['noselectedcategories'] = 'no categories were selected.';
+$string['noselecteditems'] = 'no items were selected.';
 $string['notteachererror'] = 'You must be a teacher to use this feature.';
 $string['pctoftotalgrade'] = '%% of total grade';
 $string['percent'] = 'Percent';
@@ -134,5 +138,4 @@ $string['weightedpctcontribution'] = 'weighted %% contribution';
 $string['writinggradebookinfo'] = 'Writing Gradebook settings';
 $string['yes'] = 'Yes';
 $string['yourgrade'] = 'Your grade';
-
 ?>
index e946e8528247e9ebdddafe079f3a68f043575497..fcfa70ec801ce9b003fac6facb0603bc56820adc 100644 (file)
         <FIELD NAME="itemnumber" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" ENUM="false" COMMENT="Can be used to distinguish multiple grades for an activity" PREVIOUS="iteminstance" NEXT="iteminfo"/>
         <FIELD NAME="iteminfo" TYPE="text" LENGTH="medium" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="Info and notes about this item XXX" PREVIOUS="itemnumber" NEXT="idnumber"/>
         <FIELD NAME="idnumber" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="Arbitrary idnumber provided by the module responsible" PREVIOUS="iteminfo" NEXT="gradetype"/>
-        <FIELD NAME="gradetype" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="0 = value, 1 = scale, 2 = text" PREVIOUS="idnumber" NEXT="grademax"/>
+        <FIELD NAME="gradetype" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="1" SEQUENCE="false" ENUM="false" COMMENT="0 = value, 1 = scale, 2 = text" PREVIOUS="idnumber" NEXT="grademax"/>
         <FIELD NAME="grademax" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="100" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="What is the maximum allowable grade?" PREVIOUS="gradetype" NEXT="grademin"/>
         <FIELD NAME="grademin" TYPE="number" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" DECIMALS="5" COMMENT="What is the minimum allowable grade?" 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 is it?" PREVIOUS="grademin" NEXT="outcomeid"/>
index 5a97b266eff3c85da8f6f31b1520b4ae322e4ee7..2a2281324803886951efd2ff247788629dec8a78 100644 (file)
@@ -1324,6 +1324,7 @@ function xmldb_main_upgrade($oldversion=0) {
         // Launch add field deleted
         $result = $result && add_field($table, $field); 
     }
+
     
     if ($result && $oldversion < 2007060500) {
 
@@ -1343,7 +1344,18 @@ function xmldb_main_upgrade($oldversion=0) {
     /// Launch add key usermodified
         $result = $result && add_key($table, $key);
     }
+    
+    if ($result && $oldversion < 2007060501) {
 
+        /// Changing the default of field gradetype on table grade_items to 1
+        $table = new XMLDBTable('grade_items');
+        $field = new XMLDBField('gradetype');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1', 'idnumber');
+        
+        /// Launch change of default for field gradetype
+        $result = $result && change_field_default($table, $field);
+    }
+    
     return $result; 
 }
 
index 2740e77efd385133023ffa41abf0c2d69db1fc0f..113fb3219fe668437d133252eea98bfe8c0f7e67 100644 (file)
@@ -534,9 +534,8 @@ class grade_category extends grade_object {
     }
 
     /**
-     * This method checks whether an existing child exists for this
-     * category. If the new child is of a different type, the method will return false (not allowed).
-     * Otherwise it will return true.
+     * Checks whether an existing child exists for this category. If the new child is of a 
+     * different type, the method will return false (not allowed). Otherwise it will return true.
      * @param object $child This must be a complete object, not a stdClass
      * @return boolean Success or failure
      */
@@ -721,7 +720,6 @@ class grade_category extends grade_object {
      * A number of constraints are necessary:
      *    - The children must all be of the same type and at the same level
      *    - The children cannot already be top categories
-     *    - The children cannot already have a top categorya
      *    - The children all belong to the same course
      * @param array $children An array of fully instantiated grade_category OR grade_item objects
      *
@@ -731,6 +729,11 @@ class grade_category extends grade_object {
     function set_as_parent($children) {
         global $CFG;
 
+        if (empty($children) || !is_array($children)) {
+            debugging("Passed an empty or non-array variable to grade_category::set_as_parent()");
+            return false;
+        }
+
         // Check type and sortorder of first child
         $first_child = current($children);
         $first_child_type = get_class($first_child);
@@ -748,13 +751,8 @@ class grade_category extends grade_object {
                 debugging("Violated constraint: Attempted to set a category over children which are already top categories.");
                 return false;
             }
-            if ($first_child_type == 'grade_item') {
-                $child->load_category();
-                if (!empty($child->category->parent)) {
-                    debugging("Violated constraint: Attempted to set a category over children that already have a top category.");
-                    return false;
-                }
-            } elseif ($first_child_type == 'grade_category') {
+            
+            if ($first_child_type == 'grade_category') {
                 if (!empty($child->parent)) {
                     debugging("Violated constraint: Attempted to set a category over children that already have a top category.");
                     return false; 
index 048f191dd9116f6d97766ba1b1a0cfc2327d8990..42a79c4047752f955405986b690ecfbd31ddea06 100644 (file)
@@ -373,6 +373,15 @@ class grade_item extends grade_object {
             }
         }
 
+        // If not set, generate an idnumber from itemmodule and iteminstance
+        if (empty($this->idnumber)) {
+            if (!empty($this->itemmodule) && !empty($this->iteminstance)) {
+                $this->idnumber = "$this->itemmodule.$this->iteminstance";
+            } else { // No itemmodule or iteminstance, generate a random idnumber
+                $this->idnumber = rand(0,9999999999); // TODO replace rand() with proper random generator 
+            }
+        }
+
         $result = parent::insert();
 
         // Notify parent category of need to update. Note that a grade_item may not have a categoryid.
index b8d3e10eba0c60a7105f16dd9c4d1219e41ff020..1671020c6726d77ee5365a99e67d9ec11ab39aed 100644 (file)
@@ -100,21 +100,29 @@ class grade_tree {
      * @param array $tree
      */
     function grade_tree($courseid=NULL, $include_grades=false, $tree=NULL) {
-        global $USER;
+        if (empty($courseid)) {
+            // empty object, do nothing
+        } else {
+            if ($courseid == 0) {
+                $courseid = null;
+            }
 
-        $this->courseid = $courseid;
-        $this->include_grades = $include_grades; 
-        $this->commonvars = "&amp;sesskey=$USER->sesskey&amp;courseid=$this->courseid";
+            global $USER;
 
-        if (!empty($tree)) {
-            $this->tree_array = $tree;
-        } else {
-            $this->tree_array = $this->get_tree();
-        }
-        
-        if (!empty($this->tree_array)) {
-            $this->first_sortorder = key($this->tree_array);
-            $this->renumber();
+            $this->courseid = $courseid;
+            $this->include_grades = $include_grades; 
+            $this->commonvars = "&amp;sesskey=$USER->sesskey&amp;courseid=$this->courseid";
+
+            if (!empty($tree)) {
+                $this->tree_array = $tree;
+            } else {
+                $this->tree_array = $this->get_tree();
+            }
+            
+            if (!empty($this->tree_array)) {
+                $this->first_sortorder = key($this->tree_array);
+                $this->renumber();
+            }
         }
     }
 
@@ -1002,10 +1010,6 @@ class grade_tree {
             foreach ($elements as $sortorder => $element) {
                 $object = $element['object'];
 
-                if (empty($element->next_sortorder)) {
-                    $element->next_sortorder = null;
-                }
-
                 $object_name = $object->get_name();
                 $object_class = get_class($object); 
                 $object_parent = $object->get_parent_id();
index 13d78158d9fd8f3090809ef1bcc602ecf86ea226..babdf95cd32b4c4b02136173a845fb82526b77c6 100644 (file)
@@ -40,9 +40,10 @@ define('GRADE_CHILDTYPE_ITEM', 0);
 define('GRADE_CHILDTYPE_CAT', 1);
 define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values
 define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values
-define('GRADE_TYPE_VALUE', 0);
-define('GRADE_TYPE_SCALE', 1);
-define('GRADE_TYPE_TEXT', 2);
+define('GRADE_TYPE_NONE', 0);
+define('GRADE_TYPE_VALUE', 1);
+define('GRADE_TYPE_SCALE', 2);
+define('GRADE_TYPE_TEXT', 3);
 
 require_once($CFG->libdir . '/grade/grade_category.php');
 require_once($CFG->libdir . '/grade/grade_item.php');
index 7fb900a87f5a7fe0439f4b6dea40a3c1ab3d8e9e..bd17bf93ff40192c5e01df85d4b6324267d05015 100755 (executable)
@@ -191,12 +191,12 @@ class grade_category_test extends gradelib_test {
         
         // Generate 3 random data sets
         $grade_sets = array();
-        
+  
         for ($i = 0; $i < 3; $i++) {
             for ($j = 0; $j < 200; $j++) {
                 $grade_sets[$i][] = $this->generate_random_raw_grade(new grade_item($this->grade_items[$i]), $j);
             }
-        }
+        } 
         
         $aggregated_grades = $category->aggregate_grades($grade_sets);
         $this->assertEqual(200, count($aggregated_grades)); 
@@ -244,10 +244,12 @@ class grade_category_test extends gradelib_test {
         $this->assertFalse($grade_category->set_as_parent(array($child1)));
         $CFG->debug = $debuglevel;
 
-        // 3. Child already has a top category
+        // 3. Children belong to different courses
         $child1 = new grade_item($this->grade_items[0]);
+        $child2 = new grade_item($this->grade_items[1]);
+        $child2->courseid = 543;
         $CFG->debug = 2;
-        $this->assertFalse($grade_category->set_as_parent(array($child1)));
+        $this->assertFalse($grade_category->set_as_parent(array($child1, $child2)));
         $CFG->debug = $debuglevel;
 
         // Now test setting parent correctly
index 0e88b375d4f8c9d0ff6f7c3fe9627a081fd953bd..11f36df16fea7818daca000c4ce2057e96c43bb3 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 = 2007060500;  // YYYYMMDD = date
+   $version = 2007060501;  // YYYYMMDD = date
                            //       XY = increments within a single day
 
    $release = '1.9 dev';    // Human-friendly version name