Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * A javascript module to retrieve calendar events from the server.
18
 *
19
 * @module     block_timeline/calendar_events_repository
20
 * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notification) {
24
 
25
    var DEFAULT_LIMIT = 20;
26
 
27
    /**
28
     * Retrieve a list of calendar events for the logged in user for the
29
     * given course.
30
     *
31
     * Valid args are:
32
     * int courseid     Only get events for this course
33
     * int starttime    Only get events after this time
34
     * int endtime      Only get events before this time
35
     * int limit        Limit the number of results returned
36
     * int aftereventid Offset the result set from the given id
37
     *
38
     * @method queryByCourse
39
     * @param {object} args The request arguments
40
     * @return {promise} Resolved with an array of the calendar events
41
     */
42
    var queryByCourse = function(args) {
43
        if (!args.hasOwnProperty('limit')) {
44
            args.limit = DEFAULT_LIMIT;
45
        }
46
 
47
        args.limitnum = args.limit;
48
        delete args.limit;
49
 
50
        if (args.hasOwnProperty('starttime')) {
51
            args.timesortfrom = args.starttime;
52
            delete args.starttime;
53
        }
54
 
55
        if (args.hasOwnProperty('endtime')) {
56
            args.timesortto = args.endtime;
57
            delete args.endtime;
58
        }
59
 
60
        var request = {
61
            methodname: 'core_calendar_get_action_events_by_course',
62
            args: args
63
        };
64
 
65
        var promise = Ajax.call([request])[0];
66
 
67
        promise.fail(Notification.exception);
68
 
69
        return promise;
70
    };
71
 
72
    /**
73
     * Retrieve a list of calendar events for the given courses for the
74
     * logged in user.
75
     *
76
     * Valid args are:
77
     * array courseids    Get events for these courses
78
     * int   starttime    Only get events after this time
79
     * int   endtime      Only get events before this time
80
     * int   limit        Limit the number of results returned
81
     *
82
     * @method queryByCourses
83
     * @param {object} args The request arguments
84
     * @return {promise} Resolved with an array of the calendar events
85
     */
86
    var queryByCourses = function(args) {
87
        if (!args.hasOwnProperty('limit')) {
88
            // This is intentionally smaller than the default limit.
89
            args.limit = 10;
90
        }
91
 
92
        args.limitnum = args.limit;
93
        delete args.limit;
94
 
95
        if (args.hasOwnProperty('starttime')) {
96
            args.timesortfrom = args.starttime;
97
            delete args.starttime;
98
        }
99
 
100
        if (args.hasOwnProperty('endtime')) {
101
            args.timesortto = args.endtime;
102
            delete args.endtime;
103
        }
104
 
105
        var request = {
106
            methodname: 'core_calendar_get_action_events_by_courses',
107
            args: args
108
        };
109
 
110
        var promise = Ajax.call([request])[0];
111
 
112
        promise.fail(Notification.exception);
113
 
114
        return promise;
115
    };
116
 
117
    /**
118
     * Retrieve a list of calendar events for the logged in user after the given
119
     * time.
120
     *
121
     * Valid args are:
122
     * int starttime    Only get events after this time
123
     * int endtime      Only get events before this time
124
     * int limit        Limit the number of results returned
125
     * int aftereventid Offset the result set from the given id
126
     *
127
     * @method queryByTime
128
     * @param {object} args The request arguments
129
     * @return {promise} Resolved with an array of the calendar events
130
     */
131
    var queryByTime = function(args) {
132
        if (!args.hasOwnProperty('limit')) {
133
            args.limit = DEFAULT_LIMIT;
134
        }
135
 
136
        args.limitnum = args.limit;
137
        delete args.limit;
138
 
139
        if (args.hasOwnProperty('starttime')) {
140
            args.timesortfrom = args.starttime;
141
            delete args.starttime;
142
        }
143
 
144
        if (args.hasOwnProperty('endtime')) {
145
            args.timesortto = args.endtime;
146
            delete args.endtime;
147
        }
148
        // Don't show events related to courses that the user is suspended in.
149
        args.limittononsuspendedevents = true;
150
 
151
        var request = {
152
            methodname: 'core_calendar_get_action_events_by_timesort',
153
            args: args
154
        };
155
 
156
        var promise = Ajax.call([request])[0];
157
 
158
        promise.fail(Notification.exception);
159
 
160
        return promise;
161
    };
162
 
163
    return {
164
        queryByTime: queryByTime,
165
        queryByCourse: queryByCourse,
166
        queryByCourses: queryByCourses,
167
    };
168
});