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
 * Javascript controller for the "User summary" panel at the top of the page.
18
 *
19
 * @module     mod_assign/grading_navigation_user_info
20
 * @copyright  2016 Damyon Wiese <damyon@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @since      3.1
23
 */
24
define(['jquery', 'core/notification', 'core/ajax', 'core/templates'], function($, notification, ajax, templates) {
25
 
26
    /**
27
     * UserInfo class.
28
     *
29
     * @class mod_assign/grading_navigation_user_info
30
     * @param {String} selector The selector for the page region containing the user navigation.
31
     */
32
    var UserInfo = function(selector) {
33
        this._regionSelector = selector;
34
        this._region = $(selector);
35
        this._userCache = {};
36
 
37
        $(document).on('user-changed', this._refreshUserInfo.bind(this));
38
    };
39
 
40
    /** @property {String} Selector for the page region containing the user navigation. */
41
    UserInfo.prototype._regionSelector = null;
42
 
43
    /** @property {Array} Cache of user info contexts. */
44
    UserInfo.prototype._userCache = null;
45
 
46
    /** @property {JQuery} JQuery node for the page region containing the user navigation. */
47
    UserInfo.prototype._region = null;
48
 
49
    /** @property {Integer} Remember the last user id to prevent unnecessary reloads. */
50
    UserInfo.prototype._lastUserId = 0;
51
 
52
    /**
53
     * Get the assignment id
54
     *
55
     * @private
56
     * @method _getAssignmentId
57
     * @return {Integer} assignment id
58
     */
59
    UserInfo.prototype._getAssignmentId = function() {
60
        return this._region.attr('data-assignmentid');
61
    };
62
 
63
    /**
64
     * Get the user context - re-render the template in the page.
65
     *
66
     * @private
67
     * @method _refreshUserInfo
68
     * @param {Event} event
69
     * @param {Number} userid
70
     */
71
    UserInfo.prototype._refreshUserInfo = function(event, userid) {
72
        var promise = $.Deferred();
73
 
74
        // Put the current user ID in the DOM so yui can access it.
75
        this._region.attr('data-userid', userid);
76
 
77
        // Skip reloading if it is the same user.
78
        if (this._lastUserId == userid) {
79
            return;
80
        }
81
        this._lastUserId = userid;
82
 
83
        // First insert the loading template.
84
        templates.render('mod_assign/loading', {}).done(function(html, js) {
85
            // Update the page.
86
            this._region.fadeOut("fast", function() {
87
                templates.replaceNodeContents(this._region, html, js);
88
                this._region.fadeIn("fast");
89
            }.bind(this));
90
 
91
            if (userid < 0) {
92
                // Render the template.
93
                templates.render('mod_assign/grading_navigation_no_users', {}).done(function(html, js) {
94
                    if (userid == this._lastUserId) {
95
                        // Update the page.
96
                        this._region.fadeOut("fast", function() {
97
                            templates.replaceNodeContents(this._region, html, js);
98
                            this._region.fadeIn("fast");
99
                        }.bind(this));
100
                    }
101
                }.bind(this)).fail(notification.exception);
102
                return;
103
            }
104
 
105
            if (typeof this._userCache[userid] !== "undefined") {
106
                promise.resolve(this._userCache[userid]);
107
            } else {
108
                // Load context from ajax.
109
                var assignmentId = this._getAssignmentId();
110
                var requests = ajax.call([{
111
                    methodname: 'mod_assign_get_participant',
112
                    args: {
113
                        userid: userid,
114
                        assignid: assignmentId,
115
                        embeduser: true
116
                    }
117
                }]);
118
 
119
                requests[0].done(function(participant) {
120
                    if (!participant.hasOwnProperty('id')) {
121
                        promise.reject('No users');
122
                    } else {
123
                        this._userCache[userid] = participant;
124
                        promise.resolve(this._userCache[userid]);
125
                    }
126
                }.bind(this)).fail(notification.exception);
127
            }
128
 
129
            promise.done(function(context) {
130
                var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(','),
131
                    identity = [];
132
                // Render the template.
133
                context.courseid = $('[data-region="grading-navigation-panel"]').attr('data-courseid');
134
 
135
                if (context.user) {
136
                    // Build a string for the visible identity fields listed in showuseridentity config setting.
137
                    $.each(identityfields, function(i, k) {
138
                        if (typeof context.user[k] !== 'undefined' && context.user[k] !== '') {
139
                            context.hasidentity = true;
140
                            identity.push(context.user[k]);
141
                        }
142
                    });
143
                    context.identity = identity.join(', ');
144
 
145
                    // Add profile image url to context.
146
                    if (context.user.profileimageurl) {
147
                        context.profileimageurl = context.user.profileimageurl;
148
                    }
149
                }
150
 
151
                templates.render('mod_assign/grading_navigation_user_summary', context).done(function(html, js) {
152
                    // Update the page.
153
                    if (userid == this._lastUserId) {
154
                        this._region.fadeOut("fast", function() {
155
                            templates.replaceNodeContents(this._region, html, js);
156
                            this._region.fadeIn("fast");
157
                        }.bind(this));
158
                    }
159
                }.bind(this)).fail(notification.exception);
160
            }.bind(this)).fail(function() {
161
                // Render the template.
162
                templates.render('mod_assign/grading_navigation_no_users', {}).done(function(html, js) {
163
                    // Update the page.
164
                    this._region.fadeOut("fast", function() {
165
                        templates.replaceNodeContents(this._region, html, js);
166
                        this._region.fadeIn("fast");
167
                    }.bind(this));
168
                }.bind(this)).fail(notification.exception);
169
            }
170
            .bind(this));
171
        }.bind(this)).fail(notification.exception);
172
    };
173
 
174
    return UserInfo;
175
});