]> git.mjollnir.org Git - moodle.git/commitdiff
grade_item idnumber now self generated if needed.
authornicolasconnault <nicolasconnault>
Thu, 7 Jun 2007 12:45:24 +0000 (12:45 +0000)
committernicolasconnault <nicolasconnault>
Thu, 7 Jun 2007 12:45:24 +0000 (12:45 +0000)
deleted flag implemented in grade_item::update method
added GRADE_TYPE_NONE to gradelib and updated grade_item object
Implemented multiple items (generation of itemnumber when not explicitly given)

lib/grade/grade_category.php
lib/grade/grade_item.php
lib/grade/grade_object.php
lib/grade/grade_tree.php
lib/simpletest/grade/simpletest/testgradeitem.php
lib/simpletest/testgradelib.php

index 113fb3219fe668437d133252eea98bfe8c0f7e67..21eba83a877c0a16eb86f5676e06daf37ee8cf05 100644 (file)
@@ -551,6 +551,19 @@ class grade_category extends grade_object {
         }
     }
 
+    /**
+     * Disassociates this category from its category parent(s). The object is then updated in DB.
+     * @return boolean Success or Failure
+     */
+    function divorce_parent() {
+        $this->old_parent = $this->get_parent_category(); 
+        $this->parent = null;
+        $this->parent_category = null;
+        $this->depth = 1;
+        $this->path = '/' . $this->id;
+        return $this->update();        
+    }
+
     /**
      * Looks at a path string (e.g. /2/45/56) and returns the depth level represented by this path (in this example, 3).
      * If no string is given, it looks at the obect's path and assigns the resulting depth to its $depth variable.
@@ -770,19 +783,12 @@ class grade_category extends grade_object {
 
         // We passed all the checks, time to set the category as a parent.
         foreach ($children as $child) {
-            if ($first_child_type == 'grade_item') {
-                $child->categoryid = $this->id;
-                if (!$child->update()) {
-                    debugging("Could not set this category as a parent for one of its child grade_items, DB operation failed.");
-                    return false;
-                }
-            } elseif ($first_child_type == 'grade_category') {
-                $child->parent = $this->id;
-                if (!$child->update()) {
-                    debugging("Could not set this category as a parent for one of its child categories, DB operation failed.");
-                    return false;
-                }
-            }
+            $child->divorce_parent();
+            $child->set_parent_id($this->id);
+            if (!$child->update()) {
+                debugging("Could not set this category as a parent for one of its children, DB operation failed.");
+                return false;
+            } 
         }
 
         // TODO Assign correct sortorders to the newly assigned children and parent. Simply add 1 to all of them!
@@ -843,10 +849,6 @@ class grade_category extends grade_object {
      * @param id $parentid
      */
     function set_parent_id($parentid) {
-        if ($this->parent != $parentid) {
-            $this->old_parent = $this->get_parent_category();
-        }
-
         $this->parent = $parentid;
         $this->path = grade_category::build_path($this);
         $this->depth = $this->get_depth_from_path();
index 200741a42a4afaee3133bd2adfca581618c7c9f2..3c7f4b99201680204efec8475379e1db894aa8a9 100644 (file)
@@ -229,6 +229,11 @@ class grade_item extends grade_object {
      * In addition to update() as defined in grade_object, handle the grade_outcome and grade_scale objects.
      */
     function update() {
+        // If item is flagged as deleted, only update that flag in DB. The other changes are ignored.
+        if (!empty($this->deleted) && $this->deleted) {
+            return set_field('grade_items', 'deleted', 1, 'id', $this->id);
+        }
+
         if (!empty($this->outcome->id)) {
             $this->outcomeid = $this->outcome->id;
         }
@@ -397,6 +402,22 @@ class grade_item extends grade_object {
                 $this->idnumber = rand(0,9999999999); // TODO replace rand() with proper random generator 
             }
         }
+        
+        // If a grade_item already exists with these itemtype, itemmodule and iteminstance
+        // but not itemnumber, generate an itemnumber.  
+        if (empty($this->itemnumber) && !empty($this->itemtype) && !empty($this->itemmodule) && !empty($this->iteminstance)) {
+            $existing_item = get_record('grade_items', 
+                'iteminstance', $this->iteminstance, 
+                'itemmodule', $this->itemmodule, 
+                'itemtype', $this->itemtype);
+
+            if (empty($existing_item->itemnumber)) {
+                $existing_item->itemnumber = 0;
+            }
+
+            $this->itemnumber = $existing_item->itemnumber + 1;
+        }
+
 
         $result = parent::insert();
 
@@ -708,7 +729,18 @@ class grade_item extends grade_object {
 
         return $result;
     }
-    
+
+    /**
+     * Disassociates this item from its category parent(s). The object is then updated in DB.
+     * @return boolean Success or Failure
+     */
+    function divorce_parent() {
+        $this->old_parent = $this->get_category();
+        $this->category = null;
+        $this->categoryid = null;
+        return $this->update();        
+    }
+
     /**
      * Instantiates a grade_scale object whose data is retrieved from the DB, 
      * if this item's scaleid variable is set.
@@ -808,8 +840,11 @@ class grade_item extends grade_object {
         $standardised_finals = array();
 
         $final_grades = $this->load_final(true);
-        foreach ($final_grades as $userid => $final) {
-            $standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1);
+        
+        if (!empty($final_grades)) {
+            foreach ($final_grades as $userid => $final) {
+                $standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1);
+            }
         }
 
         return $standardised_finals;
@@ -1002,10 +1037,6 @@ class grade_item extends grade_object {
      * @param int $parentid
      */
     function set_parent_id($parentid) {
-        if ($this->categoryid != $parentid) {
-            $this->old_parent = $this->get_category();
-        }
-
         $this->categoryid = $parentid;
     }
     
index dee86f744a6ddb2ebed0b6ca632a17e256aa836e..5cd82e1618edcc724ee59e4f2e5e35b2a4d24637 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.com                                            //
 //                                                                       //
-// Copyright (C) 2001-2003  Martin Dougiamas  http://dougiamas.com       //
+// Copyright (C) 2001-2007  Martin Dougiamas  http://dougiamas.com       //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
 // it under the terms of the GNU General Public License as published by  //
@@ -144,7 +144,7 @@ class grade_object {
             $class = get_class($this);
             $object = new $class(array('id' => $this->id));
             foreach ($object as $var => $val) {
-                if ($this->$var != $val) {
+                if (!in_array($var, $this->nonfields) && $this->$var != $val) {
                     $this->$var = $val;
                 }
             }
index 1671020c6726d77ee5365a99e67d9ec11ab39aed..7c79d1803c38c87e08cfb110db1b81a7afb3e959 100644 (file)
@@ -95,12 +95,12 @@ class grade_tree {
      * objects for the given courseid or the entire site if no courseid given. Full objects are instantiated
      * by default, but this can be switched off. The tree is indexed by sortorder, to facilitate CRUD operations
      * and renumbering.
-     * @param int $courseid
+     * @param int $courseid If null, a blank object is instantiated. If 0, all courses are retrieved in the entire site (can be very slow!)
      * @param boolean $include_grades
      * @param array $tree
      */
     function grade_tree($courseid=NULL, $include_grades=false, $tree=NULL) {
-        if (empty($courseid)) {
+        if (is_null($courseid)) {
             // empty object, do nothing
         } else {
             if ($courseid == 0) {
index aa8ae68bd44cb7a7d833d7462e0e118180b8dddd..270e2ac15e0028252a4e80c5c8cffd777075b857 100755 (executable)
@@ -82,6 +82,35 @@ class grade_item_test extends gradelib_test {
         $this->assertEqual(11, $grade_item->sortorder);\r
     }\r
 \r
+    function test_grade_item_generate_itemnumber() {\r
+        $grade_item = new grade_item($this->grade_items[0]);\r
+        $copy_grade_item = fullclone($grade_item);\r
+        $copy_grade_item->itemnumber = null;\r
+        $result_id = $copy_grade_item->insert();\r
+        $this->assertEqual(1, $copy_grade_item->itemnumber);\r
+\r
+    }\r
+\r
+    function test_grade_item_generate_idnumber() {\r
+\r
+    }\r
+\r
+    function test_grade_item_update_when_flagged_as_deleted() {\r
+        \r
+    }\r
+\r
+    function test_grade_item_update_guess_outcomeid() {\r
+\r
+    }\r
+\r
+    function test_grade_item_update_default_gradetype() {\r
+\r
+    }\r
+\r
+    function test_grade_item_update_guess_scaleid() {\r
+\r
+    }\r
+\r
     function test_grade_item_delete() {\r
         $grade_item = new grade_item($this->grade_items[0]);\r
         $this->assertTrue(method_exists($grade_item, 'delete'));\r
index 612762d84b998da52b2be3eef77bc914e36525c7..d5d4c44d9d6acb517ecf578528198da9ac9a36d0 100644 (file)
@@ -102,7 +102,6 @@ class gradelib_test extends UnitTestCase {
         $table = new XMLDBTable('grade_items');
         
         if (!table_exists($table)) {
-            /// Adding fields to table grade_items
             $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
             $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
             $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
@@ -113,7 +112,7 @@ class gradelib_test extends UnitTestCase {
             $table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
             $table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null);
             $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
-            $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0');
+            $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
             $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);
@@ -124,11 +123,10 @@ class gradelib_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('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);
             $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
-
-            /// Adding keys to table grade_items
             $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
             $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
             $table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id'));
@@ -144,7 +142,6 @@ class gradelib_test extends UnitTestCase {
         
         if ($result && !table_exists($table)) {
 
-            /// Adding fields to table grade_categories
             $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
             $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
             $table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
@@ -154,11 +151,8 @@ class gradelib_test extends UnitTestCase {
             $table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('droplow', 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('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
             $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
-
-            /// Adding keys to table grade_categories
             $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
             $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
             $table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id'));
@@ -272,7 +266,6 @@ class gradelib_test extends UnitTestCase {
 
         if ($result && !table_exists($table)) {
 
-            /// Adding fields to table grade_grades_final
             $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);
@@ -283,8 +276,6 @@ class gradelib_test extends UnitTestCase {
             $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_final
             $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'));
@@ -299,7 +290,6 @@ class gradelib_test extends UnitTestCase {
 
         if ($result && !table_exists($table)) {
 
-            /// 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);
@@ -310,8 +300,6 @@ class gradelib_test extends UnitTestCase {
             $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'));
@@ -327,7 +315,6 @@ class gradelib_test extends UnitTestCase {
 
         if ($result && !table_exists($table)) {
 
-            /// Adding fields to table scale
             $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
             $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
             $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
@@ -335,11 +322,7 @@ class gradelib_test extends UnitTestCase {
             $table->addFieldInfo('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
             $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
             $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
-
-            /// Adding keys to table scale
             $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
-
-            /// Adding indexes to table scale
             $table->addIndexInfo('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
 
             /// Launch create table for scale
@@ -643,7 +626,7 @@ class gradelib_test extends UnitTestCase {
         $grade_item->itemname = 'unittestorphangradeitem1';
         $grade_item->itemtype = 'mod';
         $grade_item->itemmodule = 'quiz';
-        $grade_item->iteminstance = 1;
+        $grade_item->iteminstance = 5;
         $grade_item->gradetype = GRADE_TYPE_VALUE;
         $grade_item->grademin = 10;
         $grade_item->grademax = 120;
@@ -665,7 +648,7 @@ class gradelib_test extends UnitTestCase {
         $grade_item->itemname = 'singleparentitem1';
         $grade_item->itemtype = 'mod';
         $grade_item->itemmodule = 'forum';
-        $grade_item->iteminstance = 3;
+        $grade_item->iteminstance = 7;
         $grade_item->gradetype = GRADE_TYPE_SCALE;
         $grade_item->scaleid = $this->scale[0]->id;
         $grade_item->grademin = 0;
@@ -687,7 +670,7 @@ class gradelib_test extends UnitTestCase {
         $grade_item->itemname = 'singleparentitem2';
         $grade_item->itemtype = 'mod';
         $grade_item->itemmodule = 'forum';
-        $grade_item->iteminstance = 3;
+        $grade_item->iteminstance = 9;
         $grade_item->gradetype = GRADE_TYPE_VALUE;
         $grade_item->grademin = 0;
         $grade_item->grademax = 100;