diff --git a/classes/output/courseformat/content/sectionnavigation.php b/classes/output/courseformat/content/sectionnavigation.php index 7eec928..e9f4a9d 100644 --- a/classes/output/courseformat/content/sectionnavigation.php +++ b/classes/output/courseformat/content/sectionnavigation.php @@ -32,6 +32,9 @@ * Base class to render a course add section navigation. */ class sectionnavigation extends \core_courseformat\output\local\content\sectionnavigation { + /** @var stdClass the calculated data to prevent calculations when rendered several times */ + protected $data = null; + /** * Export this data so it can be used as the context for a mustache template. * @@ -39,8 +42,59 @@ class sectionnavigation extends \core_courseformat\output\local\content\sectionn * @return stdClass data context for a mustache template */ public function export_for_template(\renderer_base $output): \stdClass { - $data = parent::export_for_template($output); + global $USER; + + if ($this->data !== null) { + return $this->data; + } + + $format = $this->format; + $course = $format->get_course(); + $context = \context_course::instance($course->id); + + $modinfo = $this->format->get_modinfo(); + $sections = $modinfo->get_section_info_all(); + + // FIXME: This is really evil and should by using the navigation API. + $canviewhidden = has_capability('moodle/course:viewhiddensections', $context, $USER); + + $data = (object)[ + 'previousurl' => '', + 'nexturl' => '', + 'larrow' => $output->larrow(), + 'rarrow' => $output->rarrow(), + 'currentsection' => $this->sectionno, + ]; + + $back = $this->sectionno - 1; + while ($back > 0 and empty($data->previousurl)) { + if ($canviewhidden || $sections[$back]->uservisible) { + if (!$sections[$back]->visible) { + $data->previoushidden = true; + } + $data->previousname = get_section_name($course, $sections[$back]); + $data->previousurl = course_get_url($course, $back, ['navigation' => true]); + $data->hasprevious = true; + } + $back--; + } + + $forward = $this->sectionno + 1; + $numsections = course_get_format($course)->get_last_section_number(); + while ($forward <= $numsections and empty($data->nexturl)) { + if ($canviewhidden || $sections[$forward]->uservisible) { + if (!$sections[$forward]->visible) { + $data->nexthidden = true; + } + $data->nextname = get_section_name($course, $sections[$forward]); + $data->nexturl = course_get_url($course, $forward, ['navigation' => true]); + $data->hasnext = true; + } + $forward++; + } + $data->rtl = right_to_left(); + $this->data = $data; return $data; } diff --git a/classes/output/courseformat/content/sectionselector.php b/classes/output/courseformat/content/sectionselector.php index 9b82b19..1e8608b 100644 --- a/classes/output/courseformat/content/sectionselector.php +++ b/classes/output/courseformat/content/sectionselector.php @@ -41,4 +41,41 @@ class sectionselector extends \core_courseformat\output\local\content\sectionsel public function get_template_name(\renderer_base $renderer): string { return 'format_topcoll/local/content/sectionselector'; } + + /** + * Export this data so it can be used as the context for a mustache template. + * + * @param renderer_base $output typically, the renderer that's calling this function + * @return stdClass data context for a mustache template + */ + public function export_for_template(\renderer_base $output): \stdClass { + + $format = $this->format; + $course = $format->get_course(); + + $modinfo = $this->format->get_modinfo(); + + $data = $this->navigation->export_for_template($output); + + // Add the section selector. + $sectionmenu = []; + $sectionmenu[course_get_url($course)->out(false)] = get_string('maincoursepage'); + $section = 0; + $numsections = $format->get_last_section_number(); + while ($section <= $numsections) { + $thissection = $modinfo->get_section_info($section); + $url = course_get_url($course, $section, ['navigation' => true]); + if ($thissection->uservisible && $url && $section != $data->currentsection) { + $sectionmenu[$url->out(false)] = get_section_name($course, $section); + } + $section++; + } + + $select = new \url_select($sectionmenu, '', ['' => get_string('jumpto')]); + $select->class = 'jumpmenu'; + $select->formid = 'sectionmenu'; + + $data->selector = $output->render($select); + return $data; + } } diff --git a/lang/en/format_topcoll.php b/lang/en/format_topcoll.php index 5c7dc19..6db00cc 100644 --- a/lang/en/format_topcoll.php +++ b/lang/en/format_topcoll.php @@ -463,11 +463,11 @@ // Information. $string['information'] = 'Information'; $string['informationsettings'] = 'Information settings'; -$string['informationsettingsdesc'] = 'Collapsed Topics format information'; +$string['informationsettingsdesc'] = 'Collapsed Topics course format information'; $string['informationchanges'] = 'Changes'; $string['settings'] = 'Settings'; $string['settingssettings'] = 'Settings settings'; -$string['settingssettingsdesc'] = 'Collapsed Topics format settings'; +$string['settingssettingsdesc'] = 'Collapsed Topics course format settings'; $string['love'] = 'love'; $string['versioninfo'] = 'Release {$a->release}, version {$a->version} on Moodle {$a->moodle}. Made with {$a->love} in Great Britain.'; $string['versionalpha'] = 'Alpha version - Almost certainly contains bugs. This is a development version for developers \'only\'! Don\'t even think of installing on a production server!';