From 88e794d6c8d4c4e1f1610669afe40dcf39b31dd4 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 31 May 2007 08:57:05 +0000 Subject: [PATCH] MDL-9628 Move work on the category edit page --- grade/report/grader/category.php | 14 +--- lib/grade/grade_category.php | 26 +++++- lib/grade/grade_item.php | 23 +++++ lib/grade/grade_tree.php | 140 +++++++++++++++++-------------- theme/standard/styles_layout.css | 12 +-- 5 files changed, 134 insertions(+), 81 deletions(-) diff --git a/grade/report/grader/category.php b/grade/report/grader/category.php index 851350035d..7f178b03d7 100644 --- a/grade/report/grader/category.php +++ b/grade/report/grader/category.php @@ -34,16 +34,13 @@ $param->movedown = optional_param('movedown', 0, PARAM_INT); $param->source = optional_param('source', 0, PARAM_INT); $param->action = optional_param('action', 0, PARAM_ALPHA); $param->move = optional_param('move', 0, PARAM_INT); +$param->type = optional_param('type', 0, PARAM_ALPHA); $tree = new grade_tree(641); $select_source = false; if (!empty($param->action)) { - if (empty($param->source)) { - $select_source = true; - } else { - print_heading("Select the destination for the selected element."); - } + print_heading("Select the destination for the selected element."); } elseif (!empty($param->source)) { if (!empty($param->moveup)) { $tree->move_element($param->source, $param->moveup); @@ -61,12 +58,7 @@ if ($select_source) { print_heading("Select an element to move"); } -echo $tree->get_edit_tree($select_source, 1, null, $param->source, $param->action); - -echo '
' . "\n"; -echo '' . "\n"; -echo '' . "\n"; -echo '
'; +echo $tree->get_edit_tree(1, null, $param->source, $param->action, $param->type); print_footer(); ?> diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index e104d89ec2..9dd1d96997 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -56,6 +56,12 @@ class grade_category extends grade_object { */ var $parent_category; + /** + * A grade_category object this category used to belong to before getting updated. Will be deleted shortly. + * @var object $old_parent + */ + var $old_parent; + /** * The number of parents this category has. * @var int $depth @@ -841,7 +847,13 @@ 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(); } /** @@ -870,6 +882,18 @@ class grade_category extends grade_object { function set_sortorder($sortorder) { $this->sortorder = $sortorder; } - + + /** + * If the old parent is set (after an update), this checks and returns whether it has any children. Important for + * deleting childless categories. + * @return boolean + */ + function is_old_parent_childless() { + if (!empty($this->old_parent)) { + return !$this->old_parent->has_children(); + } else { + return false; + } + } } ?> diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 5e9d76dd40..f7343cbee0 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -60,6 +60,12 @@ class grade_item extends grade_object { */ var $category; + /** + * A grade_category object this item used to belong to before getting updated. Will be deleted shortly. + * @var object $old_parent + */ + var $old_parent; + /** * The name of this grade_item (pushed by the module). * @var string $itemname @@ -965,7 +971,24 @@ 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; } + + /** + * If the old parent is set (after an update), this checks and returns whether it has any children. Important for + * deleting childless categories. + * @return boolean + */ + function is_old_parent_childless() { + if (!empty($this->old_parent)) { + return !$this->old_parent->has_children(); + } else { + return false; + } + } } ?> diff --git a/lib/grade/grade_tree.php b/lib/grade/grade_tree.php index b38af6add5..fda1951de8 100644 --- a/lib/grade/grade_tree.php +++ b/lib/grade/grade_tree.php @@ -533,7 +533,7 @@ class grade_tree { */ function include_fillers($tree, $object=NULL) { if (empty($this->fillers)) { - return null; + return $tree; } // Look at the current key of the fillers array. It is a sortorder. @@ -649,8 +649,8 @@ class grade_tree { // Get all top categories $query = "SELECT $category_table.*, sortorder FROM $category_table, $items_table - WHERE iteminstance = $category_table.id $catconstraint ORDER BY sortorder"; - + WHERE iteminstance = $category_table.id AND itemtype = 'category' $catconstraint ORDER BY sortorder"; + $topcats = get_records_sql($query); if (empty($topcats)) { @@ -662,7 +662,7 @@ class grade_tree { // If any of these categories has grade_items as children, create a topcategory filler with colspan=count(children) $topcats = $this->add_fillers($topcats); - + foreach ($topcats as $topcatid => $topcat) { // Check the fillers array, see if one must be inserted before this topcat @@ -714,7 +714,7 @@ class grade_tree { $topcat->sortorder = $sortorder; $tree[$topcat->sortorder] = array('object' => $topcat, 'children' => $subcattree); } - + // If there are still grade_items or grade_categories without a top category, add another filler if (!empty($this->fillers)) { ksort($this->fillers); @@ -773,7 +773,11 @@ class grade_tree { $children_for_tree[$itemid] = array('object' => $item, 'finalgrades' => $finals); } - + + if (empty($object->childrencount)) { + $object->childrencount = 1; + } + $filler_array = array('object' => 'filler', 'colspan' => $object->childrencount, 'children' => array(0 => array('object' => $object, 'children' => $children_for_tree))); } @@ -847,6 +851,7 @@ class grade_tree { /** * Using $this->tree_array, builds $this->tree_filled, which is the same array but with fake categories as * fillers. These are used by display_grades, to print out empty cells over orphan grade_items and grade_categories. + * Recursive method * @return boolean Success or Failure. */ function build_tree_filled() { @@ -856,7 +861,8 @@ class grade_tree { } $this->tree_filled = array(); - + + // Detect any category that is now child-less and delete it foreach ($this->tree_array as $level1order => $level1) { if ($this->get_element_type($level1) == 'item' || $this->get_element_type($level1) == 'subcat') { $this->tree_filled[$level1order] = $this->get_filler($level1['object']); @@ -876,7 +882,16 @@ class grade_tree { * @return boolean Success or Failure */ function update_db() { - // Perform deletions first + // Updates + foreach ($this->need_update as $object) { + if (!$object->update()) { + debugging("Could not update the object in DB."); + } elseif ($object->is_old_parent_childless()) { + $this->need_delete[$object->old_parent->id] = $object->old_parent; + } + } + + // Deletions foreach ($this->need_delete as $id => $object) { // If an item is both in the delete AND insert arrays, it must be an existing object that only needs updating, so ignore it. if (empty($this->need_insert[$id])) { @@ -886,6 +901,7 @@ class grade_tree { } } + // Insertions foreach ($this->need_insert as $id => $object) { if (empty($this->need_delete[$id])) { if (!$object->insert()) { @@ -894,45 +910,48 @@ class grade_tree { } } + $this->need_update = array(); $this->need_delete = array(); $this->need_insert = array(); - // The objects are updated - foreach ($this->need_update as $object) { - if (!$object->update()) { - debugging("Could not update the object in DB."); - } - } - - $this->need_update = array(); $this->reset_first_sortorder(); $this->renumber(); } /** * Returns a HTML list with sorting arrows and insert boxes. This is a recursive method. - * @param boolean $select_source Whether or not to display each element as a link to be selected as a source for an action * @param int $level The level of recursion * @param array $elements The elements to display in a list. Defaults to this->tree_array * @param int $source_sortorder A source sortorder, given when an element needs to be moved or inserted. * @param string $action 'move' or 'insert' + * @param string $source_type 'topcat', 'subcat' or 'item' * @return string HTML code */ - function get_edit_tree($select_source=false, $level=1, $elements=NULL, $source_sortorder=NULL, $action=NULL) { + function get_edit_tree($level=1, $elements=NULL, $source_sortorder=NULL, $action=NULL, $source_type=NULL) { if (empty($this->tree_array)) { return null; } else { global $USER; global $CFG; + $strmove = get_string("move"); $strmoveup = get_string("moveup"); $strmovedown = get_string("movedown"); + $strmovehere = get_string("movehere"); + $strcancel = get_string("cancel"); - $sesskey_input = ''; - $courseid_input = ''; + $list = ''; + + $commonvars = "&sesskey=$USER->sesskey&courseid=$this->courseid"; if (empty($elements)) { - $list = '