Skip to content

Commit

Permalink
MDL-82490 course_format: Add subsection in section selector
Browse files Browse the repository at this point in the history
* Add subsection in topic format section selector in the right order.
  • Loading branch information
laurentdavid committed Aug 19, 2024
1 parent a038947 commit 8ecfa06
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions course/format/classes/output/local/content/sectionselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use core\output\named_templatable;
use core_courseformat\base as course_format;
use core_courseformat\output\local\courseformat_named_templatable;
use core_courseformat\sectiondelegate;
use renderable;
use stdClass;
use url_select;
Expand All @@ -42,9 +43,10 @@ class sectionselector implements named_templatable, renderable {

use courseformat_named_templatable;

/** @var string the indenter */
private const INDENTER = '    ';
/** @var course_format the course format class */
protected $format;

/** @var sectionnavigation the main section navigation class */
protected $navigation;

Expand Down Expand Up @@ -81,22 +83,60 @@ public function export_for_template(\renderer_base $output): stdClass {
// Add the section selector.
$sectionmenu = [];
$sectionmenu[course_get_url($course)->out(false)] = get_string('maincoursepage');
$section = 1;
$numsections = $format->get_last_section_number();
while ($section <= $numsections) {
$thissection = $modinfo->get_section_info($section);
$url = course_get_url($course, $section, ['navigation' => true]);
if ($thissection->uservisible && $url && $section != $data->currentsection) {
$sectionmenu[$url->out(false)] = get_section_name($course, $section);
$allsections = $modinfo->get_section_info_all();
$disabledoptions = [course_get_url($course, $allsections[$data->currentsection], ['navigation' => true])->out(false)];
$sectionwithchildren = [];
// First get any section with chidren (easier to process later in a regular loop).
foreach ($allsections as $section) {
if (!$section->uservisible) {
unset($allsections[$section->sectionnum]);
continue;
}
if ($section->is_delegated()) {
unset($allsections[$section->sectionnum]);
$sectiondelegated = sectiondelegate::instance($section);
$parentsection = $sectiondelegated->get_parent_section();
// If the section is delegated we need to get the parent section and add the section to the parent section array.
if ($parentsection) {
$sectionwithchildren[$parentsection->sectionnum][] = $section;
}
}
$section++;
}

$select = new url_select($sectionmenu, '', ['' => get_string('jumpto')]);
foreach ($allsections as $section) {
$this->add_section_menu($section, $course, $format, $sectionmenu);
if (isset($sectionwithchildren[$section->sectionnum])) {
foreach ($sectionwithchildren[$section->sectionnum] as $subsection) {
if ($subsection->uservisible) {
$this->add_section_menu($subsection, $course, $format, $sectionmenu, self::INDENTER);
}
}
}
}
$select = new url_select($sectionmenu, '', ['' => get_string('jumpto')], null, null, $disabledoptions);
$select->class = 'jumpmenu';
$select->formid = 'sectionmenu';

$data->selector = $output->render($select);
return $data;
}

/**
* @param \section_info $section
* @param stdClass $course
* @param course_format $format
* @param array $sectionmenu
* @param string $indent
* @return void
*/
private function add_section_menu(
\section_info $section,
stdClass $course,
course_format $format,
array &$sectionmenu,
string $indent = ''
) {
$url = course_get_url($course, $section, ['navigation' => true]);
$sectionmenu[$url->out(false)] = $indent . $format->get_section_name($section);
}
}

0 comments on commit 8ecfa06

Please sign in to comment.