From 4d3d5b3687378e600aba91005052b5efd843d4fb Mon Sep 17 00:00:00 2001 From: ferranrecio Date: Tue, 17 Sep 2024 09:37:35 +0200 Subject: [PATCH] MDL-82845 core_course: method to get all cm_info from a section_info --- .upgradenotes/MDL-82845-2024091708245574.yml | 8 +++++ lib/modinfolib.php | 24 ++++++++++++++ lib/tests/modinfolib_test.php | 34 ++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 .upgradenotes/MDL-82845-2024091708245574.yml diff --git a/.upgradenotes/MDL-82845-2024091708245574.yml b/.upgradenotes/MDL-82845-2024091708245574.yml new file mode 100644 index 00000000000..f7e0c5ebbe2 --- /dev/null +++ b/.upgradenotes/MDL-82845-2024091708245574.yml @@ -0,0 +1,8 @@ +issueNumber: MDL-82845 +notes: + core: + - message: >- + The section_info class now includes a new method called + get_sequence_cm_infos that retrieves all cm_info instances associated + with the course section. + type: improved diff --git a/lib/modinfolib.php b/lib/modinfolib.php index 252d64ea649..a986c56dbb5 100644 --- a/lib/modinfolib.php +++ b/lib/modinfolib.php @@ -3226,6 +3226,9 @@ class section_info implements IteratorAggregate { */ private ?sectiondelegate $_delegateinstance = null; + /** @var cm_info[]|null Section cm_info activities, null when it is not loaded yet. */ + private array|null $_sequencecminfos = null; + /** * @var bool|null $_isorphan True if the section is orphan for some reason. */ @@ -3625,6 +3628,27 @@ class section_info implements IteratorAggregate { } } + /** + * Returns the course modules in this section. + * + * @return cm_info[] + */ + public function get_sequence_cm_infos(): array { + if ($this->_sequencecminfos !== null) { + return $this->_sequencecminfos; + } + $sequence = $this->modinfo->sections[$this->_sectionnum] ?? []; + $cms = $this->modinfo->get_cms(); + $result = []; + foreach ($sequence as $cmid) { + if (isset($cms[$cmid])) { + $result[] = $cms[$cmid]; + } + } + $this->_sequencecminfos = $result; + return $result; + } + /** * Returns course ID - from course_sections table * diff --git a/lib/tests/modinfolib_test.php b/lib/tests/modinfolib_test.php index a53a9dba31c..d2ecf1b6d12 100644 --- a/lib/tests/modinfolib_test.php +++ b/lib/tests/modinfolib_test.php @@ -2223,4 +2223,38 @@ class modinfolib_test extends advanced_testcase { $delegatedsection = $modinfo->get_section_info($delegatedsection->section); $this->assertTrue($delegatedsection->is_orphan()); } + + /** + * Test for section_info::get_sequence_cm_infos + * + * @covers \section_info::get_sequence_cm_infos + * @return void + */ + public function test_section_get_sequence_cm_infos(): void { + $this->resetAfterTest(); + + $course = $this->getDataGenerator()->create_course(['numsections' => 2]); + $cm1 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 0]); + $cm2 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 1]); + $cm3 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 1]); + $cm4 = $this->getDataGenerator()->create_module('page', ['course' => $course], ['section' => 1]); + + $modinfo = get_fast_modinfo($course->id); + + $sectioninfo = $modinfo->get_section_info(0); + $cms = $sectioninfo->get_sequence_cm_infos(); + $this->assertCount(1, $cms); + $this->assertEquals($cm1->cmid, $cms[0]->id); + + $sectioninfo = $modinfo->get_section_info(1); + $cms = $sectioninfo->get_sequence_cm_infos(); + $this->assertCount(3, $cms); + $this->assertEquals($cm2->cmid, $cms[0]->id); + $this->assertEquals($cm3->cmid, $cms[1]->id); + $this->assertEquals($cm4->cmid, $cms[2]->id); + + $sectioninfo = $modinfo->get_section_info(2); + $cms = $sectioninfo->get_sequence_cm_infos(); + $this->assertCount(0, $cms); + } }