Moodle

Enhance bulk user actions to allow assignment of an arbitrary role in an arbitrary context

Details

  • Type: Improvement Improvement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: 1.9.1
  • Fix Version/s: None
  • Component/s: Roles / Access
  • Labels:
    None
  • Affected Branches:
    MOODLE_19_STABLE

Description

It would be very convenient if bulk user actions included "assign a role" as one of the "With selected users..." dropdown. The feature would still be useful even if it could only do assignments in the System context.

For use cases, see these discussions: http://moodle.org/mod/forum/discuss.php?d=100873, http://moodle.org/mod/forum/discuss.php?d=100940 and many others!!

This is one of three separate suggestions for improving our ability to make bulk role assignments. The other two are MDL-15187 and MDL-14298.

Issue Links

Activity

Hide
Chris Fryer added a comment -

I've attached a .tar.gz containing some files that will allow you to assign roles to a user. Presently, role assignment is course-focussed, and you are able to assign multiple users a role in a course. The attched allows you to be user-focussed, and assign a role to a user in multiple contexts (i.e. courses).

It is not part of "bulk user actions". After installation you will find this in Admin -> Users -> Permissions -> Assign roles to user.

Extract the files into your moodle root. Two new files will be placed in /admin/. A third file will appear in your moodle root.

The third file, "users-roles.patch" is a unified diff patch. It creates a menu entry in admin/settings/users.php, and a language string for lang/en_utf8/role.php. So it's ready for internationalization, but is only localized to en_utf8 at present.

We've been using it at LSE since October 2007, and works fine.

Show
Chris Fryer added a comment - I've attached a .tar.gz containing some files that will allow you to assign roles to a user. Presently, role assignment is course-focussed, and you are able to assign multiple users a role in a course. The attched allows you to be user-focussed, and assign a role to a user in multiple contexts (i.e. courses). It is not part of "bulk user actions". After installation you will find this in Admin -> Users -> Permissions -> Assign roles to user. Extract the files into your moodle root. Two new files will be placed in /admin/. A third file will appear in your moodle root. The third file, "users-roles.patch" is a unified diff patch. It creates a menu entry in admin/settings/users.php, and a language string for lang/en_utf8/role.php. So it's ready for internationalization, but is only localized to en_utf8 at present. We've been using it at LSE since October 2007, and works fine.
Hide
John Isner added a comment -

Hi Chris,
It's ot clear from your description if this can be used to assign roles in ARBITRARY contexts (for example User, System, Category) or just in multiple course contexts. If it can be used for role assignment in arbitrary contexts, it will be of great interest to many users, and you might want to announce it in the Roles and Capabilities forum. Your patch is particularly relevant to the people interested in automating assignment of the Parent role to parents in their children's user contexts: http://moodle.org/mod/forum/discuss.php?d=70539

Show
John Isner added a comment - Hi Chris, It's ot clear from your description if this can be used to assign roles in ARBITRARY contexts (for example User, System, Category) or just in multiple course contexts. If it can be used for role assignment in arbitrary contexts, it will be of great interest to many users, and you might want to announce it in the Roles and Capabilities forum. Your patch is particularly relevant to the people interested in automating assignment of the Parent role to parents in their children's user contexts: http://moodle.org/mod/forum/discuss.php?d=70539
Hide
Chris Fryer added a comment -

Sorry. At present the script obtains a list of assignable roles from the SYSTEM context, and populates a drop-down list with those roles. It then obtains two lists of course contexts:

get_my_courses_by_role ($userid, $roleid, 'c.fullname') defined in userassignroleslib.php returns an array of courses in which the user has been assigned the role selected from the drop-down list.

get_not_my_courses_by_role ($userid, $roleid, 'c.fullname') also defined in userasssignroleslib.php returns the array of courses in which the user has not been assigned that role.

At our site, there are no roles which are assignable in the system context that are not also assignable in the course context, but if this is the case at your site, I think you would need to change

$assignableroles = get_assignable_roles($context);

to

$assignableroles = get_assignable_roles( get_context_instance(CONTEXT_COURSE, 2));

This should return the assignable roles from your first (non-site) course, but I haven't tested this. Unfortunately there would be a circularity in attempting to obtain assignable roles from the target context, because the whole point of the script is that you don't know the target context ahead of time.

If you attempt to assign a role in a context that doesn't support it, or some other problem is encountered, an array of errors is populated and you'll be told where it failed, but only after it has attempted to assign the role in all the contexts you specified. In other words, it doesn't just stop at the first error.

I'll have a look at the discussion you provided to see if this code can be adapted to accommodate those requirements, but at present it only supports the course context.

