[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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.get(ASSIGNABLEROLES)[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 var event = this.on('assignablerolesloaded', function(){ 114 event.detach(); 115 var confirmation = { 116 modal: true, 117 visible : false, 118 centered : true, 119 title : M.util.get_string('confirmunassigntitle', 'role'), 120 question : M.util.get_string('confirmunassign', 'role'), 121 yesLabel : M.util.get_string('confirmunassignyes', 'role'), 122 noLabel : M.util.get_string('confirmunassignno', 'role') 123 }; 124 new M.core.confirm(confirmation) 125 .show() 126 .on('complete-yes', this.removeRoleCallback, this, user.get(USERID), roleid); 127 }, this); 128 this._loadAssignableRoles(); 129 }, 130 removeRoleCallback : function(e, userid, roleid) { 131 Y.io(M.cfg.wwwroot+'/enrol/ajax.php', { 132 method:'POST', 133 data:'id='+this.get(COURSEID)+'&action=unassign&sesskey='+M.cfg.sesskey+'&role='+roleid+'&user='+userid, 134 on: { 135 complete: function(tid, outcome, args) { 136 var o; 137 try { 138 o = Y.JSON.parse(outcome.responseText); 139 if (o.error) { 140 new M.core.ajaxException(o); 141 } else { 142 this.users[userid].removeRoleFromDisplay(args.roleid); 143 } 144 } catch (e) { 145 new M.core.exception(e); 146 } 147 } 148 }, 149 context:this, 150 arguments:{ 151 roleid : roleid 152 } 153 }); 154 }, 155 _loadAssignableRoles : function() { 156 var c = this.get(COURSEID), params = { 157 id : this.get(COURSEID), 158 otherusers : (this.get(OTHERUSERS))?'true':'false', 159 action : 'getassignable', 160 sesskey : M.cfg.sesskey 161 }; 162 Y.io(M.cfg.wwwroot+'/enrol/ajax.php', { 163 method:'POST', 164 data:build_querystring(params), 165 on: { 166 complete: function(tid, outcome, args) { 167 try { 168 var roles = Y.JSON.parse(outcome.responseText); 169 this.set(ASSIGNABLEROLES, roles.response); 170 } catch (e) { 171 new M.core.exception(e); 172 } 173 this._loadAssignableRoles = function() { 174 this.fire('assignablerolesloaded'); 175 }; 176 this._loadAssignableRoles(); 177 } 178 }, 179 context:this 180 }); 181 }, 182 _getRoleAssignmentPanel : function() { 183 if (this.roleAssignmentPanel === null) { 184 this.roleAssignmentPanel = new ROLEPANEL({manipulator:this}); 185 } 186 return this.roleAssignmentPanel; 187 } 188 }); 189 Y.augment(ROLE, Y.EventTarget); 190 191 var ROLEUSER = function(config) { 192 ROLEUSER.superclass.constructor.apply(this, arguments); 193 }; 194 ROLEUSER.NAME = MOD_USER; 195 ROLEUSER.ATTRS = { 196 userId : { 197 validator: Y.Lang.isNumber 198 }, 199 manipulator : { 200 validator: Y.Lang.isObject 201 }, 202 container : { 203 setter : function(node) { 204 var n = Y.one(node); 205 if (!n) { 206 Y.fail(MOD_USER+': invalid container set '+node); 207 } 208 return n; 209 } 210 }, 211 assignableroles : { 212 value : [] 213 }, 214 currentroles : { 215 value : [], 216 validator: Y.Lang.isArray 217 }, 218 assignRoleLink : { 219 setter : function(node) { 220 if (node===false) { 221 return node; 222 } 223 var n = Y.one(node); 224 if (!n) { 225 Y.fail(MOD_NAME+': invalid assign role link given '+node); 226 } 227 return n; 228 }, 229 value : false 230 }, 231 assignRoleLinkSelector : { 232 value : '.assignrolelink', 233 validator : Y.Lang.isString 234 }, 235 unassignRoleLinks : { 236 }, 237 unassignRoleLinksSelector : { 238 value : '.unassignrolelink', 239 validator : Y.Lang.isString 240 } 241 }; 242 Y.extend(ROLEUSER, Y.Base, { 243 initializer : function() { 244 var container = this.get(MANIPULATOR).get(CONTAINER).one('#user_'+this.get(USERID)); 245 this.set(CONTAINER, container); 246 var assignrole = container.one(this.get(ASSIGNROLELINKSELECTOR)); 247 if (assignrole) { 248 this.set(ASSIGNROLELINK, assignrole.ancestor()); 249 } 250 this.set(UNASSIGNROLELINKS , container.all(this.get(UNASSIGNROLELINKSSELECTOR))); 251 }, 252 wire : function() { 253 var container = this.get(MANIPULATOR).get(CONTAINER).one('#user_'+this.get(USERID)); 254 var arl = this.get(ASSIGNROLELINK); 255 var uarls = this.get(UNASSIGNROLELINKS); 256 var m = this.get(MANIPULATOR); 257 if (arl) { 258 arl.ancestor().on('click', m.addRole, m, this); 259 } 260 var currentroles = []; 261 if (uarls.size() > 0) { 262 uarls.each(function(link){ 263 link.roleId = link.getAttribute('rel'); 264 link.on('click', m.removeRole, m, this, link.roleId); 265 currentroles[link.roleId] = true; 266 }, this); 267 } 268 container.all('.role.unchangeable').each(function(node){ 269 currentroles[node.getAttribute('rel')] = true; 270 }, this); 271 272 this.set(CURRENTROLES, currentroles); 273 return this; 274 }, 275 _checkIfHasAllRoles : function() { 276 var roles = this.get(MANIPULATOR).get(ASSIGNABLEROLES); 277 var current = this.get(CURRENTROLES); 278 var allroles = true, i = 0; 279 for (i in roles) { 280 if (!current[i]) { 281 allroles = false; 282 break; 283 } 284 } 285 var link = this.get(ASSIGNROLELINK); 286 if (allroles) { 287 this.get(CONTAINER).addClass('hasAllRoles'); 288 } else { 289 this.get(CONTAINER).removeClass('hasAllRoles'); 290 } 291 }, 292 addRoleToDisplay : function(roleId, roleTitle) { 293 var m = this.get(MANIPULATOR); 294 var container = this.get(CONTAINER); 295 var role = Y.Node.create('<div class="role role_'+roleId+'">'+roleTitle+'<a class="unassignrolelink"><img src="'+M.util.image_url('t/delete', 'moodle')+'" alt="" /></a></div>'); 296 var link = role.one('.unassignrolelink'); 297 link.roleId = roleId; 298 link.on('click', m.removeRole, m, this, link.roleId); 299 container.one('.col_role .roles').append(role); 300 this._toggleCurrentRole(link.roleId, true); 301 }, 302 removeRoleFromDisplay : function(roleId) { 303 var container = this.get(CONTAINER); 304 container.all('.role_'+roleId).remove(); 305 this._toggleCurrentRole(roleId, false); 306 }, 307 _toggleCurrentRole : function(roleId, hasRole) { 308 var roles = this.get(CURRENTROLES); 309 if (hasRole) { 310 roles[roleId] = true; 311 } else { 312 roles[roleId] = false; 313 } 314 this.set(CURRENTROLES, roles); 315 this._checkIfHasAllRoles(); 316 } 317 }); 318 319 var ROLEPANEL = function(config) { 320 ROLEPANEL.superclass.constructor.apply(this, arguments); 321 }; 322 ROLEPANEL.NAME = MOD_PANEL; 323 ROLEPANEL.ATTRS = { 324 elementNode : { 325 setter : function(node) { 326 var n = Y.one(node); 327 if (!n) { 328 Y.fail(MOD_PANEL+': Invalid element node'); 329 } 330 return n; 331 } 332 }, 333 contentNode : { 334 setter : function(node) { 335 var n = Y.one(node); 336 if (!n) { 337 Y.fail(MOD_PANEL+': Invalid content node'); 338 } 339 return n; 340 } 341 }, 342 manipulator : { 343 validator: Y.Lang.isObject 344 } 345 }; 346 Y.extend(ROLEPANEL, Y.Base, { 347 user : null, 348 roles : [], 349 submitevent : null, 350 initializer : function() { 351 var i, m = this.get(MANIPULATOR); 352 var element = Y.Node.create('<div class="enrolpanel roleassign"><div class="container"><div class="header"><h2>'+M.util.get_string('assignroles', 'role')+'</h2><div class="close"></div></div><div class="content"></div></div></div>'); 353 var content = element.one('.content'); 354 var roles = m.get(ASSIGNABLEROLES); 355 for (i in roles) { 356 var button = Y.Node.create('<input type="button" value="'+roles[i]+'" id="add_assignable_role_'+i+'" />'); 357 button.on('click', this.submit, this, i); 358 content.append(button); 359 } 360 Y.one(document.body).append(element); 361 this.set('elementNode', element); 362 this.set('contentNode', content); 363 element.one('.header .close').on('click', this.hide, this); 364 }, 365 display : function(user) { 366 var currentroles = user.get(CURRENTROLES), node = null; 367 for (var i in currentroles) { 368 if (currentroles[i] === true) { 369 if (node = this.get('contentNode').one('#add_assignable_role_'+i)) { 370 node.setAttribute('disabled', 'disabled'); 371 } 372 this.roles.push(i); 373 } 374 } 375 this.user = user; 376 var roles = this.user.get(CONTAINER).one('.col_role .roles'); 377 var x = roles.getX() + 10; 378 var y = roles.getY() + this.user.get(CONTAINER).get('offsetHeight') - 10; 379 if ( Y.one(document.body).hasClass('dir-rtl') ) { 380 this.get('elementNode').setStyle('right', x - 20).setStyle('top', y); 381 } else { 382 this.get('elementNode').setStyle('left', x).setStyle('top', y); 383 } 384 this.get('elementNode').addClass('visible'); 385 this.escCloseEvent = Y.on('key', this.hide, document.body, 'down:27', this); 386 this.displayed = true; 387 }, 388 hide : function() { 389 if (this._escCloseEvent) { 390 this._escCloseEvent.detach(); 391 this._escCloseEvent = null; 392 } 393 var node = null; 394 for (var i in this.roles) { 395 if (node = this.get('contentNode').one('#add_assignable_role_'+this.roles[i])) { 396 node.removeAttribute('disabled'); 397 } 398 } 399 this.roles = []; 400 this.user = null; 401 this.get('elementNode').removeClass('visible'); 402 if (this.submitevent) { 403 this.submitevent.detach(); 404 this.submitevent = null; 405 } 406 this.displayed = false; 407 return this; 408 }, 409 submit : function(e, roleid) { 410 this.fire('submit', roleid, this.user.get(USERID)); 411 } 412 }); 413 Y.augment(ROLEPANEL, Y.EventTarget); 414 415 M.enrol = M.enrol || {}; 416 M.enrol.rolemanager = { 417 instance : null, 418 init : function(config) { 419 M.enrol.rolemanager.instance = new ROLE(config); 420 return M.enrol.rolemanager.instance; 421 } 422 } 423 424 }, '@VERSION@', {requires:['base','node','io-base','json-parse','test','moodle-core-notification']});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |