From 39d111f8bad428a629821cbd74dbb261528ece90 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Thu, 20 Sep 2007 02:08:43 +0200 Subject: [PATCH] [svn r13110] Added ordering in the learnpath tool homepage --- main/newscorm/learnpath.class.php | 93 ++++++++++++++++++++++++++- main/newscorm/learnpathList.class.php | 3 +- main/newscorm/lp_controller.php | 34 +++++++++- main/newscorm/lp_list.php | 39 +++++++++-- 4 files changed, 160 insertions(+), 9 deletions(-) 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 ''.get_lang("Name").''."\n" . ''.get_lang("Progress")."\n"; if (api_is_allowed_to_edit()) { - echo "",get_lang("Description"),"\n" . - "",get_lang("ExportShort"),"\n", - '',get_lang("Modify"),"\n"; + echo ''.get_lang("Description")."\n" . + ''.get_lang("ExportShort")."\n" . + ''.get_lang("Modify")."\n" . + ''.get_lang('Move')."\n"; } echo "\n"; @@ -167,10 +168,12 @@ $list = new LearnpathList(api_get_user_id()); $flat_list = $list->get_flat_list(); $is_allowed_to_edit = api_is_allowed_to_edit(); $test_mode = api_get_setting('server_type'); +$max = count($flat_list); //var_dump($flat_list); if (is_array($flat_list)) { $counter = 0; + $current = 0; foreach ($flat_list as $id => $details) { if(!$is_allowed_to_edit && $details['lp_visibility'] == 0) @@ -194,10 +197,12 @@ if (is_array($flat_list)) $dsp_export = ''; $dsp_edit = ''; + $dsp_edit_close = ''; $dsp_delete = ''; $dsp_visible = ''; $dsp_default_view = ''; $dsp_debug = ''; + $dsp_order = ''; if($display_progress_bar) { $dsp_progress = ''.learnpath::get_progress_bar('%',learnpath::get_db_progress($id,api_get_user_id()),'').''; @@ -236,7 +241,8 @@ if (is_array($flat_list)) /* edit title and description */ $dsp_edit = ''; - + $dsp_edit_close = ''; + /* DELETE COMMAND */ $dsp_delete = "" . @@ -323,12 +329,33 @@ if (is_array($flat_list)) ' '; } } + if($details['lp_display_order'] == 1) + { + $dsp_order .= '' . + ''.get_lang(' . + ''; + } + elseif($current == $max-1) //last element + { + $dsp_order .= '' . + ''.get_lang(' . + ''; + } + else + { + $dsp_order .= '' . + ''.get_lang(' . + ' '; + $dsp_order .= '' . + ''.get_lang(' . + ''; + } } // end if($is_allowedToEdit) //echo $dsp_line.$dsp_desc.$dsp_export.$dsp_edit.$dsp_delete.$dsp_visible; - echo $dsp_line.$dsp_progress.$dsp_desc.$dsp_export.$dsp_edit.$dsp_build.$dsp_visible.$dsp_publish.$dsp_reinit.$dsp_default_view.$dsp_force_commit.$dsp_debug.$dsp_delete; + echo $dsp_line.$dsp_progress.$dsp_desc.$dsp_export.$dsp_edit.$dsp_build.$dsp_visible.$dsp_publish.$dsp_reinit.$dsp_default_view.$dsp_force_commit.$dsp_debug.$dsp_delete.$dsp_close.$dsp_order; //echo $dsp_line.$dsp_progress.$dsp_desc.$dsp_export.$dsp_edit.$dsp_build.$dsp_visible.$dsp_reinit.$dsp_force_commit.$dsp_delete; echo "\n"; - + $current ++; //counter for number of elements treated } // end foreach ($flat_list) //TODO print some user-friendly message if counter is still = 0 to tell nothing can be displayd yet }// end if ( is_array($flat_list)