This commit is contained in:
Sara Arjona 2024-09-03 19:40:38 +02:00
commit 5d5938a7ed
No known key found for this signature in database
5 changed files with 136 additions and 17 deletions

View File

@ -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

View File

@ -28,6 +28,7 @@ use core\output\named_templatable;
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;
@ -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.
*
@ -66,7 +71,7 @@ class sectionselector implements named_templatable, renderable {
/**
* 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 {
@ -78,25 +83,77 @@ class sectionselector implements named_templatable, renderable {
$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);
}
}

View File

@ -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
@ -108,6 +113,17 @@ class url_select implements renderable, templatable {
$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;
}
/**
@ -189,6 +205,7 @@ class url_select implements renderable, templatable {
'name' => $optoption,
'value' => $cleanedvalue,
'selected' => $this->selected == $optvalue,
'disabled' => $this->disabledoptions[$optvalue] ?? false,
];
}
}
@ -198,6 +215,7 @@ class url_select implements renderable, templatable {
'name' => $option,
'value' => $cleanedvalue,
'selected' => $this->selected == $value,
'disabled' => $this->disabledoptions[$value] ?? false,
];
}
}

View File

@ -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"
}
@ -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>

View File

@ -744,6 +744,38 @@ EOF;
$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().
*