]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9629 Beginning the edit category page. Also added dmllib::sql_max($field) and...
authornicolasconnault <nicolasconnault>
Thu, 24 May 2007 08:50:01 +0000 (08:50 +0000)
committernicolasconnault <nicolasconnault>
Thu, 24 May 2007 08:50:01 +0000 (08:50 +0000)
lib/dmllib.php
lib/grade/grade_category.php
lib/grade/grade_item.php
lib/grade/grade_outcome.php
lib/grade/grade_tree.php
lib/simpletest/grade/simpletest/testgradetree.php

index 7a42dbd987a93d8b8a80f3d139b9f7369a1a9e11..27b74cb296aad98356c9d098b688200c68175744 100644 (file)
@@ -1633,6 +1633,22 @@ function sql_ilike() {
 }
 
 
+/**
+ * Returns the proper SQL to do MAX
+ *
+ * @uses $CFG
+ * @param string $field
+ * @return string
+ */
+function sql_max($field) {
+    global $CFG;
+
+    switch ($CFG->dbfamily) {
+        default:
+             return "MAX($field)";
+    }
+}
+
 /**
  * Returns the proper SQL (for the dbms in use) to concatenate $firstname and $lastname
  *
index 1711b7ef968ee962983e684684a23b60af41a93b..99f7f0dd092a003c53a096776208e4e9a81eba20 100644 (file)
@@ -793,5 +793,14 @@ class grade_category extends grade_object {
             return true;
         }
     }
+
+    /**
+     * Returns the most descriptive field for this object. This is a standard method used 
+     * when we do not know the exact type of an object.
+     * @return string name
+     */
+    function get_name() {
+        return $this->fullname;
+    }
 } 
 ?>
index 06e4aedd346ce3892869fb8a031d171d4cc4b402..dd640450e3e22390230acd766694f206f1a71b9b 100644 (file)
@@ -459,6 +459,8 @@ class grade_item extends grade_object {
      * @return int ID of the new grade_item record.
      */
     function insert() {
+        global $CFG;
+
         // Retrieve scale and infer grademax from it
         if (!empty($this->scaleid)) {
             $this->load_scale();
@@ -467,7 +469,16 @@ class grade_item extends grade_object {
             $this->grademin = 0;
             $this->gradetype = GRADE_TYPE_SCALE;
         }
-        
+       
+        // If sortorder not given, extrapolate one
+        if (empty($this->sortorder)) {
+            $query = 'SELECT ' . sql_max('sortorder') . " FROM {$CFG->prefix}grade_items";
+            $last_sortorder = execute_sql($query, false);
+            if (!empty($last_sortorder)) {
+                $this->sortorder = $last_sortorder + 1;
+            }
+        }
+
         $result = parent::insert();
 
         // Notify parent category of need to update. Note that a grade_item may not have a categoryid.
@@ -894,5 +905,13 @@ class grade_item extends grade_object {
         return $this->sortorder;
     }
 
+    /**
+     * Returns the most descriptive field for this object. This is a standard method used 
+     * when we do not know the exact type of an object.
+     * @return string name
+     */
+    function get_name() {
+        return $this->itemname;
+    } 
 }
 ?>
index 707351935f0fbe45c44a2abc6de4f4624e7a0baa..6e90a0e8ff6560ffe88ff8250fd42d361ced14fe 100644 (file)
@@ -120,5 +120,14 @@ class grade_outcome extends grade_object {
             return false;
         }
     }
+
+    /**
+     * Returns the most descriptive field for this object. This is a standard method used 
+     * when we do not know the exact type of an object.
+     * @return string name
+     */
+    function get_name() {
+        return $this->shortname;
+    } 
 }
 ?>
index 5a26316945db282a620e7a9573fb9489e3e13e43..5cee9d2764f790cc93eab8624ebdbb6e3de182a6 100644 (file)
@@ -346,7 +346,7 @@ class grade_tree {
         if (empty($this->first_sortorder)) { 
             $this->reset_first_sortorder();
         } 
-
+        
         // Locate the position of the source element in the tree
         $source = $this->locate_element($source_sortorder);
 
@@ -372,6 +372,7 @@ class grade_tree {
         }
         reset($this->tree_array);
         $this->first_sortorder = key($this->tree_array);
+        
         return $this->first_sortorder;
     }
 
@@ -554,8 +555,12 @@ class grade_tree {
                 unset($topcats[$topcatid]);
             }
         }
