Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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