From 9934fc0af892d0effd2107a9fd396535ee4e7307 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 5 Dec 2022 21:34:01 -0500 Subject: [PATCH] Allow edit parents for course category - refs BT#19768 --- main/admin/course_category.php | 30 ++++++++++++++++++++++++---- main/inc/lib/course_category.lib.php | 19 ++++++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/main/admin/course_category.php b/main/admin/course_category.php index 9e34311d69..4fc1758b08 100755 --- a/main/admin/course_category.php +++ b/main/admin/course_category.php @@ -7,7 +7,7 @@ require_once __DIR__.'/../inc/global.inc.php'; $this_section = SECTION_PLATFORM_ADMIN; api_protect_admin_script(); -$category = isset($_GET['category']) ? $_GET['category'] : null; +$category = $_GET['category'] ?? null; $parentInfo = []; if (!empty($category)) { @@ -51,12 +51,14 @@ if (!empty($action)) { header('Location: '.api_get_self().'?category='.Security::remove_XSS($category)); exit(); } elseif (($action === 'add' || $action === 'edit') && isset($_POST['formSent']) && $_POST['formSent']) { + $newParentCategoryCode = $_POST['parent_id'] ?? $category; + if ($action === 'add') { $ret = CourseCategory::addNode( $_POST['code'], $_POST['name'], $_POST['auth_course_child'], - $category + $newParentCategoryCode ); $errorMsg = Display::return_message(get_lang('Created')); @@ -65,7 +67,9 @@ if (!empty($action)) { $_POST['code'], $_POST['name'], $_POST['auth_course_child'], - $categoryId + $categoryId, + $newParentCategoryCode, + $category ); $categoryInfo = CourseCategory::getCategory($_POST['code']); $ret = $categoryInfo['id']; @@ -161,6 +165,19 @@ if ($action === 'add' || $action === 'edit') { } $form->addRule('code', get_lang('PleaseEnterCategoryInfo'), 'required'); + + $categories = ['' => get_lang('Select')]; + + foreach (CourseCategory::getAllCategories() as $categoryItemInfo) { + if ($categoryId === $categoryItemInfo['code']) { + continue; + } + + $categories[$categoryItemInfo['code']] = $categoryItemInfo['name']; + } + + $form->addSelect('parent_id', get_lang('ParentCategory'), $categories); + $group = [ $form->createElement( 'radio', @@ -210,7 +227,12 @@ if ($action === 'add' || $action === 'edit') { } else { $class = 'add'; $text = get_lang('AddCategory'); - $form->setDefaults(['auth_course_child' => 'TRUE']); + $form->setDefaults( + [ + 'auth_course_child' => 'TRUE', + 'parent_id' => $category, + ] + ); $form->addButtonCreate($text); } $form->display(); diff --git a/main/inc/lib/course_category.lib.php b/main/inc/lib/course_category.lib.php index 0d12c4507f..e1bcac3191 100755 --- a/main/inc/lib/course_category.lib.php +++ b/main/inc/lib/course_category.lib.php @@ -289,8 +289,14 @@ class CourseCategory * * @return bool */ - public static function editNode($code, $name, $canHaveCourses, $old_code) - { + public static function editNode( + $code, + $name, + $canHaveCourses, + $old_code, + ?string $newParentCode = null, + ?string $oldParentCode = null + ) { $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); @@ -318,6 +324,15 @@ class CourseCategory WHERE category_code = '$old_code' "; Database::query($sql); + Database::update( + $tbl_category, + ['parent_id' => $newParentCode ?: null], + ['code = ?' => $code] + ); + + self::updateParentCategoryChildrenCount($oldParentCode, -1); + self::updateParentCategoryChildrenCount($newParentCode, 1); + return true; }