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.
This commit is contained in:
sam marshall
2012-05-14 11:15:15 +01:00
parent 1cf121e1d2
commit ae3fbf7b06
4 changed files with 31 additions and 16 deletions
+1 -4
View File
@@ -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 " <span class=\"groupinglabel\">(".format_string($groupings[$mod->groupingid]->name).')</span>';
}
} else {
+20 -11
View File
@@ -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;
global $CFG, $DB, $GROUPLIB_CACHE;
return $DB->get_records_sql("SELECT *
FROM {groupings}
WHERE courseid = ?
ORDER BY name ASC", array($courseid));
// 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.
*
+2 -1
View File
@@ -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
+8
View File
@@ -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.
*