adding new role duplicate funcitionality MDL-7392

This commit is contained in:
toyomoyo 2006-11-09 09:29:19 +00:00
parent dae2952a59
commit d471721cd7
2 changed files with 52 additions and 3 deletions

View File

@ -11,9 +11,12 @@
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()) ?>" />

View File

@ -234,10 +234,42 @@
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);
}
}
?>