Skip to content

Commit

Permalink
Merge branch 'MDL-82146-main' of https://github.com/aanabit/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Jul 24, 2024
2 parents f8654b5 + 8596d3c commit eb14ffa
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .upgradenotes/MDL-82146-2024070508361756.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
issueNumber: MDL-82146
notes:
core_courseformat:
- message: >-
New $disabled parameter has been added to select, select_optgroup and
select_option html_writers to create disabled option elements.
type: improved
7 changes: 7 additions & 0 deletions .upgradenotes/MDL-82146-2024071006310652.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
issueNumber: MDL-82146
notes:
report_log:
- message: >-
get_activities_list() function returns also an array of disabled
elements, apart from the array of activities.
type: improved
19 changes: 13 additions & 6 deletions lib/classes/output/html_writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public static function select_yes_no($name, $selected = true, ?array $attributes
* @param string|array $selected value or array of values depending on multiple attribute
* @param array|bool|null $nothing add nothing selected option, or false of not added
* @param null|array $attributes html select element attributes
* @param array $disabled An array of disabled options.
* @return string HTML fragment
*/
public static function select(
Expand All @@ -265,7 +266,8 @@ public static function select(
$selected = '',
$nothing = ['' => 'choosedots'],
?array $attributes = null,
) {
array $disabled = [],
): string {
$attributes = (array)$attributes;
if (is_array($nothing)) {
foreach ($nothing as $k => $v) {
Expand Down Expand Up @@ -314,9 +316,9 @@ public static function select(
foreach ($options as $value => $label) {
if (is_array($label)) {
// Ignore key, it just has to be unique.
$output .= self::select_optgroup(key($label), current($label), $selected);
$output .= self::select_optgroup(key($label), current($label), $selected, $disabled);
} else {
$output .= self::select_option($label, $value, $selected);
$output .= self::select_option($label, $value, $selected, $disabled);
}
}
return self::tag('select', $output, $attributes);
Expand All @@ -328,14 +330,18 @@ public static function select(
* @param string $label The label to display as the option.
* @param string|int $value The value the option represents
* @param array $selected An array of selected options
* @param array $disabled An array of disabled options.
* @return string HTML fragment
*/
private static function select_option($label, $value, array $selected) {
private static function select_option($label, $value, array $selected, array $disabled = []): string {
$attributes = [];
$value = (string)$value;
if (in_array($value, $selected, true)) {
$attributes['selected'] = 'selected';
}
if (in_array($value, $disabled, true)) {
$attributes['disabled'] = 'disabled';
}
$attributes['value'] = $value;
return self::tag('option', $label, $attributes);
}
Expand All @@ -346,16 +352,17 @@ private static function select_option($label, $value, array $selected) {
* @param string $groupname The label to use for the group
* @param array $options The options in the group
* @param array $selected An array of selected values.
* @param array $disabled An array of disabled options.
* @return string HTML fragment.
*/
private static function select_optgroup($groupname, $options, array $selected) {
private static function select_optgroup($groupname, $options, array $selected, array $disabled = []): string {
if (empty($options)) {
return '';
}
$attributes = ['label' => $groupname];
$output = '';
foreach ($options as $value => $label) {
$output .= self::select_option($label, $value, $selected);
$output .= self::select_option($label, $value, $selected, $disabled);
}
return self::tag('optgroup', $output, $attributes);
}
Expand Down
75 changes: 63 additions & 12 deletions report/log/classes/renderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,49 +196,100 @@ public function get_readers($nameonly = false) {
* @return array list of activities.
*/
public function get_activities_list() {
$activities = array();
$activities = [];
$disabled = [];

// For site just return site errors option.
$sitecontext = context_system::instance();
if ($this->course->id == SITEID && has_capability('report/log:view', $sitecontext)) {
$activities["site_errors"] = get_string("siteerrors");
return $activities;
return [$activities, $disabled];
}

$modinfo = get_fast_modinfo($this->course);
$delegatedsections = $modinfo->get_sections_delegated_by_cm();

if (!empty($modinfo->cms)) {
$section = 0;
$thissection = array();
foreach ($modinfo->cms as $cm) {
// Exclude activities that aren't visible or have no view link (e.g. label). Account for folders displayed inline.
if (!$cm->uservisible || (!$cm->has_view() && strcmp($cm->modname, 'folder') !== 0)) {
if (!$modname = $this->get_activity_name($cm)) {
continue;
}

if ($cm->sectionnum > 0 and $section <> $cm->sectionnum) {
$sectioninfo = $modinfo->get_section_info($cm->sectionnum);

// Don't show subsections here. We are showing them in the corresponding module.
if ($sectioninfo->is_delegated()) {
continue;
}

$activities[] = $thissection;
$thissection = array();
}
$section = $cm->sectionnum;
$modname = strip_tags($cm->get_formatted_name());
if (core_text::strlen($modname) > 55) {
$modname = core_text::substr($modname, 0, 50)."...";
}
if (!$cm->visible) {
$modname = "(".$modname.")";
}
$key = get_section_name($this->course, $cm->sectionnum);
if (!isset($thissection[$key])) {
$thissection[$key] = array();
$thissection[$key] = [];
}
$thissection[$key][$cm->id] = $modname;
// Check if the module is delegating a section.
if (array_key_exists($cm->id, $delegatedsections)) {
$delegated = $delegatedsections[$cm->id];
$modules = (empty($delegated->sequence)) ? [] : explode(',', $delegated->sequence);
$thissection[$key] = $thissection[$key] + $this->get_delegated_section_activities($modinfo, $modules);
$disabled[] = $cm->id;
}
}
if (!empty($thissection)) {
$activities[] = $thissection;
}
}
return [$activities, $disabled];
}

/**
* Helper function to return list of activities in a delegated section.
*
* @param course_modinfo $modinfo
* @param array $cms List of cm ids in the section.
* @return array list of activities.
*/
protected function get_delegated_section_activities(course_modinfo $modinfo, array $cmids): array {
$activities = [];
$indenter = '&nbsp;&nbsp;&nbsp;&nbsp;';
foreach ($cmids as $cmid) {
$cm = $modinfo->cms[$cmid];
if ($modname = $this->get_activity_name($cm)) {
$activities[$cmid] = $indenter.$modname;
}
}
return $activities;
}

/**
* Helper function to return the name to show in the dropdown.
*
* @param cm_info $cm
* @return string The name.
*/
private function get_activity_name(cm_info $cm): string {
// Exclude activities that aren't visible or have no view link (e.g. label). Account for folders displayed inline.
if (!$cm->uservisible || (!$cm->has_view() && strcmp($cm->modname, 'folder') !== 0)) {
return '';
}
$modname = strip_tags($cm->get_formatted_name());
if (core_text::strlen($modname) > 55) {
$modname = core_text::substr($modname, 0, 50)."...";
}
if (!$cm->visible) {
$modname = "(".$modname.")";
}

return $modname;
}

/**
* Helper function to get selected group.
*
Expand Down
4 changes: 2 additions & 2 deletions report/log/classes/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public function report_selector_form(report_log_renderable $reportlog) {
['class' => 'mr-2 mb-2']);

// Add activity selector.
$activities = $reportlog->get_activities_list();
[$activities, $disabled] = $reportlog->get_activities_list();
echo html_writer::label(get_string('activities'), 'menumodid', false, array('class' => 'accesshide'));
echo html_writer::select($activities, "modid", $reportlog->modid, get_string("allactivities"),
['class' => 'mr-2 mb-2']);
['class' => 'mr-2 mb-2'], $disabled);

// Add actions selector.
echo html_writer::label(get_string('actions'), 'menumodaction', false, array('class' => 'accesshide'));
Expand Down
10 changes: 10 additions & 0 deletions theme/boost/scss/moodle/reports.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@
padding-left: $spacer * 3.5;
}
}

#page-report-log-index #menumodid option:disabled {
// Browsers do not consider the color of a disabled option
// if it is the same as the non-disabled options.
// Since we are using disabled elements to create a sense of hierarchy,
// we intentionally use a slightly different color for them.
// We do this because HTML still does not allow nested optgroups in select elements.
color: lighten($custom-select-color, 1%);
font-weight: bolder;
}
5 changes: 5 additions & 0 deletions theme/boost/style/moodle.css
Original file line number Diff line number Diff line change
Expand Up @@ -35080,6 +35080,11 @@ img.userpicture {
padding-left: 3.5rem;
}

#page-report-log-index #menumodid option:disabled {
color: #4b535a;
font-weight: bolder;
}

.path-backup .mform {
/* These are long labels with checkboxes on the right. */
}
Expand Down
5 changes: 5 additions & 0 deletions theme/classic/style/moodle.css
Original file line number Diff line number Diff line change
Expand Up @@ -35080,6 +35080,11 @@ img.userpicture {
padding-left: 3.5rem;
}

#page-report-log-index #menumodid option:disabled {
color: #4b535a;
font-weight: bolder;
}

.path-backup .mform {
/* These are long labels with checkboxes on the right. */
}
Expand Down

0 comments on commit eb14ffa

Please sign in to comment.