From 71278e567fbb31613f91c55f7d9c3958e5dfefe8 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Fri, 25 Oct 2013 17:33:16 +0200 Subject: [PATCH] Cleaning course_category page. --- main/admin/course_category.php | 350 +++++---------------------- main/inc/lib/course_category.lib.php | 220 +++++++++++++++++ 2 files changed, 281 insertions(+), 289 deletions(-) create mode 100644 main/inc/lib/course_category.lib.php diff --git a/main/admin/course_category.php b/main/admin/course_category.php index 968026f609..cb240134bd 100644 --- a/main/admin/course_category.php +++ b/main/admin/course_category.php @@ -9,23 +9,27 @@ */ // name of the language file that needs to be included $language_file = 'admin'; - $cidReset = true; - require_once '../inc/global.inc.php'; +require_once api_get_path(LIBRARY_PATH).'course_category.lib.php'; $this_section = SECTION_PLATFORM_ADMIN; api_protect_admin_script(); -$category = $_GET['category']; -$category = Database::escape_string($category); +$category = isset($_GET['category']) ? $_GET['category'] : null; -$action = $_GET['action']; +if (!empty($category)) { + $parentInfo = getCategory($category); +} +$categoryId = isset($_GET['id']) ? $_GET['id'] : null; +if (!empty($categoryId)) { + $categoryInfo = getCategory($categoryId); +} +$action = isset($_GET['action']) ? $_GET['action'] : null; $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); $errorMsg = ''; -$delError = 0; if (!empty($action)) { if ($action == 'delete') { if (api_get_multiple_access_url()) { @@ -33,8 +37,6 @@ if (!empty($action)) { deleteNode($_GET['id']); header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category)); exit(); - } else { - $delError = 1; } } else { deleteNode($_GET['id']); @@ -42,316 +44,86 @@ if (!empty($action)) { exit(); } } elseif (($action == 'add' || $action == 'edit') && $_POST['formSent']) { - $_POST['categoryCode'] = trim($_POST['categoryCode']); - $_POST['categoryName'] = trim($_POST['categoryName']); - - if (!empty($_POST['categoryCode']) && !empty($_POST['categoryName'])) { - if ($action == 'add') { - $ret = addNode($_POST['categoryCode'], $_POST['categoryName'], $_POST['canHaveCourses'], $category); - } else { - $ret = editNode($_POST['categoryCode'], $_POST['categoryName'], $_POST['canHaveCourses'], $_GET['id']); - } - - if ($ret) { - $action = ''; - } else { - $errorMsg = get_lang('CatCodeAlreadyUsed'); - } + if ($action == 'add') { + $ret = addNode($_POST['code'], $_POST['name'], $_POST['auth_course_child'], $category); } else { - $errorMsg = get_lang('PleaseEnterCategoryInfo'); + $ret = editNode($_POST['code'], $_POST['name'], $_POST['auth_course_child'], $_GET['id']); + } + if ($ret) { + $action = ''; + } else { + $errorMsg = get_lang('CatCodeAlreadyUsed'); } - } elseif ($action == 'edit') { - $categoryCode = Database::escape_string($_GET['id']); - - $result = Database::query("SELECT name,auth_course_child FROM $tbl_category WHERE code='$categoryCode'"); - - list($categoryName, $canHaveCourses) = Database::fetch_row($result); - - $canHaveCourses = ($canHaveCourses == 'FALSE') ? 0 : 1; } elseif ($action == 'moveUp') { moveNodeUp($_GET['id'], $_GET['tree_pos'], $category); - header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category)); exit(); } } $tool_name = get_lang('AdminCategories'); - $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin')); -//$interbreadcrumb[]=array('url' => 'configure_homepage.php',"name" => get_lang('ConfigureHomePage')); Display::display_header($tool_name); -if (!empty($category)) { - $myquery = "SELECT * FROM $tbl_category WHERE code ='$category'"; - $result = Database::query($myquery); - if (Database::num_rows($result) == 0) { - $category = ''; - } -} - -if (empty($action)) { - $myquery = "SELECT t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count,COUNT(DISTINCT t3.code) AS nbr_courses - FROM $tbl_category t1 LEFT JOIN $tbl_category t2 ON t1.code=t2.parent_id LEFT JOIN $tbl_course t3 ON t3.category_code=t1.code - WHERE t1.parent_id " . (empty($category) ? "IS NULL" : "='$category'") . " - GROUP BY t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count ORDER BY t1.tree_pos"; - $result = Database::query($myquery); - $Categories = Database::store_result($result); -} - if ($action == 'add' || $action == 'edit') { - if ((api_get_multiple_access_url() && api_get_current_access_url_id() == 1) || !api_get_multiple_access_url() ) { ?> -
- -
- '; + echo Display::url( + Display::return_icon('folder_up.png', get_lang("Back"), '', ICON_SIZE_MEDIUM), + api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.$category + ); + echo ''; + $form_title = ($action == 'add') ? get_lang('AddACategory') : get_lang('EditNode'); if (!empty($category)) { $form_title .= ' ' . get_lang('Into') . ' ' . Security::remove_XSS($category); } - $form = new FormValidator('course_category'); + $url = api_get_self().'?action='.Security::remove_XSS($action).'&category='.Security::remove_XSS($category).'&id='.Security::remove_XSS($_GET['id']); + $form = new FormValidator('course_category', 'post', $url); $form->addElement('header', '', $form_title); - $form->display(); - ?> -
- - - - - - - - - - - - - - - - - - - - - - - - addElement('hidden', 'formSent', 1); + $form->addElement('text', 'code', get_lang("CategoryCode")); + $form->addElement('text', 'name', get_lang("CategoryName")); + $form->addRule('name', get_lang('PleaseEnterCategoryInfo'), 'required'); + $form->addRule('code', get_lang('PleaseEnterCategoryInfo'), 'required'); + $form->addElement('radio', 'auth_course_child', get_lang("AllowCoursesInCategory"), get_lang('Yes'), 'TRUE'); + $form->addElement('radio', 'auth_course_child', null, get_lang('No'), 'FALSE'); + if (!empty($categoryInfo)) { $class = "save"; $text = get_lang('CategoryMod'); + $form->setDefaults($categoryInfo); } else { $class = "add"; $text = get_lang('AddCategory'); + $form->setDefaults(array('auth_course_child' => 'TRUE')); } - ?> - - -
- - - -
:
:
- /> - /> -
 
