From 5df117d4385b44d7fbd023c97ef6afcc36cf960c Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 27 Feb 2017 11:26:09 +0800 Subject: [PATCH] MDL-57447 block_myoverview: build paging bar data structure Part of MDL-55611 epic. --- .../classes/output/courses_view.php | 133 +++++------------- 1 file changed, 38 insertions(+), 95 deletions(-) diff --git a/blocks/myoverview/classes/output/courses_view.php b/blocks/myoverview/classes/output/courses_view.php index 3852ae48ba7..541c117b777 100644 --- a/blocks/myoverview/classes/output/courses_view.php +++ b/blocks/myoverview/classes/output/courses_view.php @@ -34,6 +34,8 @@ use templatable; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class courses_view implements renderable, templatable { + /** Quantity of courses per page. */ + const COURSES_PER_PAGE = 6; /** * Export this data so it can be used as the context for a mustache template. @@ -42,100 +44,41 @@ class courses_view implements renderable, templatable { * @return stdClass */ public function export_for_template(renderer_base $output) { - return [ - 'past' => [ - 'pagingbar' => [ - 'pagecount' => 3, - 'first' => [ - 'page' => '«', - 'url' => '#', - ], - 'last' => [ - 'page' => '»', - 'url' => '#', - ], - 'pages' => [ - [ - 'number' => 1, - 'page' => 1, - 'url' => '#', - 'active' => true, - ], - [ - 'number' => 2, - 'page' => 2, - 'url' => '#', - ], - [ - 'number' => 3, - 'page' => 3, - 'url' => '#', - ], - ] - ], - ], - 'inprogress' => [ - 'pagingbar' => [ - 'pagecount' => 3, - 'first' => [ - 'page' => '«', - 'url' => '#', - ], - 'last' => [ - 'page' => '»', - 'url' => '#', - ], - 'pages' => [ - [ - 'number' => 1, - 'page' => 1, - 'url' => '#', - 'active' => true, - ], - [ - 'number' => 2, - 'page' => 2, - 'url' => '#', - ], - [ - 'number' => 3, - 'page' => 3, - 'url' => '#', - ], - ] - ], - ], - 'future' => [ - 'pagingbar' => [ - 'pagecount' => 3, - 'first' => [ - 'page' => '«', - 'url' => '#', - ], - 'last' => [ - 'page' => '»', - 'url' => '#', - ], - 'pages' => [ - [ - 'number' => 1, - 'page' => 1, - 'url' => '#', - 'active' => true, - ], - [ - 'number' => 2, - 'page' => 2, - 'url' => '#', - ], - [ - 'number' => 3, - 'page' => 3, - 'url' => '#', - ], - ] - ], - ], - ]; + $courses = enrol_get_my_courses('startdate, enddate'); + $today = time(); + + // How many courses we have per status? + $coursesbystatus = ['past' => 0, 'inprogress' => 0, 'future' => 0]; + foreach ($courses as $course) { + $startdate = $course->startdate; + $enddate = $course->enddate; + + if ($startdate < $today && $enddate < $today) { + $coursesbystatus['past']++; + } elseif ($startdate <= $today && $enddate >= $today) { + $coursesbystatus['inprogress']++; + } else { + $coursesbystatus['future']++; + } + } + + // Build paging bar structure. + $pagingbar = []; + foreach ($coursesbystatus as $status => $total) { + $quantpages = ceil($total / $this::COURSES_PER_PAGE); + $pagingbar[$status]['pagingbar']['pagecount'] = $quantpages; + $pagingbar[$status]['pagingbar']['first'] = ['page' => '«', 'url' => '#']; + $pagingbar[$status]['pagingbar']['last'] = ['page' => '»', 'url' => '#']; + for ($page = 0; $page < $quantpages; $page++) { + $pagingbar[$status]['pagingbar']['pages'][$page] = [ + 'number' => $page+1, + 'page' => $page+1, + 'url' => '#', + 'active' => ($page == 0 ? true : false) + ]; + } + } + + return $pagingbar; } }