MDL-58518 block_myoverview: use user's midnight for sort by courses
[moodle.git] / blocks / myoverview / amd / src / event_list_by_course.js
CommitLineData
bcd6ae73
SL
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * Javascript to load and render the list of calendar events grouping by course.
18 *
19 * @module block_myoverview/events_by_course_list
20 * @package block_myoverview
21 * @copyright 2016 Simey Lameze <simey@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
42016853
RW
24define(
25[
26 'jquery',
27 'block_myoverview/event_list',
28 'block_myoverview/calendar_events_repository'
29],
30function($, EventList, EventsRepository) {
bcd6ae73 31
5f6ff895
RW
32 var SECONDS_IN_DAY = 60 * 60 * 24;
33
bcd6ae73
SL
34 var SELECTORS = {
35 EVENTS_BY_COURSE_CONTAINER: '[data-region="course-events-container"]',
f31240eb 36 EVENT_LIST_CONTAINER: '[data-region="event-list-container"]',
bcd6ae73
SL
37 };
38
39 /**
40 * Loop through course events containers and load calendar events for that course.
41 *
42 * @method load
43 * @param {Object} root The root element of sort by course list.
44 */
45 var load = function(root) {
42016853 46 var courseBlocks = root.find(SELECTORS.EVENTS_BY_COURSE_CONTAINER);
bcd6ae73 47
42016853
RW
48 if (!courseBlocks.length) {
49 return;
50 }
51
f31240eb
RW
52 var eventList = courseBlocks.find(SELECTORS.EVENT_LIST_CONTAINER).first();
53 var midnight = eventList.attr('data-midnight');
5f6ff895 54 var startTime = midnight - (14 * SECONDS_IN_DAY);
f31240eb 55 var limit = eventList.attr('data-limit');
42016853
RW
56 var courseIds = courseBlocks.map(function() {
57 return $(this).attr('data-course-id');
58 }).get();
59
60 // Load the first set of events for each course in a single request.
61 // We want to avoid sending an individual request for each course because
62 // there could be lots of them.
63 var coursesPromise = EventsRepository.queryByCourses({
64 courseids: courseIds,
5e52a8a9 65 starttime: startTime,
42016853
RW
66 limit: limit
67 });
68
69 // Load the events into each course block.
70 courseBlocks.each(function(index, container) {
bcd6ae73 71 container = $(container);
42016853
RW
72 var courseId = container.attr('data-course-id');
73 var eventListContainer = container.find(EventList.rootSelector);
74 var promise = $.Deferred();
75
76 // Once all of the course events have been loaded then we need
77 // to extract just the ones relevant to this course block and
78 // hand them to the event list to render.
775c6bac 79 coursesPromise.done(function(result) {
42016853
RW
80 var events = [];
81 // Get this course block's events from the collection returned
82 // from the server.
83 var courseGroup = result.groupedbycourse.filter(function(group) {
84 return group.courseid == courseId;
85 });
86
87 if (courseGroup.length) {
88 events = courseGroup[0].events;
89 }
90
91 promise.resolve({events: events});
92 }).fail(function(e) {
93 promise.reject(e);
94 });
95
96 // Provide the event list with a promise that will be resolved
97 // when we have received the events from the server.
98 EventList.load(eventListContainer, promise);
bcd6ae73
SL
99 });
100 };
101
102 return {
103 init: function(root) {
104 root = $(root);
105 load(root);
106 }
107 };
108});