From 2ea6533a366a39cc19385292a4d1b650f025a067 Mon Sep 17 00:00:00 2001 From: Dan Poltawski <dan@moodle.com> Date: Thu, 24 May 2012 22:24:30 +0800 Subject: [PATCH 1/2] MDL-33307 format_weeks - highlight current week --- course/format/renderer.php | 16 ++++++++++++++-- course/format/weeks/renderer.php | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/course/format/renderer.php b/course/format/renderer.php index d969e8bc7c7..fb59c386a52 100644 --- a/course/format/renderer.php +++ b/course/format/renderer.php @@ -92,7 +92,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base { if ($section->section != 0) { // Only in the non-general sections. - if ($course->marker == $section->section) { + if ($this->is_section_current($section, $course)) { $o = get_accesshide(get_string('currentsection', 'format_'.$course->format)); } } @@ -121,7 +121,7 @@ abstract class format_section_renderer_base extends plugin_renderer_base { // Only in the non-general sections. if (!$section->visible) { $sectionstyle = ' hidden'; - } else if ($course->marker == $section->section) { + } else if ($this->is_section_current($section, $course)) { $sectionstyle = ' current'; } $linktitle = ($course->coursedisplay == COURSE_DISPLAY_MULTIPAGE); @@ -669,4 +669,16 @@ abstract class format_section_renderer_base extends plugin_renderer_base { $options->overflowdiv = true; return format_text($summarytext, $section->summaryformat, $options); } + + /** + * Is the section passed in the current section? (Note this isn't strictly + * a renderering method, but neater here). + * + * @param stdClass $course The course entry from DB + * @param stdClass $section The course_section entry from the DB + * @return bool true if the section is current + */ + protected function is_section_current($section, $course) { + return ($course->marker == $section->section); + } } diff --git a/course/format/weeks/renderer.php b/course/format/weeks/renderer.php index 33d640281b6..5184f04abfc 100644 --- a/course/format/weeks/renderer.php +++ b/course/format/weeks/renderer.php @@ -58,4 +58,24 @@ class format_weeks_renderer extends format_section_renderer_base { protected function page_title() { return get_string('weeklyoutline'); } + + /** + * Is the section passed in the current section? + * + * @param stdClass $course The course entry from DB + * @param stdClass $section The course_section entry from the DB + * @return bool true if the section is current + */ + protected function is_section_current($section, $course) { + if ($section->section < 1) { + return false; + } + $oneweekseconds = 604800; + $startdate = $course->startdate + ($oneweekseconds * ($section->section - 1)); + $enddate = $startdate + $oneweekseconds; + + $timenow = time(); + + return (($timenow >= $startdate) && ($timenow < $enddate)); + } } From b6283a49709fb137620cd8eb652f230ac8b0b115 Mon Sep 17 00:00:00 2001 From: Dan Poltawski <dan@moodle.com> Date: Thu, 24 May 2012 22:51:53 +0800 Subject: [PATCH 2/2] MDL-33307 format_weeks - sort out week date display * Introduce function format_weeks_get_section_dates which is used as the consistent place to calculate the current section start and end date * Rework callback_weeks_get_section_name to not use the above function rather than do crazy needless looping through all sections to work out the current week title. * Change the just introduced is_current renderer function to share the format_weeks_get_section_dates function to ensure both the title and 'current week' are using the same dates to do their job --- course/format/weeks/lib.php | 47 +++++++++++++++++++++----------- course/format/weeks/renderer.php | 9 +++--- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 9201c9e2d6f..834addadec4 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -67,25 +67,20 @@ function callback_weeks_definition() { function callback_weeks_get_section_name($course, $section) { // We can't add a node without text if (!empty($section->name)) { - // Return the name the user set - return format_string($section->name, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); + // Return the name the user set. + return format_string($section->name, true, array('context' => context_course::instance($course->id))); } else if ($section->section == 0) { - // Return the section0name + // Return the general section. return get_string('section0name', 'format_weeks'); } else { - // Got to work out the date of the week so that we can show it - $sections = get_all_sections($course->id); - $weekdate = $course->startdate+7200; - foreach ($sections as $sec) { - if ($sec->id == $section->id) { - break; - } else if ($sec->section != 0) { - $weekdate += 604800; - } - } - $strftimedateshort = ' '.get_string('strftimedateshort'); - $weekday = userdate($weekdate, $strftimedateshort); - $endweekday = userdate($weekdate+518400, $strftimedateshort); + $dates = format_weeks_get_section_dates($section, $course); + + // We subtract 24 hours for display purposes. + $dates->end = ($dates->end - 86400); + + $dateformat = ' '.get_string('strftimedateshort'); + $weekday = userdate($dates->start, $dateformat); + $endweekday = userdate($dates->end, $dateformat); return $weekday.' - '.$endweekday; } } @@ -102,3 +97,23 @@ function callback_weeks_ajax_support() { $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0); return $ajaxsupport; } + +/** + * Return the start and end date of the passed section + * + * @param stdClass $section The course_section entry from the DB + * @param stdClass $course The course entry from DB + * @return stdClass property start for startdate, property end for enddate + */ +function format_weeks_get_section_dates($section, $course) { + $oneweekseconds = 604800; + // Hack alert. We add 2 hours to avoid possible DST problems. (e.g. we go into daylight + // savings and the date changes. + $startdate = $course->startdate + 7200; + + $dates = new stdClass(); + $dates->start = $startdate + ($oneweekseconds * ($section->section - 1)); + $dates->end = $dates->start + $oneweekseconds; + + return $dates; +} diff --git a/course/format/weeks/renderer.php b/course/format/weeks/renderer.php index 5184f04abfc..e87936ccbf5 100644 --- a/course/format/weeks/renderer.php +++ b/course/format/weeks/renderer.php @@ -26,6 +26,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot.'/course/format/renderer.php'); +require_once($CFG->dirroot.'/course/format/weeks/lib.php'); /** @@ -62,20 +63,18 @@ class format_weeks_renderer extends format_section_renderer_base { /** * Is the section passed in the current section? * - * @param stdClass $course The course entry from DB * @param stdClass $section The course_section entry from the DB + * @param stdClass $course The course entry from DB * @return bool true if the section is current */ protected function is_section_current($section, $course) { if ($section->section < 1) { return false; } - $oneweekseconds = 604800; - $startdate = $course->startdate + ($oneweekseconds * ($section->section - 1)); - $enddate = $startdate + $oneweekseconds; $timenow = time(); + $dates = format_weeks_get_section_dates($section, $course); - return (($timenow >= $startdate) && ($timenow < $enddate)); + return (($timenow >= $dates->start) && ($timenow < $dates->end)); } }