mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
MDL-82845 core_course: method to get all cm_info from a section_info
This commit is contained in:
parent
f6141a67d8
commit
4d3d5b3687
8
.upgradenotes/MDL-82845-2024091708245574.yml
Normal file
8
.upgradenotes/MDL-82845-2024091708245574.yml
Normal file
@ -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
|
@ -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
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user