Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit ae3fbf7b061b1300695965fdd3a5a44c6dfd3628 1 parent 1cf121e
@sammarshallou sammarshallou authored
View
5 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 " <span class=\"groupinglabel\">(".format_string($groupings[$mod->groupingid]->name).')</span>';
}
} else {
View
33 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.
*
View
3  lib/phpunit/lib.php
@@ -545,7 +545,7 @@ public static function reset_dataroot() {
* @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 @@ public static function reset_all_data($logchanges = false) {
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
View
8 lib/setup.php
@@ -343,6 +343,14 @@
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.
*
* Note: Do NOT use for getting of current page URL or detection of https,
Please sign in to comment.
Something went wrong with that request. Please try again.