-        
+            
+        $last_topsortorder = null;
+
         foreach ($topcats as $topcatid => $topcat) {
+            $last_subsortorder = null;
+
             // Check the fillers array, see if one must be inserted before this topcat
             if (key($fillers) < $topcat->sortorder) {
                 $sortorder = key($fillers);
@@ -568,7 +573,29 @@ class grade_tree {
                 if (get_class($object) == 'grade_category') {
                     $children = $object->get_children(1);
                     unset($object->children);
-                    $element['children'] = $children;
+                    $last_itemsortorder = null;
+                    $itemtree = array();
+
+                    foreach ($children as $element) { 
+                        $finals = array();
+
+                        if ($this->include_grades) {
+                            $final = new grade_grades_final();
+                            $final->itemid = $element['object']->id;
+                            $finals = $final->fetch_all_using_this();
+                        }
+
+                        $element['object']->previous_sortorder = $last_itemsortorder;
+                        $itemtree[$element['object']->sortorder] = array('object' => $element['object'], 'finalgrades' => $finals);
+                        
+                        if (!empty($itemtree[$last_itemsortorder])) {
+                            $itemtree[$last_itemsortorder]['object']->next_sortorder = $element['object']->sortorder;
+                        }
+
+                        $last_itemsortorder = $element['object']->sortorder;
+                    }
+
+                    $element['children'] = $itemtree;
                 } elseif (get_class($object) == 'grade_item' && $this->include_grades) {
                     $final_grades = $object->get_final();
                     unset($object->grade_grades_final);
@@ -576,8 +603,15 @@ class grade_tree {
                 }
 
                 $object->sortorder = $sortorder;
+                $object->previous_sortorder = $last_topsortorder;
                 $element['object'] = $object;
                 $tree[$sortorder] = $element;
+                
+                if (!empty($tree[$last_topsortorder])) {
+                    $tree[$last_topsortorder]['object']->next_sortorder = $sortorder;
+                }
+                
+                $last_topsortorder = $sortorder;
             }
 
             $query = "SELECT $category_table.*, sortorder FROM $category_table, $items_table 
@@ -592,6 +626,7 @@ class grade_tree {
             foreach ($subcats as $subcatid => $subcat) {
                 $itemtree = array();
                 $items = get_records('grade_items', 'categoryid', $subcatid, 'sortorder');
+                $last_itemsortorder = null;
                 
                 if (empty($items)) {
                     continue;
@@ -610,21 +645,42 @@ class grade_tree {
                     $item = new grade_item($item);
                     $item->sortorder = $sortorder;
 
+                    $item->previous_sortorder = $last_itemsortorder;
                     $itemtree[$item->sortorder] = array('object' => $item, 'finalgrades' => $finals);
+                    
+                    if (!empty($itemtree[$last_itemsortorder])) {
+                        $itemtree[$last_itemsortorder]['object']->next_sortorder = $item->sortorder;
+                    }
+
+                    $last_itemsortorder = $item->sortorder;
                 }
                 
                 $sortorder = $subcat->sortorder;
                 $subcat = new grade_category($subcat, false);
                 $subcat->sortorder = $sortorder;
+                $subcat->previous_sortorder = $last_subsortorder;
                 $subcattree[$subcat->sortorder] = array('object' => $subcat, 'children' => $itemtree);
+                
+                if (!empty($subcattree[$last_subsortorder])) {
+                    $subcattree[$last_subsortorder]['object']->next_sortorder = $subcat->sortorder;
+                }
+
+                $last_subsortorder = $subcat->sortorder;
             }
             
             $sortorder = $topcat->sortorder;
             $topcat = new grade_category($topcat, false);
             $topcat->sortorder = $sortorder;
-
+            
+            $topcat->previous_sortorder = $last_topsortorder;
             $tree[$topcat->sortorder] = array('object' => $topcat, 'children' => $subcattree);
             $this->tree_filled[$topcat->sortorder] = array('object' => $topcat, 'children' => $subcattree);
+            
+            if (!empty($topcattree[$last_topsortorder])) {
+                $topcattree[$last_topsortorder]['object']->next_sortorder = $topcat->sortorder;
+            }
+
+            $last_topsortorder = $topcat->sortorder;
         }
 
         // If there are still grade_items or grade_categories without a top category, add another filler
@@ -635,7 +691,29 @@ class grade_tree {
                 if (get_class($object) == 'grade_category') {
                     $children = $object->get_children(1);
                     unset($object->children);
-                    $element['children'] = $children;
+                    $last_itemsortorder = null;
+                    $itemtree = array();
+
+                    foreach ($children as $element) { 
+                        $finals = array();
+
+                        if ($this->include_grades) {
+                            $final = new grade_grades_final();
+                            $final->itemid = $element['object']->id;
+                            $finals = $final->fetch_all_using_this();
+                        }
+
+                        $element['object']->previous_sortorder = $last_itemsortorder;
+                        $itemtree[$element['object']->sortorder] = array('object' => $element['object'], 'finalgrades' => $finals);
+                        
+                        if (!empty($itemtree[$last_itemsortorder])) {
+                            $itemtree[$last_itemsortorder]['object']->next_sortorder = $element['object']->sortorder;
+                        }
+
+                        $last_itemsortorder = $element['object']->sortorder;
+                    }
+
+                    $element['children'] = $itemtree;
                 } elseif (get_class($object) == 'grade_item' && $this->include_grades) {
                     $final_grades = $object->get_final();
                     unset($object->grade_grades_final);
@@ -643,8 +721,15 @@ class grade_tree {
                 }
 
                 $object->sortorder = $sortorder;
+                $object->previous_sortorder = $last_topsortorder;
                 $element['object'] = $object;
                 $tree[$sortorder] = $element;
+                
+                if (!empty($tree[$last_topsortorder])) {
+                    $tree[$last_topsortorder]['object']->next_sortorder = $sortorder;
+                }
+                
+                $last_topsortorder = $sortorder;
             }
         }
         
@@ -829,4 +914,77 @@ class grade_tree {
 
         $this->need_update = array();
     }
+
+    /**
+     * Returns a HTML list with sorting arrows and insert boxes. This is a recursive method.
+     * @return string HTML code
+     */
+    function get_edit_tree($level=1, $elements=null) {
+        if (empty($this->tree_array)) {
+            return null;
+        } else {
+            global $USER;
+            global $CFG;
+
+            $strmoveup   = get_string("moveup");
+            $strmovedown = get_string("movedown");
+            
+            if (empty($elements)) {
+                $list = '<ul id="grade_edit_tree">';
+                $elements = $this->tree_array;
+            } else {
+                $list = '<ul class="level' . $level . 'children">';
+            } 
+            
+            $first = true;
+            $count = 1;
+            $last = false;
+            
+            if (count($elements) == 1) {
+                $last = true;
+            }
+
+            foreach ($elements as $sortorder => $element) {
+                $list .= '<li class="level' . $level . 'element sortorder' 
+                      . $element['object']->get_sortorder() . '">' 
+                      . $element['object']->get_name();
+                
+                if (!$first) {
+                    $list .= '<a title="'.$strmoveup.'" href="category.php?courseid='.$this->courseid
+                          . '&amp;source=' . $sortorder . '&amp;moveup=' . $element['object']->previous_sortorder 
+                          . '&amp;sesskey='.$USER->sesskey.'">'
+                          . '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
+                } else {
+                    $list .= '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
+                }
+
+                if (!$last) {
+                    $list .= '<a title="'.$strmovedown.'" href="category.php?courseid='.$this->courseid
+                          . '&amp;source=' . $sortorder . '&amp;movedown=' . $element['object']->next_sortorder 
+                          . '&amp;sesskey='.$USER->sesskey.'">'
+                          . '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
+                } else {
+                    $list .= '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
+                }
+                
+                if (!empty($element['children'])) {
+                    $list .= $this->get_edit_tree($level + 1, $element['children']);
+                }
+                
+                $list .= '</li>';
+
+                $first = false;
+                $count++;
+                if ($count == count($elements)) {
+                    $last = true;
+                } 
+            }
+                    
+            $list .= '</ul>';
+
+            return $list;
+        }
+
+        return false;
+    }
 }
index 2959ad28a45a337b064876a8e15c33d29f9b27ff..2821f9b3417c4655df87a1bc23052982918ccd0f 100644 (file)
@@ -165,7 +165,7 @@ class grade_tree_test extends gradelib_test {
     function test_grade_tree_get_tree() {
         $tree = new grade_tree($this->courseid, true);
         $this->assertEqual(58, count($tree->tree_filled, COUNT_RECURSIVE));
-        $this->assertEqual(44, count($tree->tree_array, COUNT_RECURSIVE));
+        $this->assertEqual(48, count($tree->tree_array, COUNT_RECURSIVE));
     }
     
     function test_grade_tree_renumber() {
@@ -262,6 +262,11 @@ class grade_tree_test extends gradelib_test {
 
     function test_grade_tree_load_without_finals() {
         $tree = new grade_tree($this->courseid);
-        $this->assertEqual(27, count($tree->tree_array, COUNT_RECURSIVE));
+        $this->assertEqual(29, count($tree->tree_array, COUNT_RECURSIVE));
+    }
+
+    function test_grade_tree_display_edit_tree() {
+        $tree = new grade_tree($this->courseid);
+        echo $tree->get_edit_tree(); 
     }
 }