From 3d87eff7e1ba3dea2d643b6d517f80c31df06e87 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 10 Feb 2020 15:08:03 -0500 Subject: [PATCH] Move code to function - refs BT#15992 Code for save the LP item order --- main/inc/ajax/lp.ajax.php | 54 +------ main/lp/learnpath.class.php | 284 ++++++++++++++++++++++-------------- 2 files changed, 181 insertions(+), 157 deletions(-) diff --git a/main/inc/ajax/lp.ajax.php b/main/inc/ajax/lp.ajax.php index 7064ceb02e..10a3a986ae 100755 --- a/main/inc/ajax/lp.ajax.php +++ b/main/inc/ajax/lp.ajax.php @@ -78,59 +78,21 @@ switch ($action) { break; case 'update_lp_item_order': if (api_is_allowed_to_edit(null, true)) { + // $new_order gets a value like "647|0^648|0^649|0^" $new_order = $_POST['new_order']; $sections = explode('^', $new_order); - // We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database - $itemList = new LpItemOrderList(); + $sections = array_filter($sections); + + $orderList = []; + foreach ($sections as $items) { - if (!empty($items)) { - list($id, $parent_id) = explode('|', $items); - $item = new LpOrderItem($id, $parent_id); - $itemList->add($item); - } - } + list($id, $parentId) = explode('|', $items); - $parents = $itemList->getListOfParents(); - foreach ($parents as $parentId) { - $sameParentLpItemList = $itemList->getItemWithSameParent($parentId); - $previous_item_id = 0; - for ($i = 0; $i < count($sameParentLpItemList->list); $i++) { - $item_id = $sameParentLpItemList->list[$i]->id; - // display_order - $display_order = $i + 1; - $itemList->setParametersForId($item_id, $display_order, 'display_order'); - // previous_item_id - $itemList->setParametersForId($item_id, $previous_item_id, 'previous_item_id'); - $previous_item_id = $item_id; - // next_item_id - $next_item_id = 0; - if ($i < count($sameParentLpItemList->list) - 1) { - $next_item_id = $sameParentLpItemList->list[$i + 1]->id; - } - $itemList->setParametersForId($item_id, $next_item_id, 'next_item_id'); - } + $orderList[$id] = $parentId; } - $table = Database::get_course_table(TABLE_LP_ITEM); + learnpath::sortItemByOrderList($orderList); - foreach ($itemList->list as $item) { - $params = []; - $params['display_order'] = $item->display_order; - $params['previous_item_id'] = $item->previous_item_id; - $params['next_item_id'] = $item->next_item_id; - $params['parent_item_id'] = $item->parent_item_id; - - Database::update( - $table, - $params, - [ - 'iid = ? AND c_id = ? ' => [ - intval($item->id), - $courseId, - ], - ] - ); - } echo Display::return_message(get_lang('Saved'), 'confirm'); } break; diff --git a/main/lp/learnpath.class.php b/main/lp/learnpath.class.php index 571014f40f..8b209ca163 100755 --- a/main/lp/learnpath.class.php +++ b/main/lp/learnpath.class.php @@ -202,10 +202,10 @@ class learnpath // Selecting by view_count descending allows to get the highest view_count first. $sql = "SELECT * FROM $lp_table - WHERE - c_id = $course_id AND - lp_id = $lp_id AND - user_id = $user_id + WHERE + c_id = $course_id AND + lp_id = $lp_id AND + user_id = $user_id $session ORDER BY view_count DESC"; $res = Database::query($sql); @@ -603,7 +603,7 @@ class learnpath if (!empty($next)) { $sql = "UPDATE $tbl_lp_item - SET previous_item_id = $new_item_id + SET previous_item_id = $new_item_id WHERE c_id = $course_id AND id = $next AND item_type != '".TOOL_LP_FINAL_ITEM."'"; Database::query($sql); } @@ -781,7 +781,7 @@ class learnpath // There is already one such name, update the current one a bit. $i++; $name = $name.' - '.$i; - $check_name = "SELECT * FROM $tbl_lp + $check_name = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND name = '".Database::escape_string($name)."' "; $res_name = Database::query($check_name); } @@ -807,7 +807,7 @@ class learnpath break; case 'manual': default: - $get_max = "SELECT MAX(display_order) + $get_max = "SELECT MAX(display_order) FROM $tbl_lp WHERE c_id = $course_id"; $res_max = Database::query($get_max); if (Database::num_rows($res_max) < 1) { @@ -1052,7 +1052,7 @@ class learnpath WHERE c_id = $course_id AND lp_id = ".$this->lp_id; Database::query($sql); - $sql = "DELETE FROM $lp_view + $sql = "DELETE FROM $lp_view WHERE c_id = $course_id AND lp_id = ".$this->lp_id; Database::query($sql); @@ -1067,9 +1067,9 @@ class learnpath $row = Database::fetch_array($res); $path = $row['path']; $sql = "SELECT id FROM $lp - WHERE + WHERE c_id = $course_id AND - path = '$path' AND + path = '$path' AND iid != ".$this->lp_id; $res = Database::query($sql); if (Database::num_rows($res) > 0) { @@ -1104,7 +1104,7 @@ class learnpath WHERE c_id = $course_id AND (link LIKE '$link%' AND image='scormbuilder.gif')"; Database::query($sql); - $sql = "DELETE FROM $lp + $sql = "DELETE FROM $lp WHERE iid = ".$this->lp_id; Database::query($sql); // Updates the display order of all lps. @@ -1152,12 +1152,12 @@ class learnpath return false; } $lp_item = Database::get_course_table(TABLE_LP_ITEM); - $sql = "SELECT * FROM $lp_item + $sql = "SELECT * FROM $lp_item WHERE c_id = $course_id AND parent_item_id = $id"; $res = Database::query($sql); while ($row = Database::fetch_array($res)) { $num += $this->delete_children_items($row['iid']); - $sql = "DELETE FROM $lp_item + $sql = "DELETE FROM $lp_item WHERE c_id = $course_id AND iid = ".$row['iid']; Database::query($sql); $num++; @@ -1210,15 +1210,15 @@ class learnpath Database::query($sql_upd); // Now update all following items with new display order. $sql_all = "UPDATE $lp_item SET display_order = display_order-1 - WHERE - c_id = $course_id AND - lp_id = $lp AND - parent_item_id = $parent AND + WHERE + c_id = $course_id AND + lp_id = $lp AND + parent_item_id = $parent AND display_order > $display"; Database::query($sql_all); //Removing prerequisites since the item will not longer exist - $sql_all = "UPDATE $lp_item SET prerequisite = '' + $sql_all = "UPDATE $lp_item SET prerequisite = '' WHERE c_id = $course_id AND prerequisite = $id"; Database::query($sql_all); @@ -1230,8 +1230,8 @@ class learnpath // Remove from search engine if enabled. if (api_get_setting('search_enabled') === 'true') { $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF); - $sql = 'SELECT * FROM %s - WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d + $sql = 'SELECT * FROM %s + WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1'; $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id); $res = Database::query($sql); @@ -1240,8 +1240,8 @@ class learnpath $di = new ChamiloIndexer(); $di->remove_document($row2['search_did']); } - $sql = 'DELETE FROM %s - WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d + $sql = 'DELETE FROM %s + WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s AND ref_id_second_level=%d LIMIT 1'; $sql = sprintf($sql, $tbl_se_ref, $this->cc, TOOL_LEARNPATH, $lp, $id); Database::query($sql); @@ -1287,7 +1287,7 @@ class learnpath } $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM); - $sql = "SELECT * FROM $tbl_lp_item + $sql = "SELECT * FROM $tbl_lp_item WHERE iid = $id"; $res_select = Database::query($sql); $row_select = Database::fetch_array($res_select); @@ -1840,10 +1840,10 @@ class learnpath $settings = api_get_configuration_value('lp_view_settings'); $display = isset($settings['display']) ? $settings['display'] : false; $reportingIcon = ' - '.$reportingText.' '; @@ -1864,13 +1864,13 @@ class learnpath $nextIcon = ''; if ($hideArrows === false) { $previousIcon = ' - '.$previousText.' '; $nextIcon = ' - '.$nextText.' '; @@ -1880,9 +1880,9 @@ class learnpath $navbar = ' '.$reportingIcon.' - '.$previousIcon.' + '.$previousIcon.' '.$nextIcon.' - '.$fullScreenText.' @@ -1892,7 +1892,7 @@ class learnpath '.$reportingIcon.' '.$previousIcon.' - '.$nextIcon.' + '.$nextIcon.' '; } @@ -2101,7 +2101,7 @@ class learnpath } // Getting all the information about the item. - $sql = "SELECT lpi.audio, lpi.item_type, lp_view.status + $sql = "SELECT lpi.audio, lpi.item_type, lp_view.status FROM $tbl_lp_item as lpi INNER JOIN $tbl_lp_item_view as lp_view ON (lpi.iid = lp_view.lp_item_id) @@ -2491,8 +2491,8 @@ class learnpath { $text = $percentage.$text_add; $output = '
-
'.$text.'
@@ -6158,9 +6158,9 @@ class learnpath ); } - $delete_icon .= ' lp_id.'" + onclick="return confirmation(\''.addslashes($title).'\');" class="btn btn-default">'; $delete_icon .= Display::return_icon( 'delete.png', @@ -6269,14 +6269,14 @@ class learnpath Display::tag( 'div', "
- $previewIcon - $audio - $edit_icon + $previewIcon + $audio + $edit_icon $pluginCalendarIcon - $forumIcon - $prerequisities_icon - $move_item_icon - $audio_icon + $forumIcon + $prerequisities_icon + $move_item_icon + $audio_icon $orderIcons $delete_icon
", @@ -6948,7 +6948,7 @@ class learnpath if (empty($documentInfo)) { // Try with iid $table = Database::get_course_table(TABLE_DOCUMENT); - $sql = "SELECT id, path FROM $table + $sql = "SELECT id, path FROM $table WHERE c_id = $course_id AND iid = $document_id AND path NOT LIKE '%_DELETED_%' "; $res_doc = Database::query($sql); $row = Database::fetch_array($res_doc); @@ -8384,7 +8384,7 @@ class learnpath if ($action === 'add') { if (is_numeric($extra_info)) { $extra_info = (int) $extra_info; - $sql_doc = "SELECT path FROM $tbl_doc + $sql_doc = "SELECT path FROM $tbl_doc WHERE c_id = $course_id AND iid = ".$extra_info; $result = Database::query($sql_doc); $path_file = Database::result($result, 0, 0); @@ -9169,7 +9169,7 @@ class learnpath $item_url = stripslashes($extra_info['url']); } elseif (is_numeric($extra_info)) { $extra_info = (int) $extra_info; - $sql = "SELECT title, description, url + $sql = "SELECT title, description, url FROM $tbl_link WHERE c_id = $course_id AND iid = $extra_info"; $result = Database::query($sql); @@ -9502,7 +9502,7 @@ class learnpath $return = '
'; $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM); - $sql = "SELECT * FROM $tbl_lp_item + $sql = "SELECT * FROM $tbl_lp_item WHERE iid = ".$item_id; $result = Database::query($sql); $row = Database::fetch_assoc($result); @@ -9569,9 +9569,9 @@ class learnpath // Try with iid $table = Database::get_course_table(TABLE_DOCUMENT); $sql = "SELECT path FROM $table - WHERE - c_id = ".api_get_course_int_id()." AND - iid = ".$row['path']." AND + WHERE + c_id = ".api_get_course_int_id()." AND + iid = ".$row['path']." AND path NOT LIKE '%_DELETED_%'"; $result = Database::query($sql); $documentData = Database::fetch_array($result); @@ -9608,9 +9608,9 @@ class learnpath $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM); $sql = "SELECT * FROM ".$tbl_lp_item." - WHERE - c_id = $course_id AND - lp_id = ".$this->lp_id." AND + WHERE + c_id = $course_id AND + lp_id = ".$this->lp_id." AND parent_item_id = 0 ORDER BY display_order ASC"; $res_zero = Database::query($sql); @@ -9657,12 +9657,12 @@ class learnpath if (!id) { return false; } - + var cbo = document.getElementById('previous'); for(var i = cbo.length - 1; i > 0; i--) { cbo.options[i] = null; } - + var k=0; for(var i = 1; i <= child_name[id].length; i++){ var option = new Option(child_name[id][i - 1], child_value[id][i - 1]); @@ -9670,7 +9670,7 @@ class learnpath cbo.options[i] = option; k = i; } - + cbo.options[k].selected = true; $('#previous').selectpicker('refresh'); }"; @@ -9860,55 +9860,55 @@ class learnpath } $return .= ''; - $return .= ''; $return .= ''; $return .= ''; - $return .= ''; $return .= ''; } if ($item['item_type'] == TOOL_HOTPOTATOES) { $return .= ''; - $return .= ''; $return .= ''; $return .= ''; - $return .= ''; $return .= ''; } @@ -10114,18 +10114,18 @@ class learnpath } $sql_quiz = "SELECT * FROM $tbl_quiz - WHERE - c_id = $course_id AND + WHERE + c_id = $course_id AND $activeCondition - $condition_session + $condition_session $categoryCondition $keywordCondition ORDER BY title ASC"; $sql_hot = "SELECT * FROM $tbl_doc - WHERE - c_id = $course_id AND - path LIKE '".$uploadPath."/%/%htm%' + WHERE + c_id = $course_id AND + path LIKE '".$uploadPath."/%/%htm%' $condition_session ORDER BY id ASC"; @@ -10275,7 +10275,7 @@ class learnpath 'link.session_id' ); - $sql = "SELECT + $sql = "SELECT link.id as link_id, link.title as link_title, link.session_id as link_session_id, @@ -11748,9 +11748,9 @@ EOD; $max_order = $row_max_order->display_order; // Get the previous item ID $sql = "SELECT iid as previous FROM $table_lp_item - WHERE - c_id = $course_id AND - lp_id = ".$this->lp_id." AND + WHERE + c_id = $course_id AND + lp_id = ".$this->lp_id." AND display_order = '$max_order' "; $rs_max = Database::query($sql); $row_max = Database::fetch_object($rs_max); @@ -12787,7 +12787,7 @@ EOD; $this->accumulateScormTime = (int) $value; $lp_table = Database::get_course_table(TABLE_LP_MAIN); $lp_id = $this->get_id(); - $sql = "UPDATE $lp_table + $sql = "UPDATE $lp_table SET accumulate_scorm_time = ".$this->accumulateScormTime." WHERE iid = $lp_id"; Database::query($sql); @@ -13015,7 +13015,7 @@ EOD; $learningPathId = (int) $learningPathId; $id_in_path = (int) $id_in_path; - $sql = "SELECT item_type, title, ref + $sql = "SELECT item_type, title, ref FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = $learningPathId AND iid = $id_in_path"; $res_item = Database::query($sql); @@ -13363,8 +13363,8 @@ EOD; public function getAccumulateWorkTimeTotalCourse() { $table = Database::get_course_table(TABLE_LP_MAIN); - $sql = "SELECT SUM(accumulate_work_time) AS total - FROM $table + $sql = "SELECT SUM(accumulate_work_time) AS total + FROM $table WHERE c_id = ".$this->course_int_id; $result = Database::query($sql); $row = Database::fetch_array($result); @@ -13407,8 +13407,8 @@ EOD; $courseId = (int) $courseId; $table = Database::get_course_table(TABLE_LP_MAIN); - $sql = "SELECT accumulate_work_time - FROM $table + $sql = "SELECT accumulate_work_time + FROM $table WHERE c_id = $courseId AND id = $lpId"; $result = Database::query($sql); $row = Database::fetch_array($result); @@ -13537,7 +13537,7 @@ EOD; $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM); $course_id = api_get_course_int_id(); - $sql = "SELECT * FROM $tbl_lp_item + $sql = "SELECT * FROM $tbl_lp_item WHERE c_id = $course_id AND lp_id = ".$this->lp_id; if ($addParentCondition) { @@ -13677,4 +13677,66 @@ EOD; return ''; } + + /** + * Save the new order for learning path items + * + * We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database. + * + * @param array $orderList A associative array with item ID as key and parent ID as value. + * @param int $courseId + */ + public static function sortItemByOrderList(array $orderList, $courseId = 0) + { + $courseId = $courseId ?: api_get_course_int_id(); + $itemList = new LpItemOrderList(); + + foreach ($orderList as $id => $parentId) { + $item = new LpOrderItem($id, $parentId); + $itemList->add($item); + } + + $parents = $itemList->getListOfParents(); + + foreach ($parents as $parentId) { + $sameParentLpItemList = $itemList->getItemWithSameParent($parentId); + $previous_item_id = 0; + for ($i = 0; $i < count($sameParentLpItemList->list); $i++) { + $item_id = $sameParentLpItemList->list[$i]->id; + // display_order + $display_order = $i + 1; + $itemList->setParametersForId($item_id, $display_order, 'display_order'); + // previous_item_id + $itemList->setParametersForId($item_id, $previous_item_id, 'previous_item_id'); + $previous_item_id = $item_id; + // next_item_id + $next_item_id = 0; + if ($i < count($sameParentLpItemList->list) - 1) { + $next_item_id = $sameParentLpItemList->list[$i + 1]->id; + } + $itemList->setParametersForId($item_id, $next_item_id, 'next_item_id'); + } + } + + $table = Database::get_course_table(TABLE_LP_ITEM); + + foreach ($itemList->list as $item) { + $params = []; + $params['display_order'] = $item->display_order; + $params['previous_item_id'] = $item->previous_item_id; + $params['next_item_id'] = $item->next_item_id; + $params['parent_item_id'] = $item->parent_item_id; + + Database::update( + $table, + $params, + [ + 'iid = ? AND c_id = ? ' => [ + (int) $item->id, + (int) $courseId, + ], + ] + ); + } + } }