From f36cbf1d6e5e20d877a246137d52cb5937afbbb1 Mon Sep 17 00:00:00 2001 From: skodak Date: Fri, 1 Feb 2008 07:48:26 +0000 Subject: [PATCH] MDL-13214 rebuild_course_cache fixes and speed improvements; merged from MOODLE_19_STABLE --- course/lib.php | 27 ++++++++++++++++++++++----- lib/filelib.php | 6 ++++-- mod/assignment/lib.php | 35 +++++++++++++++-------------------- mod/label/lib.php | 15 ++++++++------- mod/resource/lib.php | 2 +- 5 files changed, 50 insertions(+), 35 deletions(-) diff --git a/course/lib.php b/course/lib.php index 8f4ce3db29..8f0c9dd3e6 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1480,10 +1480,26 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false, } } -function rebuild_course_cache($courseid=0) { +/** + * Rebuilds the cached list of course activities stored in the database + * @param int $courseid - id of course to rebuil, empty means all + * @param boolean $clearonly - only clear the modinfo fields, gets rebuild automatically on the fly + */ +function rebuild_course_cache($courseid=0, $clearonly=false) { global $COURSE; -// Rebuilds the cached list of course activities stored in the database -// If a courseid is not specified, then all are rebuilt + + if ($clearonly) { + $courseselect = empty($courseid) ? "" : "id = $courseid"; + set_field_select('course', 'modinfo', null, $courseselect); + // update cached global COURSE too ;-) + if ($courseid == $COURSE->id) { + $COURSE->modinfo = null; + } + // reset the fast modinfo cache + $reset = 'reset'; + get_fast_modinfo($reset); + return; + } if ($courseid) { $select = "id = '$courseid'"; @@ -1492,8 +1508,8 @@ function rebuild_course_cache($courseid=0) { @set_time_limit(0); // this could take a while! MDL-10954 } - if ($courses = get_records_select("course", $select,'','id,fullname')) { - foreach ($courses as $course) { + if ($rs = get_recordset_select("course", $select,'','id,fullname')) { + while($course = rs_fetch_next_record($rs)) { $modinfo = serialize(get_array_of_activities($course->id)); if (!set_field("course", "modinfo", $modinfo, "id", $course->id)) { notify("Could not cache module information for course '" . format_string($course->fullname) . "'!"); @@ -1503,6 +1519,7 @@ function rebuild_course_cache($courseid=0) { $COURSE->modinfo = $modinfo; } } + rs_close($rs); } // reset the fast modinfo cache $reset = 'reset'; diff --git a/lib/filelib.php b/lib/filelib.php index f1beef9549..92a8e70a44 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -406,8 +406,10 @@ function get_mimetypes_array() { * @return string Requested piece of information from array */ function mimeinfo($element, $filename) { - static $mimeinfo; - $mimeinfo=get_mimetypes_array(); + static $mimeinfo = null; + if (is_null($mimeinfo)) { + $mimeinfo = get_mimetypes_array(); + } if (eregi('\.([a-z0-9]+)$', $filename, $match)) { if (isset($mimeinfo[strtolower($match[1])][$element])) { diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index e58e22c639..59ab2386ab 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -21,7 +21,6 @@ class assignment_base { var $strsubmissions; var $strlastmodified; var $pagetitle; - var $currentgroup; var $usehtmleditor; var $defaultformat; var $context; @@ -81,17 +80,8 @@ class assignment_base { $this->strlastmodified = get_string('lastmodified'); $this->pagetitle = strip_tags($this->course->shortname.': '.$this->strassignment.': '.format_string($this->assignment->name,true)); - // visibility - $context = get_context_instance(CONTEXT_MODULE, $cmid); - if (!$this->cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) { - $pagetitle = strip_tags($this->course->shortname.': '.$this->strassignment); - $navigation = build_navigation('', $this->cm); - - print_header($pagetitle, $this->course->fullname, $navigation, - "", "", true, '', navmenu($this->course, $this->cm)); - notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}"); - } - $this->currentgroup = groups_get_activity_group($this->cm); + // visibility handled by require_login() with $cm parameter + // get current group only when really needed /// Set up things for a HTML editor if it's needed if ($this->usehtmleditor = can_use_html_editor()) { @@ -150,9 +140,7 @@ class assignment_base { true, update_module_button($this->cm->id, $this->course->id, $this->strassignment), navmenu($this->course, $this->cm)); - $groupmode = groups_get_activity_groupmode($this->cm); groups_print_activity_menu($this->cm, 'view.php?id=' . $this->cm->id); - $this->currentgroup = groups_get_activity_group($this->cm); // must be done after the printing! echo ''; echo '
'; @@ -305,7 +293,7 @@ class assignment_base { if ($allgroups and has_capability('moodle/site:accessallgroups', $context)) { $group = 0; } else { - $group = $this->currentgroup; + $group = groups_get_activity_group($this->cm); } if ($count = $this->count_real_submissions($group)) { $submitted = ''. @@ -2756,15 +2744,22 @@ function assignment_get_all_submissions($assignment, $sort="", $dir="DESC") { function assignment_get_coursemodule_info($coursemodule) { global $CFG; - if (! $assignment = get_record('assignment', 'id', $coursemodule->instance)) { + if (! $type = get_field('assignment', 'assignmenttype', 'id', $coursemodule->instance)) { return false; } - require_once("$CFG->dirroot/mod/assignment/type/$assignment->assignmenttype/assignment.class.php"); - $assignmentclass = "assignment_$assignment->assignmenttype"; - $ass = new $assignmentclass($coursemodule->id, $assignment); + $libfile = "$CFG->dirroot/mod/assignment/type/$type/assignment.class.php"; - return $ass->get_coursemodule_info($coursemodule); + if (file_exists($libfile)) { + require_once($libfile); + $assignmentclass = "assignment_$type"; + $ass = new $assignmentclass('staticonly'); + return $ass->get_coursemodule_info($coursemodule); + + } else { + debugging('Incorrect assignment type: '.$type); + return false; + } } diff --git a/mod/label/lib.php b/mod/label/lib.php index 0422283fd0..e4425f2682 100644 --- a/mod/label/lib.php +++ b/mod/label/lib.php @@ -64,15 +64,16 @@ function label_get_participants($labelid) { return false; } +/** + * Given a course_module object, this function returns any + * "extra" information that may be needed when printing + * this activity in a course listing. + * See get_array_of_activities() in course/lib.php + */ function label_get_coursemodule_info($coursemodule) { -/// Given a course_module object, this function returns any -/// "extra" information that may be needed when printing -/// this activity in a course listing. -/// -/// See get_array_of_activities() in course/lib.php - if ($label = get_record("label", "id", $coursemodule->instance)) { + if ($content = get_field('label', 'content', 'id', $coursemodule->instance)) { $info = new object(); - $info->extra = urlencode($label->content); + $info->extra = urlencode($content); return $info; } else { return null; diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 4ca2060058..e89605dbd7 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -349,7 +349,7 @@ function resource_get_coursemodule_info($coursemodule) { $info = NULL; - if ($resource = get_record("resource", "id", $coursemodule->instance)) { + if ($resource = get_record("resource", "id", $coursemodule->instance, '', '', '', '', 'id, popup, reference, type')) { $info = new object(); if (!empty($resource->popup)) { $info->extra = urlencode("onclick=\"this.target='resource$resource->id'; return ". -- 2.39.5