[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/yui/rolemanager/ -> rolemanager.js (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1