Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('moodle-enrol-rolemanager', function(Y) {
2
 
3
    var MOD_NAME                    = 'Moodle role manager',
4
        MOD_USER                    = 'Moodle role user',
5
        MOD_PANEL                   = 'Moodle role assignment panel',
6
        USERIDS                     = 'userIds',
7
        COURSEID                    = 'courseId',
8
        USERID                      = 'userId',
9
        CONTAINER                   = 'container',
10
        CONTAINERID                 = 'containerId',
11
        ASSIGNABLEROLES             = 'assignableRoles',
12
        ASSIGNROLELINK              = 'assignRoleLink',
13
        ASSIGNROLELINKSELECTOR      = 'assignRoleLinkSelector',
14
        UNASSIGNROLELINKS           = 'unassignRoleLinks',
15
        UNASSIGNROLELINKSSELECTOR   = 'unassignRoleLinksSelector',
16
        MANIPULATOR                 = 'manipulator',
17
        CURRENTROLES                = 'currentroles',
18
        OTHERUSERS                  = 'otherusers';
19
 
20
    var ROLE = function(config) {
21
        ROLE.superclass.constructor.apply(this, arguments);
22
    };
23
    ROLE.NAME = MOD_NAME;
24
    ROLE.ATTRS = {
25
        containerId : {
26
            validator: Y.Lang.isString
27
        },
28
        container : {
29
            setter : function(node) {
30
                var n = Y.one(node);
31
                if (!n) {
32
                    Y.fail(MOD_NAME+': invalid container set');
33
                }
34
                return n;
35
            }
36
        },
37
        courseId : {
38
            value: 0,
39
            setter : function(courseId) {
40
                if (!(/^\d+$/.test(courseId))) {
41
                    Y.fail(MOD_NAME+': Invalid course id specified');
42
                }
43
                return courseId;
44
            }
45
        },
46
        userIds : {
47
            validator: Y.Lang.isArray
48
        },
49
        assignableRoles : {
50
            value : []
51
        },
52
        otherusers : {
53
            value : false
54
        }
55
    };
56
    Y.extend(ROLE, Y.Base, {
57
        users : [],
58
        roleAssignmentPanel : null,
59
        rolesLoadedEvent : null,
60
        escCloseEvent  : null,
61
        initializer : function(config) {
62
            var i;
63
            var container = Y.one('#'+this.get(CONTAINERID));
64
            container.addClass('ajaxactive');
65
            this.set(CONTAINER, container);
66
 
67
            var userids = this.get(USERIDS);
68
            for (i in userids) {
69
                this.users[userids[i]] = new ROLEUSER({userId:userids[i],manipulator:this}).wire();
70
            }
71
        },
72
        addRole : function(e, user) {
73
            e.halt();
74
            this.rolesLoadedEvent = this.on('assignablerolesloaded', function(){
75
                this.rolesLoadedEvent.detach();
76
                var panel = this._getRoleAssignmentPanel();
77
                panel.hide();
78
                panel.submitevent = panel.on('submit', this.addRoleCallback, this);
79
                panel.display(user);
80
            }, this);
81
            this._loadAssignableRoles();
82
        },
83
        addRoleCallback : function(e, roleid, userid) {
84
            var panel = this._getRoleAssignmentPanel();
85
            panel.submitevent.detach();
86
            panel.submitevent = null;
87
            Y.io(M.cfg.wwwroot+'/enrol/ajax.php', {
88
                method:'POST',
89
                data:'id='+this.get(COURSEID)+'&action=assign&sesskey='+M.cfg.sesskey+'&roleid='+roleid+'&user='+userid,
90
                on: {
91
                    complete: function(tid, outcome, args) {
92
                        try {
93
                            var o = Y.JSON.parse(outcome.responseText);
94
                            if (o.error) {
95
                                new M.core.ajaxException(o);
96
                            } else {
97
                                this.users[userid].addRoleToDisplay(args.roleid, this._getAssignableRole(args.roleid));
98
                            }
99
                        } catch (e) {
100
                            new M.core.exception(e);
101
                        }
102
                        panel.hide();
103
                    }
104
                },
105
                context:this,
106
                arguments:{
107
                    roleid : roleid
108
                }
109
            });
110
        },
111
        removeRole: function(e, user, roleid) {
112
            e.halt();
113
            require(['core/notification'], function(Notification) {
114
                Notification.saveCancelPromise(
115
                    M.util.get_string('confirmation', 'admin'),
116
                    M.util.get_string('confirmunassign', 'role'),
117
                    M.util.get_string('confirmunassignyes', 'role')
118
                ).then(function() {
119
                    return this.removeRoleCallback(user.get(USERID), roleid);
120
                }.bind(this)).catch(function() {
121
                    // User cancelled.
122
                });
123
            }.bind(this));
124
            this._loadAssignableRoles();
125
        },
126
        removeRoleCallback: function(userid, roleid) {
127
            Y.io(M.cfg.wwwroot+'/enrol/ajax.php', {
128
                method:'POST',
129
                data:'id='+this.get(COURSEID)+'&action=unassign&sesskey='+M.cfg.sesskey+'&role='+roleid+'&user='+userid,
130
                on: {
131
                    complete: function(tid, outcome, args) {
132
                        var o;
133
                        try {
134
                            o = Y.JSON.parse(outcome.responseText);
135
                            if (o.error) {
136
                                new M.core.ajaxException(o);
137
                            } else {
138
                                this.users[userid].removeRoleFromDisplay(args.roleid);
139
                            }
140
                        } catch (e) {
141
                            new M.core.exception(e);
142
                        }
143
                    }
144
                },
145
                context:this,
146
                arguments:{
147
                    roleid : roleid
148
                }
149
            });
150
        },
151
        _getAssignableRole: function(roleid) {
152
            var roles = this.get(ASSIGNABLEROLES);
153
            for (var i in roles) {
154
                if (roles[i].id == roleid) {
155
                    return roles[i].name;
156
                }
157
            }
158
            return null;
159
        },
160
        _loadAssignableRoles : function() {
161
            var c = this.get(COURSEID), params = {
162
                id : this.get(COURSEID),
163
                otherusers : (this.get(OTHERUSERS))?'true':'false',
164
                action : 'getassignable',
165
                sesskey : M.cfg.sesskey
166
            };
167
            Y.io(M.cfg.wwwroot+'/enrol/ajax.php', {
168
                method:'POST',
169
                data:build_querystring(params),
170
                on: {
171
                    complete: function(tid, outcome, args) {
172
                        try {
173
                            var roles = Y.JSON.parse(outcome.responseText);
174
                            this.set(ASSIGNABLEROLES, roles.response);
175
                        } catch (e) {
176
                            new M.core.exception(e);
177
                        }
178
                        this._loadAssignableRoles = function() {
179
                            this.fire('assignablerolesloaded');
180
                        };
181
                        this._loadAssignableRoles();
182
                    }
183
                },
184
                context:this
185
            });
186
        },
187
        _getRoleAssignmentPanel : function() {
188
            if (this.roleAssignmentPanel === null) {
189
                this.roleAssignmentPanel = new ROLEPANEL({manipulator:this});
190
            }
191
            return this.roleAssignmentPanel;
192
        }
193
    });
194
    Y.augment(ROLE, Y.EventTarget);
195
 
196
    var ROLEUSER = function(config) {
197
        ROLEUSER.superclass.constructor.apply(this, arguments);
198
    };
199
    ROLEUSER.NAME = MOD_USER;
200
    ROLEUSER.ATTRS = {
201
        userId  : {
202
            validator: Y.Lang.isNumber
203
        },
204
        manipulator : {
205
            validator: Y.Lang.isObject
206
        },
207
        container : {
208
            setter : function(node) {
209
                var n = Y.one(node);
210
                if (!n) {
211
                    Y.fail(MOD_USER+': invalid container set '+node);
212
                }
213
                return n;
214
            }
215
        },
216
        assignableroles : {
217
            value : []
218
        },
219
        currentroles : {
220
            value : [],
221
            validator: Y.Lang.isArray
222
        },
223
        assignRoleLink : {
224
            setter : function(node) {
225
                if (node===false) {
226
                    return node;
227
                }
228
                var n = Y.one(node);
229
                if (!n) {
230
                    Y.fail(MOD_NAME+': invalid assign role link given '+node);
231
                }
232
                return n;
233
            },
234
            value : false
235
        },
236
        assignRoleLinkSelector : {
237
            value : '.assignrolelink',
238
            validator : Y.Lang.isString
239
        },
240
        unassignRoleLinks : {
241
        },
242
        unassignRoleLinksSelector : {
243
            value : '.unassignrolelink',
244
            validator : Y.Lang.isString
245
        }
246
    };
247
    Y.extend(ROLEUSER, Y.Base, {
248
        initializer : function() {
249
            var container = this.get(MANIPULATOR).get(CONTAINER).one('#user_'+this.get(USERID));
250
            this.set(CONTAINER,        container);
251
            var assignrole = container.one(this.get(ASSIGNROLELINKSELECTOR));
252
            if (assignrole) {
253
                this.set(ASSIGNROLELINK, assignrole.ancestor());
254
            }
255
            this.set(UNASSIGNROLELINKS , container.all(this.get(UNASSIGNROLELINKSSELECTOR)));
256
        },
257
        wire : function() {
258
            var container = this.get(MANIPULATOR).get(CONTAINER).one('#user_'+this.get(USERID));
259
            var arl = this.get(ASSIGNROLELINK);
260
            var uarls = this.get(UNASSIGNROLELINKS);
261
            var m = this.get(MANIPULATOR);
262
            if (arl) {
263
                arl.ancestor().on('click', m.addRole, m, this);
264
            }
265
            var currentroles = [];
266
            if (uarls.size() > 0) {
267
                uarls.each(function(link){
268
                    link.roleId = link.getAttribute('rel');
269
                    link.on('click', m.removeRole, m, this, link.roleId);
270
                    currentroles[link.roleId] = true;
271
                }, this);
272
            }
273
            container.all('.role.unchangeable').each(function(node){
274
                currentroles[node.getAttribute('rel')] = true;
275
            }, this);
276
 
277
            this.set(CURRENTROLES, currentroles);
278
            return this;
279
        },
280
        _checkIfHasAllRoles : function() {
281
            var roles = this.get(MANIPULATOR).get(ASSIGNABLEROLES);
282
            var current = this.get(CURRENTROLES);
283
            var allroles = true, i = 0;
284
            for (i in roles) {
285
                if (!current[roles[i].id]) {
286
                    allroles = false;
287
                    break;
288
                }
289
            }
290
            var link = this.get(ASSIGNROLELINK);
291
            if (allroles) {
292
                this.get(CONTAINER).addClass('hasAllRoles');
293
            } else {
294
                this.get(CONTAINER).removeClass('hasAllRoles');
295
            }
296
        },
297
        addRoleToDisplay : function(roleId, roleTitle) {
298
            var m = this.get(MANIPULATOR);
299
            var container = this.get(CONTAINER);
300
            window.require(['core/templates'], function(Templates) {
301
                Templates.renderPix('t/delete', 'core').then(function(pix) {
302
                    var role = Y.Node.create('<div class="role role_' + roleId + '">' +
303
                                             roleTitle +
304
                                             '<a class="unassignrolelink">' + pix + '</a></div>');
305
                    var link = role.one('.unassignrolelink');
306
                    link.roleId = roleId;
307
                    link.on('click', m.removeRole, m, this, link.roleId);
308
                    container.one('.col_role .roles').append(role);
309
                    this._toggleCurrentRole(link.roleId, true);
310
                }.bind(this));
311
            }.bind(this));
312
        },
313
        removeRoleFromDisplay : function(roleId) {
314
            var container = this.get(CONTAINER);
315
            container.all('.role_'+roleId).remove();
316
            this._toggleCurrentRole(roleId, false);
317
        },
318
        _toggleCurrentRole : function(roleId, hasRole) {
319
            var roles = this.get(CURRENTROLES);
320
            if (hasRole) {
321
                roles[roleId] = true;
322
            } else {
323
                roles[roleId] = false;
324
            }
325
            this.set(CURRENTROLES, roles);
326
            this._checkIfHasAllRoles();
327
        }
328
    });
329
 
330
    var ROLEPANEL = function(config) {
331
        ROLEPANEL.superclass.constructor.apply(this, arguments);
332
    };
333
    ROLEPANEL.NAME = MOD_PANEL;
334
    ROLEPANEL.ATTRS = {
335
        elementNode : {
336
            setter : function(node) {
337
                var n = Y.one(node);
338
                if (!n) {
339
                    Y.fail(MOD_PANEL+': Invalid element node');
340
                }
341
                return n;
342
            }
343
        },
344
        contentNode : {
345
            setter : function(node) {
346
                var n = Y.one(node);
347
                if (!n) {
348
                    Y.fail(MOD_PANEL+': Invalid content node');
349
                }
350
                return n;
351
            }
352
        },
353
        manipulator : {
354
            validator: Y.Lang.isObject
355
        }
356
    };
357
    Y.extend(ROLEPANEL, Y.Base, {
358
        user : null,
359
        roles : [],
360
        submitevent : null,
361
        initializer : function() {
362
            var i, m = this.get(MANIPULATOR);
363
            var element = Y.Node.create('<div class="popover popover-bottom"><div class="arrow"></div>' +
364
                                        '<div class="header popover-title">' +
365
                                        '<div role="button" class="close" aria-label="' +
366
                                        M.util.get_string('closebuttontitle', 'moodle') + '">' +
367
                                        '<span aria-hidden="true">&times;</span></div>' +
368
                                        '<h3>'+M.util.get_string('assignroles', 'role')+'</h3>' +
369
                                        '</div><div class="content popover-content' +
370
                                        ' d-flex flex-wrap align-items-center mb-3"></div></div>');
371
            var content = element.one('.content');
372
            var roles = m.get(ASSIGNABLEROLES);
373
            for (i in roles) {
374
                var buttonid = 'add_assignable_role_' + roles[i].id;
375
                var buttonhtml = '<input type="button" class="btn btn-secondary mr-1" value="' +
376
                                 roles[i].name + '" id="' + buttonid + '" />';
377
                var button = Y.Node.create(buttonhtml);
378
                button.on('click', this.submit, this, roles[i].id);
379
                content.append(button);
380
            }
381
            Y.one(document.body).append(element);
382
            this.set('elementNode', element);
383
            this.set('contentNode', content);
384
            element.one('.header .close').on('click', this.hide, this);
385
        },
386
        display : function(user) {
387
            var currentroles = user.get(CURRENTROLES), node = null;
388
            for (var i in currentroles) {
389
                if (currentroles[i] === true) {
390
                    if (node = this.get('contentNode').one('#add_assignable_role_'+i)) {
391
                        node.setAttribute('disabled', 'disabled');
392
                    }
393
                    this.roles.push(i);
394
                }
395
            }
396
            this.user = user;
397
            var roles = this.user.get(CONTAINER).one('.col_role .roles');
398
            var x = roles.getX() + 10;
399
            var y = roles.getY() + this.user.get(CONTAINER).get('offsetHeight') - 10;
400
            if ( Y.one(document.body).hasClass('dir-rtl') ) {
401
                this.get('elementNode').setStyle('right', x - 20).setStyle('top', y);
402
            } else {
403
                this.get('elementNode').setStyle('left', x).setStyle('top', y);
404
            }
405
            this.get('elementNode').setStyle('display', 'block');
406
            this.escCloseEvent = Y.on('key', this.hide, document.body, 'down:27', this);
407
            this.displayed = true;
408
        },
409
        hide : function() {
410
            if (this._escCloseEvent) {
411
                this._escCloseEvent.detach();
412
                this._escCloseEvent = null;
413
            }
414
            var node = null;
415
            for (var i in this.roles) {
416
                if (node = this.get('contentNode').one('#add_assignable_role_'+this.roles[i])) {
417
                    node.removeAttribute('disabled');
418
                }
419
            }
420
            this.roles = [];
421
            this.user = null;
422
            this.get('elementNode').setStyle('display', 'none');
423
            if (this.submitevent) {
424
                this.submitevent.detach();
425
                this.submitevent = null;
426
            }
427
            this.displayed = false;
428
            return this;
429
        },
430
        submit : function(e, roleid) {
431
            this.fire('submit', roleid, this.user.get(USERID));
432
        }
433
    });
434
    Y.augment(ROLEPANEL, Y.EventTarget);
435
 
436
    M.enrol = M.enrol || {};
437
    M.enrol.rolemanager = {
438
        instance : null,
439
        init : function(config) {
440
            M.enrol.rolemanager.instance = new ROLE(config);
441
            return M.enrol.rolemanager.instance;
442
        }
443
    }
444
 
445
}, '@VERSION@', {requires:['base','node','io-base','json-parse','test','moodle-core-notification']});