]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19119 moving get_fast_modinfo() to moodlelib.php - the recent changes related...
authorskodak <skodak>
Sun, 17 May 2009 18:45:00 +0000 (18:45 +0000)
committerskodak <skodak>
Sun, 17 May 2009 18:45:00 +0000 (18:45 +0000)
course/lib.php
lib/datalib.php
lib/moodlelib.php

index b74917544fd3c55a56813b465243bda22a1d0ef9..f105aa82ebb814b8f15a2425220bf77eb9f1359d 100644 (file)
@@ -1084,187 +1084,6 @@ function get_array_of_activities($courseid) {
 }
 
 
-/**
- * Returns reference to full info about modules in course (including visibility).
- * Cached and as fast as possible (0 or 1 db query).
- * @param $course object or 'reset' string to reset caches, modinfo may be updated in db
- * @return mixed courseinfo object or nothing if resetting
- */
-function &get_fast_modinfo(&$course, $userid=0) {
-    global $CFG, $USER, $DB;
-    if (!empty($CFG->enableavailability)) {
-        require_once($CFG->libdir.'/conditionlib.php');
-    }
-
-    static $cache = array();
-
-    if ($course === 'reset') {
-        $cache = array();
-        $nothing = null;
-        return $nothing; // we must return some reference
-    }
-
-    if (empty($userid)) {
-        $userid = $USER->id;
-    }
-
-    if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) {
-        return $cache[$course->id];
-    }
-
-    if (empty($course->modinfo)) {
-        // no modinfo yet - load it
-        rebuild_course_cache($course->id);
-        $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
-    }
-
-    $modinfo = new object();
-    $modinfo->courseid  = $course->id;
-    $modinfo->userid    = $userid;
-    $modinfo->sections  = array();
-    $modinfo->cms       = array();
-    $modinfo->instances = array();
-    $modinfo->groups    = null; // loaded only when really needed - the only one db query
-
-    $info = unserialize($course->modinfo);
-    if (!is_array($info)) {
-        // hmm, something is wrong - lets try to fix it
-        rebuild_course_cache($course->id);
-        $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
-        $info = unserialize($course->modinfo);
-        if (!is_array($info)) {
-            return $modinfo;
-        }
-    }
-
-    if ($info) {
-        // detect if upgrade required
-        $first = reset($info);
-        if (!isset($first->id)) {
-            rebuild_course_cache($course->id);
-            $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
-            $info = unserialize($course->modinfo);
-            if (!is_array($info)) {
-                return $modinfo;
-            }
-        }
-    }
-
-    $modlurals = array();
-
-    // If we haven't already preloaded contexts for the course, do it now
-    preload_course_contexts($course->id);
-
-    foreach ($info as $mod) {
-        if (empty($mod->name)) {
-            // something is wrong here
-            continue;
-        }
-        // reconstruct minimalistic $cm
-        $cm = new object();
-        $cm->id               = $mod->cm;
-        $cm->instance         = $mod->id;
-        $cm->course           = $course->id;
-        $cm->modname          = $mod->mod;
-        $cm->name             = urldecode($mod->name);
-        $cm->visible          = $mod->visible;
-        $cm->sectionnum       = $mod->section;
-        $cm->groupmode        = $mod->groupmode;
-        $cm->groupingid       = $mod->groupingid;
-        $cm->groupmembersonly = $mod->groupmembersonly;
-        $cm->indent           = $mod->indent;
-        $cm->completion       = $mod->completion;
-        $cm->extra            = isset($mod->extra) ? urldecode($mod->extra) : '';
-        $cm->icon             = isset($mod->icon) ? $mod->icon : '';
-        $cm->uservisible      = true;
-        if(!empty($CFG->enableavailability)) {
-            // We must have completion information from modinfo. If it's not
-            // there, cache needs rebuilding
-            if(!isset($mod->availablefrom)) {
-                debugging('enableavailability option was changed; rebuilding '.
-                    'cache for course '.$course->id);
-                rebuild_course_cache($course->id,true);
-                // Re-enter this routine to do it all properly
-                return get_fast_modinfo($course,$userid);
-            }
-            $cm->availablefrom    = $mod->availablefrom;
-            $cm->availableuntil   = $mod->availableuntil;
-            $cm->showavailability = $mod->showavailability;
-            $cm->conditionscompletion = $mod->conditionscompletion;
-            $cm->conditionsgrade  = $mod->conditionsgrade;
-        }
-
-        // preload long names plurals and also check module is installed properly
-        if (!isset($modlurals[$cm->modname])) {
-            if (!file_exists("$CFG->dirroot/mod/$cm->modname/lib.php")) {
-                continue;
-            }
-            $modlurals[$cm->modname] = get_string('modulenameplural', $cm->modname);
-        }
-        $cm->modplural = $modlurals[$cm->modname];
-        $modcontext = get_context_instance(CONTEXT_MODULE,$cm->id);
-        
-        if(!empty($CFG->enableavailability)) {
-            // Unfortunately the next call really wants to call 
-            // get_fast_modinfo, but that would be recursive, so we fake up a 
-            // modinfo for it already
-            if(empty($minimalmodinfo)) {
-                $minimalmodinfo=new stdClass();
-                $minimalmodinfo->cms=array();
-                foreach($info as $mod) {
-                    $minimalcm=new stdClass();
-                    $minimalcm->id=$mod->cm;
-                    $minimalcm->name=urldecode($mod->name);
-                    $minimalmodinfo->cms[$minimalcm->id]=$minimalcm;
-                }
-            }
-
-            // Get availability information
-            $ci = new condition_info($cm);
-            $cm->available=$ci->is_available($cm->availableinfo,true,$userid,
-                $minimalmodinfo);
-        } else {
-            $cm->available=true;
-        }
-        if ((!$cm->visible or !$cm->available) and !has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) {
-            $cm->uservisible = false;
-
-        } else if (!empty($CFG->enablegroupings) and !empty($cm->groupmembersonly)
-                and !has_capability('moodle/site:accessallgroups', $modcontext, $userid)) {
-            if (is_null($modinfo->groups)) {
-                $modinfo->groups = groups_get_user_groups($course->id, $userid);
-            }
-            if (empty($modinfo->groups[$cm->groupingid])) {
-                $cm->uservisible = false;
-            }
-        }
-
-        if (!isset($modinfo->instances[$cm->modname])) {
-            $modinfo->instances[$cm->modname] = array();
-        }
-        $modinfo->instances[$cm->modname][$cm->instance] =& $cm;
-        $modinfo->cms[$cm->id] =& $cm;
-
-        // reconstruct sections
-        if (!isset($modinfo->sections[$cm->sectionnum])) {
-            $modinfo->sections[$cm->sectionnum] = array();
-        }
-        $modinfo->sections[$cm->sectionnum][] = $cm->id;
-
-        unset($cm);
-    }
-
-    unset($cache[$course->id]); // prevent potential reference problems when switching users
-    $cache[$course->id] = $modinfo;
-
-    // Ensure cache does not use too much RAM
-    if (count($cache) > MAX_MODINFO_CACHE_SIZE) {
-        array_shift($cache);
-    }
-
-    return $cache[$course->id];
-}
-
 /**
  * Returns a number of useful structures for course displays
  */
