diff --git a/main/newscorm/learnpath.class.php b/main/newscorm/learnpath.class.php index d36812835b..e29be4d8f9 100644 --- a/main/newscorm/learnpath.class.php +++ b/main/newscorm/learnpath.class.php @@ -2742,7 +2742,98 @@ class learnpath { } return $display; } - + /** + * Move a learnpath up (display_order) + * @param integer Learnpath ID + */ + function move_up($lp_id) + { + $lp_table = Database::get_course_table(TABLE_LP_MAIN); + $sql = "SELECT * FROM $lp_table ORDER BY display_order"; + $res = api_sql_query($sql); + if($res === false) return false; + $lps = array(); + $lp_order = array(); + $num = Database::num_rows($res); + //first check the order is correct, globally (might be wrong because + //of versions < 1.8.4) + if($num>0) + { + $i = 1; + $need_fix = false; + while($row = Database::fetch_array($res)) + { + if($row['display_order'] != $i) + { //if we find a gap in the order, we need to fix it + $need_fix = true; + $sql_u = "UPDATE $lp_table SET display_order = $i WHERE id = ".$row['id']; + $res_u = api_sql_query($sql_u); + } + $row['display_order'] = $i; + $lps[$row['id']] = $row; + $lp_order[$i] = $row['id']; + $i++; + } + } + if($num>1) //if there's only one element, no need to sort + { + $order = $lps[$lp_id]['display_order']; + if($order>1) //if it's the first element, no need to move up + { + $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE id = ".$lp_order[$order-1]; + $res_u1 = api_sql_query($sql_u1); + $sql_u2 = "UPDATE $lp_table SET display_order = ".($order-1)." WHERE id = ".$lp_id; + $res_u2 = api_sql_query($sql_u2); + } + } + } + /** + * Move a learnpath down (display_order) + * @param integer Learnpath ID + */ + function move_down($lp_id) + { + $lp_table = Database::get_course_table(TABLE_LP_MAIN); + $sql = "SELECT * FROM $lp_table ORDER BY display_order"; + $res = api_sql_query($sql); + if($res === false) return false; + $lps = array(); + $lp_order = array(); + $num = Database::num_rows($res); + $max = 0; + //first check the order is correct, globally (might be wrong because + //of versions < 1.8.4) + if($num>0) + { + $i = 1; + $need_fix = false; + while($row = Database::fetch_array($res)) + { + $max = $i; + if($row['display_order'] != $i) + { //if we find a gap in the order, we need to fix it + $need_fix = true; + $sql_u = "UPDATE $lp_table SET display_order = $i WHERE id = ".$row['id']; + $res_u = api_sql_query($sql_u); + } + $row['display_order'] = $i; + $lps[$row['id']] = $row; + $lp_order[$i] = $row['id']; + $i++; + } + } + if($num>1) //if there's only one element, no need to sort + { + $order = $lps[$lp_id]['display_order']; + if($order<$max) //if it's the first element, no need to move up + { + $sql_u1 = "UPDATE $lp_table SET display_order = $order WHERE id = ".$lp_order[$order+1]; + $res_u1 = api_sql_query($sql_u1); + $sql_u2 = "UPDATE $lp_table SET display_order = ".($order+1)." WHERE id = ".$lp_id; + $res_u2 = api_sql_query($sql_u2); + } + } + } /** * Updates learnpath attributes to point to the next element * The last part is similar to set_current_item but processing the other way around diff --git a/main/newscorm/learnpathList.class.php b/main/newscorm/learnpathList.class.php index fe12ef6464..ed52b04b83 100644 --- a/main/newscorm/learnpathList.class.php +++ b/main/newscorm/learnpathList.class.php @@ -35,7 +35,7 @@ class learnpathList { } $this->course_code = $course_code; $this->user_id = $user_id; - $sql = "SELECT * FROM $lp_table ORDER BY name ASC"; + $sql = "SELECT * FROM $lp_table ORDER BY display_order ASC, name ASC"; $res = api_sql_query($sql); $names = array(); while ($row = Database::fetch_array($res)) @@ -75,6 +75,7 @@ class learnpathList { 'lp_published' => $pub, 'lp_prevent_reinit' => $row['prevent_reinit'], 'lp_scorm_debug' => $row['debug'], + 'lp_display_order' => $row['display_order'], ); $names[$row['name']]=$row['id']; } diff --git a/main/newscorm/lp_controller.php b/main/newscorm/lp_controller.php index 82f995454a..6b53b56f7c 100644 --- a/main/newscorm/lp_controller.php +++ b/main/newscorm/lp_controller.php @@ -493,10 +493,42 @@ switch($_REQUEST['action']) require('lp_list.php'); } break; - case 'edit': + case 'move_lp_up': //change lp published status (visibility on homepage) + if(!api_is_allowed_to_edit()){ + api_not_allowed(true); + } + if($debug>0) error_log('New LP - publish action triggered',0); + if(!$lp_found) + { + error_log('New LP - No learnpath given for publish',0); + require('lp_list.php'); + } + else + { + learnpath::move_up($_REQUEST['lp_id']); + require('lp_list.php'); + } + break; + case 'move_lp_down': //change lp published status (visibility on homepage) if(!api_is_allowed_to_edit()){ api_not_allowed(true); } + if($debug>0) error_log('New LP - publish action triggered',0); + if(!$lp_found){ + error_log('New LP - No learnpath given for publish',0); + require('lp_list.php'); + } + else + { + learnpath::move_down($_REQUEST['lp_id']); + require('lp_list.php'); + } + break; + case 'edit': + if(!api_is_allowed_to_edit()) + { + api_not_allowed(true); + } if($debug>0) error_log('New LP - edit action triggered',0); if(!$lp_found){ error_log('New LP - No learnpath given for edit',0); require('lp_list.php'); } else{ diff --git a/main/newscorm/lp_list.php b/main/newscorm/lp_list.php index 85b6412d24..f07b84c212 100644 --- a/main/newscorm/lp_list.php +++ b/main/newscorm/lp_list.php @@ -153,9 +153,10 @@ echo '