From 71290decf4d1235056537eb61fea6cc3865b2872 Mon Sep 17 00:00:00 2001 From: Gareth Barnard <1058419+gjb2048@users.noreply.github.com> Date: Wed, 22 May 2024 19:13:22 +0100 Subject: [PATCH] Fix updating of course when adding or removing sections. Note: Known issue of the section not looking as it should. --- Changes.md | 1 + classes/courseformat/stateactions.php | 4 ++-- classes/output/courseformat/content.php | 2 +- lib.php | 19 ++++++++++++++++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Changes.md b/Changes.md index fec2eb2c..2b34ec4c 100644 --- a/Changes.md +++ b/Changes.md @@ -3,6 +3,7 @@ History Version 402.2.4 - In development -------------------------------- +1. Fix updating of course when adding or removing sections. Note: Known issue of the section not looking as it should. Version 402.2.3 - 08/04/2024 ---------------------------- diff --git a/classes/courseformat/stateactions.php b/classes/courseformat/stateactions.php index 2b4eeaab..89505f6f 100644 --- a/classes/courseformat/stateactions.php +++ b/classes/courseformat/stateactions.php @@ -49,9 +49,9 @@ public function section_add( ?int $targetsectionid = null, ?int $targetcmid = null ): void { - parent::section_add($updates, $course, $ids, $targetsectionid, $targetcmid); $format = course_get_format($course); $format->section_added(); + parent::section_add($updates, $course, $ids, $targetsectionid, $targetcmid); } /** @@ -72,8 +72,8 @@ public function section_delete( ?int $targetsectionid = null, ?int $targetcmid = null ): void { - parent::section_delete($updates, $course, $ids, $targetsectionid, $targetcmid); $format = course_get_format($course); $format->section_deleted(); + parent::section_delete($updates, $course, $ids, $targetsectionid, $targetcmid); } } diff --git a/classes/output/courseformat/content.php b/classes/output/courseformat/content.php index 58be2cea..feb2123f 100644 --- a/classes/output/courseformat/content.php +++ b/classes/output/courseformat/content.php @@ -81,7 +81,7 @@ public function export_for_template(\renderer_base $output) { $data = (object)[ 'title' => $format->page_title(), 'format' => $format->get_format(), - 'sectionreturn' => 0, + 'sectionreturn' => null, ]; $singlesection = $this->format->get_section_number(); diff --git a/lib.php b/lib.php index 531bb31f..88eb507c 100755 --- a/lib.php +++ b/lib.php @@ -961,15 +961,28 @@ public function restore_gnumsections($numsections) { * A section has been added. Should only be called from the state actions instance. */ public function section_added() { - $data = ['gnumsections' => $this->settings['gnumsections'] + 1]; - $this->update_format_options($data); + $this->change_gnumsections(true); } /** * A section has been deleted. Should only be called from the state actions instance. */ public function section_deleted() { - $data = ['gnumsections' => $this->settings['gnumsections'] - 1]; + $this->change_gnumsections(false); + } + + /** + * A section has been added or deleted. Should only be called via the state actions instance. + * + * @param bool $add Add a section or delete if false. + */ + protected function change_gnumsections($add) { + if ($add) { + $newgnumsetions = $this->settings['gnumsections'] + 1; + } else { + $newgnumsetions = $this->settings['gnumsections'] - 1; + } + $data = ['gnumsections' => $newgnumsetions]; $this->update_format_options($data); }