From ae3fbf7b061b1300695965fdd3a5a44c6dfd3628 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 2 Apr 2012 12:08:30 +0100 Subject: [PATCH] MDL-24419 (2): Moved groupings cache to get_all_groupings function There was a static cache inside course/lib.php. I need to access this information in other places, so to avoid making two queries, I am moving the cache into the groups_get_all_groupings function instead. --- course/lib.php | 5 +---- lib/grouplib.php | 33 +++++++++++++++++++++------------ lib/phpunit/lib.php | 3 ++- lib/setup.php | 8 ++++++++ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/course/lib.php b/course/lib.php index 262582f4c5c..6f94bcd015b 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1384,7 +1384,6 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, static $strmovehere; static $strmovefull; static $strunreadpostsone; - static $groupings; static $modulenames; if (!isset($initialised)) { @@ -1586,9 +1585,7 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, } if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) { - if (!isset($groupings)) { - $groupings = groups_get_all_groupings($course->id); - } + $groupings = groups_get_all_groupings($course->id); echo " (".format_string($groupings[$mod->groupingid]->name).')'; } } else { diff --git a/lib/grouplib.php b/lib/grouplib.php index 82fa4d3a904..c929497ba69 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -229,24 +229,33 @@ function groups_get_user_groups($courseid, $userid=0) { } /** - * Gets an array of all groupings in a specified course. + * Gets an array of all groupings in a specified course. This value is cached + * for a single course (so you can call it repeatedly for the same course + * without a performance penalty). * * @category group - * @param int $courseid return only groupings in this with this courseid - * @return array|bool Returns an array of the grouping objects or false if no records - * or an error occurred. + * @param int $courseid return all groupings from course with this courseid + * @return array Returns an array of the grouping objects (empty if none) */ function groups_get_all_groupings($courseid) { - global $CFG, $DB; - - return $DB->get_records_sql("SELECT * - FROM {groupings} - WHERE courseid = ? - ORDER BY name ASC", array($courseid)); + global $CFG, $DB, $GROUPLIB_CACHE; + + // Use cached data if available. (Note: We only cache a single request, so + // as not to waste memory if processing is happening for multiple courses.) + if (!empty($GROUPLIB_CACHE->groupings) && + $GROUPLIB_CACHE->groupings->courseid == $courseid) { + return $GROUPLIB_CACHE->groupings->result; + } + if (empty($GROUPLIB_CACHE)) { + $GROUPLIB_CACHE = new stdClass(); + } + $GROUPLIB_CACHE->groupings = new stdClass(); + $GROUPLIB_CACHE->groupings->courseid = $courseid; + $GROUPLIB_CACHE->groupings->result = + $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC'); + return $GROUPLIB_CACHE->groupings->result; } - - /** * Determines if the user is a member of the given group. * diff --git a/lib/phpunit/lib.php b/lib/phpunit/lib.php index ce05ae8a1c9..df39b6f8e57 100644 --- a/lib/phpunit/lib.php +++ b/lib/phpunit/lib.php @@ -545,7 +545,7 @@ class phpunit_util { * @return void */ public static function reset_all_data($logchanges = false) { - global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION; + global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $GROUPLIB_CACHE; // reset global $DB in case somebody mocked it $DB = self::get_global_backup('DB'); @@ -615,6 +615,7 @@ class phpunit_util { get_string_manager()->reset_caches(); events_get_handlers('reset'); textlib::reset_caches(); + $GROUPLIB_CACHE = null; //TODO: add more resets here and probably refactor them to new core function // purge dataroot directory diff --git a/lib/setup.php b/lib/setup.php index 15da642e663..1a635f3ec15 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -342,6 +342,14 @@ global $OUTPUT; */ global $MCACHE; +/** + * Cache used within grouplib to cache data within current request only. + * + * @global object $GROUPLLIB_CACHE + * @name $GROUPLIB_CACHE + */ +global $GROUPLIB_CACHE; + /** * Full script path including all params, slash arguments, scheme and host. * -- 2.43.0