mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
restore: MDL-17480 Potentially expensive function user_can_create_courses called repeatedly from restore UI.
This commit is contained in:
parent
77f60b432a
commit
df73f8d4e1
3 changed files with 20 additions and 47 deletions
|
@ -218,28 +218,18 @@
|
||||||
// 2-New course: Create the restore object and launch the execute.
|
// 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 the user is a teacher and not a creator or we are restoring from within SITEID
|
||||||
|
$cancreatecourses = user_can_create_courses();
|
||||||
if (!user_can_create_courses() || $id == SITEID) {
|
if (!$cancreatecourses || $id == SITEID) {
|
||||||
$restore->course_id = $id;
|
$restore->course_id = $id;
|
||||||
if ($restore->restoreto == 0) {
|
|
||||||
$restore->deleting = true;
|
|
||||||
} else {
|
|
||||||
$restore->deleting = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//If the user is a creator (or admin)
|
//Set restore->deleting as needed
|
||||||
if (user_can_create_courses()) {
|
if ($restore->restoreto == 0) {
|
||||||
//Set restore->deleting as needed
|
$restore->deleting = true;
|
||||||
if ($restore->restoreto == 0) {
|
} else {
|
||||||
$restore->deleting = true;
|
$restore->deleting = false;
|
||||||
} else {
|
|
||||||
$restore->deleting = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Non-cached - get accessinfo
|
// Non-cached - get accessinfo
|
||||||
if (isset($USER->access)) {
|
if (isset($USER->access)) {
|
||||||
$accessinfo = $USER->access;
|
$accessinfo = $USER->access;
|
||||||
|
@ -274,7 +264,7 @@
|
||||||
//Checks everything and execute restore
|
//Checks everything and execute restore
|
||||||
} else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
|
} else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
|
||||||
//Final access control check
|
//Final access control check
|
||||||
if ($restore->course_id == 0 and !user_can_create_courses()) {
|
if ($restore->course_id == 0 and !$cancreatecourses) {
|
||||||
print_error("cannotrestoreadminorcreator");
|
print_error("cannotrestoreadminorcreator");
|
||||||
} else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
|
} else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
|
||||||
print_error("cannotrestoreadminoredit");
|
print_error("cannotrestoreadminoredit");
|
||||||
|
|
|
@ -111,8 +111,9 @@
|
||||||
$restore_blogs = 1;
|
$restore_blogs = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cancreatecourses = user_can_create_courses();
|
||||||
if (!isset($restore_restoreto)) {
|
if (!isset($restore_restoreto)) {
|
||||||
if (!user_can_create_courses()) {
|
if (!$cancreatecourses) {
|
||||||
$restore_restoreto = 1;
|
$restore_restoreto = 1;
|
||||||
} else {
|
} else {
|
||||||
$restore_restoreto = 2;
|
$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 any course at all, give the option
|
||||||
if (user_can_create_courses()) {
|
if ($cancreatecourses) {
|
||||||
$restore_restoreto_options[2] = get_string("newcourse");
|
$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, "");
|
choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, "");
|
||||||
echo "</td></tr>";
|
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
|
// find the list of cates user can edit
|
||||||
echo "<tr valign=\"top\" >";
|
echo "<tr valign=\"top\" >";
|
||||||
|
|
|
@ -2255,37 +2255,19 @@ function xmldb_debug($message, $object) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* true or false function to see if user can create any courses at all
|
* @return boolean Whether the user can create courses in any category in the system.
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
function user_can_create_courses() {
|
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;
|
global $DB;
|
||||||
|
$catsrs = $DB->get_recordset('course_categories');
|
||||||
$creatablecats = array();
|
foreach ($cats as $cat) {
|
||||||
if ($cats = $DB->get_records('course_categories')) {
|
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
|
||||||
foreach ($cats as $cat) {
|
$catsrs->close();
|
||||||
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
|
return true;
|
||||||
$creatablecats[$cat->id] = $cat->name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $creatablecats;
|
$catsrs->close();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue