Skip to content
Browse files

MDL-23939 minor course_display coding style improvements and inline docs

  • Loading branch information...
1 parent 13801a4 commit cc10c0b9791ec2216eb305498a466bcd7100ef14 @skodak skodak committed Feb 12, 2011
Showing with 39 additions and 29 deletions.
  1. +36 −22 course/lib.php
  2. +3 −7 lib/navigationlib.php
View
58 course/lib.php
@@ -1240,10 +1240,11 @@ function get_all_sections($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)
+ * Returns the course section to display or 0 meaning show all sections. Returns 0 for 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
+ * @return int Course section to display, 0 means all
*/
function course_get_display($courseid) {
global $USER, $DB;
@@ -1254,42 +1255,55 @@ function course_get_display($courseid) {
}
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
+ if (!$display = $DB->get_field('course_display', 'display', array('userid' => $USER->id, 'course'=>$courseid))) {
+ $display = 0; // all sections option is not stored in DB, this makes the table much smaller
}
- //set display cache to only this course.
- return $USER->display = array($courseid => (int)$display);
+ //use display cache for one course only - we need to keep session small
+ $USER->display = array($courseid => $display);
}
+
return $USER->display[$courseid];
}
-function course_set_display($courseid, $display=0) {
+/**
+ * Show one section only or all sections.
+ *
+ * @param int $courseid The course id
+ * @param mixed $display show only this section, 0 or 'all' means show all sections
+ * @return int Course section to display, 0 means all
+ */
+function course_set_display($courseid, $display) {
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
+ return 0;
+ }
+
+ if ($display == 0) {
+ //show all, do not store anything in database
+ $DB->delete_records('course_display', array('userid' => $USER->id, 'course' => $courseid));
+
} else {
- if ($display == 0) { //show all, so remove the explicit setting.
- $DB->delete_records('course_display', array('userid' => $USER->id, 'course' => $courseid));
+ 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 {
- 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);
- }
+ $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 ==
+ //use display cache for one course only - we need to keep session small
+ $USER->display = array($courseid => $display);
+
+ return $display;
}
/**
View
10 lib/navigationlib.php
@@ -1384,11 +1384,7 @@ public function load_generic_course_sections(stdClass $course, navigation_node $
$viewhiddensections = has_capability('moodle/course:viewhiddensections', $this->page->context);
- if (isloggedin() && !isguestuser()) {
- $activesection = course_get_display($course->id);
- } else {
- $activesection = null;
- }
+ $activesection = course_get_display($course->id);
$namingfunction = 'callback_'.$courseformat.'_get_section_name';
$namingfunctionexists = (function_exists($namingfunction));
@@ -1418,7 +1414,7 @@ public function load_generic_course_sections(stdClass $course, navigation_node $
$sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id);
$sectionnode->nodetype = navigation_node::NODETYPE_BRANCH;
$sectionnode->hidden = (!$section->visible);
- if ($this->page->context->contextlevel != CONTEXT_MODULE && ($sectionnode->isactive || ($activesection != null && $section->section == $activesection))) {
+ if ($this->page->context->contextlevel != CONTEXT_MODULE && ($sectionnode->isactive || ($activesection && $section->section == $activesection))) {
$sectionnode->force_open();
$this->load_section_activities($sectionnode, $section->section, $modinfo);
}
@@ -1517,7 +1513,7 @@ protected function load_stealth_activity(navigation_node $coursenode, $modinfo)
*/
protected function load_activity($cm, stdClass $course, navigation_node $activity) {
global $CFG, $DB;
-
+
// make sure we have a $cm from get_fast_modinfo as this contains activity access details
if (!($cm instanceof cm_info)) {
$modinfo = get_fast_modinfo($course);

0 comments on commit cc10c0b

Please sign in to comment.
Something went wrong with that request. Please try again.