restore: MDL-17480 Potentially expensive function user_can_create_courses called repeatedly from restore UI.
This commit is contained in:
@@ -214,28 +214,18 @@
|
||||
// 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;
|
||||
}
|
||||
//Set restore->deleting as needed
|
||||
if ($restore->restoreto == 0) {
|
||||
$restore->deleting = true;
|
||||
} else {
|
||||
$restore->deleting = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Non-cached - get accessinfo
|
||||
if (isset($USER->access)) {
|
||||
$accessinfo = $USER->access;
|
||||
@@ -270,7 +260,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) {
|
||||
error("You need to be a creator or admin to restore into new course!");
|
||||
} else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
|
||||
error("You need to be an edit teacher or admin to restore into selected course!");
|
||||
|
||||
@@ -109,8 +109,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;
|
||||
@@ -229,7 +230,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");
|
||||
}
|
||||
|
||||
@@ -238,7 +239,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\" >";
|
||||
|
||||
+2
-14
@@ -2253,27 +2253,15 @@ function user_can_create_courses() {
|
||||
|
||||
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() {
|
||||
|
||||
$creatablecats = array();
|
||||
if ($cats = get_records('course_categories')) {
|
||||
foreach ($cats as $cat) {
|
||||
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
|
||||
$creatablecats[$cat->id] = $cat->name;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $creatablecats;
|
||||
return false;
|
||||
}
|
||||
|
||||
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
|
||||
|
||||
Reference in New Issue
Block a user