]> git.mjollnir.org Git - moodle.git/commitdiff
adding new role duplicate funcitionality MDL-7392
authortoyomoyo <toyomoyo>
Thu, 9 Nov 2006 09:29:19 +0000 (09:29 +0000)
committertoyomoyo <toyomoyo>
Thu, 9 Nov 2006 09:29:19 +0000 (09:29 +0000)
admin/roles/manage.html
admin/roles/manage.php

index 94c5aed2ba812f1e18a52f1e57e2c1f37a696e63..c8b583620113d12a574dc2e16c347f2200451eb5 100755 (executable)
         default:
             $submitlabel = get_string('savechanges');
     }
-?>
 
+?>
 
+<div align="right">
+<a href="manage.php?roleid=<?php p($roleid); ?>&amp;action=duplicate"><?php print_string('duplicaterole'); ?></a>
+</div>
 <form name="rolesform" action="manage.php" method="post">
 <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
 <input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
index 87621a6d020821c53588f045bf3003dec3950c54..939ff1c622ff5c10ee5c9c730684a25d5842d178 100755 (executable)
 
             redirect('manage.php');
             break;
-
+        case 'duplicate':
+            // duplicate current role
+            $sourcerole = get_record('role','id',$roleid);
+            
+            $fullname = $sourcerole->name;
+            $shortname = $sourcerole->shortname;
+            $currentfullname = "";
+            $currentshortname = "";
+            $counter = 0;
+            
+            // find a name for the duplicated role  
+            do {
+                if ($counter) {
+                    $suffixfull = " ".get_string("copyasnoun")." ".$counter;
+                    $suffixshort = "_".$counter;
+                } else {
+                    $suffixfull = "";
+                    $suffixshort = "";
+                }
+                $currentfullname = $fullname.$suffixfull;
+                // Limit the size of shortname - database column accepts <= 15 chars
+                $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
+                $coursefull  = get_record("role","name",addslashes($currentfullname));
+                $courseshort = get_record("role","shortname",addslashes($currentshortname));
+                $counter++;
+            } while ($coursefull || $courseshort);
+                
+            $description = 'duplicate of '.$fullname;    
+            if ($newrole = create_role($currentfullname, $currentshortname, $description)) {
+                // dupilcate all the capabilities
+                role_cap_duplicate($sourcerole, $newrole);
+            }         
+            redirect('manage.php');
+            break;
         default:
             break;
-
     }
 
 /// print UI now
@@ -417,4 +449,18 @@ function switch_roles($first, $second) {
     return $status;
 }
 
+// duplicates all the base definitions of a role
+function role_cap_duplicate($sourcerole, $targetrole) {
+    global $CFG;
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
+    $caps = get_records_sql("SELECT * FROM {$CFG->prefix}role_capabilities
+                             WHERE roleid = $sourcerole->id
+                             AND contextid = $systemcontext->id");                                           
+    // adding capabilities
+    foreach ($caps as $cap) {
+        unset($cap->id);
+        $cap->roleid = $targetrole;
+        insert_record('role_capabilities', $cap);
+    }  
+}
 ?>