From: martinlanghoff Date: Sun, 6 May 2007 04:28:11 +0000 (+0000) Subject: calendar: performance optimisations + behaviour fix (MDL-7416 MDL-9617) X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=37d87d11f963dfcd3ffcd5f56e5af4fca08eab0e;p=moodle.git 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 --- diff --git a/blocks/calendar_month/block_calendar_month.php b/blocks/calendar_month/block_calendar_month.php index ad4ed09e0d..dd5debbc8a 100644 --- a/blocks/calendar_month/block_calendar_month.php +++ b/blocks/calendar_month/block_calendar_month.php @@ -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 = ''; diff --git a/blocks/calendar_upcoming/block_calendar_upcoming.php b/blocks/calendar_upcoming/block_calendar_upcoming.php index f0e1566b80..f7d4770896 100644 --- a/blocks/calendar_upcoming/block_calendar_upcoming.php +++ b/blocks/calendar_upcoming/block_calendar_upcoming.php @@ -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 = ''; diff --git a/calendar/event.php b/calendar/event.php index 472e3b70d8..a3fe5e3c41 100644 --- a/calendar/event.php +++ b/calendar/event.php @@ -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']); diff --git a/calendar/export.php b/calendar/export.php index 9a73b3b877..e7caced2ce 100644 --- a/calendar/export.php +++ b/calendar/export.php @@ -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; diff --git a/calendar/lib.php b/calendar/lib.php index 8cfbbfb724..5939ac7c1a 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -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() { diff --git a/calendar/preferences.php b/calendar/preferences.php index e168acf839..048fb99974 100644 --- a/calendar/preferences.php +++ b/calendar/preferences.php @@ -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()) { diff --git a/calendar/set.php b/calendar/set.php index 267bddc939..9835770e5f 100644 --- a/calendar/set.php +++ b/calendar/set.php @@ -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) diff --git a/calendar/view.php b/calendar/view.php index bd16f659a9..8fe19f658b 100644 --- a/calendar/view.php +++ b/calendar/view.php @@ -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 = '';