From: toyomoyo Date: Thu, 20 Sep 2007 09:16:49 +0000 (+0000) Subject: change restore code to use the more efficient get_user_courses_bycap X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=05a71b334168ad2d1be7f9f0903e5a101aa62e2d;p=moodle.git change restore code to use the more efficient get_user_courses_bycap --- diff --git a/backup/restore_check.html b/backup/restore_check.html index a0f3940060..2bf20935d9 100644 --- a/backup/restore_check.html +++ b/backup/restore_check.html @@ -235,11 +235,21 @@ } } + + + // Non-cached - get accessinfo + if (isset($USER->access)) { + $accessinfo = $USER->access; + } else { + $accessinfo = get_user_access_sitewide($USER->id); + } + $mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true, 'c.sortorder ASC', array('id', 'fullname', 'shortname', 'visible')); + //Now, select the course if needed //if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and get_capability_courses('moodle/site:restore')) { - if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0)) { + if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) && count($mycourses) > 1) { - if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) { + if ($courses = $mycourses) { print_heading(get_string("choosecourse")); print_simple_box_start("center"); foreach ($courses as $course) { diff --git a/backup/restore_form.html b/backup/restore_form.html index 92b1d963fe..83b5f14cf0 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -193,14 +193,20 @@ function selectItemInCheckboxByName(formId, checkName, checked ) { * if user has course:create in any category, we show * newcourse */ - - //$mycourses = get_capability_courses('moodle/site:restore'); - + + // Non-cached - get accessinfo + if (isset($USER->access)) { + $accessinfo = $USER->access; + } else { + $accessinfo = get_user_access_sitewide($USER->id); + } + + $mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true); // if the user can manage 2 or more courses, // or if the only course the user can manage is not the current course // we show options for existing courses - if (user_can_create_courses()) { // this is not entirely accurate but much faster - //if (count($mycourses) > 1 || !in_array($id, $mycourses)) { + + if (count($mycourses) > 1) { // if user can manage more than 1 course, or if user can restore to a single different course $restore_restoreto_options[0] = get_string("existingcoursedeleting"); $restore_restoreto_options[1] = get_string("existingcourseadding"); diff --git a/lib/datalib.php b/lib/datalib.php index 7f13fa084e..7f176fd23e 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -2134,30 +2134,6 @@ function xmldb_debug($message, $object) { debugging($message, DEBUG_DEVELOPER); } -/** - * Get the lists of courses the current user has $cap capability in - * I am not sure if this is needed, it loops through all courses so - * could cause performance problems. - * If it's not used, we can use a faster function to detect - * capability in restorelib.php - * @param string $cap - * @return array - */ -function get_capability_courses($cap) { - global $USER; - - $mycourses = array(); - if ($courses = get_records('course')) { - foreach ($courses as $course) { - if (has_capability($cap, get_context_instance(CONTEXT_COURSE, $course->id))) { - $mycourses[] = $course->id; - } - } - } - - return $mycourses; -} - /** * true or false function to see if user can create any courses at all * @return bool