restore: MDL-17480 Potentially expensive function user_can_create_courses called repeatedly from restore UI.

This commit is contained in:
tjhunt 2008-12-01 06:55:11 +00:00
parent 77f60b432a
commit df73f8d4e1
3 changed files with 20 additions and 47 deletions

View file

@ -218,27 +218,17 @@
// 2-New course: Create the restore object and launch the execute.
//If the user is a teacher and not a creator or we are restoring from within SITEID
if (!user_can_create_courses() || $id == SITEID) {
$cancreatecourses = user_can_create_courses();
if (!$cancreatecourses || $id == SITEID) {
$restore->course_id = $id;
if ($restore->restoreto == 0) {
$restore->deleting = true;
} else {
$restore->deleting = false;
}
}
//If the user is a creator (or admin)
if (user_can_create_courses()) {
//Set restore->deleting as needed
if ($restore->restoreto == 0) {
$restore->deleting = true;
} else {
$restore->deleting = false;
}
}
// Non-cached - get accessinfo
if (isset($USER->access)) {
@ -274,7 +264,7 @@
//Checks everything and execute restore
} else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
//Final access control check
if ($restore->course_id == 0 and !user_can_create_courses()) {
if ($restore->course_id == 0 and !$cancreatecourses) {
print_error("cannotrestoreadminorcreator");
} else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
print_error("cannotrestoreadminoredit");

View file

@ -111,8 +111,9 @@
$restore_blogs = 1;
}
$cancreatecourses = user_can_create_courses();
if (!isset($restore_restoreto)) {
if (!user_can_create_courses()) {
if (!$cancreatecourses) {
$restore_restoreto = 1;
} else {
$restore_restoreto = 2;
@ -231,7 +232,7 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
}
// if user can create any course at all, give the option
if (user_can_create_courses()) {
if ($cancreatecourses) {
$restore_restoreto_options[2] = get_string("newcourse");
}
@ -240,7 +241,7 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
echo "</td></tr>";
if (user_can_create_courses()) { //display these fields conditionally
if ($cancreatecourses) { //display these fields conditionally
// find the list of cates user can edit
echo "<tr valign=\"top\" >";

View file

@ -2255,37 +2255,19 @@ function xmldb_debug($message, $object) {
}
/**
* true or false function to see if user can create any courses at all
* @return bool
* @return boolean Whether the user can create courses in any category in the system.
*/
function user_can_create_courses() {
global $USER;
// if user has course creation capability at any site or course cat, then return true;
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM))) {
return true;
} else {
return (bool) count(get_creatable_categories());
}
}
/**
* Get the list of categories the current user can create courses in
* @return array
*/
function get_creatable_categories() {
global $DB;
$creatablecats = array();
if ($cats = $DB->get_records('course_categories')) {
$catsrs = $DB->get_recordset('course_categories');
foreach ($cats as $cat) {
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
$creatablecats[$cat->id] = $cat->name;
$catsrs->close();
return true;
}
}
}
return $creatablecats;
$catsrs->close();
return false;
}
?>