calendar: performance optimisations + behaviour fix (MDL-7416 MDL-9617)

With 2500 courses the calendar was causing around 15000 queries at login and 5000 on the home page.

- Replaced queries inside loops with single unlooped queries.
- Fixed calendar so that course events will show when the users role is assigned at a category level.
- Moved calendar_session_vars() function call out of calendar/lib.php (including a lib should not have side effects)

Author: Matt Clarkson - with some conflict resolution from MartinL
This commit is contained in:
martinlanghoff 2007-05-06 04:28:11 +00:00
parent f261e052f2
commit 37d87d11f9
8 changed files with 39 additions and 16 deletions

View File

@ -20,7 +20,9 @@ class block_calendar_month extends block_base {
if ($this->content !== NULL) {
return $this->content;
}
// Initialize the session variables
calendar_session_vars();
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';

View File

@ -16,7 +16,8 @@ class block_calendar_upcoming extends block_base {
if ($this->content !== NULL) {
return $this->content;
}
// Initialize the session variables
calendar_session_vars();
$this->content = new stdClass;
$this->content->text = '';

View File

@ -66,6 +66,9 @@
$strcalendar = get_string('calendar', 'calendar');
// Initialize the session variables
calendar_session_vars();
$now = usergetdate(time());
$nav = calendar_get_link_tag($strcalendar, CALENDAR_URL.'view.php?view=upcoming&', $now['mday'], $now['mon'], $now['year']);
$day = intval($now['mday']);

View File

@ -17,6 +17,9 @@ if(!$site = get_site()) {
redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php');
}
// Initialize the session variables
calendar_session_vars();
$pagetitle = get_string('export', 'calendar');
$now = usergetdate(time());
$nav = calendar_get_link_tag(get_string('calendar', 'calendar'), CALENDAR_URL.'view.php?view=upcoming&', $now['mday'], $now['mon'], $now['year']).' -> '.$pagetitle;

View File

@ -64,8 +64,7 @@ define ('CALENDAR_TF_12', '%I:%M %p');
$CALENDARDAYS = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
// Initialize the session variables here to be sure
calendar_session_vars();
function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) {
global $CFG, $USER;
@ -1137,23 +1136,18 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU
}
else {
$grouparray = array();
$groupmodes = NULL;
// We already have the courses to examine in $courses
// For each course...
foreach($groupcourses as $courseid) {
// If the user is an editing teacher in there,
if(!empty($USER->id) && has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $courseid))) {
// The first time we get in here, retrieve all groupmodes at once
if($groupmodes === NULL) {
$groupmodes = get_records_list('course', 'id', implode(',', $groupcourses), '', 'id, groupmode, groupmodeforce');
}
// If this course has groups, show events from all of them
if(isset($groupmodes[$courseid]) && ($groupmodes[$courseid]->groupmode != NOGROUPS || !$groupmodes[$courseid]->groupmodeforce) && ($grouprecords = get_groups($courseid)) !== false) {
$grouparray = array_merge($grouparray, array_keys($grouprecords));
if(($SESSION->cal_courses_shown[$courseid]->groupmode != NOGROUPS || !$SESSION->cal_courses_shown[$courseid]->groupmodeforce)) {
$groupids[] = $courseid;
}
}
@ -1165,6 +1159,14 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU
}
}
}
$sql = "SELECT id, groupid
FROM {$CFG->prefix}groups_courses_groups
WHERE courseid IN (".implode(',', $groupids).')';
$grouprecords= get_records_sql($sql);
$grouparray = array_merge($grouparray, array_keys($grouprecords));
if(empty($grouparray)) {
$group = false;
}
@ -1237,11 +1239,15 @@ function calendar_get_default_courses($ignoreref = false) {
}
if (isset($CFG->adminseesall)) {
return get_my_courses($USER->id, 'visible DESC', '*', $CFG->adminseesall);
$courses = get_my_courses($USER->id, 'visible DESC', '*', $CFG->adminseesall);
}
else {
return get_my_courses($USER->id, 'visible DESC', '*', false);
$courses = get_my_courses($USER->id, 'visible DESC', '*', false);
}
// Make sure global events are included
$courses[0] = true;
return $courses;
}
function calendar_preferences_button() {

View File

@ -13,7 +13,9 @@
if ($course->id != SITEID) {
require_login($course->id);
}
// Initialize the session variables
calendar_session_vars();
/// If data submitted, then process and store.
if ($form = data_submitted()) {

View File

@ -49,6 +49,9 @@
$cal_m = optional_param('cal_m');
$cal_y = optional_param('cal_y');
// Initialize the session variables
calendar_session_vars();
switch($var) {
case 'setuser':
// Not implemented yet (or possibly at all)

View File

@ -60,6 +60,9 @@
require_login();
}
// Initialize the session variables
calendar_session_vars();
//add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
$now = usergetdate(time());
$pagetitle = '';