diff --git a/app/Migrations/Schema/V111/Version111.php b/app/Migrations/Schema/V111/Version111.php index 8d3e1723f5..194db58e80 100644 --- a/app/Migrations/Schema/V111/Version111.php +++ b/app/Migrations/Schema/V111/Version111.php @@ -50,6 +50,8 @@ class Version111 extends AbstractMigrationChamilo if (!$table->hasColumn('duration')) { $this->addSql("ALTER TABLE session_rel_user ADD duration INT DEFAULT NULL"); } + + $this->addSql('CREATE TABLE access_url_rel_course_category (id INT AUTO_INCREMENT NOT NULL, access_url_id INT NOT NULL, course_category_id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); } /** @@ -65,6 +67,6 @@ class Version111 extends AbstractMigrationChamilo public function down(Schema $schema) { $this->addSql('DROP TABLE c_lp_category_user'); - + $this->addSql('DROP TABLE access_url_rel_course_category'); } } diff --git a/main/admin/access_url_edit_course_category_to_url.php b/main/admin/access_url_edit_course_category_to_url.php index ba5f409834..be710f9220 100755 --- a/main/admin/access_url_edit_course_category_to_url.php +++ b/main/admin/access_url_edit_course_category_to_url.php @@ -4,6 +4,7 @@ * @package chamilo.admin * @author Julio Montoya */ + // resetting the course id $cidReset = true; @@ -120,7 +121,7 @@ if ($ajax_search) { $userGroupList[$item['id']] = $item ; } } - $noUserGroupList = getCourseCategoryNotInList(array_keys($userGroupList)); + $noUserGroupList = CourseCategory::getCourseCategoryNotInList(array_keys($userGroupList)); } if ($add_type == 'multiple') { diff --git a/main/admin/access_urls.php b/main/admin/access_urls.php index 5942a817f4..7ac8b5c942 100755 --- a/main/admin/access_urls.php +++ b/main/admin/access_urls.php @@ -124,12 +124,10 @@ if ($userGroup->getUseMultipleUrl()) { ); } -if (isMultipleUrlSupport()) { - echo Display::url( - Display::return_icon('folder.png', get_lang('ManageCourseCategories'), array(), ICON_SIZE_MEDIUM), - api_get_path(WEB_CODE_PATH).'admin/access_url_edit_course_category_to_url.php' - ); -} +echo Display::url( + Display::return_icon('folder.png', get_lang('ManageCourseCategories'), array(), ICON_SIZE_MEDIUM), + api_get_path(WEB_CODE_PATH).'admin/access_url_edit_course_category_to_url.php' +); echo ''; diff --git a/main/admin/configure_homepage.php b/main/admin/configure_homepage.php index 5a349693fd..9464d5d8a8 100755 --- a/main/admin/configure_homepage.php +++ b/main/admin/configure_homepage.php @@ -773,7 +773,7 @@ if (!empty($action)) { }// end of "else" in if($_POST['formSent']) condition } else { //if $action is empty, then prepare a list of the course categories to display (?) - $Categories = getCategoriesToDisplayInHomePage(); + $Categories = CourseCategory::getCategoriesToDisplayInHomePage(); } // Display section diff --git a/main/admin/course_category.php b/main/admin/course_category.php index 7be7f86f50..b1ddd22bf7 100755 --- a/main/admin/course_category.php +++ b/main/admin/course_category.php @@ -12,37 +12,25 @@ $category = isset($_GET['category']) ? $_GET['category'] : null; $parentInfo = []; if (!empty($category)) { - $parentInfo = getCategory($category); + $parentInfo = CourseCategory::getCategory($category); } $categoryId = isset($_GET['id']) ? Security::remove_XSS($_GET['id']) : null; if (!empty($categoryId)) { - $categoryInfo = getCategory($categoryId); + $categoryInfo = CourseCategory::getCategory($categoryId); } $action = isset($_GET['action']) ? $_GET['action'] : null; $errorMsg = ''; if (!empty($action)) { if ($action == 'delete') { - if (api_get_multiple_access_url()) { - if (api_get_current_access_url_id() == 1 || - (isset($_configuration['enable_multiple_url_support_for_course_category']) && - $_configuration['enable_multiple_url_support_for_course_category']) - ) { - deleteNode($categoryId); - Display::addFlash(Display::return_message(get_lang('Deleted'))); - header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category)); - exit(); - } - } else { - deleteNode($categoryId); - Display::addFlash(Display::return_message(get_lang('Deleted'))); - header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category)); - exit(); - } + CourseCategory::deleteNode($categoryId); + Display::addFlash(Display::return_message(get_lang('Deleted'))); + header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category)); + exit(); } elseif (($action == 'add' || $action == 'edit') && isset($_POST['formSent']) && $_POST['formSent']) { if ($action == 'add') { - $ret = addNode( + $ret = CourseCategory::addNode( $_POST['code'], $_POST['name'], $_POST['auth_course_child'], @@ -51,7 +39,7 @@ if (!empty($action)) { Display::addFlash(Display::return_message(get_lang('Created'))); } else { - $ret = editNode( + $ret = CourseCategory::editNode( $_POST['code'], $_POST['name'], $_POST['auth_course_child'], @@ -65,7 +53,7 @@ if (!empty($action)) { $errorMsg = get_lang('CatCodeAlreadyUsed'); } } elseif ($action == 'moveUp') { - moveNodeUp($categoryId, $_GET['tree_pos'], $category); + CourseCategory::moveNodeUp($categoryId, $_GET['tree_pos'], $category); header('Location: ' . api_get_self() . '?category=' . Security::remove_XSS($category)); Display::addFlash(Display::return_message(get_lang('Updated'))); exit(); @@ -81,53 +69,45 @@ $interbreadcrumb[] = array( Display::display_header($tool_name); if ($action == 'add' || $action == 'edit') { - if ((api_get_multiple_access_url() && api_get_current_access_url_id() == 1) || - !api_get_multiple_access_url() || - (isset($_configuration['enable_multiple_url_support_for_course_category']) && - $_configuration['enable_multiple_url_support_for_course_category']) - ) { - echo '
'; - 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='.Security::remove_XSS($category) - ); - echo '
'; + echo '
'; + 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='.Security::remove_XSS($category) + ); + echo '
'; - $form_title = ($action == 'add') ? get_lang('AddACategory') : get_lang('EditNode'); - if (!empty($category)) { - $form_title .= ' ' . get_lang('Into') . ' ' . Security::remove_XSS($category); - } - $url = api_get_self().'?action='.Security::remove_XSS($action).'&category='.Security::remove_XSS($category).'&id='.Security::remove_XSS($categoryId); - $form = new FormValidator('course_category', 'post', $url); - $form->addElement('header', '', $form_title); - $form->addElement('hidden', 'formSent', 1); + $form_title = ($action == 'add') ? get_lang('AddACategory') : get_lang('EditNode'); + if (!empty($category)) { + $form_title .= ' ' . get_lang('Into') . ' ' . Security::remove_XSS($category); + } + $url = api_get_self().'?action='.Security::remove_XSS($action).'&category='.Security::remove_XSS($category).'&id='.Security::remove_XSS($categoryId); + $form = new FormValidator('course_category', 'post', $url); + $form->addElement('header', '', $form_title); + $form->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'); - $group = array( - $form->createElement('radio', 'auth_course_child', get_lang("AllowCoursesInCategory"), get_lang('Yes'), 'TRUE'), - $form->createElement('radio', 'auth_course_child', null, get_lang('No'), 'FALSE') - ); - $form->addGroup($group, null, get_lang("AllowCoursesInCategory")); + $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'); + $group = array( + $form->createElement('radio', 'auth_course_child', get_lang("AllowCoursesInCategory"), get_lang('Yes'), 'TRUE'), + $form->createElement('radio', 'auth_course_child', null, get_lang('No'), 'FALSE') + ); + $form->addGroup($group, null, get_lang("AllowCoursesInCategory")); - if (!empty($categoryInfo)) { - $class = "save"; - $text = get_lang('Save'); - $form->setDefaults($categoryInfo); - $form->addButtonSave($text); - } else { - $class = "add"; - $text = get_lang('AddCategory'); - $form->setDefaults(array('auth_course_child' => 'TRUE')); - $form->addButtonCreate($text); - } - $form->display(); - } elseif (api_get_multiple_access_url() && api_get_current_access_url_id() != 1) { - // If multiple URLs and not main URL, prevent edition and inform user - Display::display_warning_message(get_lang('CourseCategoriesAreGlobal')); + if (!empty($categoryInfo)) { + $class = "save"; + $text = get_lang('Save'); + $form->setDefaults($categoryInfo); + $form->addButtonSave($text); + } else { + $class = "add"; + $text = get_lang('AddCategory'); + $form->setDefaults(array('auth_course_child' => 'TRUE')); + $form->addButtonCreate($text); } + $form->display(); + } else { // If multiple URLs and not main URL, prevent deletion and inform user if ($action == 'delete' && api_get_multiple_access_url() && api_get_current_access_url_id() != 1) { @@ -154,7 +134,7 @@ if ($action == 'add' || $action == 'edit') { if (!empty($parentInfo)) { echo Display::page_subheader($parentInfo['name'].' ('.$parentInfo['code'].')'); } - echo listCategories($category); + echo CourseCategory::listCategories($category); } Display::display_footer(); diff --git a/main/admin/course_edit.php b/main/admin/course_edit.php index fe09c5d511..66fe016015 100755 --- a/main/admin/course_edit.php +++ b/main/admin/course_edit.php @@ -185,7 +185,7 @@ $categorySelect = $form->addElement( ); if (!empty($courseInfo['categoryCode'])) { - $data = getCategory($courseInfo['categoryCode']); + $data = CourseCategory::getCategory($courseInfo['categoryCode']); $categorySelect->addOption($data['name'], $data['code']); } diff --git a/main/admin/course_import.php b/main/admin/course_import.php index 55b7c443bf..62c4468638 100755 --- a/main/admin/course_import.php +++ b/main/admin/course_import.php @@ -63,7 +63,7 @@ function validate_data($courses) // 4. Check whether course category exists. if (isset($course['CourseCategory']) && strlen($course['CourseCategory']) != 0) { - $categoryInfo = getCategory($course['CourseCategory']); + $categoryInfo = CourseCategory::getCategory($course['CourseCategory']); if (empty($categoryInfo)) { //@todo this is so bad even all lang variables are wrong ... $course['error'] = get_lang('UnkownCategoryCourseCode').' ('.$course['CourseCategory'].')'; diff --git a/main/admin/course_request_edit.php b/main/admin/course_request_edit.php index cb8a4fdbfc..9ef5ac9a4b 100755 --- a/main/admin/course_request_edit.php +++ b/main/admin/course_request_edit.php @@ -57,7 +57,7 @@ if ($course_validation_feature) { ); if (!empty($course_request_info['category_code'])) { - $data = getCategory($course_request_info['category_code']); + $data = CourseCategory::getCategory($course_request_info['category_code']); $courseSelect->addOption($data['name'], $data['code'], ['selected' => 'selected']); } diff --git a/main/auth/courses.php b/main/auth/courses.php index c8938f241d..dd9a254860 100755 --- a/main/auth/courses.php +++ b/main/auth/courses.php @@ -18,7 +18,7 @@ require_once '../inc/global.inc.php'; $ctok = Security::get_existing_token(); // Get Limit data -$limit = getLimitArray(); +$limit = CourseCategory::getLimitArray(); // Section for the tabs. $this_section = SECTION_COURSES; @@ -63,8 +63,7 @@ if (isset($_GET['action']) && in_array($_GET['action'], $actions)) { } $categoryCode = isset($_GET['category_code']) ? $_GET['category_code'] : 'ALL'; - -$nameTools = getCourseCatalogNameTools($action); +$nameTools = CourseCategory::getCourseCatalogNameTools($action); if (empty($nameTools)) { $nameTools = get_lang('CourseManagement'); } else { diff --git a/main/auth/courses_controller.php b/main/auth/courses_controller.php index fa61d16704..b059fc02a1 100755 --- a/main/auth/courses_controller.php +++ b/main/auth/courses_controller.php @@ -102,7 +102,7 @@ class CoursesController if (!isset($category_code)) { $category_code = $browse_course_categories[0][1]['code']; // by default first category } - $limit = isset($limit) ? $limit : getLimitArray(); + $limit = isset($limit) ? $limit : CourseCategory::getLimitArray(); $data['browse_courses_in_category'] = $this->model->browse_courses_in_category($category_code, null, $limit); } @@ -158,7 +158,7 @@ class CoursesController public function search_courses($search_term, $message = '', $error = '', $content = null, $limit = array()) { $data = array(); - $limit = !empty($limit) ? $limit : getLimitArray(); + $limit = !empty($limit) ? $limit : CourseCategory::getLimitArray(); $browse_course_categories = $this->model->browse_course_categories(); $data['countCoursesInCategory'] = $this->model->count_courses_in_category('ALL', $search_term); @@ -385,7 +385,7 @@ class CoursesController $html .= ''; } else { if (!empty($categoryCourses)) { - $html .= 'model->countSessions($date); $sessions = $this->model->browseSessions($date, $limit); @@ -626,14 +626,14 @@ class CoursesController $pageTotal = intval(ceil(intval($countSessions) / $limit['length'])); // Do NOT show pagination if only one page or less $cataloguePagination = $pageTotal > 1 ? - getCataloguePagination($limit['current'], $limit['length'], $pageTotal) : + CourseCategory::getCatalogPagination($limit['current'], $limit['length'], $pageTotal) : ''; $sessionsBlocks = $this->getFormatedSessionsBlock($sessions); // Get session list catalogue URL - //$sessionUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'display_sessions'); + //$sessionUrl = CourseCategory::getCourseCategoryUrl(1, $limit['length'], null, 0, 'display_sessions'); // Get session search catalogue URL - $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe'); + $courseUrl = CourseCategory::getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe'); $tpl = new Template(); $tpl->assign('show_courses', CoursesAndSessionsCatalog::showCourses()); @@ -662,7 +662,7 @@ class CoursesController $searchTag = isset($_POST['search_tag']) ? $_POST['search_tag'] : null; $searchDate = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d'); $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false; - $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe'); + $courseUrl = CourseCategory::getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe'); $sessions = $this->model->browseSessionsByTags($searchTag, $limit); $sessionsBlocks = $this->getFormatedSessionsBlock($sessions); @@ -693,7 +693,7 @@ class CoursesController { $q = isset($_REQUEST['q']) ? Security::remove_XSS($_REQUEST['q']) : null; $hiddenLinks = isset($_GET['hidden_links']) ? intval($_GET['hidden_links']) == 1 : false; - $courseUrl = getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe'); + $courseUrl = CourseCategory::getCourseCategoryUrl(1, $limit['length'], null, 0, 'subscribe'); $searchDate = isset($_POST['date']) ? $_POST['date'] : date('Y-m-d'); $sessions = $this->model->browseSessionsBySearch($q, $limit); diff --git a/main/course_info/infocours.php b/main/course_info/infocours.php index 76dc1e13d0..b24dbd5b8d 100755 --- a/main/course_info/infocours.php +++ b/main/course_info/infocours.php @@ -142,7 +142,7 @@ while ($a_titulars = Database::fetch_array($q_result_titulars)) { $a_profs[api_get_person_name($s_firstname, $s_lastname)] = api_get_person_name($s_lastname, $s_firstname).' ('.$s_username.')'; } -$categories = getCategoriesCanBeAddedInCourse($_course['categoryCode']); +$categories = CourseCategory::getCategoriesCanBeAddedInCourse($_course['categoryCode']); $linebreak = '
'; diff --git a/main/inc/ajax/course.ajax.php b/main/inc/ajax/course.ajax.php index b8a78dc73c..ab421b4fcf 100755 --- a/main/inc/ajax/course.ajax.php +++ b/main/inc/ajax/course.ajax.php @@ -38,7 +38,7 @@ switch ($action) { break; case 'search_category': if (api_is_platform_admin() || api_is_allowed_to_create_course()) { - $categories = searchCategoryByKeyword($_REQUEST['q']); + $categories = CourseCategory::searchCategoryByKeyword($_REQUEST['q']); if (empty($categories)) { echo json_encode([]); @@ -91,7 +91,7 @@ switch ($action) { $title = $course['title']; if (!empty($course['category_code'])) { - $parents = getParentsToString($course['category_code']); + $parents = self::getParentsToString($course['category_code']); $title = $parents . $course['title']; } diff --git a/main/inc/lib/auth.lib.php b/main/inc/lib/auth.lib.php index fc9d6bd33b..342be81d0a 100755 --- a/main/inc/lib/auth.lib.php +++ b/main/inc/lib/auth.lib.php @@ -406,7 +406,7 @@ class Auth $extraFieldTable = Database :: get_main_table(TABLE_EXTRA_FIELD); $extraFieldValuesTable = Database :: get_main_table(TABLE_EXTRA_FIELD_VALUES); - $limitFilter = getLimitFilterFromArray($limit); + $limitFilter = CourseCategory::getLimitFilterFromArray($limit); // get course list auto-register $sql = "SELECT item_id @@ -415,7 +415,7 @@ class Auth WHERE tcf.variable = 'special_course' AND tcfv.value = 1 "; - + $special_course_result = Database::query($sql); if (Database::num_rows($special_course_result) > 0) { $special_course_list = array(); @@ -570,7 +570,7 @@ class Auth */ public function count_courses_in_category($categoryCode, $searchTerm = '') { - return countCoursesInCategory($categoryCode, $searchTerm); + return CourseCategory::countCoursesInCategory($categoryCode, $searchTerm); } /** @@ -579,7 +579,7 @@ class Auth */ public function browse_course_categories() { - return browseCourseCategories(); + return CourseCategory::browseCourseCategories(); } /** @@ -592,7 +592,7 @@ class Auth */ public function browse_courses_in_category($categoryCode, $randomValue = null, $limit = array()) { - return browseCoursesInCategory($categoryCode, $randomValue, $limit); + return CourseCategory::browseCoursesInCategory($categoryCode, $randomValue, $limit); } /** diff --git a/main/inc/lib/course_category.lib.php b/main/inc/lib/course_category.lib.php index 345eb8fcf6..a19ca8e8dd 100755 --- a/main/inc/lib/course_category.lib.php +++ b/main/inc/lib/course_category.lib.php @@ -2,76 +2,68 @@ /* 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 + * Class CourseCategory */ -function isMultipleUrlSupport() +class CourseCategory { - return api_get_configuration_value('enable_multiple_url_support_for_course_category'); -} + /** + * Returns the category fields from the database from an int ID + * @param int $categoryId The category ID + * @return array + */ + public static function getCategoryById($categoryId) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $categoryId = intval($categoryId); + $sql = "SELECT * FROM $tbl_category WHERE id = $categoryId"; + $result = Database::query($sql); + if (Database::num_rows($result)) { + return Database::fetch_array($result, 'ASSOC'); + } -/** - * Returns the category fields from the database from an int ID - * @param int $categoryId The category ID - * @return array - */ -function getCategoryById($categoryId) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $categoryId = intval($categoryId); - $sql = "SELECT * FROM $tbl_category WHERE id = $categoryId"; - $result = Database::query($sql); - if (Database::num_rows($result)) { - return Database::fetch_array($result, 'ASSOC'); + return array(); } - return array(); -} + /** + * Get category details from a simple category code + * @param string $category The literal category code + * @return array + */ + public static function getCategory($category) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $category = Database::escape_string($category); + $sql = "SELECT * FROM $tbl_category WHERE code ='$category'"; + $result = Database::query($sql); + if (Database::num_rows($result)) { + return Database::fetch_array($result, 'ASSOC'); + } -/** - * Get category details from a simple category code - * @param string $category The literal category code - * @return array - */ -function getCategory($category) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $category = Database::escape_string($category); - $sql = "SELECT * FROM $tbl_category WHERE code ='$category'"; - $result = Database::query($sql); - if (Database::num_rows($result)) { - return Database::fetch_array($result, 'ASSOC'); + return array(); } - return array(); -} - -/** - * @param string $category - * - * @return array - */ -function getCategories($category) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - $category = Database::escape_string($category); - $conditions = null; - $whereCondition = ''; + /** + * @param string $category + * + * @return array + */ + public static function getCategories($category) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); + $category = Database::escape_string($category); + $conditions = null; - if (isMultipleUrlSupport()) { $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); $conditions = " INNER JOIN $table a ON (t1.id = a.course_category_id)"; $whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id(); - } - $parentIdCondition = " AND (t1.parent_id IS NULL OR t1.parent_id = '' )"; - if (!empty($category)) { - $parentIdCondition = " AND t1.parent_id = '$category' "; - } + $parentIdCondition = " AND (t1.parent_id IS NULL OR t1.parent_id = '' )"; + if (!empty($category)) { + $parentIdCondition = " AND t1.parent_id = '$category' "; + } - $sql = "SELECT + $sql = "SELECT t1.name, t1.code, t1.parent_id, @@ -95,179 +87,178 @@ function getCategories($category) t1.children_count ORDER BY t1.tree_pos"; - $result = Database::query($sql); - - $categories = Database::store_result($result); - foreach ($categories as $category) { - $category['nbr_courses'] = 1; - } + $result = Database::query($sql); - return $categories; -} + $categories = Database::store_result($result); + foreach ($categories as $category) { + $category['nbr_courses'] = 1; + } -/** - * @param string $code - * @param string $name - * @param string $canHaveCourses - * @param int $parent_id - * - * @return bool - */ -function addNode($code, $name, $canHaveCourses, $parent_id) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $code = trim($code); - $name = trim($name); - $parent_id = trim($parent_id); + return $categories; + } - $code = CourseManager::generate_course_code($code); - $sql = "SELECT 1 FROM $tbl_category + /** + * @param string $code + * @param string $name + * @param string $canHaveCourses + * @param int $parent_id + * + * @return bool + */ + public static function addNode($code, $name, $canHaveCourses, $parent_id) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $code = trim($code); + $name = trim($name); + $parent_id = trim($parent_id); + + $code = CourseManager::generate_course_code($code); + $sql = "SELECT 1 FROM $tbl_category WHERE code = '".Database::escape_string($code)."'"; - $result = Database::query($sql); - 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; - - $params = [ - 'name' => $name, - 'code' => $code, - 'parent_id' => empty($parent_id) ? null : $parent_id, - 'tree_pos' => $tree_pos, - 'children_count' => 0, - 'auth_course_child' => $canHaveCourses, - 'auth_cat_child' => 'TRUE' - ]; - - $categoryId = Database::insert($tbl_category, $params); - - updateParentCategoryChildrenCount($parent_id, 1); - - if (isMultipleUrlSupport()) { - addToUrl($categoryId); + $result = Database::query($sql); + 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; + + $params = [ + 'name' => $name, + 'code' => $code, + 'parent_id' => empty($parent_id) ? null : $parent_id, + 'tree_pos' => $tree_pos, + 'children_count' => 0, + 'auth_course_child' => $canHaveCourses, + 'auth_cat_child' => 'TRUE' + ]; + + $categoryId = Database::insert($tbl_category, $params); + + self::updateParentCategoryChildrenCount($parent_id, 1); + self::addToUrl($categoryId); + + return $categoryId; } - return $categoryId; -} - -/** - * 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 updateParentCategoryChildrenCount($categoryId, $delta = 1) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $categoryId = Database::escape_string($categoryId); - $delta = intval($delta); - // First get to the highest level possible in the tree - $result = Database::query("SELECT parent_id FROM $tbl_category WHERE code = '$categoryId'"); - $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) + /** + * 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) + */ + public static function updateParentCategoryChildrenCount($categoryId, $delta = 1) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $categoryId = Database::escape_string($categoryId); + $delta = intval($delta); + // First get to the highest level possible in the tree + $result = Database::query("SELECT parent_id FROM $tbl_category WHERE code = '$categoryId'"); + $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 + self::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).") + } else { + $sql = "UPDATE $tbl_category SET children_count = (children_count - ".abs($delta).") WHERE code = '$categoryId'"; + } + Database::query($sql); } - Database::query($sql); -} -/** - * @param string $node - */ -function deleteNode($node) -{ - $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + /** + * @param string $node + */ + public static function deleteNode($node) + { + $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $node = Database::escape_string($node); + $node = Database::escape_string($node); - $result = Database::query("SELECT parent_id, tree_pos FROM $tbl_category WHERE code='$node'"); + $result = Database::query("SELECT parent_id, tree_pos FROM $tbl_category WHERE code='$node'"); - if ($row = Database::fetch_array($result)) { - if (!empty($row['parent_id'])) { - Database::query("UPDATE $tbl_course SET category_code = '".$row['parent_id']."' WHERE category_code='$node'"); - Database::query("UPDATE $tbl_category SET parent_id='" . $row['parent_id'] . "' WHERE parent_id='$node'"); - } else { - Database::query("UPDATE $tbl_course SET category_code='' WHERE category_code='$node'"); - Database::query("UPDATE $tbl_category SET parent_id=NULL WHERE parent_id='$node'"); - } + if ($row = Database::fetch_array($result)) { + if (!empty($row['parent_id'])) { + Database::query( + "UPDATE $tbl_course SET category_code = '".$row['parent_id']."' WHERE category_code='$node'" + ); + Database::query("UPDATE $tbl_category SET parent_id='".$row['parent_id']."' WHERE parent_id='$node'"); + } else { + Database::query("UPDATE $tbl_course SET category_code='' WHERE category_code='$node'"); + Database::query("UPDATE $tbl_category SET parent_id=NULL WHERE parent_id='$node'"); + } - Database::query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '" . $row['tree_pos'] . "'"); - Database::query("DELETE FROM $tbl_category WHERE code='$node'"); + Database::query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '".$row['tree_pos']."'"); + Database::query("DELETE FROM $tbl_category WHERE code='$node'"); - if (!empty($row['parent_id'])) { - updateParentCategoryChildrenCount($row['parent_id'], -1); + if (!empty($row['parent_id'])) { + self::updateParentCategoryChildrenCount($row['parent_id'], -1); + } } } -} - -/** - * @param string $code - * @param string $name - * @param string $canHaveCourses - * @param string $old_code - * @return bool - */ -function editNode($code, $name, $canHaveCourses, $old_code) -{ - $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - - $code = trim(Database::escape_string($code)); - $name = trim(Database::escape_string($name)); - $old_code = Database::escape_string($old_code); - $canHaveCourses = Database::escape_string($canHaveCourses); - $code = CourseManager::generate_course_code($code); - // Updating category - $sql = "UPDATE $tbl_category SET name='$name', code='$code', auth_course_child = '$canHaveCourses' + /** + * @param string $code + * @param string $name + * @param string $canHaveCourses + * @param string $old_code + * @return bool + */ + public static function editNode($code, $name, $canHaveCourses, $old_code) + { + $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + + $code = trim(Database::escape_string($code)); + $name = trim(Database::escape_string($name)); + $old_code = Database::escape_string($old_code); + $canHaveCourses = Database::escape_string($canHaveCourses); + + $code = CourseManager::generate_course_code($code); + // Updating category + $sql = "UPDATE $tbl_category SET name='$name', code='$code', auth_course_child = '$canHaveCourses' WHERE code = '$old_code'"; - Database::query($sql); + Database::query($sql); - // Updating children - $sql = "UPDATE $tbl_category SET parent_id = '$code' + // Updating children + $sql = "UPDATE $tbl_category SET parent_id = '$code' WHERE parent_id = '$old_code'"; - Database::query($sql); + Database::query($sql); - // Updating course category - $sql = "UPDATE $tbl_course SET category_code = '$code' + // Updating course category + $sql = "UPDATE $tbl_course SET category_code = '$code' WHERE category_code = '$old_code' "; - Database::query($sql); + Database::query($sql); - return true; -} - -/** - * Move a node up on display - * @param string $code - * @param int $tree_pos - * @param string $parent_id - * - * @return bool - */ -function moveNodeUp($code, $tree_pos, $parent_id) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $code = Database::escape_string($code); - $tree_pos = intval($tree_pos); - $parent_id = Database::escape_string($parent_id); - - $parentIdCondition = " AND (parent_id IS NULL OR parent_id = '' )"; - if (!empty($parent_id)) { - $parentIdCondition = " AND parent_id = '$parent_id' "; + return true; } - $sql = "SELECT code,tree_pos + /** + * Move a node up on display + * @param string $code + * @param int $tree_pos + * @param string $parent_id + * + * @return bool + */ + public static function moveNodeUp($code, $tree_pos, $parent_id) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $code = Database::escape_string($code); + $tree_pos = intval($tree_pos); + $parent_id = Database::escape_string($parent_id); + + $parentIdCondition = " AND (parent_id IS NULL OR parent_id = '' )"; + if (!empty($parent_id)) { + $parentIdCondition = " AND parent_id = '$parent_id' "; + } + + $sql = "SELECT code,tree_pos FROM $tbl_category WHERE tree_pos < $tree_pos @@ -275,352 +266,355 @@ function moveNodeUp($code, $tree_pos, $parent_id) ORDER BY tree_pos DESC LIMIT 0,1"; - $result = Database::query($sql); - if (!$row = Database::fetch_array($result)) { - $sql = "SELECT code, tree_pos + $result = Database::query($sql); + if (!$row = Database::fetch_array($result)) { + $sql = "SELECT code, tree_pos FROM $tbl_category WHERE tree_pos > $tree_pos $parentIdCondition ORDER BY tree_pos DESC LIMIT 0,1"; - $result2 = Database::query($sql); - if (!$row = Database::fetch_array($result2)) { - return false; + $result2 = Database::query($sql); + if (!$row = Database::fetch_array($result2)) { + return false; + } } - } - $sql = "UPDATE $tbl_category - SET tree_pos ='" . $row['tree_pos'] . "' + $sql = "UPDATE $tbl_category + SET tree_pos ='".$row['tree_pos']."' WHERE code='$code'"; - Database::query($sql); + Database::query($sql); - $sql = "UPDATE $tbl_category + $sql = "UPDATE $tbl_category SET tree_pos = '$tree_pos' - WHERE code= '" . $row['code'] . "'"; - Database::query($sql); - - return true; -} - -/** - * Counts the number of children categories a category has - * @param int $categoryId The ID of the category of which we want to count the children - * @param int $count The number of subcategories we counted this far - * @return mixed The number of subcategories this category has - */ -function courseCategoryChildrenCount($categoryId) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $categoryId = intval($categoryId); - $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)) { - $count += courseCategoryChildrenCount($row['id']); - } - $sql = "UPDATE $tbl_category SET children_count = $count WHERE id = $categoryId"; - Database::query($sql); - - return $count + 1; -} + WHERE code= '".$row['code']."'"; + Database::query($sql); -/** - * @param string $categoryCode - * - * @return array - */ -function getChildren($categoryCode) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $categoryCode = Database::escape_string($categoryCode); - $result = Database::query("SELECT code, id FROM $tbl_category WHERE parent_id = '$categoryCode'"); - $children = array(); - while ($row = Database::fetch_array($result, 'ASSOC')) { - $children[] = $row; - $subChildren = getChildren($row['code']); - $children = array_merge($children, $subChildren); + return true; } - return $children; -} + /** + * Counts the number of children categories a category has + * @param int $categoryId The ID of the category of which we want to count the children + * @param int $count The number of subcategories we counted this far + * @return mixed The number of subcategories this category has + */ + public static function courseCategoryChildrenCount($categoryId) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $categoryId = intval($categoryId); + $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)) { + $count += self::courseCategoryChildrenCount($row['id']); + } + $sql = "UPDATE $tbl_category SET children_count = $count WHERE id = $categoryId"; + Database::query($sql); -/** - * @param string $categoryCode - * - * @return array - */ -function getParents($categoryCode) -{ - if (empty($categoryCode)) { - return array(); + return $count + 1; } - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $categoryCode = Database::escape_string($categoryCode); - $sql = "SELECT code, parent_id FROM $tbl_category - WHERE code = '$categoryCode'"; + /** + * @param string $categoryCode + * + * @return array + */ + public static function getChildren($categoryCode) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $categoryCode = Database::escape_string($categoryCode); + $result = Database::query("SELECT code, id FROM $tbl_category WHERE parent_id = '$categoryCode'"); + $children = array(); + while ($row = Database::fetch_array($result, 'ASSOC')) { + $children[] = $row; + $subChildren = self::getChildren($row['code']); + $children = array_merge($children, $subChildren); + } - $result = Database::query($sql); - $children = array(); - while ($row = Database::fetch_array($result, 'ASSOC')) { - $parent = getCategory($row['parent_id']); - $children[] = $row; - $subChildren = getParents($parent['code']); - $children = array_merge($children, $subChildren); + return $children; } - return $children; -} + /** + * @param string $categoryCode + * + * @return array + */ + public static function getParents($categoryCode) + { + if (empty($categoryCode)) { + return array(); + } -/** - * @param string $categoryCode - * @return null|string - */ -function getParentsToString($categoryCode) -{ - $parents = getParents($categoryCode); + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $categoryCode = Database::escape_string($categoryCode); + $sql = "SELECT code, parent_id FROM $tbl_category + WHERE code = '$categoryCode'"; - if (!empty($parents)) { - $parents = array_reverse($parents); - $categories = array(); - foreach ($parents as $category) { - $categories[] = $category['code']; + $result = Database::query($sql); + $children = array(); + while ($row = Database::fetch_array($result, 'ASSOC')) { + $parent = self::getCategory($row['parent_id']); + $children[] = $row; + $subChildren = self::getParents($parent['code']); + $children = array_merge($children, $subChildren); } - $categoriesInString = implode(' > ', $categories).' > '; - return $categoriesInString; + return $children; } - return null; -} + /** + * @param string $categoryCode + * @return null|string + */ + public static function getParentsToString($categoryCode) + { + $parents = self::getParents($categoryCode); + + if (!empty($parents)) { + $parents = array_reverse($parents); + $categories = array(); + foreach ($parents as $category) { + $categories[] = $category['code']; + } + $categoriesInString = implode(' > ', $categories).' > '; -/** - * @param string $categorySource - * - * @return string - */ -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++; + return $categoriesInString; } - $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'; - $moveUrl = $mainUrl.'&id='.$category['code'].'&action=moveUp&tree_pos='.$category['tree_pos']; - $deleteUrl = $mainUrl.'&id='.$category['code'].'&action=delete'; + return null; + } - $actions = Display::url($editIcon, $editUrl).Display::url($moveIcon, $moveUrl).Display::url($deleteIcon, $deleteUrl); - $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 - ); + /** + * @param string $categorySource + * + * @return string + */ + public static function listCategories($categorySource) + { + $categorySource = isset($categorySource) ? $categorySource : null; + $categories = self::getCategories($categorySource); + + if (count($categories) > 0) { + $table = new HTML_Table(array('class' => 'data_table')); $column = 0; - foreach ($content as $value) { - $table->setCellContents($row, $column, $value); + $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'; + $moveUrl = $mainUrl.'&id='.$category['code'].'&action=moveUp&tree_pos='.$category['tree_pos']; + $deleteUrl = $mainUrl.'&id='.$category['code'].'&action=delete'; + + $actions = Display::url($editIcon, $editUrl).Display::url($moveIcon, $moveUrl).Display::url( + $deleteIcon, + $deleteUrl + ); + $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'); + return $table->toHtml(); + } else { + return Display::return_message(get_lang("NoCategories"), 'warning'); + } } -} -/** - * @return array - */ -function getCategoriesToDisplayInHomePage() -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $sql = "SELECT name FROM $tbl_category + /** + * @return array + */ + public static function getCategoriesToDisplayInHomePage() + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $sql = "SELECT name FROM $tbl_category WHERE parent_id IS NULL ORDER BY tree_pos"; - return Database::store_result(Database::query($sql)); -} + return Database::store_result(Database::query($sql)); + } -/** - * @param int $id - * - * @return bool - */ -function addToUrl($id) -{ - if (!isMultipleUrlSupport()) { - return false; + /** + * @param int $id + * + * @return bool + */ + public static function addToUrl($id) + { + UrlManager::addCourseCategoryListToUrl(array($id), array(api_get_current_access_url_id())); } - UrlManager::addCourseCategoryListToUrl(array($id), array(api_get_current_access_url_id())); -} -/** - * @param string $categoryCode - * - * @return array - */ -function getCategoriesCanBeAddedInCourse($categoryCode) -{ - $conditions = null; - $whereCondition = null; - if (isMultipleUrlSupport()) { + /** + * @param string $categoryCode + * + * @return array + */ + public static function getCategoriesCanBeAddedInCourse($categoryCode) + { $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); $conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)"; $whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id(); - } - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $sql = "SELECT code, name + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $sql = "SELECT code, name FROM $tbl_category c $conditions WHERE (auth_course_child = 'TRUE' OR code = '".Database::escape_string($categoryCode)."') $whereCondition ORDER BY tree_pos"; - $res = Database::query($sql); + $res = Database::query($sql); + + $categories[''] = '-'; + while ($cat = Database::fetch_array($res)) { + $categories[$cat['code']] = '('.$cat['code'].') '.$cat['name']; + ksort($categories); + } - $categories[''] = '-'; - while ($cat = Database::fetch_array($res)) { - $categories[$cat['code']] = '('.$cat['code'].') '.$cat['name']; - ksort($categories); + return $categories; } - return $categories; -} -/** - * @return array - */ -function browseCourseCategories() -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $conditions = null; - $whereCondition = null; + /** + * @return array + */ + public static function browseCourseCategories() + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); + $conditions = null; - if (isMultipleUrlSupport()) { $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); $conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)"; $whereCondition = " WHERE a.access_url_id = ".api_get_current_access_url_id(); - } - $sql = "SELECT c.* FROM $tbl_category c + + $sql = "SELECT c.* FROM $tbl_category c $conditions $whereCondition ORDER BY tree_pos ASC"; - $result = Database::query($sql); - $url_access_id = 1; - if (api_is_multiple_url_enabled()) { - $url_access_id = api_get_current_access_url_id(); - } - $countCourses = CourseManager :: countAvailableCourses($url_access_id); - - $categories = array(); - $categories[0][0] = array( - 'id' => 0, - 'name' => get_lang('DisplayAll'), - 'code' => 'ALL', - 'parent_id' => null, - 'tree_pos' => 0, - 'count_courses' => $countCourses - - ); - while ($row = Database::fetch_array($result)) { - $count_courses = countCoursesInCategory($row['code']); - $row['count_courses'] = $count_courses; - if (!isset($row['parent_id'])) { - $categories[0][$row['tree_pos']] = $row; - } else { - $categories[$row['parent_id']][$row['tree_pos']] = $row; + $result = Database::query($sql); + $url_access_id = 1; + if (api_is_multiple_url_enabled()) { + $url_access_id = api_get_current_access_url_id(); } - } + $countCourses = CourseManager::countAvailableCourses($url_access_id); - $count_courses = countCoursesInCategory(); - - $categories[0][count($categories[0])+1] = array( - 'id' =>0, - 'name' => get_lang('None'), - 'code' => 'NONE', - 'parent_id' => null, - 'tree_pos' => $row['tree_pos']+1, - 'children_count' => 0, - 'auth_course_child' => true, - 'auth_cat_child' => true, - 'count_courses' => $count_courses - ); - - return $categories; -} + $categories = array(); + $categories[0][0] = array( + 'id' => 0, + 'name' => get_lang('DisplayAll'), + 'code' => 'ALL', + 'parent_id' => null, + 'tree_pos' => 0, + 'count_courses' => $countCourses -/** - * @param string $category_code - * @param string $searchTerm - * @return int - */ -function countCoursesInCategory($category_code="", $searchTerm = '') -{ - $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - $categoryCode = Database::escape_string($category_code); - $searchTerm = Database::escape_string($searchTerm); - $categoryFilter = ''; - $searchFilter = ''; + ); + while ($row = Database::fetch_array($result)) { + $count_courses = self::countCoursesInCategory($row['code']); + $row['count_courses'] = $count_courses; + if (!isset($row['parent_id'])) { + $categories[0][$row['tree_pos']] = $row; + } else { + $categories[$row['parent_id']][$row['tree_pos']] = $row; + } + } - $specialCourseList = CourseManager::get_special_course_list(); + $count_courses = self::countCoursesInCategory(); + + $categories[0][count($categories[0]) + 1] = array( + 'id' => 0, + 'name' => get_lang('None'), + 'code' => 'NONE', + 'parent_id' => null, + 'tree_pos' => $row['tree_pos'] + 1, + 'children_count' => 0, + 'auth_course_child' => true, + 'auth_cat_child' => true, + 'count_courses' => $count_courses + ); - $without_special_courses = ''; - if (!empty($specialCourseList)) { - $without_special_courses = ' AND course.code NOT IN ("' . implode('","', $special_course_list) . '")'; + return $categories; } - $visibilityCondition = null; - $hidePrivate = api_get_setting('course_catalog_hide_private'); - if ($hidePrivate === 'true') { - $courseInfo = api_get_course_info(); - $courseVisibility = $courseInfo['visibility']; - $visibilityCondition = ' AND course.visibility <> 1'; - } + /** + * @param string $category_code + * @param string $searchTerm + * @return int + */ + public static function countCoursesInCategory($category_code = "", $searchTerm = '') + { + $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); + $categoryCode = Database::escape_string($category_code); + $searchTerm = Database::escape_string($searchTerm); + $categoryFilter = ''; + $searchFilter = ''; + + $specialCourseList = CourseManager::get_special_course_list(); + + $without_special_courses = ''; + if (!empty($specialCourseList)) { + $without_special_courses = ' AND course.code NOT IN ("'.implode('","', $specialCourseList).'")'; + } - if ($categoryCode == 'ALL') { - // Nothing to do - } elseif ($categoryCode == 'NONE') { - $categoryFilter = ' AND category_code = "" '; - } else { - $categoryFilter = ' AND category_code = "' . $categoryCode . '" '; - } + $visibilityCondition = null; + $hidePrivate = api_get_setting('course_catalog_hide_private'); + if ($hidePrivate === 'true') { + $courseInfo = api_get_course_info(); + $courseVisibility = $courseInfo['visibility']; + $visibilityCondition = ' AND course.visibility <> 1'; + } - if (!empty($searchTerm)) { - $searchFilter = ' AND (code LIKE "%' . $searchTerm . '%" - OR title LIKE "%' . $searchTerm . '%" - OR tutor_name LIKE "%' . $searchTerm . '%") '; - } + if ($categoryCode == 'ALL') { + // Nothing to do + } elseif ($categoryCode == 'NONE') { + $categoryFilter = ' AND category_code = "" '; + } else { + $categoryFilter = ' AND category_code = "'.$categoryCode.'" '; + } + + if (!empty($searchTerm)) { + $searchFilter = ' AND (code LIKE "%'.$searchTerm.'%" + OR title LIKE "%'.$searchTerm.'%" + OR tutor_name LIKE "%'.$searchTerm.'%") '; + } - $sql = "SELECT * FROM $tbl_course + $sql = "SELECT * FROM $tbl_course WHERE visibility != '0' AND visibility != '4' @@ -629,13 +623,13 @@ function countCoursesInCategory($category_code="", $searchTerm = '') $without_special_courses $visibilityCondition "; - // Showing only the courses of the current portal access_url_id. + // Showing only the courses of the current portal access_url_id. - if (api_is_multiple_url_enabled()) { - $url_access_id = api_get_current_access_url_id(); - if ($url_access_id != -1) { - $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); - $sql = "SELECT * FROM $tbl_course as course + if (api_is_multiple_url_enabled()) { + $url_access_id = api_get_current_access_url_id(); + if ($url_access_id != -1) { + $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.c_id = course.id) WHERE @@ -647,56 +641,56 @@ function countCoursesInCategory($category_code="", $searchTerm = '') $without_special_courses $visibilityCondition "; + } } - } - - return Database::num_rows(Database::query($sql)); -} - -/** - * @param string $category_code - * @param int $random_value - * @param array $limit will be used if $random_value is not set. - * This array should contains 'start' and 'length' keys - * @return array - */ -function browseCoursesInCategory($category_code, $random_value = null, $limit = array()) -{ - $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); - - $specialCourseList = CourseManager::get_special_course_list(); - $without_special_courses = ''; - if (!empty($specialCourseList)) { - $without_special_courses = ' AND course.code NOT IN ("' . implode('","', $special_course_list) . '")'; - } - $visibilityCondition = null; - $hidePrivate = api_get_setting('course_catalog_hide_private'); - if ($hidePrivate === 'true') { - $courseInfo = api_get_course_info(); - $courseVisibility = $courseInfo['visibility']; - $visibilityCondition = ' AND course.visibility <> 1'; + return Database::num_rows(Database::query($sql)); } - if (!empty($random_value)) { - $random_value = intval($random_value); - $sql = "SELECT COUNT(*) FROM $tbl_course"; - $result = Database::query($sql); - list($num_records) = Database::fetch_row($result); + /** + * @param string $category_code + * @param int $random_value + * @param array $limit will be used if $random_value is not set. + * This array should contains 'start' and 'length' keys + * @return array + */ + public static function browseCoursesInCategory($category_code, $random_value = null, $limit = array()) + { + $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); + + $specialCourseList = CourseManager::get_special_course_list(); + + $without_special_courses = ''; + if (!empty($specialCourseList)) { + $without_special_courses = ' AND course.code NOT IN ("'.implode('","', $specialCourseList).'")'; + } + $visibilityCondition = null; + $hidePrivate = api_get_setting('course_catalog_hide_private'); + if ($hidePrivate === 'true') { + $courseInfo = api_get_course_info(); + $courseVisibility = $courseInfo['visibility']; + $visibilityCondition = ' AND course.visibility <> 1'; + } + if (!empty($random_value)) { + $random_value = intval($random_value); - if (api_is_multiple_url_enabled()) { + $sql = "SELECT COUNT(*) FROM $tbl_course"; + $result = Database::query($sql); + list($num_records) = Database::fetch_row($result); - $url_access_id = api_get_current_access_url_id(); - $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + if (api_is_multiple_url_enabled()) { - $sql = "SELECT COUNT(*) FROM $tbl_course course + $url_access_id = api_get_current_access_url_id(); + $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + + $sql = "SELECT COUNT(*) FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.c_id = course.id) WHERE access_url_id = $url_access_id "; - $result = Database::query($sql); - list($num_records) = Database::fetch_row($result); + $result = Database::query($sql); + list($num_records) = Database::fetch_row($result); - $sql = "SELECT course.id FROM $tbl_course course + $sql = "SELECT course.id FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.c_id = course.id) WHERE @@ -705,54 +699,54 @@ function browseCoursesInCategory($category_code, $random_value = null, $limit = $without_special_courses $visibilityCondition ORDER BY RAND() LIMIT 0, $random_value"; - } else { - $sql = "SELECT id FROM $tbl_course course + } else { + $sql = "SELECT id FROM $tbl_course course WHERE RAND()*$num_records< $random_value $without_special_courses $visibilityCondition ORDER BY RAND() LIMIT 0, $random_value"; - } + } - $result = Database::query($sql); - $id_in = null; - while (list($id) = Database::fetch_row($result)) { - if ($id_in) { - $id_in.=",$id"; - } else { - $id_in = "$id"; + $result = Database::query($sql); + $id_in = null; + while (list($id) = Database::fetch_row($result)) { + if ($id_in) { + $id_in .= ",$id"; + } else { + $id_in = "$id"; + } } - } - if ($id_in === null) { - return array(); - } - $sql = "SELECT * FROM $tbl_course WHERE id IN($id_in)"; - } else { - $limitFilter = getLimitFilterFromArray($limit); - $category_code = Database::escape_string($category_code); - if (empty($category_code) || $category_code == "ALL") { - $sql = "SELECT * FROM $tbl_course + if ($id_in === null) { + return array(); + } + $sql = "SELECT * FROM $tbl_course WHERE id IN($id_in)"; + } else { + $limitFilter = self::getLimitFilterFromArray($limit); + $category_code = Database::escape_string($category_code); + if (empty($category_code) || $category_code == "ALL") { + $sql = "SELECT * FROM $tbl_course WHERE 1=1 $without_special_courses $visibilityCondition ORDER BY title $limitFilter "; - } else { - if ($category_code == 'NONE') { - $category_code = ''; - } - $sql = "SELECT * FROM $tbl_course + } else { + if ($category_code == 'NONE') { + $category_code = ''; + } + $sql = "SELECT * FROM $tbl_course WHERE category_code='$category_code' $without_special_courses $visibilityCondition ORDER BY title $limitFilter "; - } + } - //showing only the courses of the current Chamilo access_url_id - if (api_is_multiple_url_enabled()) { - $url_access_id = api_get_current_access_url_id(); - $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); - if ($category_code != "ALL") { - $sql = "SELECT * FROM $tbl_course as course + //showing only the courses of the current Chamilo access_url_id + if (api_is_multiple_url_enabled()) { + $url_access_id = api_get_current_access_url_id(); + $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); + if ($category_code != "ALL") { + $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.c_id = course.id) WHERE @@ -761,8 +755,8 @@ function browseCoursesInCategory($category_code, $random_value = null, $limit = $without_special_courses $visibilityCondition ORDER BY title $limitFilter"; - } else { - $sql = "SELECT * FROM $tbl_course as course + } else { + $sql = "SELECT * FROM $tbl_course as course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.c_id = course.id) WHERE @@ -770,128 +764,122 @@ function browseCoursesInCategory($category_code, $random_value = null, $limit = $without_special_courses $visibilityCondition ORDER BY title $limitFilter"; + } } } - } - $result = Database::query($sql); - $courses = array(); - while ($row = Database::fetch_array($result)) { - $row['registration_code'] = !empty($row['registration_code']); - $count_users = CourseManager::get_users_count_in_course($row['code']); - $count_connections_last_month = Tracking::get_course_connections_count( - $row['id'], - 0, - api_get_utc_datetime(time() - (30 * 86400)) - ); + $result = Database::query($sql); + $courses = array(); + while ($row = Database::fetch_array($result)) { + $row['registration_code'] = !empty($row['registration_code']); + $count_users = CourseManager::get_users_count_in_course($row['code']); + $count_connections_last_month = Tracking::get_course_connections_count( + $row['id'], + 0, + api_get_utc_datetime(time() - (30 * 86400)) + ); - if ($row['tutor_name'] == '0') { - $row['tutor_name'] = get_lang('NoManager'); + if ($row['tutor_name'] == '0') { + $row['tutor_name'] = get_lang('NoManager'); + } + $point_info = CourseManager::get_course_ranking($row['id'], 0); + $courses[] = array( + 'real_id' => $row['id'], + 'point_info' => $point_info, + 'code' => $row['code'], + 'directory' => $row['directory'], + 'visual_code' => $row['visual_code'], + 'title' => $row['title'], + 'tutor' => $row['tutor_name'], + 'subscribe' => $row['subscribe'], + 'unsubscribe' => $row['unsubscribe'], + 'registration_code' => $row['registration_code'], + 'creation_date' => $row['creation_date'], + 'visibility' => $row['visibility'], + 'count_users' => $count_users, + 'count_connections' => $count_connections_last_month + ); } - $point_info = CourseManager::get_course_ranking($row['id'], 0); - $courses[] = array( - 'real_id' => $row['id'], - 'point_info' => $point_info, - 'code' => $row['code'], - 'directory' => $row['directory'], - 'visual_code' => $row['visual_code'], - 'title' => $row['title'], - 'tutor' => $row['tutor_name'], - 'subscribe' => $row['subscribe'], - 'unsubscribe' => $row['unsubscribe'], - 'registration_code' => $row['registration_code'], - 'creation_date' => $row['creation_date'], - 'visibility' => $row['visibility'], - 'count_users' => $count_users, - 'count_connections' => $count_connections_last_month - ); + + return $courses; } - return $courses; -} + /** + * create recursively all categories as option of the select passed in parameter. + * + * @param HTML_QuickForm_Element $element + * @param string $defaultCode the option value to select by default (used mainly for edition of courses) + * @param string $parentCode the parent category of the categories added (default=null for root category) + * @param string $padding the indent param (you shouldn't indicate something here) + */ + public static function setCategoriesInForm($element, $defaultCode = null, $parentCode = null, $padding = null) + { + $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); -/** - * create recursively all categories as option of the select passed in parameter. - * - * @param HTML_QuickForm_Element $element - * @param string $defaultCode the option value to select by default (used mainly for edition of courses) - * @param string $parentCode the parent category of the categories added (default=null for root category) - * @param string $padding the indent param (you shouldn't indicate something here) - */ -function setCategoriesInForm($element, $defaultCode = null, $parentCode = null, $padding = null) -{ - $tbl_category = Database::get_main_table(TABLE_MAIN_CATEGORY); - $conditions = null; - $whereCondition = null; - if (isMultipleUrlSupport()) { $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); $conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)"; $whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id(); - } - $sql = "SELECT code, name, auth_course_child, auth_cat_child + $sql = "SELECT code, name, auth_course_child, auth_cat_child FROM ".$tbl_category." c $conditions WHERE parent_id ".(empty($parentCode) ? "IS NULL" : "='".Database::escape_string($parentCode)."'")." $whereCondition ORDER BY name, code"; - $res = Database::query($sql); + $res = Database::query($sql); - while ($cat = Database::fetch_array($res, 'ASSOC')) { - $params = $cat['auth_course_child'] == 'TRUE' ? '' : 'disabled'; - $params .= ($cat['code'] == $defaultCode) ? ' selected' : ''; - $option = $padding.' '.$cat['name'].' ('.$cat['code'].')'; + while ($cat = Database::fetch_array($res, 'ASSOC')) { + $params = $cat['auth_course_child'] == 'TRUE' ? '' : 'disabled'; + $params .= ($cat['code'] == $defaultCode) ? ' selected' : ''; + $option = $padding.' '.$cat['name'].' ('.$cat['code'].')'; - $element->addOption($option, $cat['code'], $params); - if ($cat['auth_cat_child'] == 'TRUE') { - setCategoriesInForm($element, $defaultCode, $cat['code'], $padding.' - '); + $element->addOption($option, $cat['code'], $params); + if ($cat['auth_cat_child'] == 'TRUE') { + self::setCategoriesInForm($element, $defaultCode, $cat['code'], $padding.' - '); + } } } -} -/** - * @param array $list - * @return array - */ -function getCourseCategoryNotInList($list) -{ - $table = Database::get_main_table(TABLE_MAIN_CATEGORY); - if (empty($list)) { - return array(); - } + /** + * @param array $list + * @return array + */ + public static function getCourseCategoryNotInList($list) + { + $table = Database::get_main_table(TABLE_MAIN_CATEGORY); + if (empty($list)) { + return array(); + } - $list = array_map('intval', $list); - $listToString = implode("','", $list); + $list = array_map('intval', $list); + $listToString = implode("','", $list); - $sql = "SELECT * FROM $table + $sql = "SELECT * FROM $table WHERE id NOT IN ('$listToString') AND (parent_id IS NULL) "; - $result = Database::query($sql); - - return Database::store_result($result, 'ASSOC'); -} + $result = Database::query($sql); -/** - * @param string $keyword - * @return array|null - */ -function searchCategoryByKeyword($keyword) -{ - if (empty($keyword)) { - return null; + return Database::store_result($result, 'ASSOC'); } - $tableCategory = Database::get_main_table(TABLE_MAIN_CATEGORY); - $conditions = null; - $whereCondition = null; - if (isMultipleUrlSupport()) { + /** + * @param string $keyword + * @return array|null + */ + public static function searchCategoryByKeyword($keyword) + { + if (empty($keyword)) { + return null; + } + + $tableCategory = Database::get_main_table(TABLE_MAIN_CATEGORY); + $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); $conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)"; $whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id(); - } - $keyword = Database::escape_string($keyword); + $keyword = Database::escape_string($keyword); - $sql = "SELECT c.*, c.name as text + $sql = "SELECT c.*, c.name as text FROM $tableCategory c $conditions WHERE ( @@ -899,257 +887,252 @@ function searchCategoryByKeyword($keyword) ) AND auth_course_child = 'TRUE' $whereCondition "; - $result = Database::query($sql); - return Database::store_result($result, 'ASSOC'); -} + $result = Database::query($sql); -/** - * @param array $list - * @return array - */ -function searchCategoryById($list) -{ - if (empty($list)) { - return array(); - } else { - $list = array_map('intval', $list); - $list = implode("','", $list); + return Database::store_result($result, 'ASSOC'); } - $tableCategory = Database::get_main_table(TABLE_MAIN_CATEGORY); - $conditions = null; - $whereCondition = null; - if (isMultipleUrlSupport()) { + /** + * @param array $list + * @return array + */ + public static function searchCategoryById($list) + { + if (empty($list)) { + return array(); + } else { + $list = array_map('intval', $list); + $list = implode("','", $list); + } + + $tableCategory = Database::get_main_table(TABLE_MAIN_CATEGORY); + $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); $conditions = " INNER JOIN $table a ON (c.id = a.course_category_id)"; $whereCondition = " AND a.access_url_id = ".api_get_current_access_url_id(); - } - $sql = "SELECT c.*, c.name as text FROM $tableCategory c $conditions + $sql = "SELECT c.*, c.name as text FROM $tableCategory c $conditions WHERE c.id IN $list $whereCondition"; - $result = Database::query($sql); - return Database::store_result($result, 'ASSOC'); -} - -/** - * @return array - */ -function getLimitArray() -{ - $pageCurrent = isset($_REQUEST['pageCurrent']) ? - intval($_GET['pageCurrent']) : - 1; - $pageLength = isset($_REQUEST['pageLength']) ? - intval($_GET['pageLength']) : - 10; - return array( - 'start' => ($pageCurrent - 1) * $pageLength, - 'current' => $pageCurrent, - 'length' => $pageLength, - ); -} + $result = Database::query($sql); -/** - * Return LIMIT to filter SQL query - * @param array $limit - * @return string - */ -function getLimitFilterFromArray($limit) -{ - $limitFilter = ''; - if (!empty($limit) && is_array($limit)) { - $limitStart = isset($limit['start']) ? $limit['start'] : 0; - $limitLength = isset($limit['length']) ? $limit['length'] : 10; - $limitFilter = 'LIMIT ' . $limitStart . ', ' . $limitLength; + return Database::store_result($result, 'ASSOC'); } - return $limitFilter; -} - -/** - * Get Pagination HTML div - * @param $pageCurrent - * @param $pageLength - * @param $pageTotal - * @return string - */ -function getCataloguePagination($pageCurrent, $pageLength, $pageTotal) -{ - // Start empty html - $pageDiv = ''; - $html=''; - $pageBottom = max(1, $pageCurrent - 3); - $pageTop = min($pageTotal, $pageCurrent + 3); - - if ($pageBottom > 1) { - $pageDiv .= getPageNumberItem(1, $pageLength); - if ($pageBottom > 2) { - $pageDiv .= getPageNumberItem($pageBottom - 1, $pageLength, null, '...'); - } - } else { - // Nothing to do + /** + * @return array + */ + public static function getLimitArray() + { + $pageCurrent = isset($_REQUEST['pageCurrent']) ? + intval($_GET['pageCurrent']) : + 1; + $pageLength = isset($_REQUEST['pageLength']) ? + intval($_GET['pageLength']) : + 10; + + return array( + 'start' => ($pageCurrent - 1) * $pageLength, + 'current' => $pageCurrent, + 'length' => $pageLength, + ); } - // For each page add its page button to html - for ( - $i = $pageBottom; - $i <= $pageTop; - $i++ - ) { - if ($i === $pageCurrent) { - $pageItemAttributes = array('class' => 'active'); - } else { - $pageItemAttributes = array(); + /** + * Return LIMIT to filter SQL query + * @param array $limit + * @return string + */ + public static function getLimitFilterFromArray($limit) + { + $limitFilter = ''; + if (!empty($limit) && is_array($limit)) { + $limitStart = isset($limit['start']) ? $limit['start'] : 0; + $limitLength = isset($limit['length']) ? $limit['length'] : 10; + $limitFilter = 'LIMIT '.$limitStart.', '.$limitLength; } - $pageDiv .= getPageNumberItem($i, $pageLength, $pageItemAttributes); + return $limitFilter; } - // Check if current page is the last page - if ($pageTop < $pageTotal) { - if ($pageTop < ($pageTotal - 1)) { - $pageDiv .= getPageNumberItem($pageTop + 1, $pageLength, null, '...'); + /** + * Get Pagination HTML div + * @param $pageCurrent + * @param $pageLength + * @param $pageTotal + * @return string + */ + public static function getCatalogPagination($pageCurrent, $pageLength, $pageTotal) + { + // Start empty html + $pageDiv = ''; + $html = ''; + $pageBottom = max(1, $pageCurrent - 3); + $pageTop = min($pageTotal, $pageCurrent + 3); + + if ($pageBottom > 1) { + $pageDiv .= self::getPageNumberItem(1, $pageLength); + if ($pageBottom > 2) { + $pageDiv .= self::getPageNumberItem($pageBottom - 1, $pageLength, null, '...'); + } } - $pageDiv .= getPageNumberItem($pageTotal, $pageLength); - } - // Complete pagination html - $pageDiv = Display::tag('ul', $pageDiv, array('class' => 'pagination')); + // For each page add its page button to html + for ( + $i = $pageBottom; + $i <= $pageTop; + $i++ + ) { + if ($i === $pageCurrent) { + $pageItemAttributes = array('class' => 'active'); + } else { + $pageItemAttributes = array(); + } + $pageDiv .= self::getPageNumberItem($i, $pageLength, $pageItemAttributes); + } + // Check if current page is the last page - $html .= ''; - return $html; -} + if ($pageTop < $pageTotal) { + if ($pageTop < ($pageTotal - 1)) { + $pageDiv .= self::getPageNumberItem($pageTop + 1, $pageLength, null, '...'); + } + $pageDiv .= self::getPageNumberItem($pageTotal, $pageLength); + } -/** - * Return URL to course catalog - * @param int $pageCurrent - * @param int $pageLength - * @param string $categoryCode - * @param int $hiddenLinks - * @param string $action - * @return string - */ -function getCourseCategoryUrl( - $pageCurrent, - $pageLength, - $categoryCode = null, - $hiddenLinks = null, - $action = null -) { - $requestAction = isset($_REQUEST['action']) ? Security::remove_XSS($_REQUEST['action']) : null; - $action = isset($action) ? Security::remove_XSS($action) : $requestAction; - $searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : null; - - if ($action === 'subscribe_user_with_password') { - $action = 'subscribe'; + // Complete pagination html + $pageDiv = Display::tag('ul', $pageDiv, array('class' => 'pagination')); + $html .= ''; + + return $html; } - $categoryCodeRequest = isset($_REQUEST['category_code']) ? Security::remove_XSS($_REQUEST['category_code']) : null; - $categoryCode = isset($categoryCode) ? Security::remove_XSS($categoryCode) : $categoryCodeRequest; + /** + * Return URL to course catalog + * @param int $pageCurrent + * @param int $pageLength + * @param string $categoryCode + * @param int $hiddenLinks + * @param string $action + * @return string + */ + public static function getCourseCategoryUrl( + $pageCurrent, + $pageLength, + $categoryCode = null, + $hiddenLinks = null, + $action = null + ) { + $requestAction = isset($_REQUEST['action']) ? Security::remove_XSS($_REQUEST['action']) : null; + $action = isset($action) ? Security::remove_XSS($action) : $requestAction; + $searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : null; + + if ($action === 'subscribe_user_with_password') { + $action = 'subscribe'; + } + + $categoryCodeRequest = isset($_REQUEST['category_code']) ? Security::remove_XSS( + $_REQUEST['category_code'] + ) : null; + $categoryCode = isset($categoryCode) ? Security::remove_XSS($categoryCode) : $categoryCodeRequest; - $hiddenLinksRequest = isset($_REQUEST['hidden_links']) ? Security::remove_XSS($_REQUEST['hidden_links']) : null; - $hiddenLinks = isset($hiddenLinks) ? Security::remove_XSS($hiddenLinksRequest) : $categoryCodeRequest; + $hiddenLinksRequest = isset($_REQUEST['hidden_links']) ? Security::remove_XSS($_REQUEST['hidden_links']) : null; + $hiddenLinks = isset($hiddenLinks) ? Security::remove_XSS($hiddenLinksRequest) : $categoryCodeRequest; // Start URL with params - $pageUrl = api_get_self() . - '?action=' . $action . - '&category_code=' .$categoryCode. - '&hidden_links=' .$hiddenLinks. - '&pageCurrent=' . $pageCurrent . - '&pageLength=' . $pageLength - ; - - switch ($action) { - case 'subscribe' : - // for search - $pageUrl .= - '&search_term=' . $searchTerm . - '&search_course=1' . - '&sec_token=' . $_SESSION['sec_token']; - break; - case 'display_courses' : - // No break - default : - break; + $pageUrl = api_get_self(). + '?action='.$action. + '&category_code='.$categoryCode. + '&hidden_links='.$hiddenLinks. + '&pageCurrent='.$pageCurrent. + '&pageLength='.$pageLength; + + switch ($action) { + case 'subscribe': + // for search + $pageUrl .= + '&search_term='.$searchTerm. + '&search_course=1'. + '&sec_token='.$_SESSION['sec_token']; + break; + case 'display_courses': + // No break + default: + break; + } + return $pageUrl; } - return $pageUrl; -} + /** + * Get li HTML of page number + * @param $pageNumber + * @param $pageLength + * @param array $liAttributes + * @param string $content + * @return string + */ + public static function getPageNumberItem($pageNumber, $pageLength, $liAttributes = array(), $content = '') + { + // Get page URL + $url = self::getCourseCategoryUrl( + $pageNumber, + $pageLength + ); -/** - * Get li HTML of page number - * @param $pageNumber - * @param $pageLength - * @param array $liAttributes - * @param string $content - * @return string - */ -function getPageNumberItem($pageNumber, $pageLength, $liAttributes = array(), $content = '') -{ - // Get page URL - $url = getCourseCategoryUrl( - $pageNumber, - $pageLength - ); - - // If is current page ('active' class) clear URL - if (isset($liAttributes) && is_array($liAttributes) && isset($liAttributes['class'])) { - if (strpos('active', $liAttributes['class']) !== false) { - $url = ''; + // If is current page ('active' class) clear URL + if (isset($liAttributes) && is_array($liAttributes) && isset($liAttributes['class'])) { + if (strpos('active', $liAttributes['class']) !== false) { + $url = ''; + } } - } - $content = !empty($content) ? $content : $pageNumber; + $content = !empty($content) ? $content : $pageNumber; - return Display::tag( - 'li', - Display::url( - $content, - $url - ), - $liAttributes - ); -} -/** - * Return the name tool by action - * @param string $action - * @return string - */ -function getCourseCatalogNameTools($action) -{ - $nameTools = get_lang('SortMyCourses'); - if (empty($action)) { - return $nameTools; //should never happen + return Display::tag( + 'li', + Display::url( + $content, + $url + ), + $liAttributes + ); } - switch ($action) { - case 'createcoursecategory' : - $nameTools = get_lang('CreateCourseCategory'); - break; - case 'subscribe' : - $nameTools = get_lang('CourseManagement'); - break; - case 'subscribe_user_with_password' : - $nameTools = get_lang('CourseManagement'); - break; - case 'display_random_courses' : - // No break - case 'display_courses' : - $nameTools = get_lang('CourseManagement'); - break; - case 'display_sessions' : - $nameTools = get_lang('Sessions'); - break; - default : - // Nothing to do - break; - } + /** + * Return the name tool by action + * @param string $action + * @return string + */ + public static function getCourseCatalogNameTools($action) + { + $nameTools = get_lang('SortMyCourses'); + if (empty($action)) { + return $nameTools; //should never happen + } - return $nameTools; -} + switch ($action) { + case 'createcoursecategory': + $nameTools = get_lang('CreateCourseCategory'); + break; + case 'subscribe': + $nameTools = get_lang('CourseManagement'); + break; + case 'subscribe_user_with_password': + $nameTools = get_lang('CourseManagement'); + break; + case 'display_random_courses': + // No break + case 'display_courses': + $nameTools = get_lang('CourseManagement'); + break; + case 'display_sessions': + $nameTools = get_lang('Sessions'); + break; + default: + // Nothing to do + break; + } -/** - CREATE TABLE IF NOT EXISTS access_url_rel_course_category (access_url_id int unsigned NOT NULL, course_category_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, course_category_id)); - */ + return $nameTools; + } +} diff --git a/main/inc/lib/urlmanager.lib.php b/main/inc/lib/urlmanager.lib.php index dc026b3b99..267c8f0e7b 100755 --- a/main/inc/lib/urlmanager.lib.php +++ b/main/inc/lib/urlmanager.lib.php @@ -1006,8 +1006,8 @@ class UrlManager foreach ($list as $id) { UrlManager::addCourseCategoryToUrl($id, $urlId); - $categoryInfo = getCategoryById($id); - $children = getChildren($categoryInfo['code']); + $categoryInfo = CourseCategory::getCategoryById($id); + $children = CourseCategory::getChildren($categoryInfo['code']); if (!empty($children)) { foreach ($children as $category) { UrlManager::addCourseCategoryToUrl($category['id'], $urlId); @@ -1019,9 +1019,9 @@ class UrlManager foreach ($existingItems as $id) { if (!in_array($id, $list)) { UrlManager::deleteUrlRelCourseCategory($id, $urlId); - $categoryInfo = getCategoryById($id); + $categoryInfo = CourseCategory::getCategoryById($id); - $children = getChildren($categoryInfo['code']); + $children = CourseCategory::getChildren($categoryInfo['code']); if (!empty($children)) { foreach ($children as $category) { UrlManager::deleteUrlRelCourseCategory($category['id'], $urlId); diff --git a/main/inc/lib/userportal.lib.php b/main/inc/lib/userportal.lib.php index fed1fc79b6..e28c698848 100755 --- a/main/inc/lib/userportal.lib.php +++ b/main/inc/lib/userportal.lib.php @@ -456,11 +456,9 @@ class IndexManager // Showing only the category of courses of the current access_url_id if (api_is_multiple_url_enabled()) { - $courseCategoryCondition = null; - if (isMultipleUrlSupport()) { - $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); - $courseCategoryCondition = " INNER JOIN $table a ON (t1.id = a.course_category_id)"; - } + + $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); + $courseCategoryCondition = " INNER JOIN $table a ON (t1.id = a.course_category_id)"; $url_access_id = api_get_current_access_url_id(); if ($url_access_id != -1) { diff --git a/main/session/session_list.php b/main/session/session_list.php index f5faa65dbd..420256be7e 100644 --- a/main/session/session_list.php +++ b/main/session/session_list.php @@ -57,7 +57,7 @@ $courseSelect = $sessionFilter->addElement( if (!empty($courseId)) { $courseInfo = api_get_course_info_by_id($courseId); - $parents = getParentsToString($courseInfo['categoryCode']); + $parents = CourseCategory::getParentsToString($courseInfo['categoryCode']); $courseSelect->addOption($parents . $courseInfo['title'], $courseInfo['code'], ['selected' => 'selected']); } diff --git a/main/template/default/auth/courses_categories.php b/main/template/default/auth/courses_categories.php index 101514c305..7734d8c107 100755 --- a/main/template/default/auth/courses_categories.php +++ b/main/template/default/auth/courses_categories.php @@ -23,7 +23,7 @@ $pageLength = isset($pageLength) ? $pageLength : 10; $pageTotal = intval(ceil(intval($countCoursesInCategory) / $pageLength)); $cataloguePagination = $pageTotal > 1 ? - getCataloguePagination($pageCurrent, $pageLength, $pageTotal) : + CourseCategory::getCatalogPagination($pageCurrent, $pageLength, $pageTotal) : ''; $search_term = isset($search_term) ? $search_term :null; @@ -111,7 +111,7 @@ $code = isset($code) ? $code : null; -
+
@@ -164,7 +164,7 @@ $code = isset($code) ? $code : null; if ($showSessions) { ?> diff --git a/src/Chamilo/CoreBundle/Entity/AccessUrlRelCourseCategory.php b/src/Chamilo/CoreBundle/Entity/AccessUrlRelCourseCategory.php new file mode 100644 index 0000000000..36b7d0e354 --- /dev/null +++ b/src/Chamilo/CoreBundle/Entity/AccessUrlRelCourseCategory.php @@ -0,0 +1,80 @@ +accessUrlId = $accessUrlId; + + return $this; + } + + /** + * Get accessUrlId + * + * @return integer + */ + public function getAccessUrlId() + { + return $this->accessUrlId; + } + + /** + * @return int + */ + public function getCourseCategoryId() + { + return $this->courseCategoryId; + } + + /** + * @param int $courseCategoryId + * @return AccessUrlRelCourseCategory + */ + public function setCourseCategoryId($courseCategoryId) + { + $this->courseCategoryId = $courseCategoryId; + + return $this; + } +} diff --git a/src/Chamilo/CoreBundle/Entity/AccessUrlRelSession.php b/src/Chamilo/CoreBundle/Entity/AccessUrlRelSession.php index 0471e2823c..642cf8f6ed 100644 --- a/src/Chamilo/CoreBundle/Entity/AccessUrlRelSession.php +++ b/src/Chamilo/CoreBundle/Entity/AccessUrlRelSession.php @@ -1,4 +1,5 @@