From 81e15d8bba3b734ada4106afff4b0128b57e4318 Mon Sep 17 00:00:00 2001 From: Gareth Barnard <1058419+gjb2048@users.noreply.github.com> Date: Mon, 6 May 2024 14:03:22 +0100 Subject: [PATCH] Only show a section if it has modules that are visible to the user when not editing. And a little logic tidy. --- Changes.md | 1 + classes/output/renderer.php | 62 +++++++++---------- lib.php | 28 ++++++++- .../local/content/section/cmlist.mustache | 2 +- 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/Changes.md b/Changes.md index f939449..c8ce19c 100644 --- a/Changes.md +++ b/Changes.md @@ -5,6 +5,7 @@ Version 404.0.2 - In development -------------------------------- 1. Fix section zero with a summary is shown when there are no modules. 2. Add flexible modules. +3. Only show a section if it has modules that are visible to the user when not editing. Version 404.0.1 - 22/04/2024 ---------------------------- diff --git a/classes/output/renderer.php b/classes/output/renderer.php index 05869d4..f790d38 100644 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -535,11 +535,9 @@ protected function topcoll_section($section, $course, $onsectionpage, $sectionre ); } - if ($section->uservisible) { - $sectioncontext['cscml'] = $this->course_section_cmlist($section); - if ($this->courseformat->show_editor()) { - $sectioncontext['cscml'] .= $this->course_section_add_cm_control($course, $section->section, $sectionreturn); - } + $sectioncontext['cscml'] = $this->course_section_cmlist($section); + if ($this->courseformat->show_editor()) { + $sectioncontext['cscml'] .= $this->course_section_add_cm_control($course, $section->section, $sectionreturn); } return $this->render_from_template('format_topcoll/section', $sectioncontext); @@ -791,7 +789,7 @@ public function multiple_section_page() { // General section if non-empty. $thissection = $sections[0]; unset($sections[0]); - if (!empty($modinfo->sections[0]) || $this->userisediting) { + if ($this->courseformat->is_section_visible($thissection)) { $content .= $this->topcoll_section($thissection, $course, false); } @@ -890,12 +888,12 @@ public function multiple_section_page() { /* Show the section if the user is permitted to access it, OR if it's not available but there is some available info text which explains the reason & should display. */ - if (($this->tcsettings['layoutstructure'] != 3) || ($this->userisediting)) { - $showsection = ($this->courseformat->is_section_visible($thissection)); - } else { - $showsection = (($this->courseformat->is_section_visible($thissection)) && ($nextweekdate <= $timenow)); + $showsection = ($this->courseformat->is_section_visible($thissection)); + if ($showsection && ($this->tcsettings['layoutstructure'] == 3) && (!$this->userisediting)) { + $showsection = ($nextweekdate <= $timenow); } - if (($currentsectionfirst == true) && ($showsection == true)) { + + if ($currentsectionfirst && $showsection) { // Show the section if we were meant to and it is the current section:.... $showsection = ($course->marker == $section); } else if ( @@ -904,7 +902,27 @@ public function multiple_section_page() { ) { $showsection = false; // Do not reshow current section. } - if (!$showsection) { + if ($showsection) { + if ($this->isoldtogglepreference == true) { + $togglestate = substr($this->togglelib->get_toggles(), $section, 1); + if ($togglestate == '1') { + $extrasectioninfo[$thissection->id]->toggle = true; + } else { + $extrasectioninfo[$thissection->id]->toggle = false; + } + } else { + $extrasectioninfo[$thissection->id]->toggle = $this->togglelib->get_toggle_state($thissection->section); + } + + if ($this->courseformat->is_section_current($thissection)) { + $this->currentsection = $thissection->section; + $extrasectioninfo[$thissection->id]->toggle = true; // Open current section regardless of toggle state. + $this->togglelib->set_toggle_state($thissection->section, true); + } + + $extrasectioninfo[$thissection->id]->isshown = true; + $sectiondisplayarray[] = $thissection; + } else { // Hidden section message is overridden by 'unavailable' control. $testhidden = false; if ($this->tcsettings['layoutstructure'] != 4) { @@ -926,26 +944,6 @@ public function multiple_section_page() { $sectiondisplayarray[] = $thissection; } } - } else { - if ($this->isoldtogglepreference == true) { - $togglestate = substr($this->togglelib->get_toggles(), $section, 1); - if ($togglestate == '1') { - $extrasectioninfo[$thissection->id]->toggle = true; - } else { - $extrasectioninfo[$thissection->id]->toggle = false; - } - } else { - $extrasectioninfo[$thissection->id]->toggle = $this->togglelib->get_toggle_state($thissection->section); - } - - if ($this->courseformat->is_section_current($thissection)) { - $this->currentsection = $thissection->section; - $extrasectioninfo[$thissection->id]->toggle = true; // Open current section regardless of toggle state. - $this->togglelib->set_toggle_state($thissection->section, true); - } - - $extrasectioninfo[$thissection->id]->isshown = true; - $sectiondisplayarray[] = $thissection; } if (($this->tcsettings['layoutstructure'] != 3) || ($this->userisediting)) { diff --git a/lib.php b/lib.php index d98eaad..c1b663d 100644 --- a/lib.php +++ b/lib.php @@ -268,7 +268,33 @@ public function is_section_visible(section_info $section): bool { // Don't show. return false; } - return parent::is_section_visible($section); + $shown = parent::is_section_visible($section); + if ($shown) { + // Don't show if no modules or all modules unavailable to user. + $showmovehere = ismoving($this->course->id); + if (!$showmovehere) { + global $PAGE; + $context = context_course::instance($this->course->id); + if (!($PAGE->user_is_editing() && has_capability('moodle/course:update', $context))) { + $modshown = false; + $modinfo = get_fast_modinfo($this->course); + + if (!empty($modinfo->sections[$section->section])) { + foreach ($modinfo->sections[$section->section] as $modnumber) { + $mod = $modinfo->cms[$modnumber]; + if ($mod->is_visible_on_course_page()) { + // At least one is. + $modshown = true; + break; + } + } + } + $shown = $modshown; + } + } + } + + return $shown; } /** diff --git a/templates/local/content/section/cmlist.mustache b/templates/local/content/section/cmlist.mustache index 73bddbe..5dac87d 100644 --- a/templates/local/content/section/cmlist.mustache +++ b/templates/local/content/section/cmlist.mustache @@ -58,7 +58,7 @@ {{#showmovehere}}

{{movingstr}} ({{#str}} cancel {{/str}})

{{/showmovehere}} -