diff --git a/enrol/ajax.php b/enrol/ajax.php index 2d41a9892c1c9..4134c397280b4 100644 --- a/enrol/ajax.php +++ b/enrol/ajax.php @@ -88,7 +88,7 @@ break; case 'getassignable': $otheruserroles = optional_param('otherusers', false, PARAM_BOOL); - $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true); + $outcome->response = $manager->get_assignable_roles_for_json($otheruserroles); break; case 'searchotherusers': $search = optional_param('search', '', PARAM_RAW); diff --git a/enrol/locallib.php b/enrol/locallib.php index 2d918da0e5419..4699b3ad34c4a 100644 --- a/enrol/locallib.php +++ b/enrol/locallib.php @@ -621,6 +621,22 @@ public function get_assignable_roles($otherusers = false) { } } + /** + * Gets all of the assignable roles for this course, wrapped in an array to ensure + * role sort order is not lost during json deserialisation. + * + * @param boolean $otherusers whether to include the assignable roles for other users + * @return array + */ + public function get_assignable_roles_for_json($otherusers = false) { + $rolesarray = array(); + $assignable = $this->get_assignable_roles($otherusers); + foreach ($assignable as $id => $role) { + $rolesarray[] = array('id' => $id, 'name' => $role); + } + return $rolesarray; + } + /** * Gets all of the groups for this course. * diff --git a/enrol/manual/yui/quickenrolment/quickenrolment.js b/enrol/manual/yui/quickenrolment/quickenrolment.js index 5aeca3202d0f1..012cafb0415c0 100644 --- a/enrol/manual/yui/quickenrolment/quickenrolment.js +++ b/enrol/manual/yui/quickenrolment/quickenrolment.js @@ -198,8 +198,8 @@ YUI.add('moodle-enrol_manual-quickenrolment', function(Y) { var index = 0, count = 0; for (var i in roles) { count++; - var option = create(''); - if (i == v) { + var option = create(''); + if (roles[i].id == v) { index = count; } s.append(option); diff --git a/enrol/yui/otherusersmanager/otherusersmanager.js b/enrol/yui/otherusersmanager/otherusersmanager.js index 89390ee979d77..23d8ba9c0a53c 100644 --- a/enrol/yui/otherusersmanager/otherusersmanager.js +++ b/enrol/yui/otherusersmanager/otherusersmanager.js @@ -314,10 +314,10 @@ YUI.add('moodle-enrol-otherusersmanager', function(Y) { ) .append(Y.Node.create('
'+M.util.get_string('assignrole', 'role')+':
')) ); - var id = 0, roles = this._manager.get(ASSIGNABLEROLES); - for (id in roles) { - var role = Y.Node.create(''+roles[id]+''); - role.on('click', this.assignRoleToUser, this, id, role); + var roles = this._manager.get(ASSIGNABLEROLES); + for (var i in roles) { + var role = Y.Node.create('' + roles[i].name + ''); + role.on('click', this.assignRoleToUser, this, roles[i].id, role); this._node.one('.'+CSS.OPTIONS).append(role); } return this._node; diff --git a/enrol/yui/rolemanager/rolemanager.js b/enrol/yui/rolemanager/rolemanager.js index 8fd9f65541b06..a75d61ae01908 100644 --- a/enrol/yui/rolemanager/rolemanager.js +++ b/enrol/yui/rolemanager/rolemanager.js @@ -94,7 +94,7 @@ YUI.add('moodle-enrol-rolemanager', function(Y) { if (o.error) { new M.core.ajaxException(o); } else { - this.users[userid].addRoleToDisplay(args.roleid, this.get(ASSIGNABLEROLES)[args.roleid]); + this.users[userid].addRoleToDisplay(args.roleid, this._getAssignableRole(args.roleid)); } } catch (e) { new M.core.exception(e); @@ -152,6 +152,15 @@ YUI.add('moodle-enrol-rolemanager', function(Y) { } }); }, + _getAssignableRole: function(roleid) { + var roles = this.get(ASSIGNABLEROLES); + for (var i in roles) { + if (roles[i].id == roleid) { + return roles[i].name; + } + } + return null; + }, _loadAssignableRoles : function() { var c = this.get(COURSEID), params = { id : this.get(COURSEID), @@ -277,7 +286,7 @@ YUI.add('moodle-enrol-rolemanager', function(Y) { var current = this.get(CURRENTROLES); var allroles = true, i = 0; for (i in roles) { - if (!current[i]) { + if (!current[roles[i].id]) { allroles = false; break; } @@ -353,8 +362,10 @@ YUI.add('moodle-enrol-rolemanager', function(Y) { var content = element.one('.content'); var roles = m.get(ASSIGNABLEROLES); for (i in roles) { - var button = Y.Node.create(''); - button.on('click', this.submit, this, i); + var buttonid = 'add_assignable_role_' + roles[i].id; + var buttonhtml = ''; + var button = Y.Node.create(buttonhtml); + button.on('click', this.submit, this, roles[i].id); content.append(button); } Y.one(document.body).append(element);