Fix buy courses plugin pagination + fix price BT#16054

pull/2999/head
Julio Montoya 6 years ago
parent 6c30ab0221
commit 221c67a093
  1. 2
      main/inc/lib/display.lib.php
  2. 2
      main/template/default/session/about.tpl
  3. 77
      plugin/buycourses/src/buy_course_plugin.class.php
  4. 13
      plugin/buycourses/src/course_catalog.php
  5. 10
      plugin/buycourses/src/service_catalog.php
  6. 10
      plugin/buycourses/src/session_catalog.php
  7. 1
      plugin/buycourses/view/catalog.tpl

@ -2460,7 +2460,7 @@ class Display
public static function getPagination($url, $currentPage, $pagesCount, $totalItems)
{
$pagination = '';
if ($totalItems > 1) {
if ($totalItems > 1 && $pagesCount > 1) {
$pagination .= '<ul class="pagination">';
for ($i = 0; $i < $pagesCount; $i++) {
$newPage = $i + 1;

@ -85,7 +85,7 @@
{{ 'SalePrice'|get_lang }}
</div>
<div class="price-text">
{{ is_premium.iso_code }} {{ is_premium.price }}
{{ is_premium.iso_code }} {{ is_premium.total_price }}
</div>
<div class="buy-box">
<a href="{{ _p.web }}plugin/buycourses/src/process.php?i={{ is_premium.product_id }}&t={{ is_premium.product_type }}"

@ -565,22 +565,6 @@ class BuyCoursesPlugin extends Plugin
public function getCourseList($first, $maxResults)
{
return $this->getCourses($first, $maxResults);
if (empty($courses)) {
return [];
}
$currency = $this->getSelectedCurrency();
$courseList = [];
foreach ($courses as $course) {
$courseList[] = $this->getCourseForConfiguration(
$course,
$currency
);
}
return $courseList;
}
/**
@ -592,9 +576,13 @@ class BuyCoursesPlugin extends Plugin
*
* @return array
*/
public function getCatalogSessionList($name = null, $min = 0, $max = 0)
public function getCatalogSessionList($start, $end, $name = null, $min = 0, $max = 0, $typeResult = 'all')
{
$sessions = $this->filterSessionList($name, $min, $max);
$sessions = $this->filterSessionList($start, $end, $name, $min, $max, $typeResult);
if ($typeResult === 'count') {
return $sessions;
}
$sessionCatalog = [];
// loop through all sessions
@ -657,9 +645,13 @@ class BuyCoursesPlugin extends Plugin
*
* @return array
*/
public function getCatalogCourseList($name = null, $min = 0, $max = 0)
public function getCatalogCourseList($first, $pageSize, $name = null, $min = 0, $max = 0, $typeResult = 'all')
{
$courses = $this->filterCourseList($name, $min, $max);
$courses = $this->filterCourseList($first, $pageSize, $name, $min, $max, $typeResult);
if ($typeResult === 'count') {
return $courses;
}
if (empty($courses)) {
return [];
@ -2114,7 +2106,7 @@ class BuyCoursesPlugin extends Plugin
$start = (int) $start;
$end = (int) $end;
$conditions = ['LIMIT' => "$start, $end"];
$conditions = ['limit' => "$start, $end"];
$innerJoins = "INNER JOIN $userTable u ON s.owner_id = u.id";
$return = Database::select(
's.id',
@ -2365,7 +2357,7 @@ class BuyCoursesPlugin extends Plugin
*
* @return array
*/
public function getCatalogServiceList($name = null, $min = 0, $max = 0, $appliesTo = '')
public function getCatalogServiceList($start, $end, $name = null, $min = 0, $max = 0, $appliesTo = '', $typeResult = 'all')
{
$servicesTable = Database::get_main_table(self::TABLE_SERVICES);
$userTable = Database::get_main_table(TABLE_MAIN_USER);
@ -2390,13 +2382,21 @@ class BuyCoursesPlugin extends Plugin
$whereConditions['AND s.applies_to = ?'] = $appliesTo;
}
$start = (int) $start;
$end = (int) $end;
$innerJoins = "INNER JOIN $userTable u ON s.owner_id = u.id";
$return = Database::select(
's.*',
"$servicesTable s $innerJoins",
['WHERE' => $whereConditions]
['WHERE' => $whereConditions, 'limit' => "$start, $end"],
$typeResult
);
if ($typeResult === 'count') {
return $return;
}
$services = [];
foreach ($return as $index => $service) {
$services[$index] = $this->getService($service['id']);
@ -2858,12 +2858,8 @@ class BuyCoursesPlugin extends Plugin
*
* @return array
*/
private function filterSessionList($name = null, $min = 0, $max = 0)
private function filterSessionList($start, $end, $name = null, $min = 0, $max = 0, $typeResult = 'all')
{
if (empty($name) && empty($min) && empty($max)) {
return CoursesAndSessionsCatalog::browseSessions();
}
$itemTable = Database::get_main_table(self::TABLE_ITEM);
$sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
@ -2887,12 +2883,20 @@ class BuyCoursesPlugin extends Plugin
$whereConditions['AND i.price <= ?'] = $max;
}
$start = (int) $start;
$end = (int) $end;
$sessionIds = Database::select(
's.id',
"$sessionTable s INNER JOIN $innerJoin",
['where' => $whereConditions]
['where' => $whereConditions, 'limit' => "$start, $end"],
$typeResult
);
if ($typeResult === 'count') {
return $sessionIds;
}
if (!$sessionIds) {
return [];
}
@ -2918,12 +2922,8 @@ class BuyCoursesPlugin extends Plugin
*
* @return array
*/
private function filterCourseList($name = null, $min = 0, $max = 0)
private function filterCourseList($start, $end, $name = '', $min = 0, $max = 0, $typeResult = 'all')
{
if (empty($name) && empty($min) && empty($max)) {
return $this->getCourses();
}
$itemTable = Database::get_main_table(self::TABLE_ITEM);
$courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
$urlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
@ -2950,6 +2950,8 @@ class BuyCoursesPlugin extends Plugin
}
$whereConditions['AND url.access_url_id = ?'] = $urlId;
$start = (int) $start;
$end = (int) $end;
$courseIds = Database::select(
'c.id',
@ -2959,9 +2961,14 @@ class BuyCoursesPlugin extends Plugin
INNER JOIN $urlTable url
ON c.id = url.c_id
",
['where' => $whereConditions]
['where' => $whereConditions, 'limit' => "$start, $end"],
$typeResult
);
if ($typeResult === 'count') {
return $courseIds;
}
if (!$courseIds) {
return [];
}

@ -14,7 +14,7 @@ $plugin = BuyCoursesPlugin::create();
$includeSessions = $plugin->get('include_sessions') === 'true';
$includeServices = $plugin->get('include_services') === 'true';
$nameFilter = null;
$nameFilter = '';
$minFilter = 0;
$maxFilter = 0;
@ -51,7 +51,15 @@ $form->addElement(
$form->addHtml('<hr>');
$form->addButtonFilter(get_lang('Search'));
$courseList = $plugin->getCatalogCourseList($nameFilter, $minFilter, $maxFilter);
$pageSize = BuyCoursesPlugin::PAGINATION_PAGE_SIZE;
$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$first = $pageSize * ($currentPage - 1);
$courseList = $plugin->getCatalogCourseList($first, $pageSize, $nameFilter, $minFilter, $maxFilter);
$totalItems = $plugin->getCatalogCourseList($first, $pageSize, $nameFilter, $minFilter, $maxFilter, 'count');
$pagesCount = ceil($totalItems / $pageSize);
$url = api_get_self().'?';
$pagination = Display::getPagination($url, $currentPage, $pagesCount, $totalItems);
// View
if (api_is_platform_admin()) {
@ -77,6 +85,7 @@ $tpl->assign('showing_courses', true);
$tpl->assign('courses', $courseList);
$tpl->assign('sessions_are_included', $includeSessions);
$tpl->assign('services_are_included', $includeServices);
$tpl->assign('pagination', $pagination);
$content = $tpl->fetch('buycourses/view/catalog.tpl');

@ -60,7 +60,14 @@ $form->addSelect('applies_to', $plugin->get_lang('AppliesTo'), $appliesToOptions
$form->addHtml('<hr>');
$form->addButtonFilter(get_lang('Search'));
$serviceList = $plugin->getCatalogServiceList($nameFilter, $minFilter, $maxFilter, $appliesToFilter);
$pageSize = BuyCoursesPlugin::PAGINATION_PAGE_SIZE;
$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$first = $pageSize * ($currentPage - 1);
$serviceList = $plugin->getCatalogServiceList($first, $pageSize, $nameFilter, $minFilter, $maxFilter, $appliesToFilter);
$totalItems = $plugin->getCatalogServiceList($first, $pageSize, $nameFilter, $minFilter, $maxFilter, $appliesToFilter, 'count');
$pagesCount = ceil($totalItems / $pageSize);
$url = api_get_self().'?';
$pagination = Display::getPagination($url, $currentPage, $pagesCount, $totalItems);
// View
if (api_is_platform_admin()) {
@ -86,6 +93,7 @@ $tpl->assign('showing_services', true);
$tpl->assign('services', $serviceList);
$tpl->assign('sessions_are_included', $includeSessions);
$tpl->assign('services_are_included', $includeServices);
$tpl->assign('pagination', $pagination);
$content = $tpl->fetch('buycourses/view/catalog.tpl');

@ -55,7 +55,14 @@ $form->addElement(
$form->addHtml('<hr>');
$form->addButtonFilter(get_lang('Search'));
$sessionList = $plugin->getCatalogSessionList($nameFilter, $minFilter, $maxFilter);
$pageSize = BuyCoursesPlugin::PAGINATION_PAGE_SIZE;
$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$first = $pageSize * ($currentPage - 1);
$sessionList = $plugin->getCatalogSessionList($first, $pageSize,$nameFilter, $minFilter, $maxFilter);
$totalItems = $plugin->getCatalogSessionList($first, $pageSize, $nameFilter, $minFilter, $maxFilter, 'count');
$pagesCount = ceil($totalItems / $pageSize);
$url = api_get_self().'?';
$pagination = Display::getPagination($url, $currentPage, $pagesCount, $totalItems);
// View
if (api_is_platform_admin()) {
@ -77,6 +84,7 @@ $template->assign('sessions_are_included', $includeSessions);
$template->assign('services_are_included', $includeServices);
$template->assign('showing_sessions', true);
$template->assign('sessions', $sessionList);
$template->assign('pagination', $pagination);
$content = $template->fetch('buycourses/view/catalog.tpl');

@ -196,6 +196,7 @@
{% endfor %}
{% endif %}
</div>
{{ pagination }}
</div>
</div>
</div>

Loading…
Cancel
Save