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. // 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)
if (user_can_create_courses()) {
//Set restore->deleting as needed //Set restore->deleting as needed
if ($restore->restoreto == 0) { if ($restore->restoreto == 0) {
$restore->deleting = true; $restore->deleting = true;
} else { } else {
$restore->deleting = false; $restore->deleting = false;
} }
}
// Non-cached - get accessinfo // Non-cached - get accessinfo
if (isset($USER->access)) { if (isset($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");

View file

@ -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\" >";

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 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();
if ($cats = $DB->get_records('course_categories')) {
foreach ($cats as $cat) { foreach ($cats as $cat) {
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) { if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) {
$creatablecats[$cat->id] = $cat->name; $catsrs->close();
return true;
} }
} }
} $catsrs->close();
return $creatablecats; return false;
} }
?> ?>