/// get all roles
- $roles = get_records('role');
+ $roles = get_all_roles();
if ($grant = data_submitted()) {
/// get all roles
- $roles = get_records('role');
+ $roles = get_all_roles();
if ($grant = data_submitted()) {
$errors = array();
$newrole = false;
- $roles = get_records('role', '', '', 'sortorder ASC, id ASC');
+ $roles = get_all_roles();
$rolescount = count($roles);
/// fix sort order if needed
$role = $roles[$roleid];
if ($role->sortorder > 0) {
$above = $roles[$rolesort[$role->sortorder - 1]];
- $r = new object();
- $r->id = $role->id;
- $r->sortorder = $above->sortorder;
- if (!update_record('role', $r)) {
- $errors[] = "Can not update role with ID $r->id!";
- }
-
- $r->id = $above->id;
- $r->sortorder = $role->sortorder;
- if (!update_record('role', $r)) {
- $errors[] = "Can not update role with ID $r->id!";
- }
-
- if (count($errors)) {
- $msg = '<p>';
- foreach ($errors as $e) {
- $msg .= $e.'<br />';
- }
- $msg .= '</p>';
- admin_externalpage_print_header($adminroot);
- notify($msg);
- print_continue('manage.php');
- admin_externalpage_print_footer($adminroot);
- die;
+ if (!switch_roles($role, $above)) {
+ error("Cannot move role with ID $roleid");
}
}
}
$role = $roles[$roleid];
if ($role->sortorder + 1 < $rolescount) {
$bellow = $roles[$rolesort[$role->sortorder + 1]];
- $r = new object();
-
- $r->id = $role->id;
- $r->sortorder = $bellow->sortorder;
- if (!update_record('role', $r)) {
- $errors[] = "Can not update role with ID $r->id!";
- }
-
- $r->id = $bellow->id;
- $r->sortorder = $role->sortorder;
- if (!update_record('role', $r)) {
- $errors[] = "Can not update role with ID $r->id!";
- }
- if (count($errors)) {
- $msg = '<p>';
- foreach ($errors as $e) {
- $msg .= $e.'<br />';
- }
- $msg .= '</p>';
- admin_externalpage_print_header($adminroot);
- notify($msg);
- print_continue('manage.php');
- admin_externalpage_print_footer($adminroot);
- die;
+ if (!switch_roles($role, $bellow)) {
+ error("Cannot move role with ID $roleid");
}
}
}
}
admin_externalpage_print_footer($adminroot);
+ die;
+
+
+/// ================ some internal functions ====================////
+
+function switch_roles($first, $second) {
+ $status = true;
+ //first find temorary sortorder number
+ $tempsort = count_records('role') + 3;
+ while (get_record('role','sortorder', $tempsort)) {
+ $tempsort += 3;
+ }
+
+ $r1 = new object();
+ $r1->id = $first->id;
+ $r1->sortorder = $tempsort;
+ $r2 = new object();
+ $r2->id = $second->id;
+ $r2->sortorder = $first->sortorder;
+
+ if (!update_record('role', $r1)) {
+ debugging("Can not update role with ID $r1->id!");
+ $status = false;
+ }
+
+ if (!update_record('role', $r2)) {
+ debugging("Can not update role with ID $r2->id!");
+ $status = false;
+ }
+
+ $r1->sortorder = $second->sortorder;
+ if (!update_record('role', $r1)) {
+ debugging("Can not update role with ID $r1->id!");
+ $status = false;
+ }
+
+ return $status;
+}
+
?>
<script type="text/javascript">
<!-- Begin
<?php
- $roles = get_records('role');
+ $roles = get_all_roles();
$roleoptions = array();
foreach ($roles as $rolex) {
$roleoptions[$rolex->id] = $rolex->name;
if ($userteachers = get_records('user_teachers')) {
foreach ($userteachers as $teacher) {
// populate the user_lastaccess table
- unset($access);
+ $access = new object();
$access->timeaccess = $teacher->timeaccess;
$access->userid = $teacher->userid;
$access->courseid = $teacher->course;
// finally delete the role itself
if ($success and !delete_records('role', 'id', $roleid)) {
- debugging("Could not delete role with ID $roleid!");
+ debugging("Could not delete role record with ID $roleid!");
$success = false;
}
return false;
}
+/** Returns all site roles in correct sort order.
+ *
+ */
+function get_all_roles() {
+ return get_records('role', '', '', 'sortorder ASC');
+}
+
/**
* gets all the user roles assigned in this context, or higher contexts
* this is mainly used when checking if a user can assign a role, or overriding a role
' AND ra.roleid = r.id
AND ra.contextid = c.id
AND '.$contexts.
- ' ORDER BY c.contextlevel DESC');
+ ' ORDER BY c.contextlevel DESC, r.sortorder ASC');
}
/**
* @return int - id or false
*/
function allow_override($sroleid, $troleid) {
+ $record = new object();
$record->roleid = $sroleid;
$record->allowoverride = $troleid;
return insert_record('role_allow_override', $record);
$options = array();
- if ($roles = get_records('role', '', '', 'sortorder ASC')) {
+ if ($roles = get_all_roles()) {
foreach ($roles as $role) {
if (user_can_assign($context, $role->id)) {
$options[$role->id] = strip_tags(format_string($role->name, true));
$options = array();
- if ($roles = get_records('role', '', '', 'sortorder ASC')) {
+ if ($roles = get_all_roles()) {
foreach ($roles as $role) {
if (user_can_override($context, $role->id)) {
$options[$role->id] = strip_tags(format_string($role->name, true));
// find out which roles has assignment on this context
function get_roles_with_assignment_on_context($context) {
-
+
global $CFG;
-
+
return get_records_sql("SELECT DISTINCT r.*
FROM {$CFG->prefix}role_assignments ra,
{$CFG->prefix}role r
AND roleid = $role->id");
}
-?>
+?>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20060922" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20060923" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for role"/>
</KEYS>
<INDEXES>
- <INDEX NAME="sortorder" UNIQUE="false" FIELDS="sortorder"/>
+ <INDEX NAME="sortorder" UNIQUE="true" FIELDS="sortorder"/>
</INDEXES>
</TABLE>
<TABLE NAME="context" COMMENT="one of these must be set" PREVIOUS="role" NEXT="capabilities">
</SENTENCES>
</STATEMENT>
</STATEMENTS>
-</XMLDB>
+</XMLDB>
\ No newline at end of file
table_column('cache_text','formattedtext','formattedtext','longtext','','','','not null');
}
+ if ($oldversion < 2006092302) {
+ // fix sortorder first if needed
+ if ($roles = get_all_roles()) {
+ $i = 0;
+ foreach ($roles as $rolex) {
+ if ($rolex->sortorder != $i) {
+ $r = new object();
+ $r->id = $rolex->id;
+ $r->sortorder = $i;
+ update_record('role', $r);
+ }
+ $i++;
+ }
+ }
+ execute_sql("ALTER TABLE {$CFG->prefix}role DROP INDEX {$CFG->prefix}role_sor_ix;", false);
+ execute_sql("ALTER TABLE {$CFG->prefix}role ADD UNIQUE INDEX {$CFG->prefix}role_sor_uix (sortorder)", false);
+ }
+
return $result;
}
execute_sql("ALTER TABLE `{$CFG->prefix}context` ADD UNIQUE INDEX `contextlevel-instanceid` (`contextlevel`, `instanceid`)",false);
}
+ if ($oldversion < 2006092302) {
+ // fix sortorder first if needed
+ if ($roles = get_all_roles()) {
+ $i = 0;
+ foreach ($roles as $rolex) {
+ if ($rolex->sortorder != $i) {
+ $r = new object();
+ $r->id = $rolex->id;
+ $r->sortorder = $i;
+ update_record('role', $r);
+ }
+ $i++;
+ }
+ }
+ execute_sql("ALTER TABLE {$CFG->prefix}role DROP INDEX {$CFG->prefix}role_sor_ix;");
+ execute_sql("ALTER TABLE {$CFG->prefix}role ADD UNIQUE INDEX {$CFG->prefix}role_sor_uix (sortorder)");
+ }
return $result;
}
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)
- $version = 2006092201; // YYYYMMDD = date
+ $version = 2006092302; // YYYYMMDD = date
// XY = increments within a single day
$release = '1.7 dev'; // Human-friendly version name