diff --git a/main/auth/courses_controller.php b/main/auth/courses_controller.php index 6d89d9484c..0d69b35b92 100755 --- a/main/auth/courses_controller.php +++ b/main/auth/courses_controller.php @@ -43,7 +43,6 @@ class CoursesController $data['user_courses'] = $this->model->get_courses_of_user($user_id); $data['user_course_categories'] = $this->model->get_user_course_categories(); $data['courses_in_category'] = $this->model->get_courses_in_category(); - $data['all_user_categories'] = $this->model->get_user_course_categories(); $data['action'] = $action; $data['message'] = $message; diff --git a/main/auth/courses_list.php b/main/auth/courses_list.php index c6b9e5f0e0..0cb19588d6 100755 --- a/main/auth/courses_list.php +++ b/main/auth/courses_list.php @@ -3,6 +3,7 @@ /** * View (MVC patter) for courses + * @todo fix this. use twig templates * @author Christian Fasanando - Beeznest * @package chamilo.auth */ @@ -11,9 +12,7 @@ api_block_anonymous_users(); $stok = Security::get_token(); $courses_without_category = isset($courses_in_category[0]) ? $courses_in_category[0] : null; - ?> -
@@ -21,7 +20,6 @@ $courses_without_category = isset($courses_in_category[0]) ? $courses_in_categor
- '; - - if (isset($_GET['categoryid']) && $_GET['categoryid'] == $row['id']) { ?> - + $counter = 0; + $last = end($user_course_categories); + foreach ($user_course_categories as $row) { + echo Display::page_subheader($row['title']); + echo ''; + if (isset($_GET['categoryid']) && $_GET['categoryid'] == $row['id']) { ?> + +
+ + + + +
+ + + + + + + + + + + -
- - - - -
+ + + + + + + + "javascript: if (!confirm('".addslashes(api_htmlentities(get_lang("CourseCategoryAbout2bedeleted"), ENT_QUOTES, api_get_system_encoding()))."')) return false;"),22) ?> + +
'; + // Show the courses inside this category + echo ''; + $number_of_courses = isset($courses_in_category[$row['id']]) ? count($courses_in_category[$row['id']]) : 0; + $key = 0; + if (!empty($courses_in_category[$row['id']])) { + foreach ($courses_in_category[$row['id']] as $course) { + ?> + + +
+ +
+ +
+ + - - - - +
+ + + + +
+ +
+ + + + - - - - + + - - + + + + - - - + 0) { ?> + + - + - - "javascript: if (!confirm('".addslashes(api_htmlentities(get_lang("CourseCategoryAbout2bedeleted"), ENT_QUOTES, api_get_system_encoding()))."')) return false;"),22) ?> - -
'; - // Show the courses inside this category - echo ''; - - $number_of_courses = isset($courses_in_category[$row['id']]) ? count($courses_in_category[$row['id']]) : 0; - $key = 0; - if (!empty($courses_in_category[$row['id']])) { - foreach ($courses_in_category[$row['id']] as $course) { - ?> - - -
- -
- -
- - - -
- - - - -
- -
- - - - + + + + + + - - - - - - - - - 0) { ?> - - - - - - - - - - - - - - - -
-
- +
+
+ -
')) return false"> - - - -
-
- " method="post" onsubmit="javascript: if (!confirm('')) return false"> + + + + + + '; + $key++; } + echo '
'; + } } } diff --git a/main/inc/lib/auth.lib.php b/main/inc/lib/auth.lib.php index cac78dbebe..58597866ee 100755 --- a/main/inc/lib/auth.lib.php +++ b/main/inc/lib/auth.lib.php @@ -104,18 +104,7 @@ class Auth */ public function get_user_course_categories() { - $user_id = api_get_user_id(); - $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); - $sql = "SELECT * FROM " . $table_category . " - WHERE user_id=$user_id - ORDER BY sort ASC"; - $result = Database::query($sql); - $output = array(); - while ($row = Database::fetch_array($result)) { - $output[] = $row; - } - - return $output; + return CourseManager::get_user_course_categories(api_get_user_id()); } /** @@ -283,65 +272,53 @@ class Auth /** * Moves the course one place up or down - * @param string Direction up/down - * @param string Category id + * @param string $direction Direction up/down + * @param string $category2move Category id * @return bool True If it success */ public function move_category($direction, $category2move) { - // the database definition of the table that stores the user defined course categories - $table_user_defined_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); + $userId = api_get_user_id(); + $userCategories = $this->get_user_course_categories(); + $categories = array_values($userCategories); - $current_user_id = api_get_user_id(); - $user_coursecategories = $this->get_user_course_categories(); - $user_course_categories_info = $this->get_user_course_categories_info(); - $result = false; - - foreach ($user_coursecategories as $key => $category) { + $previous = null; + $target_category = []; + foreach ($categories as $key => $category) { $category_id = $category['id']; if ($category2move == $category_id) { // source_course is the course where we clicked the up or down icon - $source_category = $user_course_categories_info[$category2move]; + $source_category = $userCategories[$category2move]; // target_course is the course before/after the source_course (depending on the up/down icon) if ($direction == 'up') { - $target_category = $user_course_categories_info[$user_coursecategories[$key - 1]['id']]; + if (isset($categories[$key - 1])) { + $target_category = $userCategories[$categories[$key - 1]['id']]; + } } else { - $target_category = $user_course_categories_info[$user_coursecategories[$key + 1]['id']]; + if (isset($categories[$key + 1])) { + $target_category = $userCategories[$categories[$key + 1]['id']]; + } } } } + $result = false; if (count($target_category) > 0 && count($source_category) > 0) { - $sql_update1 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($target_category['sort']) . "' - WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $current_user_id . "'"; - $sql_update2 = "UPDATE $table_user_defined_category SET sort='" . Database::escape_string($source_category['sort']) . "' - WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $current_user_id . "'"; - - $result1 = Database::query($sql_update2); - $result2 = Database::query($sql_update1); - if (Database::affected_rows($result1) && Database::affected_rows($result2)) { + $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); + $sql = "UPDATE $table SET + sort = '" . Database::escape_string($target_category['sort']) . "' + WHERE id='" . intval($source_category['id']) . "' AND user_id='" . $userId . "'"; + $resultFirst = Database::query($sql); + $sql = "UPDATE $table SET + sort = '" . Database::escape_string($source_category['sort']) . "' + WHERE id='" . intval($target_category['id']) . "' AND user_id='" . $userId . "'"; + $resultSecond = Database::query($sql); + if (Database::affected_rows($resultFirst) && Database::affected_rows($resultSecond)) { $result = true; } } - return $result; - } - /** - * Retrieves the user defined course categories and all the info that goes with it - * @return array containing all the info of the user defined courses categories with the id as key of the array - */ - public function get_user_course_categories_info() - { - $current_user_id = api_get_user_id(); - $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); - $sql = "SELECT * FROM " . $table_category . " - WHERE user_id='" . $current_user_id . "' - ORDER BY sort ASC"; - $result = Database::query($sql); - while ($row = Database::fetch_array($result)) { - $output[$row['id']] = $row; - } - return $output; + return $result; } /** diff --git a/main/inc/lib/course.lib.php b/main/inc/lib/course.lib.php index b1a156783a..eab169bb57 100755 --- a/main/inc/lib/course.lib.php +++ b/main/inc/lib/course.lib.php @@ -3965,24 +3965,21 @@ class CourseManager /** * Retrieves the user defined course categories - * @param string $userId - * @return array containing all the titles of the user defined courses with the id as key of the array + * @param int $userId + * @return array */ - public static function get_user_course_categories($userId = '') + public static function get_user_course_categories($userId = 0) { - if ($userId == '') { - $realUserId = api_get_user_id(); - } else { - $realUserId = $userId; - } - - $output = array(); + $userId = empty($userId) ? api_get_user_id() : (int) $userId; $table_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); $sql = "SELECT * FROM $table_category - WHERE user_id = '".intval($realUserId)."'"; + WHERE user_id = $userId + ORDER BY sort ASC + "; $result = Database::query($sql); - while ($row = Database::fetch_array($result)) { - $output[$row['id']] = $row['title']; + $output = array(); + while ($row = Database::fetch_array($result, 'ASSOC')) { + $output[$row['id']] = $row; } return $output; } diff --git a/main/inc/lib/userportal.lib.php b/main/inc/lib/userportal.lib.php index 4f482b848f..be9a9a7bbb 100755 --- a/main/inc/lib/userportal.lib.php +++ b/main/inc/lib/userportal.lib.php @@ -1853,15 +1853,13 @@ class IndexManager $listUserCategories[0] = ''; $html .= '
'; - - foreach ($listUserCategories as $userCategoryId => $userCatTitle) { + foreach ($listUserCategories as $userCategoryId => $userCat) { // add user category $userCategoryHtml = ''; if ($userCategoryId != 0) { $userCategoryHtml = '
'; + $userCategoryHtml .= self::getHtmlForUserCategory($userCategoryId, $userCat['title']); } - $userCategoryHtml .= self::getHtmlForUserCategory($userCategoryId, $userCatTitle); - // look for course in this userCat in session courses : $listCoursesInSession $htmlCategory = ''; if (isset($listCoursesInSession[$userCategoryId])) {