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 94dde98
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 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 Down Expand Up @@ -80,23 +81,71 @@ public function export_for_template(\renderer_base $output): stdClass {

// Add the section selector.
$sectionmenu = [];
$disabledoptions = [];
$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();
$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;
}
}
}

foreach ($allsections as $section) {
$this->add_section_menu($section, $data->currentsection, $sectionmenu, $disabledoptions);
if (isset($sectionwithchildren[$section->sectionnum])) {
foreach ($sectionwithchildren[$section->sectionnum] as $section) {
if ($section->uservisible) {
$this->add_section_menu($section, $data->currentsection, $sectionmenu, $disabledoptions, true);
}
}
}
$section++;
}

$select = new url_select($sectionmenu, '', ['' => get_string('jumpto')]);
$select = new url_select($sectionmenu, '', ['' => get_string('jumpto')], null, null, $disabledoptions);
$select->class = 'jumpmenu';
$select->formid = 'sectionmenu';

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

/**
* Create a new section menu
*
* @param \section_info $section
* @param int $currentsectionnum
* @param array $sectionmenu
* @param array $disabledoptions
* @param bool $shouldindent should indent the section (this is used for delegated sections)
* @return void
*/
private function add_section_menu(
\section_info $section,
int $currentsectionnum,
array &$sectionmenu,
array &$disabledoptions,
bool $shouldindent = false,
) {
$indenter = '&nbsp;&nbsp;&nbsp;&nbsp;';
$format = $this->format;
$course = $format->get_course();
$url = course_get_url($course, $section, ['navigation' => true]);
$urlkey = $url->out(false);
$sectionmenu[$urlkey] = ($shouldindent ? $indenter : '') . $format->get_section_name($section);
if ($section->sectionnum == $currentsectionnum) {
$disabledoptions[] = $urlkey;
}
}
}

0 comments on commit 94dde98

Please sign in to comment.