Show
Chris Fryer added a comment - Sorry. At present the script obtains a list of assignable roles from the SYSTEM context, and populates a drop-down list with those roles. It then obtains two lists of course contexts: get_my_courses_by_role ($userid, $roleid, 'c.fullname') defined in userassignroleslib.php returns an array of courses in which the user has been assigned the role selected from the drop-down list. get_not_my_courses_by_role ($userid, $roleid, 'c.fullname') also defined in userasssignroleslib.php returns the array of courses in which the user has not been assigned that role. At our site, there are no roles which are assignable in the system context that are not also assignable in the course context, but if this is the case at your site, I think you would need to change $assignableroles = get_assignable_roles($context); to $assignableroles = get_assignable_roles( get_context_instance(CONTEXT_COURSE, 2)); This should return the assignable roles from your first (non-site) course, but I haven't tested this. Unfortunately there would be a circularity in attempting to obtain assignable roles from the target context, because the whole point of the script is that you don't know the target context ahead of time. If you attempt to assign a role in a context that doesn't support it, or some other problem is encountered, an array of errors is populated and you'll be told where it failed, but only after it has attempted to assign the role in all the contexts you specified. In other words, it doesn't just stop at the first error. I'll have a look at the discussion you provided to see if this code can be adapted to accommodate those requirements, but at present it only supports the course context.
Hide
Chris Fryer added a comment -

Having read the various discussions you suggested, I think I have an idea.

I imagine a screen which asks you to choose the context in which you want to assign a role, e.g. a drop-down list that contains "site", "category", "course", "user" etc. Once you've chosen the context, you are shown a list of the roles assignable in that context level.

However, the key word here is "level", because I think it's possible to create a role that is assignable in a subset of contexts. For example, your "parent" role should only be assigned to student users, but because a "user" is a context and not a role, it would be meaningless to assign a "parent" to a teacher. Maybe that can be avoided by common sense.

Anyway, I'd be grateful if someone else tested this patch to see if it's useful. It might not do everything you want, but it'd be useful feedback to know whether it does what's expected.

Chris

Show
Chris Fryer added a comment - Having read the various discussions you suggested, I think I have an idea. I imagine a screen which asks you to choose the context in which you want to assign a role, e.g. a drop-down list that contains "site", "category", "course", "user" etc. Once you've chosen the context, you are shown a list of the roles assignable in that context level. However, the key word here is "level", because I think it's possible to create a role that is assignable in a subset of contexts. For example, your "parent" role should only be assigned to student users, but because a "user" is a context and not a role, it would be meaningless to assign a "parent" to a teacher. Maybe that can be avoided by common sense. Anyway, I'd be grateful if someone else tested this patch to see if it's useful. It might not do everything you want, but it'd be useful feedback to know whether it does what's expected. Chris
Hide
John Isner added a comment -

Any role should be assignable in any context, even if it doesn't "make sense." Who is to say what permissions are relevant in a given context? There are so many anomalies in the Roles and Capabilities system that trying to second guess it by restricting certain roles to certain contexts would just cause additional frustration. IMO adding contextlevel to capabilities was a mistake. Capabilities should not be bound to contexts, they should be bound to actions. The same action (e.g., editing your profile) can occur in multiple contexts. When capabilities are bound to contexts, you end up making multiple copies (e.g., moodle/user:editownprofile and moodle/user:editprofile). Ugh.

The logic can be drastically simplified. All you need is three parameters from the user:
1) the role shortname. No need to provide a list. Let users type it in.
2) the context type, one of: System, User, Course category, Course, Module, or Block (OK, this could be a dropdown)
3) the context id, an integer.

The context id is easy to determine. Just look at the address bar when you're in the context. For example, if I go to your User context on moodle.org, I can see that your context id is 86884.

http://moodle.org/user/view.php?id=86884

We should assume that whoever uses this system knows what he's doing and will not do anything stupid. On the other hand we should not try to prevent him from doing something stupid.

Show
John Isner added a comment - Any role should be assignable in any context, even if it doesn't "make sense." Who is to say what permissions are relevant in a given context? There are so many anomalies in the Roles and Capabilities system that trying to second guess it by restricting certain roles to certain contexts would just cause additional frustration. IMO adding contextlevel to capabilities was a mistake. Capabilities should not be bound to contexts, they should be bound to actions. The same action (e.g., editing your profile) can occur in multiple contexts. When capabilities are bound to contexts, you end up making multiple copies (e.g., moodle/user:editownprofile and moodle/user:editprofile). Ugh. The logic can be drastically simplified. All you need is three parameters from the user: 1) the role shortname. No need to provide a list. Let users type it in. 2) the context type, one of: System, User, Course category, Course, Module, or Block (OK, this could be a dropdown) 3) the context id, an integer. The context id is easy to determine. Just look at the address bar when you're in the context. For example, if I go to your User context on moodle.org, I can see that your context id is 86884. http://moodle.org/user/view.php?id=86884 We should assume that whoever uses this system knows what he's doing and will not do anything stupid. On the other hand we should not try to prevent him from doing something stupid.
Hide
Artem Andreev added a comment -

I think our improvment (MDL-16793) can facilitate the creation of new actions(no patches, change core code, etc). And peoples will be able to distribute its actions in Modules and plugins database.

We also have a similar solution for assigning roles in courses and adding users to groups (some screenshots here: http://moodle.org/mod/forum/discuss.php?d=106961).

Show
Artem Andreev added a comment - I think our improvment (MDL-16793) can facilitate the creation of new actions(no patches, change core code, etc). And peoples will be able to distribute its actions in Modules and plugins database. We also have a similar solution for assigning roles in courses and adding users to groups (some screenshots here: http://moodle.org/mod/forum/discuss.php?d=106961).

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated: