Fix course categories count when adding/removing subcategories

1.9.x
Yannick Warnier 11 years ago
parent 2410962062
commit 4871f47658
  1. 87
      main/inc/lib/course_category.lib.php

@ -1,6 +1,11 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
/**
* Returns whether we are in a mode where multiple URLs are configured to work
* with course categories
* @return bool
*/
function isMultipleUrlSupport() function isMultipleUrlSupport()
{ {
global $_configuration; global $_configuration;
@ -11,15 +16,15 @@ function isMultipleUrlSupport()
} }
/** /**
* @param int $categoryId * Returns the category fields from the database from an int ID
* * @param int $categoryId The category ID
* @return array * @return array
*/ */
function getCategoryById($categoryId) function getCategoryById($categoryId)
{ {
$tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
$categoryId = intval($categoryId); $categoryId = intval($categoryId);
$sql = "SELECT * FROM $tbl_category WHERE id = '$categoryId'"; $sql = "SELECT * FROM $tbl_category WHERE id = $categoryId";
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result)) { if (Database::num_rows($result)) {
return Database::fetch_array($result, 'ASSOC'); return Database::fetch_array($result, 'ASSOC');
@ -28,8 +33,8 @@ function getCategoryById($categoryId)
} }
/** /**
* @param string $category * Get category details from a simple category code
* * @param string $category The literal category code
* @return array * @return array
*/ */
function getCategory($category) function getCategory($category)
@ -118,7 +123,7 @@ function addNode($code, $name, $canHaveCourses, $parent_id)
Database::query($sql); Database::query($sql);
$categoryId = Database::insert_id(); $categoryId = Database::insert_id();
updateCategoryChildren($parent_id); updateParentCategoryChildrenCount($parent_id, 1);
if (isMultipleUrlSupport()) { if (isMultipleUrlSupport()) {
addToUrl($categoryId); addToUrl($categoryId);
@ -128,21 +133,30 @@ function addNode($code, $name, $canHaveCourses, $parent_id)
} }
/** /**
* @param string $category * Recursive function that updates the count of children in the parent
* @param string $categoryId Category ID
* @param int $delta The number to add or delete (1 to add one, -1 to remove one)
*/ */
function updateCategoryChildren($category) function updateParentCategoryChildrenCount($categoryId, $delta = 1)
{ {
$tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
$category = Database::escape_string($category); $categoryId = Database::escape_string($categoryId);
$result = Database::query("SELECT parent_id FROM $tbl_category WHERE code='$category'"); $delta = intval($delta);
// First get to the highest level possible in the tree
if ($row = Database::fetch_array($result)) { $result = Database::query("SELECT parent_id FROM $tbl_category WHERE code = '$categoryId'");
updateCategoryChildren($row['parent_id']); $row = Database::fetch_array($result);
if ($row !== false and $row['parent_id'] != 0) {
// if a parent was found, enter there to see if he's got one more parent
updateParentCategoryChildrenCount($row['parent_id'], $delta);
}
// Now we're at the top, get back down to update each child
//$children_count = courseCategoryChildrenCount($categoryId);
if ($delta >= 0) {
$sql = "UPDATE $tbl_category SET children_count = (children_count + $delta) WHERE code = '$categoryId'";
} else {
$sql = "UPDATE $tbl_category SET children_count = (children_count - ".abs($delta).") WHERE code = '$categoryId'";
} }
$result = Database::query($sql);
$children_count = compterFils($category, 0) - 1;
$sql = "UPDATE $tbl_category SET children_count='$children_count' WHERE code='$category'";
Database::query($sql);
} }
/** /**
@ -170,7 +184,7 @@ function deleteNode($node)
Database::query("DELETE FROM $tbl_category WHERE code='$node'"); Database::query("DELETE FROM $tbl_category WHERE code='$node'");
if (!empty($row['parent_id'])) { if (!empty($row['parent_id'])) {
updateCategoryChildren($row['parent_id']); updateParentCategoryChildrenCount($row['parent_id'], -1);
} }
} }
} }
@ -210,6 +224,7 @@ function editNode($code, $name, $canHaveCourses, $old_code)
} }
/** /**
* Move a node up on display
* @param string $code * @param string $code
* @param string $tree_pos * @param string $tree_pos
* @param int $parent_id * @param int $parent_id
@ -219,17 +234,17 @@ function moveNodeUp($code, $tree_pos, $parent_id)
{ {
$tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
$code = Database::escape_string($code); $code = Database::escape_string($code);
$tree_pos = Database::escape_string($tree_pos); $tree_pos = intval($tree_pos);
$parent_id = intval($parent_id); $parent_id = intval($parent_id);
$sql = "SELECT code,tree_pos $sql = "SELECT code,tree_pos
FROM $tbl_category FROM $tbl_category
WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos<'$tree_pos' WHERE parent_id " . (empty($parent_id) ? "IS NULL" : " = $parent_id") . " AND tree_pos < $tree_pos
ORDER BY tree_pos DESC LIMIT 0,1"; ORDER BY tree_pos DESC LIMIT 0,1";
$result = Database::query($sql); $result = Database::query($sql);
if (!$row = Database::fetch_array($result)) { if (!$row = Database::fetch_array($result)) {
$sql = "SELECT code,tree_pos FROM $tbl_category $sql = "SELECT code,tree_pos FROM $tbl_category
WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos>'$tree_pos' WHERE parent_id " . (empty($parent_id) ? "IS NULL" : " = $parent_id") . " AND tree_pos > $tree_pos
ORDER BY tree_pos DESC LIMIT 0,1"; ORDER BY tree_pos DESC LIMIT 0,1";
$result2 = Database::query($sql); $result2 = Database::query($sql);
if (!$row2 = Database::fetch_array($result2)) { if (!$row2 = Database::fetch_array($result2)) {
@ -238,24 +253,34 @@ function moveNodeUp($code, $tree_pos, $parent_id)
} }
Database::query("UPDATE $tbl_category SET tree_pos='" . $row['tree_pos'] . "' WHERE code='$code'"); Database::query("UPDATE $tbl_category SET tree_pos='" . $row['tree_pos'] . "' WHERE code='$code'");
Database::query("UPDATE $tbl_category SET tree_pos='$tree_pos' WHERE code='$row[code]'"); Database::query("UPDATE $tbl_category SET tree_pos='$tree_pos' WHERE code='" . $code . "'");
return true;
} }
/** /**
* @param $pere * Counts the number of children categories a category has
* @param $cpt * @param int $categoryId The ID of the category of which we want to count the children
* @return mixed * @param int $count The number of subcategories we counted this far
* @return mixed The number of subcategories this category has
*/ */
function compterFils($parent, $cpt) function courseCategoryChildrenCount($categoryId)
{ {
$tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
$parent = intval($parent); $categoryId = intval($categoryId);
$result = Database::query("SELECT code FROM $tbl_category WHERE parent_id='$parent'"); $count = 0;
if (empty($categoryId)) {
return 0;
}
$sql = "SELECT id, code FROM $tbl_category WHERE parent_id = $categoryId";
$result = Database::query($sql);
while ($row = Database::fetch_array($result)) { while ($row = Database::fetch_array($result)) {
$cpt = compterFils($row['code'], $cpt); $count += courseCategoryChildrenCount($row['id']);
} }
return ($cpt + 1); $sql = "UPDATE $tbl_category SET children_count = $count WHERE id = $categoryId";
Database::query($sql);
return $count + 1;
} }
/** /**

Loading…
Cancel
Save