Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-23939 lazy load course_display with new course_get_display()

This change speeds up user login and makes the session smaller.
  • Loading branch information...
commit 13801a49a268760e708ed0700acd3896b27a9c5b 1 parent d911c72
@nebgor nebgor authored skodak committed
View
6 course/format/topics/format.php
@@ -35,11 +35,7 @@
if ($topic != -1) {
$displaysection = course_set_display($course->id, $topic);
} else {
- if (isset($USER->display[$course->id])) {
- $displaysection = $USER->display[$course->id];
- } else {
- $displaysection = course_set_display($course->id, 0);
- }
+ $displaysection = course_get_display($course->id);
}
$context = get_context_instance(CONTEXT_COURSE, $course->id);
View
6 course/format/weeks/format.php
@@ -34,11 +34,7 @@
if ($week != -1) {
$displaysection = course_set_display($course->id, $week);
} else {
- if (isset($USER->display[$course->id])) {
- $displaysection = $USER->display[$course->id];
- } else {
- $displaysection = course_set_display($course->id, 0);
- }
+ $displaysection = course_get_display($course->id);
}
$streditsummary = get_string('editsummary');
View
51 course/lib.php
@@ -1239,23 +1239,54 @@ function get_all_sections($courseid) {
return $coursesections[$courseid];
}
+/**
+ * Returns the course section to display. Returns 0 to mean show all sections.
+ * Returns 0 all guests. It also sets the $USER->display cache to array($courseid=>return value)
+ * @param int $courseid The course id
+ * @return int Course section to display
+ */
+function course_get_display($courseid) {
+ global $USER, $DB;
+
+ if (!isloggedin() or isguestuser()) {
+ //do not get settings in db for guests
+ return 0; //return the implicit setting
+ }
+
+ if (!isset($USER->display[$courseid])) {
+ $display = $DB->get_field('course_display', 'display', array('userid' => $USER->id, 'course'=>$courseid));
+ if ($display === false) {
+ $display = 0; //return the implicit setting
+ }
+ //set display cache to only this course.
+ return $USER->display = array($courseid => (int)$display);
+ }
+ return $USER->display[$courseid];
+}
+
function course_set_display($courseid, $display=0) {
global $USER, $DB;
- if ($display == "all" or empty($display)) {
+ if ($display == 'all' or empty($display)) {
$display = 0;
}
if (!isloggedin() or isguestuser()) {
//do not store settings in db for guests
- } else if ($DB->record_exists("course_display", array("userid" => $USER->id, "course"=>$courseid))) {
- $DB->set_field("course_display", "display", $display, array("userid"=>$USER->id, "course"=>$courseid));
} else {
- $record = new stdClass();
- $record->userid = $USER->id;
- $record->course = $courseid;
- $record->display = $display;
- $DB->insert_record("course_display", $record);
+ if ($display == 0) { //show all, so remove the explicit setting.
+ $DB->delete_records('course_display', array('userid' => $USER->id, 'course' => $courseid));
+ } else {
+ if ($DB->record_exists('course_display', array('userid' => $USER->id, 'course' => $courseid))) {
+ $DB->set_field('course_display', 'display', $display, array('userid' => $USER->id, 'course' => $courseid));
+ } else {
+ $record = new stdClass();
+ $record->userid = $USER->id;
+ $record->course = $courseid;
+ $record->display = $display;
+ $DB->insert_record('course_display', $record);
+ }
+ }
}
return $USER->display[$courseid] = $display; // Note: = not ==
@@ -2837,7 +2868,7 @@ function move_section($course, $section, $move) {
$DB->set_field("course_sections", "section", $section, array("id"=>$sectiondestrecord->id));
// if the focus is on the section that is being moved, then move the focus along
- if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
+ if (course_get_display($course->id) == $section) {
course_set_display($course->id, $sectiondest);
}
@@ -2890,7 +2921,7 @@ function move_section_to($course, $section, $destination) {
}
// if the focus is on the section that is being moved, then move the focus along
- if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
+ if (course_get_display($course->id) == $section) {
course_set_display($course->id, $destination);
}
return true;
View
5 lib/db/upgrade.php
@@ -6021,6 +6021,11 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2011020200.01);
}
+ if ($oldversion < 2011020900.07) {
+ $DB->delete_records('course_display', array('display' => 0));
+ upgrade_main_savepoint(true, 2011020900.07);
+ }
+
return true;
}
View
6 lib/moodlelib.php
@@ -3784,12 +3784,6 @@ function get_complete_user_data($field, $value, $mnethostid = null) {
/// Get various settings and preferences
- if ($displays = $DB->get_records('course_display', array('userid'=>$user->id))) {
- foreach ($displays as $display) {
- $user->display[$display->course] = $display->display;
- }
- }
-
// preload preference cache
check_user_preferences_loaded($user);
View
2  lib/navigationlib.php
@@ -1385,7 +1385,7 @@ public function load_generic_course_sections(stdClass $course, navigation_node $
$viewhiddensections = has_capability('moodle/course:viewhiddensections', $this->page->context);
if (isloggedin() && !isguestuser()) {
- $activesection = $DB->get_field("course_display", "display", array("userid"=>$USER->id, "course"=>$course->id));
+ $activesection = course_get_display($course->id);
} else {
$activesection = null;
}
View
2  version.php
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2011020900; // YYYYMMDD = date of the last version bump
+$version = 2011020900.07; // YYYYMMDD = date of the last version bump
// XX = daily increments
$release = '2.0.1+ (Build: 20110209)'; // Human-friendly version name
Please sign in to comment.
Something went wrong with that request. Please try again.