Commit 13801a49 authored by Aparup Banerjee's avatar Aparup Banerjee Committed by Petr Skoda
Browse files

MDL-23939 lazy load course_display with new course_get_display()

This change speeds up user login and makes the session smaller.
parent d911c72b
......@@ -35,11 +35,7 @@ $topic = optional_param('topic', -1, PARAM_INT);
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);
......
......@@ -34,11 +34,7 @@ defined('MOODLE_INTERNAL') || die();
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');
......
......@@ -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;
......
......@@ -6021,6 +6021,11 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
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;
}
......
......@@ -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);
......
......@@ -1385,7 +1385,7 @@ class global_navigation extends 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;
}
......
......@@ -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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment