1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Javascript to load and render the list of calendar events grouping by course.
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
27 'block_myoverview/event_list',
28 'block_myoverview/calendar_events_repository'
30 function($, EventList, EventsRepository) {
32 var SECONDS_IN_DAY = 60 * 60 * 24;
35 EVENTS_BY_COURSE_CONTAINER: '[data-region="course-events-container"]',
36 EVENT_LIST_CONTAINER: '[data-region="event-list-container"]',
40 * Loop through course events containers and load calendar events for that course.
43 * @param {Object} root The root element of sort by course list.
45 var load = function(root) {
46 var courseBlocks = root.find(SELECTORS.EVENTS_BY_COURSE_CONTAINER);
48 if (!courseBlocks.length) {
52 var eventList = courseBlocks.find(SELECTORS.EVENT_LIST_CONTAINER).first();
53 var midnight = eventList.attr('data-midnight');
54 var startTime = midnight - (14 * SECONDS_IN_DAY);
55 var limit = eventList.attr('data-limit');
56 var courseIds = courseBlocks.map(function() {
57 return $(this).attr('data-course-id');
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({
69 // Load the events into each course block.
70 courseBlocks.each(function(index, container) {
71 container = $(container);
72 var courseId = container.attr('data-course-id');
73 var eventListContainer = container.find(EventList.rootSelector);
74 var promise = $.Deferred();
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.
79 coursesPromise.done(function(result) {
81 // Get this course block's events from the collection returned
83 var courseGroup = result.groupedbycourse.filter(function(group) {
84 return group.courseid == courseId;
87 if (courseGroup.length) {
88 events = courseGroup[0].events;
91 promise.resolve({events: events});
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);
103 init: function(root) {