Merge pull request #4487 from AngelFQC/BT19768

Allow edit parents for course category
pull/4488/head
Nicolas Ducoulombier 3 years ago committed by GitHub
commit 9bef48d9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      main/admin/course_category.php
  2. 19
      main/inc/lib/course_category.lib.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();

@ -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;
}

Loading…
Cancel
Save