]> git.mjollnir.org Git - moodle.git/commitdiff
role sortorder column now has unique index; new function get_all_roles() that returns...
authorskodak <skodak>
Sat, 23 Sep 2006 12:51:00 +0000 (12:51 +0000)
committerskodak <skodak>
Sat, 23 Sep 2006 12:51:00 +0000 (12:51 +0000)
admin/roles/allowassign.php
admin/roles/allowoverride.php
admin/roles/manage.php
course/groups-edit.html
lib/accesslib.php
lib/db/install.xml
lib/db/mysql.php
lib/db/postgres7.php
version.php

index a2c046c286edbe4995f831e844bbc4d8596586ac..61626faa94f0bbf48f996fd142de42ceedfe8568 100755 (executable)
@@ -22,7 +22,7 @@
 
 /// get all roles
 
-    $roles = get_records('role');
+    $roles = get_all_roles();
 
     if ($grant = data_submitted()) {
 
index b76f48e24c063f8cbe5b326f5c8ed0571a633c9f..3db7cb9ccc38e73d3d98fa97bbe3ffb037c9ea18 100755 (executable)
@@ -20,7 +20,7 @@
 
 /// get all roles
 
-    $roles = get_records('role');
+    $roles = get_all_roles();
 
     if ($grant = data_submitted()) {
 
index 9e017cd003a6d6967968a268a614ba634a166452..a1a4f0a3ad3a6666ef5752f504d157223e08d537 100755 (executable)
@@ -24,7 +24,7 @@
     $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;
+}
+
 ?>
index 9c0f3bef324df49bde9cb0fc219759edd8657d71..05620d4443305b70bc803a8125ee1af54d701c09 100755 (executable)
@@ -1,7 +1,7 @@
 <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;
index a459e1aa4dd96ee1552ac9081019d969bfaf52ac..c11b85bb6711a51df34cab0a8f7106481e153118 100755 (executable)
@@ -953,7 +953,7 @@ function moodle_install_roles() {
         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;
@@ -1277,7 +1277,7 @@ function delete_role($roleid) {
 
 // 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;
     }
 
@@ -2402,6 +2402,13 @@ function user_can_assign($context, $targetroleid) {
     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
@@ -2436,7 +2443,7 @@ function get_user_roles($context, $userid=0, $checkparentcontexts=true) {
                            '   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');
 }
 
 /**
@@ -2446,6 +2453,7 @@ function get_user_roles($context, $userid=0, $checkparentcontexts=true) {
  * @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);
@@ -2473,7 +2481,7 @@ function get_assignable_roles ($context) {
 
     $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));
@@ -2492,7 +2500,7 @@ function get_overridable_roles ($context) {
 
     $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));
@@ -2774,9 +2782,9 @@ function get_capabilities_from_role_on_context($role, $context) {
 
 // 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
@@ -2798,4 +2806,4 @@ function get_users_from_role_on_context($role, $context) {
                                   AND roleid = $role->id");
 }
 
-?>
+?>
\ No newline at end of file
index b6c4283f88cd88b9c3e49c6b699fba26f4fe8e39..3421465dedd2af0343056541e98e987c7ef4ef0c 100644 (file)
@@ -1,5 +1,5 @@
 <?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
index 27fa0cb9e470e98f62e77297e701c1151733a94b..b71aff24fca020c3f539b21b2c6beb2738c0d86c 100644 (file)
@@ -2296,6 +2296,24 @@ function main_upgrade($oldversion=0) {
         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;
 }
index 9fe8117a49be7eb1567d7f122be37e66bba0da69..4f71b2e56104beaff2f3d9ae43304fccffd4de30 100644 (file)
@@ -1890,6 +1890,23 @@ function main_upgrade($oldversion=0) {
         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;
 }
index fdcf404e7cc4ffbe2d9e1ef10787c6f9b4476d05..98b417565b3c341302700a5c66ab1db6bcf9a00c 100644 (file)
@@ -6,7 +6,7 @@
 // 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