Skip to content

Commit

Permalink
MDL-58098 block_myoverview: load courses view data on server side
Browse files Browse the repository at this point in the history
Part of MDL-55611 epic.
  • Loading branch information
lameze authored and Damyon Wiese committed Apr 3, 2017
1 parent eb481a3 commit 788a79e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 37 deletions.
1 change: 1 addition & 0 deletions blocks/myoverview/amd/build/paging_bar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions blocks/myoverview/amd/build/paging_content.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions blocks/myoverview/amd/src/paging_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ define(['jquery', 'core/templates', 'block_myoverview/paging_bar'],
* @param loadContentCallback
* @constructor
*/
var PagingContent = function(root, pagingBarElement, loadContentCallback) {
var PagingContent = function(root, pagingBarElement) {
this.root = $(root);
this.pagingBar = $(pagingBarElement);
this.loadContent = loadContentCallback;

};

PagingContent.rootSelector = SELECTORS.ROOT;
Expand Down
35 changes: 27 additions & 8 deletions blocks/myoverview/classes/output/courses_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,54 @@ public function __construct($courses) {
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $output
* @return stdClass
* @return array
*/
public function export_for_template(renderer_base $output) {
$today = time();

// Build courses view data structure.
$coursesview = [];

// How many courses we have per status?
$coursesbystatus = ['past' => 0, 'inprogress' => 0, 'future' => 0];
foreach ($this->courses as $course) {
$startdate = $course->startdate;
$enddate = $course->enddate;

if ($startdate < $today && $enddate < $today) {

$pastpages = floor($coursesbystatus['past'] / $this::COURSES_PER_PAGE);

$coursesview['past']['pages'][$pastpages]['courses'][] = $course;
$coursesview['past']['pages'][$pastpages]['active'] = ($pastpages == 0 ? true : false);
$coursesview['past']['pages'][$pastpages]['page'] = $pastpages + 1;
$coursesbystatus['past']++;

} elseif ($startdate <= $today && $enddate >= $today) {
$inprogresspages = floor($coursesbystatus['inprogress'] / $this::COURSES_PER_PAGE);

$coursesview['inprogress']['pages'][$inprogresspages]['courses'][] = $course;
$coursesview['inprogress']['pages'][$inprogresspages]['active'] = ($inprogresspages == 0 ? true : false);
$coursesview['inprogress']['pages'][$inprogresspages]['page'] = $inprogresspages + 1;
$coursesbystatus['inprogress']++;
} else {
$futurepages = floor($coursesbystatus['future'] / $this::COURSES_PER_PAGE);

$coursesview['future']['pages'][$futurepages]['courses'][] = $course;
$coursesview['future']['pages'][$futurepages]['active'] = ($futurepages == 0 ? true : false);
$coursesview['future']['pages'][$futurepages]['page'] = $futurepages + 1;
$coursesbystatus['future']++;
}
}

// Build paging bar structure.
$pagingbar = [];
// Build courses view paging bar structure.
foreach ($coursesbystatus as $status => $total) {
$quantpages = ceil($total / $this::COURSES_PER_PAGE);
$pagingbar[$status]['pagingbar']['pagecount'] = $quantpages;
$pagingbar[$status]['pagingbar']['first'] = ['page' => '&laquo;', 'url' => '#'];
$pagingbar[$status]['pagingbar']['last'] = ['page' => '&raquo;', 'url' => '#'];
$coursesview[$status]['pagingbar']['pagecount'] = $quantpages;
$coursesview[$status]['pagingbar']['first'] = ['page' => '&laquo;', 'url' => '#'];
$coursesview[$status]['pagingbar']['last'] = ['page' => '&raquo;', 'url' => '#'];
for ($page = 0; $page < $quantpages; $page++) {
$pagingbar[$status]['pagingbar']['pages'][$page] = [
$coursesview[$status]['pagingbar']['pages'][$page] = [
'number' => $page+1,
'page' => $page+1,
'url' => '#',
Expand All @@ -90,6 +109,6 @@ public function export_for_template(renderer_base $output) {
}
}

return $pagingbar;
return $coursesview;
}
}
2 changes: 0 additions & 2 deletions blocks/myoverview/classes/output/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class main implements renderable, templatable {
* @return stdClass
*/
public function export_for_template(renderer_base $output) {

$courses = enrol_get_my_courses('id, shortname, startdate, enddate, summary');

$coursesummary = new course_summary($courses);
$coursesview = new courses_view($courses);

Expand Down
2 changes: 1 addition & 1 deletion blocks/myoverview/templates/course-paging-content.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
}}
{{< block_myoverview/paging-content }}
{{$paging-content-item}}
{{> block_myoverview/course-summary }}
{{> block_myoverview/course-paging-content-item }}
{{/paging-content-item}}
{{/ block_myoverview/paging-content }}
27 changes: 4 additions & 23 deletions blocks/myoverview/templates/courses-view-by-status.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,14 @@
</div>
</div>
{{#js}}
require(
[
'jquery',
'core/templates',
'block_myoverview/paging_bar',
'block_myoverview/paging_content',
'block_myoverview/courses_view_repository'
],
function($, Templates, PagingBar, PagingContent, CoursesRepository) {
require(['jquery', 'block_myoverview/paging_bar', 'block_myoverview/paging_content'],
function($, PagingBar, PagingContent) {
var root = $('#{{$id}}courses-view-status-{{uniqid}}{{/id}}');
var pagingBarElement = root.find(PagingBar.rootSelector);
var pagingContentElement = root.find(PagingContent.rootSelector);
var loadContentCallback = function(pageNumber) {
var limit = root.attr('data-limit');
var offset = (pageNumber - 1) * limit;
var status = root.attr('data-status');
return CoursesRepository.queryFromStatus(status, limit, offset)
.then(function(courses) {
return Templates.render('block_myoverview/course-paging-content-item', {courses: courses, page: pageNumber});
})
};

var content = new PagingContent(pagingContentElement, pagingBarElement, loadContentCallback);
var content = new PagingContent(pagingContentElement, pagingBarElement);
content.registerEventListeners();
content.showPage(1);
});
{{/js}}
{{/js}}
4 changes: 3 additions & 1 deletion blocks/myoverview/templates/paging-content-item.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
{
}
}}
<div data-region="paging-content-item" data-page="{{page}}" class="container-fluid">
<div data-region="paging-content-item"
data-page="{{page}}"
class="container-fluid {{#active}}active{{/active}}{{^active}}hidden{{/active}}">
{{$content}}
{{{content}}}
{{/content}}
Expand Down

0 comments on commit 788a79e

Please sign in to comment.