Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * A module to manage ajax requests.
3
 *
4
 * @module moodle-report_loglive-fetchlogs
5
 */
6
 
7
/**
8
 * A module to manage ajax requests.
9
 *
10
 * @class moodle-report_loglive.fetchlogs
11
 * @extends Base
12
 * @constructor
13
 */
14
function FetchLogs() {
15
    FetchLogs.superclass.constructor.apply(this, arguments);
16
}
17
 
18
 
19
var CSS = {
20
        NEWROW: 'newrow',
21
        SPINNER: 'spinner',
22
        ICONSMALL: 'iconsmall'
23
    },
24
    SELECTORS = {
25
        NEWROW: '.' + CSS.NEWROW,
26
        TBODY: '.flexible tbody',
27
        ACTIONLINK: '[data-action="action-popup"]',
28
        PAUSEBUTTON: '#livelogs-pause-button',
29
        SPINNER: '.' + CSS.SPINNER
30
    };
31
 
32
Y.extend(FetchLogs, Y.Base, {
33
    /**
34
     * Reference to the callBack object generated by Y.later
35
     *
36
     * @property callBack
37
     * @type Object
38
     * @default {}
39
     * @protected
40
     */
41
    callBack: {},
42
 
43
    /**
44
     * Reference to the spinner node.
45
     *
46
     * @property callBack
47
     * @type Object
48
     * @default {}
49
     * @protected
50
     */
51
    spinner: {},
52
 
53
    /**
54
     * Reference to the toggleButton node.
55
     *
56
     * @property pauseButton
57
     * @type Object
58
     * @default {}
59
     * @protected
60
     */
61
    pauseButton: {},
62
 
63
    /**
64
     * Initializer.
65
     * Basic setup and event listeners.
66
     *
67
     * @method initializer
68
     */
69
    initializer: function() {
70
        // We don't want the pages to be updated when viewing a specific page in the chain.
71
        if (this.get('page') === 0) {
72
            this.callBack = Y.later(this.get('interval') * 1000, this, this.fetchRecentLogs, null, true);
73
        }
74
 
75
        this.spinner = Y.one(SELECTORS.SPINNER);
76
        this.pauseButton = Y.one(SELECTORS.PAUSEBUTTON);
77
        this.spinner.hide();
78
        Y.one(SELECTORS.TBODY).delegate('click', this.openActionLink, SELECTORS.ACTIONLINK, this);
79
        Y.one(SELECTORS.PAUSEBUTTON).on('click', this.toggleUpdate, this);
80
    },
81
 
82
    /**
83
     * Method to fetch recent logs.
84
     *
85
     * @method fetchRecentLogs
86
     */
87
    fetchRecentLogs: function() {
88
        this.spinner.show(); // Show a loading icon.
89
        var data = {
90
            logreader: this.get('logreader'),
91
            since: this.get('since'),
92
            page: this.get('page'),
93
            id: this.get('courseid')
94
        };
95
        var cfg = {
96
            method: 'get',
97
            context: this,
98
            on: {
99
                complete: this.updateLogTable
100
            },
101
            data: data
102
        };
103
        var url = M.cfg.wwwroot + '/report/loglive/loglive_ajax.php';
104
        Y.io(url, cfg);
105
    },
106
 
107
    /**
108
     * Method to update the log table, populate the table with new entries and remove old entries if needed.
109
     *
110
     * @method updateLogTable
111
     */
112
    updateLogTable: function(tid, response) {
113
        // Hide loading icon, give sometime to people to actually see it. We should do it, event in case of an error.
114
        Y.later(600, this, 'hideLoadingIcon');
115
        var responseobject;
116
        // Attempt to parse the response into an object.
117
        try {
118
            responseobject = Y.JSON.parse(response.responseText);
119
            if (responseobject.error) {
120
                Y.use('moodle-core-notification-ajaxexception', function() {
121
                    return new M.core.ajaxException(responseobject);
122
                });
123
                return this;
124
            }
125
        } catch (error) {
126
            Y.use('moodle-core-notification-exception', function() {
127
                return new M.core.exception(error);
128
            });
129
            return this;
130
        }
131
        this.set('since', responseobject.until);
132
        var logs = responseobject.logs;
133
        var tbody = Y.one(SELECTORS.TBODY);
134
        var firstTr = null;
135
        if (tbody && logs) {
136
            firstTr = tbody.get('firstChild');
137
            if (firstTr) {
138
                tbody.insertBefore(logs, firstTr);
139
            }
140
            // @Todo, when no data is present our library should generate an empty table. so data can be added
141
            // dynamically (MDL-44525).
142
 
143
            // Let us chop off some data from end of table to prevent really long pages.
144
            var oldChildren = tbody.get('children').slice(this.get('perpage'));
145
            oldChildren.remove();
146
            Y.later(5000, this, 'removeHighlight'); // Remove highlighting from new rows.
147
        }
148
    },
149
 
150
    /**
151
     * Remove background highlight from the newly added rows.
152
     *
153
     * @method removeHighlight
154
     */
155
    removeHighlight: function() {
156
        Y.all(SELECTORS.NEWROW).removeClass(CSS.NEWROW);
157
    },
158
 
159
    /**
160
     * Hide the spinning icon.
161
     *
162
     * @method hideLoadingIcon
163
     */
164
    hideLoadingIcon: function() {
165
        this.spinner.hide();
166
    },
167
 
168
    /**
169
     * Open a report action link
170
     *
171
     * @param {Event} event
172
     * @method openActionLink
173
     */
174
    openActionLink: function(event) {
175
        var popupAction = JSON.parse(event.target.get('dataset').popupAction);
176
        window.openpopup(event, popupAction.jsfunctionargs);
177
    },
178
 
179
    /**
180
     * Toggle live update.
181
     *
182
     * @method toggleUpdate
183
     */
184
    toggleUpdate: function() {
185
        if (this.callBack) {
186
            this.callBack.cancel();
187
            this.callBack = '';
188
            this.pauseButton.setContent(M.util.get_string('resume', 'report_loglive'));
189
        } else {
190
            this.callBack = Y.later(this.get('interval') * 1000, this, this.fetchRecentLogs, null, true);
191
            this.pauseButton.setContent(M.util.get_string('pause', 'report_loglive'));
192
        }
193
    }
194
}, {
195
    NAME: 'fetchLogs',
196
    ATTRS: {
197
        /**
198
         * time stamp from where the new logs needs to be fetched.
199
         *
200
         * @attribute since
201
         * @default null
202
         * @type String
203
         */
204
        since: null,
205
 
206
        /**
207
         * courseid for which the logs are shown.
208
         *
209
         * @attribute courseid
210
         * @default 0
211
         * @type int
212
         */
213
        courseid: 0,
214
 
215
        /**
216
         * Page number shown to user.
217
         *
218
         * @attribute page
219
         * @default 0
220
         * @type int
221
         */
222
        page: 0,
223
 
224
        /**
225
         * Items to show per page.
226
         *
227
         * @attribute perpage
228
         * @default 100
229
         * @type int
230
         */
231
        perpage: 100,
232
 
233
        /**
234
         * Refresh interval.
235
         *
236
         * @attribute interval
237
         * @default 60
238
         * @type int
239
         */
240
        interval: 60,
241
 
242
        /**
243
         * Which logstore is being used.
244
         *
245
         * @attribute logreader
246
         * @default logstore_standard
247
         * @type String
248
         */
249
        logreader: 'logstore_standard'
250
    }
251
});
252
 
253
Y.namespace('M.report_loglive.FetchLogs').init = function(config) {
254
    return new FetchLogs(config);
255
};