Skip to content

Commit

Permalink
Merge branch 'MDL-82490-main' of https://github.com/laurentdavid/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Sep 3, 2024
2 parents e812811 + e47bad8 commit 5d5938a
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .upgradenotes/MDL-82490-2024080509320820.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
issueNumber: MDL-82490
notes:
core:
- message: >-
Add set_disabled_option method to url_select to enable or disable an option
from its url (the key for the option).
type: improved
83 changes: 70 additions & 13 deletions course/format/classes/output/local/content/sectionselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use core_courseformat\base as course_format;
use core_courseformat\output\local\courseformat_named_templatable;
use renderable;
use section_info;
use stdClass;
use url_select;

Expand All @@ -42,12 +43,16 @@ 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;

/** @var array $sectionmenu the sections indexed by url. */
protected $sectionmenu = [];

/**
* Constructor.
*
Expand All @@ -66,7 +71,7 @@ public function __construct(course_format $format, sectionnavigation $navigation
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output typically, the renderer that's calling this function
* @param \renderer_base $output typically, the renderer that's calling this function
* @return stdClass data context for a mustache template
*/
public function export_for_template(\renderer_base $output): stdClass {
Expand All @@ -78,25 +83,77 @@ public function export_for_template(\renderer_base $output): stdClass {

$data = $this->navigation->export_for_template($output);

$this->sectionmenu[course_get_url($course)->out(false)] = get_string('maincoursepage');

// 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();
$disabledlink = $this->get_section_url($course, $allsections[$data->currentsection]);
$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;
}
$sectiondelegated = $section->get_component_instance();
if ($sectiondelegated) {
unset($allsections[$section->sectionnum]);
$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($format, $course, $section);
if (isset($sectionwithchildren[$section->sectionnum])) {
foreach ($sectionwithchildren[$section->sectionnum] as $subsection) {
$this->add_section_menu($format, $course, $subsection, true);
}
}
}
$select = new url_select(
urls: $this->sectionmenu,
selected: '',
nothing: ['' => get_string('jumpto')],
);
// Disable the current section.
$select->set_option_disabled($disabledlink);
$select->class = 'jumpmenu';
$select->formid = 'sectionmenu';

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

/**
* Add a section to the section menu.
*
* @param course_format $format
* @param stdClass $course
* @param section_info $section
* @param bool $indent
*/
private function add_section_menu(
course_format $format,
stdClass $course,
section_info $section,
bool $indent = false
) {
$url = $this->get_section_url($course, $section);
$indentation = $indent ? self::INDENTER : '';
$this->sectionmenu[$url] = $indentation . $format->get_section_name($section);
}

/**
* Get the section url.
* @param stdClass $course
* @param section_info $section
* @return string
*/
private function get_section_url(stdClass $course, section_info $section): string {
return course_get_url($course, (object) $section, ['navigation' => true])->out(false);
}
}
18 changes: 18 additions & 0 deletions lib/classes/output/url_select.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class url_select implements renderable, templatable {
*/
public $showbutton = null;

/**
* @var array $disabledoptions array of disabled options
*/
public $disabledoptions = [];

/**
* Constructor
* @param array $urls list of options
Expand All @@ -108,6 +113,17 @@ public function __construct(array $urls, $selected = '', $nothing = ['' => 'choo
$this->nothing = $nothing;
$this->formid = $formid;
$this->showbutton = $showbutton;
$this->disabledoptions = [];
}

/**
* Disable the option(url).
*
* @param string $urlkey
* @param bool $disabled
*/
public function set_option_disabled(string $urlkey, bool $disabled = true) {
$this->disabledoptions[$urlkey] = $disabled;
}

/**
Expand Down Expand Up @@ -189,6 +205,7 @@ protected function flatten_options($options, $nothing) {
'name' => $optoption,
'value' => $cleanedvalue,
'selected' => $this->selected == $optvalue,
'disabled' => $this->disabledoptions[$optvalue] ?? false,
];
}
}
Expand All @@ -198,6 +215,7 @@ protected function flatten_options($options, $nothing) {
'name' => $option,
'value' => $cleanedvalue,
'selected' => $this->selected == $value,
'disabled' => $this->disabledoptions[$value] ?? false,
];
}
}
Expand Down
13 changes: 9 additions & 4 deletions lib/templates/url_select.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
"name": "Group 1", "isgroup": true, "options":
[
{"name": "Item 1", "isgroup": false, "value": "1"},
{"name": "Item 2", "isgroup": false, "value": "2"}
{"name": "Item 2", "isgroup": false, "value": "2"},
{"name": "Item 1", "isgroup": false, "value": "3", "disabled": true}
]},
{"name": "Group 2", "isgroup": true, "options":
[
{"name": "Item 3", "isgroup": false, "value": "3"},
{"name": "Item 4", "isgroup": false, "value": "4"}
]}],
]},
{"name": "Group 3", "isgroup": false, "value":"1"},
{"name": "Group 4", "isgroup": false, "value":"1", "disabled": true}
],

"disabled": false,
"title": "Some cool title"
}
Expand All @@ -54,12 +59,12 @@
{{#isgroup}}
<optgroup label="{{name}}">
{{#options}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{name}}}</option>
<option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}}>{{{name}}}</option>
{{/options}}
</optgroup>
{{/isgroup}}
{{^isgroup}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{name}}}</option>
<option value="{{value}}" {{#selected}}selected{{/selected}} {{#disabled}}disabled{{/disabled}}>{{{name}}}</option>
{{/isgroup}}
{{/options}}
</select>
Expand Down
32 changes: 32 additions & 0 deletions lib/tests/outputcomponents_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,38 @@ public function test_url_select(): void {
$this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
}

/**
* Test for checking the template context data for the url_select element.
* @covers \url_select::disable_option
* @covers \url_select::enable_option
*/
public function test_url_select_disabled_options(): void {
global $PAGE;
$url1 = new \moodle_url("/#a");
$url2 = new \moodle_url("/#b");
$url3 = new \moodle_url("/#c");

$urls = [
$url1->out() => 'A',
$url2->out() => 'B',
$url3->out() => 'C',
];
$urlselect = new url_select($urls,
null,
null,
'someformid',
null);
$renderer = $PAGE->get_renderer('core');
$urlselect->set_option_disabled($url2->out(), true);
$data = $urlselect->export_for_template($renderer);
$this->assertFalse($data->options[0]['disabled']);
$this->assertTrue($data->options[1]['disabled']);
$urlselect->set_option_disabled($url2->out(), false);
$data = $urlselect->export_for_template($renderer);
$this->assertFalse($data->options[0]['disabled']);
$this->assertFalse($data->options[1]['disabled']);
}

/**
* Data provider for test_block_contents_is_fake().
*
Expand Down

0 comments on commit 5d5938a

Please sign in to comment.