index dcc863a7a9af17f167ddfc9c7b0d94b883d859ef..d37bffd1da65515eafb4186ce2e4e6b8757d1728 100644 (file)
@@ -1748,8 +1748,6 @@ function get_all_instances_in_courses($modulename, $courses, $userid=NULL, $incl
         return $outputarray;
     }
 
-    require_once($CFG->dirroot.'/course/lib.php');
-
     foreach ($courses as $course) {
         $modinfo = get_fast_modinfo($course, $userid);
 
index 16e153c5be001c84489474d10162b88891c07890..1358e41540133844c86a3a1046814513a267e59a 100644 (file)
@@ -2456,6 +2456,189 @@ function sync_metacourses() {
     }
 }
 
+/**
+ * Returns reference to full info about modules in course (including visibility).
+ * Cached and as fast as possible (0 or 1 db query).
+ * @param $course object or 'reset' string to reset caches, modinfo may be updated in db
+ * @return mixed courseinfo object or nothing if resetting
+ */
+function &get_fast_modinfo(&$course, $userid=0) {
+    global $CFG, $USER, $DB;
+    require_once($CFG->dirroot.'/course/lib.php');
+
+    if (!empty($CFG->enableavailability)) {
+        require_once($CFG->libdir.'/conditionlib.php');
+    }
+
+    static $cache = array();
+
+    if ($course === 'reset') {
+        $cache = array();
+        $nothing = null;
+        return $nothing; // we must return some reference
+    }
+
+    if (empty($userid)) {
+        $userid = $USER->id;
+    }
+
+    if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) {
+        return $cache[$course->id];
+    }
+
+    if (empty($course->modinfo)) {
+        // no modinfo yet - load it
+        rebuild_course_cache($course->id);
+        $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
+    }
+
+    $modinfo = new object();
+    $modinfo->courseid  = $course->id;
+    $modinfo->userid    = $userid;
+    $modinfo->sections  = array();
+    $modinfo->cms       = array();
+    $modinfo->instances = array();
+    $modinfo->groups    = null; // loaded only when really needed - the only one db query
+
+    $info = unserialize($course->modinfo);
+    if (!is_array($info)) {
+        // hmm, something is wrong - lets try to fix it
+        rebuild_course_cache($course->id);
+        $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
+        $info = unserialize($course->modinfo);
+        if (!is_array($info)) {
+            return $modinfo;
+        }
+    }
+
+    if ($info) {
+        // detect if upgrade required
+        $first = reset($info);
+        if (!isset($first->id)) {
+            rebuild_course_cache($course->id);
+            $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
+            $info = unserialize($course->modinfo);
+            if (!is_array($info)) {
+                return $modinfo;
+            }
+        }
+    }
+
+    $modlurals = array();
+
+    // If we haven't already preloaded contexts for the course, do it now
+    preload_course_contexts($course->id);
+
+    foreach ($info as $mod) {
+        if (empty($mod->name)) {
+            // something is wrong here
+            continue;
+        }
+        // reconstruct minimalistic $cm
+        $cm = new object();
+        $cm->id               = $mod->cm;
+        $cm->instance         = $mod->id;
+        $cm->course           = $course->id;
+        $cm->modname          = $mod->mod;
+        $cm->name             = urldecode($mod->name);
+        $cm->visible          = $mod->visible;
+        $cm->sectionnum       = $mod->section;
+        $cm->groupmode        = $mod->groupmode;
+        $cm->groupingid       = $mod->groupingid;
+        $cm->groupmembersonly = $mod->groupmembersonly;
+        $cm->indent           = $mod->indent;
+        $cm->completion       = $mod->completion;
+        $cm->extra            = isset($mod->extra) ? urldecode($mod->extra) : '';
+        $cm->icon             = isset($mod->icon) ? $mod->icon : '';
+        $cm->uservisible      = true;
+        if(!empty($CFG->enableavailability)) {
+            // We must have completion information from modinfo. If it's not
+            // there, cache needs rebuilding
+            if(!isset($mod->availablefrom)) {
+                debugging('enableavailability option was changed; rebuilding '.
+                    'cache for course '.$course->id);
+                rebuild_course_cache($course->id,true);
+                // Re-enter this routine to do it all properly
+                return get_fast_modinfo($course,$userid);
+            }
+            $cm->availablefrom    = $mod->availablefrom;
+            $cm->availableuntil   = $mod->availableuntil;
+            $cm->showavailability = $mod->showavailability;
+            $cm->conditionscompletion = $mod->conditionscompletion;
+            $cm->conditionsgrade  = $mod->conditionsgrade;
+        }
+
+        // preload long names plurals and also check module is installed properly
+        if (!isset($modlurals[$cm->modname])) {
+            if (!file_exists("$CFG->dirroot/mod/$cm->modname/lib.php")) {
+                continue;
+            }
+            $modlurals[$cm->modname] = get_string('modulenameplural', $cm->modname);
+        }
+        $cm->modplural = $modlurals[$cm->modname];
+        $modcontext = get_context_instance(CONTEXT_MODULE,$cm->id);
+        
+        if(!empty($CFG->enableavailability)) {
+            // Unfortunately the next call really wants to call 
+            // get_fast_modinfo, but that would be recursive, so we fake up a 
+            // modinfo for it already
+            if(empty($minimalmodinfo)) {
+                $minimalmodinfo=new stdClass();
+                $minimalmodinfo->cms=array();
+                foreach($info as $mod) {
+                    $minimalcm=new stdClass();
+                    $minimalcm->id=$mod->cm;
+                    $minimalcm->name=urldecode($mod->name);
+                    $minimalmodinfo->cms[$minimalcm->id]=$minimalcm;
+                }
+            }
+
+            // Get availability information
+            $ci = new condition_info($cm);
+            $cm->available=$ci->is_available($cm->availableinfo,true,$userid,
+                $minimalmodinfo);
+        } else {
+            $cm->available=true;
+        }
+        if ((!$cm->visible or !$cm->available) and !has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) {
+            $cm->uservisible = false;
+
+        } else if (!empty($CFG->enablegroupings) and !empty($cm->groupmembersonly)
+                and !has_capability('moodle/site:accessallgroups', $modcontext, $userid)) {
+            if (is_null($modinfo->groups)) {
+                $modinfo->groups = groups_get_user_groups($course->id, $userid);
+            }
+            if (empty($modinfo->groups[$cm->groupingid])) {
+                $cm->uservisible = false;
+            }
+        }
+
+        if (!isset($modinfo->instances[$cm->modname])) {
+            $modinfo->instances[$cm->modname] = array();
+        }
+        $modinfo->instances[$cm->modname][$cm->instance] =& $cm;
+        $modinfo->cms[$cm->id] =& $cm;
+
+        // reconstruct sections
+        if (!isset($modinfo->sections[$cm->sectionnum])) {
+            $modinfo->sections[$cm->sectionnum] = array();
+        }
+        $modinfo->sections[$cm->sectionnum][] = $cm->id;
+
+        unset($cm);
+    }
+
+    unset($cache[$course->id]); // prevent potential reference problems when switching users
+    $cache[$course->id] = $modinfo;
+
+    // Ensure cache does not use too much RAM
+    if (count($cache) > MAX_MODINFO_CACHE_SIZE) {
+        array_shift($cache);
+    }
+
+    return $cache[$course->id];
+}
+
 /**
  * Goes through all enrolment records for the courses inside the metacourse and sync with them.
  *