MDL-75329 course: defensive counting of course sections.

Avoids errors due to pre-existing integrity issues with course
data, where the absence entirely of section data would result in
PHP errors.
This commit is contained in:
Paul Holden
2023-09-26 12:18:34 +01:00
parent 3070f8e760
commit 1fcbdf24b8
2 changed files with 34 additions and 3 deletions
+7 -1
View File
@@ -383,7 +383,7 @@ abstract class base {
* This method ensures that 3rd party course format plugins that still use 'numsections' continue to
* work but at the same time we no longer expect formats to have 'numsections' property.
*
* @return int
* @return int The last section number, or -1 if sections are entirely missing
*/
public function get_last_section_number() {
$course = $this->get_course();
@@ -392,6 +392,12 @@ abstract class base {
}
$modinfo = get_fast_modinfo($course);
$sections = $modinfo->get_section_info_all();
// Sections seem to be missing entirely. Avoid subsequent errors and return early.
if (count($sections) === 0) {
return -1;
}
return (int)max(array_keys($sections));
}