}
if (empty($errors)) {
- $newrole = create_role($name, $shortname, $description, '', $rolescount);
+ $newrole = create_role($name, $shortname, $description);
} else {
$newrole = new object();
$newrole->name = $name;
case 'delete':
if ($confirm and data_submitted() and confirm_sesskey()) {
-
- // first unssign all users
- if (!role_unassign($roleid)) {
- error("Error while unassigning all users from role with ID $roleid!");
- }
-
- if (!delete_records('role', 'id', $roleid)) {
- error("Could not delete role with ID $roleid!");
+ if (!delete_role($roleid)) {
+ error('Could not delete role with ID '.$roleid);
}
} else if (confirm_sesskey()){
* @param legacy - optional legacy capability
* @return id or false
*/
-function create_role($name, $shortname, $description, $legacy='', $sortorder = -1) {
+function create_role($name, $shortname, $description, $legacy='') {
// check for duplicate role name
$role->shortname = $shortname;
$role->description = $description;
- if ($sortorder = -1) {
- $role->sortorder = count_records('role');
- } else {
- $role->sortorder = $sortorder;
+ //find free sortorder number
+ $role->sortorder = count_records('role');
+ while (get_record('role','sortorder', $role->sortorder)) {
+ $role->sortorder += 1;
}
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
}
+/**
+ * function that deletes a role and cleanups up after it
+ * @param roleid - id of role to delete
+ * @return success
+ */
+function delete_role($roleid) {
+ $success = true;
+
+// first unssign all users
+ if (!role_unassign($roleid)) {
+ debugging("Error while unassigning all users from role with ID $roleid!");
+ $success = false;
+ }
+
+// cleanup all references to this role, ignore errors
+ if ($success) {
+ delete_records('role_capabilities', 'roleid', $roleid);
+ delete_records('role_allow_assign', 'roleid', $roleid);
+ delete_records('role_allow_assign', 'allowassign', $roleid);
+ delete_records('role_allow_override', 'roleid', $roleid);
+ delete_records('role_allow_override', 'allowoverride', $roleid);
+ delete_records('role_names', 'roleid', $roleid);
+ }
+
+// finally delete the role itself
+ if ($success and !delete_records('role', 'id', $roleid)) {
+ debugging("Could not delete role with ID $roleid!");
+ $success = false;
+ }
+
+ return $success;
+}
+
/**
* Function to write context specific overrides, or default capabilities.
* @param module - string name