Skip to content

Commit

Permalink
admin/modules: Avoid calling rebuild_course_cache()
Browse files Browse the repository at this point in the history
rebuild_course_cache() over all courses is extremely expensive,
not suitable for interactive calling. Better to clear modinfo
and let course/view.php repopulate it as needed.

With this patch we clear out modinfo _only_ for courses affected
by the module we show/hide/delete.
  • Loading branch information
martinlanghoff committed Sep 12, 2007
1 parent f6297ed commit cfca081
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions admin/modules.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,29 +34,45 @@


print_heading($stractivities); print_heading($stractivities);


$coursesaffected = false;


/// If data submitted, then process and store. /// If data submitted, then process and store.


if (!empty($hide) and confirm_sesskey()) { if (!empty($hide) and confirm_sesskey()) {
if (!$module = get_record("modules", "name", $hide)) { if (!$module = get_record("modules", "name", $hide)) {
error("Module doesn't exist!"); error("Module doesn't exist!");
} }
set_field("modules", "visible", "0", "id", $module->id); // Hide main module set_field("modules", "visible", "0", "id", $module->id); // Hide main module
set_field('course_modules', 'visibleold', '1', 'visible' ,'1', 'module', $module->id); // Remember the previous visible state so we can toggle this back if the module is unhidden. // Remember the visibility status in visibleold
set_field('course_modules', 'visibleold', '0', 'visible' ,'0', 'module', $module->id); // and hide...
set_field("course_modules", "visible", "0", "module", $module->id); // Hide all related activity modules $sql = "UPDATE {$CFG->prefix}course_modules
$coursesaffected = true; SET visibleold=visible,
visible=0
WHERE module={$module->id}";
execute_sql($sql, false);
// clear the course modinfo cache for courses
// where we just deleted something
$sql = "UPDATE {$CFG->prefix}course
SET modinfo=''
WHERE id IN (SELECT DISTINCT course
FROM {$CFG->prefix}course_modules
WHERE visibleold=1 AND module={$module->id})";
execute_sql($sql, false);
} }


if (!empty($show) and confirm_sesskey()) { if (!empty($show) and confirm_sesskey()) {
if (!$module = get_record("modules", "name", $show)) { if (!$module = get_record("modules", "name", $show)) {
error("Module doesn't exist!"); error("Module doesn't exist!");
} }
set_field("modules", "visible", "1", "id", $module->id); // Show main module set_field("modules", "visible", "1", "id", $module->id); // Show main module
set_field('course_modules', 'visible', '1', 'visibleold', '1', 'module', $module->id); // Get the previous saved visible state for the course module. set_field('course_modules', 'visible', '1', 'visibleold',
$coursesaffected = true; '1', 'module', $module->id); // Get the previous saved visible state for the course module.
// clear the course modinfo cache for courses
// where we just made something visible
$sql = "UPDATE {$CFG->prefix}course
SET modinfo=''
WHERE id IN (SELECT DISTINCT course
FROM {$CFG->prefix}course_modules
WHERE visible=1 AND module={$module->id})";
execute_sql($sql, false);
} }


if (!empty($delete) and confirm_sesskey()) { if (!empty($delete) and confirm_sesskey()) {
Expand Down Expand Up @@ -89,6 +105,15 @@
} }
} }


// clear course.modinfo for courses
// that used this module...
$sql = "UPDATE {$CFG->prefix}course
SET modinfo=''
WHERE id IN (SELECT DISTINCT course
FROM {$CFG->prefix}course_modules
WHERE module={$module->id})";
execute_sql($sql, false);

// Now delete all the course module records // Now delete all the course module records
if (!delete_records("course_modules", "module", $module->id)) { if (!delete_records("course_modules", "module", $module->id)) {
notify("Error occurred while deleting all $strmodulename records in course_modules table"); notify("Error occurred while deleting all $strmodulename records in course_modules table");
Expand Down Expand Up @@ -124,20 +149,12 @@
// Delete the capabilities that were defined by this module // Delete the capabilities that were defined by this module
capabilities_cleanup('mod/'.$module->name); capabilities_cleanup('mod/'.$module->name);


// rebuild_course_cache(); // Because things have changed
$coursesaffected = true;

$a->module = $strmodulename; $a->module = $strmodulename;
$a->directory = "$CFG->dirroot/mod/$delete"; $a->directory = "$CFG->dirroot/mod/$delete";
notice(get_string("moduledeletefiles", "", $a), "modules.php"); notice(get_string("moduledeletefiles", "", $a), "modules.php");
} }
} }


if ($coursesaffected) {
rebuild_course_cache(); // Because things have changed
}


/// Get and sort the existing modules /// Get and sort the existing modules


if (!$modules = get_records("modules")) { if (!$modules = get_records("modules")) {
Expand Down

0 comments on commit cfca081

Please sign in to comment.