-
- - -
- 0) { - $parent_id = Database::fetch_array($result); - } - - $parent_id['parent_id'] ? $link = ' (' . $parent_id['parent_id'] . ')' : $link = ''; - ?> - - - - - - -
- - - - - '" . $row['tree_pos'] . "'"); - Database::query("DELETE FROM $tbl_category WHERE code='$node'"); - - if (!empty($row['parent_id'])) { - updateFils($row['parent_id']); - } - } -} - -function addNode($code, $name, $canHaveCourses, $parent_id) { - global $tbl_category; - - $canHaveCourses = $canHaveCourses ? 'TRUE' : 'FALSE'; - $code = Database::escape_string($code); - $name = Database::escape_string($name); - $parent_id = Database::escape_string($parent_id); - - $result = Database::query("SELECT 1 FROM $tbl_category WHERE code='$code'"); - - if (Database::num_rows($result)) { - return false; - } - - $result = Database::query("SELECT MAX(tree_pos) AS maxTreePos FROM $tbl_category"); - - $row = Database::fetch_array($result); - - $tree_pos = $row['maxTreePos'] + 1; - - $code = generate_course_code($code); - Database::query("INSERT INTO $tbl_category(name,code,parent_id,tree_pos,children_count,auth_course_child) VALUES('$name','$code'," . (empty($parent_id) ? "NULL" : "'$parent_id'") . ",'$tree_pos','0','$canHaveCourses')"); - - updateFils($parent_id); - - return true; -} - -function editNode($code, $name, $canHaveCourses, $old_code) { - global $tbl_category, $tbl_course; - - $canHaveCourses = $canHaveCourses ? 'TRUE' : 'FALSE'; - $code = Database::escape_string($code); - $name = Database::escape_string($name); - $old_code = Database::escape_string($old_code); - - if ($code != $old_code) { - $result = Database::query("SELECT 1 FROM $tbl_category WHERE code='$code'"); - if (Database::num_rows($result)) { - return false; - } + $form->addElement('button', 'submit', $text); + $form->display(); + } elseif (api_get_multiple_access_url() && api_get_current_access_url_id() != 1) { + Display::display_warning_message(get_lang('CourseCategoriesAreGlobal')); } - $code = generate_course_code($code); - Database::query("UPDATE $tbl_category SET name='$name', code='$code',auth_course_child='$canHaveCourses' WHERE code='$old_code'"); - $sql = "UPDATE $tbl_course SET category_code = '$code' WHERE category_code = '$old_code' "; - Database::query($sql); - - return true; -} - -function moveNodeUp($code, $tree_pos, $parent_id) { - global $tbl_category; - $code = Database::escape_string($code); - $tree_pos = Database::escape_string($tree_pos); - $parent_id = Database::escape_string($parent_id); - - $result = Database::query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos<'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1"); - - if (!$row = Database::fetch_array($result)) { - $result = Database::query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos>'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1"); - - if (!$row = Database::fetch_array($result)) { - return false; - } +} else { + echo '
'; + $link = null; + if (!empty($parentInfo)) { + $parentCode = $parentInfo['parent_id']; + echo Display::url( + Display::return_icon('back.png', get_lang("Back"), '', ICON_SIZE_MEDIUM), + api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.$parentCode + ); } - - 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]'"); -} - -function updateFils($category) { - global $tbl_category; - $category = Database::escape_string($category); - $result = Database::query("SELECT parent_id FROM $tbl_category WHERE code='$category'"); - - if ($row = Database::fetch_array($result)) { - updateFils($row['parent_id']); + echo Display::url( + Display::return_icon('new_folder.png', get_lang("AddACategory"), '', ICON_SIZE_MEDIUM), + api_get_path(WEB_CODE_PATH).'admin/course_category.php?action=add&category='.$category + ); + + echo '
'; + if (!empty($parentInfo)) { + echo Display::page_subheader($parentInfo['name']); } + echo listCategories($category); - $children_count = compterFils($category, 0) - 1; - Database::query("UPDATE $tbl_category SET children_count='$children_count' WHERE code='$category'"); } -function compterFils($pere, $cpt) { - global $tbl_category; - $pere = Database::escape_string($pere); - $result = Database::query("SELECT code FROM $tbl_category WHERE parent_id='$pere'"); - - while ($row = Database::fetch_array($result)) { - $cpt = compterFils($row['code'], $cpt); - } - - return ($cpt + 1); -} \ No newline at end of file +Display::display_footer(); diff --git a/main/inc/lib/course_category.lib.php b/main/inc/lib/course_category.lib.php new file mode 100644 index 0000000000..1978f05f6a --- /dev/null +++ b/main/inc/lib/course_category.lib.php @@ -0,0 +1,220 @@ + '" . $row['tree_pos'] . "'"); + Database::query("DELETE FROM $tbl_category WHERE code='$node'"); + + if (!empty($row['parent_id'])) { + updateFils($row['parent_id']); + } + } +} + +function addNode($code, $name, $canHaveCourses, $parent_id) +{ + global $tbl_category; + $code = trim(Database::escape_string($code)); + $name = trim(Database::escape_string($name)); + $parent_id = Database::escape_string($parent_id); + $canHaveCourses = Database::escape_string($canHaveCourses); + + $result = Database::query("SELECT 1 FROM $tbl_category WHERE code='$code'"); + if (Database::num_rows($result)) { + return false; + } + + $result = Database::query("SELECT MAX(tree_pos) AS maxTreePos FROM $tbl_category"); + $row = Database::fetch_array($result); + $tree_pos = $row['maxTreePos'] + 1; + $code = generate_course_code($code); + $sql = "INSERT INTO $tbl_category(name,code,parent_id,tree_pos,children_count,auth_course_child) + VALUES('$name','$code'," . (empty($parent_id) ? "NULL" : "'$parent_id'") . ",'$tree_pos','0','$canHaveCourses')"; + Database::query($sql); + updateFils($parent_id); + return true; +} + +function editNode($code, $name, $canHaveCourses, $old_code) +{ + global $tbl_category, $tbl_course; + + $code = trim(Database::escape_string($code)); + $name = trim(Database::escape_string($name)); + $old_code = Database::escape_string($old_code); + $canHaveCourses = Database::escape_string($canHaveCourses); + + if ($code != $old_code) { + $result = Database::query("SELECT 1 FROM $tbl_category WHERE code='$code'"); + if (Database::num_rows($result)) { + return false; + } + } + $code = generate_course_code($code); + $sql = "UPDATE $tbl_category SET name='$name', code='$code', auth_course_child = '$canHaveCourses' + WHERE code='$old_code'"; + Database::query($sql); + var_dump($sql); + + $sql = "UPDATE $tbl_course SET category_code = '$code' WHERE category_code = '$old_code' "; + Database::query($sql); + return true; +} + +function moveNodeUp($code, $tree_pos, $parent_id) +{ + global $tbl_category; + $code = Database::escape_string($code); + $tree_pos = Database::escape_string($tree_pos); + $parent_id = Database::escape_string($parent_id); + + $result = Database::query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos<'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1"); + + if (!$row = Database::fetch_array($result)) { + $result = Database::query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id " . (empty($parent_id) ? "IS NULL" : "='$parent_id'") . " AND tree_pos>'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1"); + + if (!$row = Database::fetch_array($result)) { + return false; + } + } + + 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]'"); +} + +function updateFils($category) +{ + global $tbl_category; + $category = Database::escape_string($category); + $result = Database::query("SELECT parent_id FROM $tbl_category WHERE code='$category'"); + + if ($row = Database::fetch_array($result)) { + updateFils($row['parent_id']); + } + + $children_count = compterFils($category, 0) - 1; + Database::query("UPDATE $tbl_category SET children_count='$children_count' WHERE code='$category'"); +} + +function compterFils($pere, $cpt) +{ + global $tbl_category; + $pere = Database::escape_string($pere); + $result = Database::query("SELECT code FROM $tbl_category WHERE parent_id='$pere'"); + + while ($row = Database::fetch_array($result)) { + $cpt = compterFils($row['code'], $cpt); + } + return ($cpt + 1); +} + + +function listCategories($categorySource) +{ + $categorySource = isset($categorySource) ? $categorySource : null; + $categories = getCategories($categorySource); + + if (count($categories) > 0) { + $table = new HTML_Table(array('class' => 'data_table')); + $column = 0; + $row = 0; + $headers = array( + get_lang('Category'), get_lang('CategoriesNumber'), get_lang('Courses'), get_lang('Actions') + ); + foreach ($headers as $header) { + $table->setHeaderContents($row, $column, $header); + $column++; + } + $row++; + $mainUrl = api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.$categorySource; + + $editIcon = Display::return_icon('edit.png', get_lang('EditNode'), null, ICON_SIZE_SMALL); + $deleteIcon = Display::return_icon('delete.png', get_lang('DeleteNode'), null, ICON_SIZE_SMALL); + $moveIcon = Display::return_icon('up.png', get_lang('UpInSameLevel'), null, ICON_SIZE_SMALL); + + foreach ($categories as $category) { + + $editUrl = $mainUrl.'&id='.$category['code'].'&action=edit'; + $deleteUrl = $mainUrl.'&id='.$category['code'].'&action=delete'; + $moveUrl = $mainUrl.'&id='.$category['code'].'&action=moveUp&tree_pos='.$category['tree_pos']; + + $actions = Display::url($editIcon, $editUrl).Display::url($deleteIcon, $deleteUrl).Display::url($moveIcon, $moveUrl); + $url = api_get_path(WEB_CODE_PATH).'admin/course_category.php?category='.$category['code']; + $title = Display::url( + Display::return_icon('folder_document.gif', get_lang('OpenNode'), null, ICON_SIZE_SMALL).' '.$category['name'], + $url + ); + $content = array( + $title, + $category['children_count'], + $category['nbr_courses'], + + $actions + ); + $column = 0; + foreach ($content as $value) { + $table->setCellContents($row, $column, $value); + $column++; + } + $row++; + } + return $table->toHtml(); + } else { + return Display::return_message(get_lang("NoCategories"), 'warning'); + } +}