LP: Improve lp list loop performance see BT#15881

pull/2958/head
Julio Montoya 6 years ago
parent ac346848b1
commit 84bd3df795
  1. 90
      main/lp/learnpath.class.php
  2. 169
      main/lp/lp_list.php

@ -2646,7 +2646,7 @@ class learnpath
$sessionCondition = api_get_session_condition($sessionId);
$table = Database::get_course_table(TABLE_LP_VIEW);
$sql = "SELECT * FROM $table
$sql = "SELECT progress FROM $table
WHERE
c_id = $courseId AND
lp_id = $lpId AND
@ -2655,13 +2655,55 @@ class learnpath
$progress = 0;
if (Database::num_rows($res) > 0) {
$row = Database:: fetch_array($res);
$row = Database::fetch_array($res);
$progress = (int) $row['progress'];
}
return $progress;
}
/**
* @param array $lpList
* @param int $userId
* @param int $courseId
* @param int $sessionId
*
* @return int
*/
public static function getProgressFromLpList($lpList, $userId, $courseId, $sessionId = 0)
{
$lpList = array_map('intval', $lpList);
if (empty($lpList)) {
return [];
}
$lpList = implode("','", $lpList);
$userId = (int) $userId;
$courseId = (int) $courseId;
$sessionId = (int) $sessionId;
$sessionCondition = api_get_session_condition($sessionId);
$table = Database::get_course_table(TABLE_LP_VIEW);
$sql = "SELECT lp_id, progress FROM $table
WHERE
c_id = $courseId AND
lp_id IN ('".$lpList."') AND
user_id = $userId $sessionCondition ";
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
$list = [];
while ($row = Database::fetch_array($res)) {
$list[$row['lp_id']] = $row['progress'];
}
return $list;
}
return [];
}
/**
* Displays a progress bar
* completed so far.
@ -3705,8 +3747,8 @@ class learnpath
FROM $lp_table l
INNER JOIN $lp_item_table li
ON (li.lp_id = l.iid)
WHERE
li.iid = $item_id
WHERE
li.iid = $item_id
";
if ($this->debug > 2) {
error_log('In learnpath::get_link() - selecting item '.$sql, 0);
@ -4147,7 +4189,7 @@ class learnpath
$tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
$sql_sel = "SELECT *
FROM $tbl_lp_item
WHERE
WHERE
iid = $id
";
$res_sel = Database::query($sql_sel);
@ -4234,7 +4276,7 @@ class learnpath
error_log('Movement down detected', 0);
}
if ($next != 0) {
$sql_sel2 = "SELECT * FROM $tbl_lp_item
$sql_sel2 = "SELECT * FROM $tbl_lp_item
WHERE iid = $next";
if ($this->debug > 2) {
error_log('Selecting next: '.$sql_sel2, 0);
@ -4248,7 +4290,7 @@ class learnpath
$next_next = $row2['next_item_id'];
// Update previous item (switch with current).
if ($previous != 0) {
$sql_upd2 = "UPDATE $tbl_lp_item
$sql_upd2 = "UPDATE $tbl_lp_item
SET next_item_id = $next
WHERE iid = $previous";
Database::query($sql_upd2);
@ -4256,8 +4298,8 @@ class learnpath
// Update current item (switch with previous).
if ($id != 0) {
$sql_upd2 = "UPDATE $tbl_lp_item SET
previous_item_id = $next,
next_item_id = $next_next,
previous_item_id = $next,
next_item_id = $next_next,
display_order = display_order + 1
WHERE iid = $id";
Database::query($sql_upd2);
@ -4266,8 +4308,8 @@ class learnpath
// Update next item (new previous item).
if ($next != 0) {
$sql_upd2 = "UPDATE $tbl_lp_item SET
previous_item_id = $previous,
next_item_id = $id,
previous_item_id = $previous,
next_item_id = $id,
display_order = display_order-1
WHERE iid = $next";
Database::query($sql_upd2);
@ -4689,10 +4731,10 @@ class learnpath
$num = Database::num_rows($result);
if ($set_visibility == 'i' && $num > 0) {
$sql = "DELETE FROM $tbl_tool
WHERE
c_id = $course_id AND
(link = '$link' OR link = '$oldLink') AND
image='scormbuilder.gif'
WHERE
c_id = $course_id AND
(link = '$link' OR link = '$oldLink') AND
image='scormbuilder.gif'
$session_condition";
Database::query($sql);
} elseif ($set_visibility == 'v' && $num == 0) {
@ -4717,8 +4759,8 @@ class learnpath
session_id = $session_id
WHERE
c_id = ".$course_id." AND
(link = '$link' OR link = '$oldLink') AND
image='scormbuilder.gif'
(link = '$link' OR link = '$oldLink') AND
image='scormbuilder.gif'
$session_condition
";
Database::query($sql);
@ -4938,7 +4980,7 @@ class learnpath
$tools = $em
->createQuery("
SELECT t FROM ChamiloCourseBundle:CTool t
WHERE t.cId = :course AND
WHERE t.cId = :course AND
t.name = :name AND
t.image = 'lp_category.gif' AND
t.link LIKE :link
@ -5222,7 +5264,7 @@ class learnpath
$lp = $this->get_id();
if ($lp != 0) {
$tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
$sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
$sql = "UPDATE $tbl_lp SET default_encoding = '$enc'
WHERE iid = ".$lp;
$res = Database::query($sql);
@ -5250,7 +5292,7 @@ class learnpath
if ($lp != 0) {
$tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
$sql = "UPDATE $tbl_lp SET js_lib = '$lib'
$sql = "UPDATE $tbl_lp SET js_lib = '$lib'
WHERE iid = $lp";
$res = Database::query($sql);
@ -5426,7 +5468,7 @@ class learnpath
$this->theme = $name;
$table = Database::get_course_table(TABLE_LP_MAIN);
$lp_id = $this->get_id();
$sql = "UPDATE $table
$sql = "UPDATE $table
SET theme = '".Database::escape_string($this->theme)."'
WHERE iid = $lp_id";
if ($this->debug > 2) {
@ -5945,8 +5987,8 @@ class learnpath
{
$course_id = api_get_course_int_id();
$table = Database::get_course_table(TABLE_LP_MAIN);
$sql = "SELECT * FROM $table
WHERE c_id = $course_id
$sql = "SELECT * FROM $table
WHERE c_id = $course_id
ORDER BY display_order";
$res = Database::query($sql);
if ($res === false) {
@ -6125,7 +6167,7 @@ class learnpath
error_log('In learnpath::set_seriousgame_mode()', 0);
}
$lp_table = Database::get_course_table(TABLE_LP_MAIN);
$sql = "SELECT * FROM $lp_table
$sql = "SELECT * FROM $lp_table
WHERE iid = ".$this->get_id();
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {

@ -134,7 +134,6 @@ $categories = [
if (!empty($categoriesTempList)) {
$categories = array_merge($categories, $categoriesTempList);
}
$userId = api_get_user_id();
$userInfo = api_get_user_info();
$lpIsShown = false;
@ -156,14 +155,44 @@ $test_mode = api_get_setting('server_type');
$showBlockedPrerequisite = api_get_configuration_value('show_prerequisite_as_blocked');
$allowLpChamiloExport = api_get_configuration_value('allow_lp_chamilo_export');
$allowMinTime = Tracking::minimunTimeAvailable(api_get_session_id(), api_get_course_int_id());
$accumulateWorkTimeTotal = 0;
if ($allowMinTime) {
$accumulateWorkTimeTotal = learnpath::getAccumulateWorkTimeTotal(api_get_course_int_id());
}
$user = api_get_user_entity($userId);
$ending = true;
$isInvitee = api_is_invitee();
$hideScormExportLink = api_get_setting('hide_scorm_export_link');
$hideScormCopyLink = api_get_setting('hide_scorm_copy_link');
$hideScormPdfLink = api_get_setting('hide_scorm_pdf_link');
$options = learnpath::getIconSelect();
$cidReq = api_get_cidreq();
$defaultLpIcon = Display::return_icon(
'learnpath.png',
get_lang('LPName')
);
$defaultDisableLpIcon = Display::return_icon(
'learnpath_na.png',
get_lang('LPName')
);
$courseSettingsIcon = Display::return_icon(
'settings.png',
get_lang('CourseSettings')
);
$courseSettingsDisableIcon = Display::return_icon(
'settings_na.png',
get_lang('CourseSettings')
);
$data = [];
/** @var CLpCategory $item */
foreach ($categories as $item) {
$categoryId = $item->getId();
if ($categoryId !== 0 && $subscriptionSettings['allow_add_users_to_lp_category'] == true) {
// "Without category" has id = 0
$categoryVisibility = api_get_item_visibility(
@ -206,17 +235,18 @@ foreach ($categories as $item) {
$lpTimeList = Tracking::getCalculateTime($userId, api_get_course_int_id(), api_get_session_id());
}
$options = learnpath::getIconSelect();
if (!empty($flat_list)) {
$max = count($flat_list);
$counter = 0;
$current = 0;
$autolaunch_exists = false;
$accumulateWorkTimeTotal = 0;
if ($allowMinTime) {
$accumulateWorkTimeTotal = learnpath::getAccumulateWorkTimeTotal(api_get_course_int_id());
}
$progressList = learnpath::getProgressFromLpList(
array_column($flat_list, 'lp_old_id'),
$userId,
api_get_course_int_id(),
api_get_session_id()
);
foreach ($flat_list as $id => $details) {
$id = $details['lp_old_id'];
@ -249,7 +279,6 @@ foreach ($categories as $item) {
$start_time = $end_time = '';
if (!$is_allowed_to_edit) {
$time_limits = false;
// This is an old LP (from a migration 1.8.7) so we do nothing
if (empty($details['created_on']) &&
empty($details['modified_on'])
@ -303,13 +332,12 @@ foreach ($categories as $item) {
}
$counter++;
$oddclass = 'row_even';
if (($counter % 2) == 0) {
$oddclass = 'row_odd';
} else {
$oddclass = 'row_even';
}
$url_start_lp = 'lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$id;
$url_start_lp = 'lp_controller.php?'.$cidReq.'&action=view&lp_id='.$id;
$name = Security::remove_XSS($details['lp_name']);
$extra = null;
@ -327,21 +355,14 @@ foreach ($categories as $item) {
}
$my_title = $name;
$icon_learnpath = Display::return_icon(
'learnpath.png',
get_lang('LPName')
);
$icon_learnpath = $defaultLpIcon;
if ($details['lp_visibility'] == 0) {
$my_title = Display::tag(
'font',
$name,
['class' => 'text-muted']
);
$icon_learnpath = Display::return_icon(
'learnpath_na.png',
get_lang('LPName')
);
$icon_learnpath = $defaultDisableLpIcon;
}
if (!empty($options)) {
@ -360,10 +381,7 @@ foreach ($categories as $item) {
$name,
['class' => 'text-muted']
);
$icon_learnpath = Display::return_icon(
'learnpath_na.png',
get_lang('LPName')
);
$icon_learnpath = $defaultDisableLpIcon;
$url_start_lp = '#';
}
@ -376,22 +394,17 @@ foreach ($categories as $item) {
$dsp_default_view = '';
$dsp_debug = '';
$dsp_order = '';
$progress = 0;
if (!api_is_invitee()) {
$progress = learnpath::getProgress(
$id,
$userId,
api_get_course_int_id(),
api_get_session_id()
);
$progress = 0;
if (!$isInvitee) {
$progress = isset($progressList[$id]) && !empty($progressList[$id]) ? $progressList[$id] : 0;
}
if ($is_allowed_to_edit) {
$dsp_progress = '<center>'.$progress.'%</center>';
} else {
$dsp_progress = '';
if (!api_is_invitee()) {
if (!$isInvitee) {
$dsp_progress = learnpath::get_progress_bar($progress, '%');
}
}
@ -444,7 +457,7 @@ foreach ($categories as $item) {
$time_progress_perc = '100%';
$time_progress_value = 100;
} else {
$time_progress_value = intval(($lpTime * 100) / ($accumulateWorkTime * 60));
$time_progress_value = (int) (($lpTime * 100) / ($accumulateWorkTime * 60));
}
if ($time_progress_value < 100) {
@ -472,18 +485,11 @@ foreach ($categories as $item) {
// EDIT LP
if ($sessionId == $details['lp_session']) {
$dsp_edit_lp = Display::url(
Display::return_icon(
'settings.png',
get_lang('CourseSettings')
),
"lp_controller.php?".api_get_cidreq()
."&action=edit&lp_id=$id"
$courseSettingsIcon,
'lp_controller.php?'.$cidReq."&action=edit&lp_id=$id"
);
} else {
$dsp_edit_lp = Display::return_icon(
'settings_na.png',
get_lang('CourseSettings')
);
$dsp_edit_lp = $courseSettingsDisableIcon;
}
// BUILD
@ -494,7 +500,7 @@ foreach ($categories as $item) {
'edit.png',
get_lang('LearnpathEditLearnpath')
),
'lp_controller.php?'.api_get_cidreq().'&'
'lp_controller.php?'.$cidReq.'&'
.http_build_query(
[
'action' => 'add_item',
@ -531,13 +537,13 @@ foreach ($categories as $item) {
'invisible.png',
get_lang('Show')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&lp_id=$id&action=toggle_visible&new_status=1"
);
} else {
$dsp_visible = Display::url(
Display::return_icon('visible.png', get_lang('Hide')),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&lp_id=$id&action=toggle_visible&new_status=0"
);
}
@ -545,7 +551,7 @@ foreach ($categories as $item) {
// Tracking command
$trackingActionUrl = 'lp_controller.php?'
.api_get_cidreq().'&'
.$cidReq.'&'
.http_build_query(['action' => 'report', 'lp_id' => $id]);
$trackingAction = Display::url(
@ -558,13 +564,13 @@ foreach ($categories as $item) {
/* PUBLISH COMMAND */
if ($sessionId == $details['lp_session']) {
if ($details['lp_published'] == "i") {
if ($details['lp_published'] == 'i') {
$dsp_publish = Display::url(
Display::return_icon(
'lp_publish_na.png',
get_lang('LearnpathPublish')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&lp_id=$id&action=toggle_publish&new_status=v"
);
} else {
@ -573,7 +579,7 @@ foreach ($categories as $item) {
'lp_publish.png',
get_lang('LearnpathDoNotPublish')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&lp_id=$id&action=toggle_publish&new_status=i"
);
}
@ -600,7 +606,7 @@ foreach ($categories as $item) {
'reload.png',
get_lang('PreventMultipleAttempts')
),
"lp_controller.php?".api_get_cidreq()
'lp_controller.php?'.$cidReq
."&action=switch_attempt_mode&lp_id=$id"
);
}
@ -613,7 +619,7 @@ foreach ($categories as $item) {
'reload_na.png',
get_lang('AllowMultipleAttempts')
),
"lp_controller.php?".api_get_cidreq()
'lp_controller.php?'.$cidReq
."&action=switch_attempt_mode&lp_id=$id"
);
}
@ -626,7 +632,7 @@ foreach ($categories as $item) {
'reload.png',
get_lang('AllowMultipleAttempts')
),
"lp_controller.php?".api_get_cidreq()
'lp_controller.php?'.$cidReq
."&action=switch_attempt_mode&lp_id=$id"
);
}
@ -646,7 +652,7 @@ foreach ($categories as $item) {
'view_fullscreen.png',
get_lang('ViewModeFullScreen')
),
'lp_controller.php?'.api_get_cidreq()
'lp_controller.php?'.$cidReq
.'&action=switch_view_mode&lp_id='.$id
.$token_parameter
);
@ -657,7 +663,7 @@ foreach ($categories as $item) {
'view_left_right.png',
get_lang('ViewModeEmbedded')
),
'lp_controller.php?'.api_get_cidreq()
'lp_controller.php?'.$cidReq
.'&action=switch_view_mode&lp_id='.$id
.$token_parameter
);
@ -668,7 +674,7 @@ foreach ($categories as $item) {
'view_nofullscreen.png',
get_lang('ViewModeEmbedFrame')
),
'lp_controller.php?'.api_get_cidreq()
'lp_controller.php?'.$cidReq
.'&action=switch_view_mode&lp_id='.$id
.$token_parameter
);
@ -679,14 +685,14 @@ foreach ($categories as $item) {
'window_list_slide.png',
get_lang('ViewModeImpress')
),
'lp_controller.php?'.api_get_cidreq()
'lp_controller.php?'.$cidReq
.'&action=switch_view_mode&lp_id='.$id
.$token_parameter
);
break;
}
} else {
if ($details['lp_view_mode'] == 'fullscreen') {
if ($details['lp_view_mode'] === 'fullscreen') {
$dsp_default_view = Display::return_icon(
'view_fullscreen_na.png',
get_lang('ViewModeEmbedded')
@ -707,8 +713,7 @@ foreach ($categories as $item) {
'bug.png',
get_lang('HideDebug')
),
"lp_controller.php?".api_get_cidreq()
."&action=switch_scorm_debug&lp_id=$id"
"lp_controller.php?$cidReq&action=switch_scorm_debug&lp_id=$id"
);
} else {
$dsp_debug = Display::url(
@ -716,8 +721,7 @@ foreach ($categories as $item) {
'bug_na.png',
get_lang('ShowDebug')
),
"lp_controller.php?".api_get_cidreq()
."&action=switch_scorm_debug&lp_id=$id"
'lp_controller.php?'.$cidReq."&action=switch_scorm_debug&lp_id=$id"
);
}
}
@ -726,13 +730,13 @@ foreach ($categories as $item) {
if ($details['lp_type'] == 1) {
$dsp_disk = Display::url(
Display::return_icon('cd.png', get_lang('ExportShort')),
api_get_self()."?".api_get_cidreq()
api_get_self()."?".$cidReq
."&action=export&lp_id=$id"
);
} elseif ($details['lp_type'] == 2) {
$dsp_disk = Display::url(
Display::return_icon('cd.png', get_lang('ExportShort')),
api_get_self()."?".api_get_cidreq()
api_get_self()."?".$cidReq
."&action=export&lp_id=$id&export_name="
.api_replace_dangerous_char($name).".zip"
);
@ -746,7 +750,7 @@ foreach ($categories as $item) {
// Copy
$copy = Display::url(
Display::return_icon('cd_copy.png', get_lang('Copy')),
api_get_self()."?".api_get_cidreq()."&action=copy&lp_id=$id"
api_get_self().'?'.$cidReq."&action=copy&lp_id=$id"
);
// Subscribe users
@ -759,7 +763,7 @@ foreach ($categories as $item) {
'user.png',
get_lang('SubscribeUsersToLp')
),
api_get_path(WEB_CODE_PATH)."lp/lp_subscribe_users.php?lp_id=$id&".api_get_cidreq()
api_get_path(WEB_CODE_PATH)."lp/lp_subscribe_users.php?lp_id=$id&".$cidReq
);
}
@ -774,7 +778,7 @@ foreach ($categories as $item) {
'launch.png',
get_lang('DisableLPAutoLaunch')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&action=auto_launch&status=0&lp_id=$id"
);
} else {
@ -783,7 +787,7 @@ foreach ($categories as $item) {
'launch_na.png',
get_lang('EnableLPAutoLaunch')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&action=auto_launch&status=1&lp_id=$id"
);
}
@ -795,7 +799,7 @@ foreach ($categories as $item) {
'pdf.png',
get_lang('ExportToPDFOnlyHTMLAndImages')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&action=export_to_pdf&lp_id=$id"
);
@ -806,7 +810,7 @@ foreach ($categories as $item) {
'delete.png',
get_lang('LearnpathDeleteLearnpath')
),
'lp_controller.php?'.api_get_cidreq()."&action=delete&lp_id=$id",
'lp_controller.php?'.$cidReq."&action=delete&lp_id=$id",
[
'onclick' => "javascript: return confirmation('".addslashes($name)."');",
]
@ -824,13 +828,13 @@ foreach ($categories as $item) {
if ($details['lp_display_order'] == 1 && $max != 1) {
$dsp_order .= Display::url(
Display::return_icon('down.png', get_lang('MoveDown')),
"lp_controller.php?".api_get_cidreq()
"lp_controller.php?".$cidReq
."&action=move_lp_down&lp_id=$id&category_id=$categoryId"
);
} elseif ($current == $max - 1 && $max != 1) {
$dsp_order .= Display::url(
Display::return_icon('up.png', get_lang('MoveUp')),
"lp_controller.php?".api_get_cidreq()
"lp_controller.php?".$cidReq
."&action=move_lp_up&lp_id=$id&category_id=$categoryId"
);
} elseif ($max == 1) {
@ -838,19 +842,19 @@ foreach ($categories as $item) {
} else {
$dsp_order .= Display::url(
Display::return_icon('down.png', get_lang('MoveDown')),
"lp_controller.php?".api_get_cidreq()
"lp_controller.php?".$cidReq
."&action=move_lp_down&lp_id=$id&category_id=$categoryId"
);
$dsp_order .= Display::url(
Display::return_icon('up.png', get_lang('MoveUp')),
"lp_controller.php?".api_get_cidreq()
"lp_controller.php?".$cidReq
."&action=move_lp_up&lp_id=$id&category_id=$categoryId"
);
}
}
if ($details['lp_type'] == 2) {
$url = api_get_path(WEB_CODE_PATH)."lp/lp_update_scorm.php?".api_get_cidreq()."&lp_id=$id";
$url = api_get_path(WEB_CODE_PATH).'lp/lp_update_scorm.php?'.$cidReq."&lp_id=$id";
$actionUpdateScormFile = Display::url(
Display::return_icon('upload_file.png', get_lang('Update')),
$url
@ -863,7 +867,7 @@ foreach ($categories as $item) {
'backup.png',
get_lang('ExportToChamiloFormat')
),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&action=export_to_course_build&lp_id=$id"
);
}
@ -879,7 +883,7 @@ foreach ($categories as $item) {
if ($details['seriousgame_mode'] == 0) {
$actionSeriousGame = Display::toolbarButton(
null,
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&lp_id=$id&action=toggle_seriousgame",
'trophy',
'default',
@ -891,7 +895,7 @@ foreach ($categories as $item) {
} else {
$actionSeriousGame = Display::toolbarButton(
null,
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&lp_id=$id&action=toggle_seriousgame",
'trophy',
'warning',
@ -906,22 +910,19 @@ foreach ($categories as $item) {
// Student
$export_icon = Display::url(
Display::return_icon('pdf.png', get_lang('ExportToPDF')),
api_get_self().'?'.api_get_cidreq()
api_get_self().'?'.$cidReq
."&action=export_to_pdf&lp_id=$id"
);
}
$hideScormExportLink = api_get_setting('hide_scorm_export_link');
if ($hideScormExportLink === 'true') {
$dsp_disk = null;
}
$hideScormCopyLink = api_get_setting('hide_scorm_copy_link');
if ($hideScormCopyLink === 'true') {
$copy = null;
}
$hideScormPdfLink = api_get_setting('hide_scorm_pdf_link');
if ($hideScormPdfLink === 'true') {
$export_icon = null;
}

Loading…
Cancel
Save