@@ -87,14 +87,12 @@ public static function getCategoryById($categoryId)
8787 public static function getAllCategories ()
8888 {
8989 $ tbl_category = Database::get_main_table (TABLE_MAIN_CATEGORY );
90- $ tbl_course = Database::get_main_table (TABLE_MAIN_COURSE );
91-
9290 $ table = Database::get_main_table (TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY );
9391 $ conditions = " INNER JOIN $ table a ON (t1.id = a.course_category_id) " ;
9492 $ whereCondition = " AND a.access_url_id = " .api_get_current_access_url_id ();
9593 $ allowBaseCategories = ('true ' === api_get_setting ('course.allow_base_course_category ' ));
9694 if ($ allowBaseCategories ) {
97- $ whereCondition = " AND (a.access_url_id = " .api_get_current_access_url_id ()." OR a.access_url_id = 1) " ;
95+ $ whereCondition = " AND (a.access_url_id = " .api_get_current_access_url_id ()." OR a.access_url_id = 1) " ;
9896 }
9997
10098 $ sql = "SELECT
@@ -103,21 +101,19 @@ public static function getAllCategories()
103101 t1.code,
104102 t1.parent_id,
105103 t1.tree_pos,
106- t1.children_count,
107- COUNT(DISTINCT t3.code) AS number_courses
108- FROM $ tbl_category t1
109- $ conditions
110- LEFT JOIN $ tbl_course t3
111- ON t3.category_id=t1.id
112- WHERE 1=1
113- $ whereCondition
114- GROUP BY
115- t1.title,
116- t1.code,
117- t1.parent_id,
118- t1.tree_pos,
119- t1.children_count
120- ORDER BY t1.parent_id, t1.tree_pos " ;
104+ t1.children_count
105+ FROM $ tbl_category t1
106+ $ conditions
107+ WHERE 1=1
108+ $ whereCondition
109+ GROUP BY
110+ t1.id,
111+ t1.title,
112+ t1.code,
113+ t1.parent_id,
114+ t1.tree_pos,
115+ t1.children_count
116+ ORDER BY t1.parent_id, t1.tree_pos " ;
121117
122118 $ result = Database::query ($ sql );
123119
@@ -205,7 +201,7 @@ public static function updateParentCategoryChildrenCount($categoryId, $delta = 1
205201 Database::query ($ sql );
206202 }
207203
208- public static function edit ($ categoryId , $ name , $ canHaveCourses , $ code , $ description ): ?CourseCategoryEntity
204+ public static function edit ($ categoryId , $ name , $ canHaveCourses , $ code , $ description, $ parentId = null ): ?CourseCategoryEntity
209205 {
210206 $ repo = Container::getCourseCategoryRepository ();
211207 $ category = $ repo ->find ($ categoryId );
@@ -221,6 +217,10 @@ public static function edit($categoryId, $name, $canHaveCourses, $code, $descrip
221217 ->setAuthCourseChild ($ canHaveCourses )
222218 ;
223219
220+ if (!empty ($ parentId )) {
221+ $ category ->setParent (Container::getCourseCategoryRepository ()->find ($ parentId ));
222+ }
223+
224224 $ repo ->save ($ category );
225225
226226 // Updating children
@@ -272,6 +272,38 @@ public static function moveNodeUp($categoryId, $treePos, $parentId): bool
272272 return true ;
273273 }
274274
275+ public static function moveNodeDown ($ categoryId , $ treePos , $ parentId ): bool
276+ {
277+ $ table = Database::get_main_table (TABLE_MAIN_CATEGORY );
278+ $ categoryId = (int ) $ categoryId ;
279+ $ treePos = (int ) $ treePos ;
280+
281+ $ parentIdCondition = "parent_id IS NULL " ;
282+ if (!empty ($ parentId )) {
283+ $ parentIdCondition = "parent_id = ' " .Database::escape_string ($ parentId )."' " ;
284+ }
285+
286+ self ::reorganizeTreePos ($ parentId );
287+
288+ $ sql = "SELECT id, tree_pos
289+ FROM $ table
290+ WHERE $ parentIdCondition AND tree_pos > $ treePos
291+ ORDER BY tree_pos ASC
292+ LIMIT 1 " ;
293+
294+ $ result = Database::query ($ sql );
295+ $ nextCategory = Database::fetch_array ($ result );
296+
297+ if (!$ nextCategory ) {
298+ return false ;
299+ }
300+
301+ Database::query ("UPDATE $ table SET tree_pos = {$ nextCategory ['tree_pos ' ]} WHERE id = $ categoryId " );
302+ Database::query ("UPDATE $ table SET tree_pos = $ treePos WHERE id = {$ nextCategory ['id ' ]}" );
303+
304+ return true ;
305+ }
306+
275307 public static function reorganizeTreePos ($ parentId ): void
276308 {
277309 $ table = Database::get_main_table (TABLE_MAIN_CATEGORY );
@@ -384,21 +416,51 @@ public static function listCategories(array $categorySource = []): string
384416 $ column ++;
385417 }
386418 $ row ++;
387- $ mainUrl = api_get_path (WEB_CODE_PATH ).'admin/course_category.php?category= ' .$ categoryCode ;
419+ $ baseUrl = api_get_path (WEB_CODE_PATH ).'admin/course_category.php ' ;
420+ $ baseParams = [];
421+ if (!empty ($ categorySource ['id ' ])) {
422+ $ baseParams ['id ' ] = (int ) $ categorySource ['id ' ];
423+ }
388424
389425 $ editIcon = Display::getMdiIcon (ActionIcon::EDIT , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('Edit ' ));
390426 $ exportIcon = Display::getMdiIcon (ActionIcon::EXPORT_CSV , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('ExportAsCSV ' ));
391427 $ deleteIcon = Display::getMdiIcon (ActionIcon::DELETE , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('Delete ' ));
392- $ moveIcon = Display::getMdiIcon (ActionIcon::UP , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('Up in same level ' ));
393-
394428 $ urlId = api_get_current_access_url_id ();
429+
430+ $ positions = array_map (fn ($ c ) => $ c ->getTreePos (), $ categories );
431+ $ minTreePos = min ($ positions );
432+ $ maxTreePos = max ($ positions );
433+
395434 foreach ($ categories as $ category ) {
396435 $ categoryId = $ category ->getId ();
397436 $ code = $ category ->getCode ();
398- $ editUrl = $ mainUrl .'&id= ' .$ categoryId .'&action=edit ' ;
399- $ moveUrl = $ mainUrl .'&id= ' .$ categoryId .'&action=moveUp&tree_pos= ' .$ category ->getTreePos ();
400- $ deleteUrl = $ mainUrl .'&id= ' .$ categoryId .'&action=delete ' ;
401- $ exportUrl = $ mainUrl .'&id= ' .$ categoryId .'&action=export ' ;
437+ $ treePos = $ category ->getTreePos ();
438+ $ editUrl = $ baseUrl .'? ' .http_build_query (array_merge ($ baseParams , [
439+ 'action ' => 'edit ' ,
440+ 'id ' => $ categoryId ,
441+ ]));
442+
443+ $ moveUpUrl = $ baseUrl .'? ' .http_build_query (array_merge ($ baseParams , [
444+ 'action ' => 'moveUp ' ,
445+ 'id ' => $ categoryId ,
446+ 'tree_pos ' => $ treePos ,
447+ ]));
448+
449+ $ moveDownUrl = $ baseUrl .'? ' .http_build_query (array_merge ($ baseParams , [
450+ 'action ' => 'moveDown ' ,
451+ 'id ' => $ categoryId ,
452+ 'tree_pos ' => $ treePos ,
453+ ]));
454+
455+ $ deleteUrl = $ baseUrl .'? ' .http_build_query (array_merge ($ baseParams , [
456+ 'action ' => 'delete ' ,
457+ 'id ' => $ categoryId ,
458+ ]));
459+
460+ $ exportUrl = $ baseUrl .'? ' .http_build_query (array_merge ($ baseParams , [
461+ 'action ' => 'export ' ,
462+ 'id ' => $ categoryId ,
463+ ]));
402464
403465 $ actions = [];
404466
@@ -410,7 +472,25 @@ function ($entry) use ($urlId) {
410472
411473 if ($ inUrl ->count () > 0 ) {
412474 $ actions [] = Display::url ($ editIcon , $ editUrl );
413- $ actions [] = Display::url ($ moveIcon , $ moveUrl );
475+
476+ if ($ treePos > $ minTreePos ) {
477+ $ actions [] = Display::url (
478+ Display::getMdiIcon (ActionIcon::UP , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('Move up ' )),
479+ $ moveUpUrl
480+ );
481+ } else {
482+ $ actions [] = Display::getMdiIcon (ActionIcon::UP , 'ch-tool-icon-disabled ' , null , ICON_SIZE_SMALL , get_lang ('Move up ' ));
483+ }
484+
485+ if ($ treePos < $ maxTreePos ) {
486+ $ actions [] = Display::url (
487+ Display::getMdiIcon (ActionIcon::DOWN , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('Move down ' )),
488+ $ moveDownUrl
489+ );
490+ } else {
491+ $ actions [] = Display::getMdiIcon (ActionIcon::DOWN , 'ch-tool-icon-disabled ' , null , ICON_SIZE_SMALL , get_lang ('Move down ' ));
492+ }
493+
414494 $ actions [] = Display::url ($ exportIcon , $ exportUrl );
415495 $ actions [] = Display::url (
416496 $ deleteIcon ,
0 commit comments