MDL-81725 course: Get delegated section from cm_info

This commit is contained in:
ferranrecio
2024-07-02 11:58:18 +02:00
committed by Laurent David
parent 5aef789ac6
commit d95b7a3883
+47
View File
@@ -93,6 +93,12 @@ class course_modinfo {
*/
private $delegatedsections;
/**
* Index of sections delegated by course modules, indexed by course module instance.
* @var null|section_info[]
*/
private ?array $delegatedbycm = null;
/**
* User ID
* @var int
@@ -139,6 +145,7 @@ class course_modinfo {
'cms' => 'get_cms',
'instances' => 'get_instances',
'groups' => 'get_groups_all',
'delegatedbycm' => 'get_sections_delegated_by_cm',
);
/**
@@ -410,6 +417,33 @@ class course_modinfo {
return !empty($this->delegatedsections);
}
/**
* Gets data about section delegated by course modules.
*
* @return section_info[] sections array indexed by course module ID
*/
public function get_sections_delegated_by_cm(): array {
if (!is_null($this->delegatedbycm)) {
return $this->delegatedbycm;
}
$this->delegatedbycm = [];
foreach ($this->delegatedsections as $componentsections) {
foreach ($componentsections as $section) {
$delegateinstance = $section->get_component_instance();
// We only return sections delegated by course modules. Sections delegated to other
// types of components must implement their own methods to get the section.
if (!$delegateinstance || !($delegateinstance instanceof \core_courseformat\sectiondelegatemodule)) {
continue;
}
if (!$cm = $delegateinstance->get_cm()) {
continue;
}
$this->delegatedbycm[$cm->id] = $section;
}
}
return $this->delegatedbycm;
}
/**
* Static cache for generated course_modinfo instances
*
@@ -2171,6 +2205,19 @@ class cm_info implements IteratorAggregate {
return $cmrecord;
}
/**
* Returns the section delegated by this module, if any.
*
* @return ?section_info
*/
public function get_delegated_section_info(): ?section_info {
$delegatedsections = $this->modinfo->get_sections_delegated_by_cm();
if (!array_key_exists($this->id, $delegatedsections)) {
return null;
}
return $delegatedsections[$this->id];
}
// Set functions
////////////////