From ee2e084e89a6ca14b1e3402ff4faa92a1c61c005 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Thu, 20 Jul 2023 12:16:04 +0200 Subject: [PATCH] MDL-78291 core_courseformat: availability in activity action menu --- .../output/local/content/cm/visibility.php | 15 ++-- .../local/content/cm/visibility.mustache | 6 +- course/format/upgrade.txt | 2 + course/lib.php | 84 +++---------------- lang/en/deprecated.txt | 2 + lang/en/moodle.php | 4 +- lib/classes/output/choicelist.php | 8 ++ 7 files changed, 38 insertions(+), 83 deletions(-) diff --git a/course/format/classes/output/local/content/cm/visibility.php b/course/format/classes/output/local/content/cm/visibility.php index 04b847df70d..e1fc3ed9d54 100644 --- a/course/format/classes/output/local/content/cm/visibility.php +++ b/course/format/classes/output/local/content/cm/visibility.php @@ -153,7 +153,7 @@ class visibility implements named_templatable, renderable { * Get the availability choice list. * @return choicelist */ - protected function get_choice_list(): choicelist { + public function get_choice_list(): choicelist { $choice = $this->create_choice_list(); if (!$this->mod->visible) { $selected = 'hide'; @@ -174,11 +174,14 @@ class visibility implements named_templatable, renderable { global $CFG; $choice = new choicelist(); - $choice->add_option( - 'show', - get_string('availability_show', 'core_courseformat'), - $this->get_option_data('show', 'cmShow') - ); + if ($this->section->visible || $this->mod->has_view()) { + $label = $this->section->visible ? 'show' : 'stealth'; + $choice->add_option( + 'show', + get_string("availability_{$label}", 'core_courseformat'), + $this->get_option_data($label, 'cmShow') + ); + } $choice->add_option( 'hide', get_string('availability_hide', 'core_courseformat'), diff --git a/course/format/templates/local/content/cm/visibility.mustache b/course/format/templates/local/content/cm/visibility.mustache index 82a954b05d2..3ec4e089f3a 100644 --- a/course/format/templates/local/content/cm/visibility.mustache +++ b/course/format/templates/local/content/cm/visibility.mustache @@ -64,7 +64,7 @@ } }} {{#isInteractive}} -
+
{{#dropwdown}} {{< core/local/dropdown/status}} {{$ buttonclasses }} badge badge-pill badge-secondary dropdown-toggle border-0 {{/ buttonclasses }} @@ -74,12 +74,12 @@ {{/isInteractive}} {{^isInteractive}} {{#modhiddenfromstudents}} -
+
{{#pix}}i/show, core{{/pix}}{{#str}}hiddenfromstudents{{/str}}
{{/modhiddenfromstudents}} {{#modstealth}} -
+
{{#pix}}t/stealth, core{{/pix}}{{#str}}hiddenoncoursepage{{/str}}
{{/modstealth}} diff --git a/course/format/upgrade.txt b/course/format/upgrade.txt index 6bd026461a2..84ab8791254 100644 --- a/course/format/upgrade.txt +++ b/course/format/upgrade.txt @@ -45,6 +45,8 @@ Some considerations about the activitybadge feature: * New core_courseformat\base::get_format_string() to get strings that can be overridden by the format plugin. The method will check first the string in the format_PLUGINNAME.php lang file and, if not, it will return the core_courseformat string instead. +* New course format output classes: + - core_courseformat\output\local\content\cm\visibility to display the activity availability. === 4.1 === * New \core_courseformat\stateupdates methods add_section_remove() and add_cm_remove() have been added to replace diff --git a/course/lib.php b/course/lib.php index 2fe83a0eaf2..f3373a97b02 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1661,8 +1661,8 @@ function course_get_cm_edit_actions(cm_info $mod, $indent = -1, $sr = null) { if (!isset($str)) { $str = get_strings( [ - 'delete', 'move', 'moveright', 'moveleft', 'editsettings', 'duplicate', 'modhide', - 'makeavailable', 'makeunavailable', 'modshow', 'modshowcmtitle', 'makeavailablecmtitle', + 'delete', 'move', 'moveright', 'moveleft', 'editsettings', + 'duplicate', 'availability' ], 'moodle' ); @@ -1759,76 +1759,16 @@ function course_get_cm_edit_actions(cm_info $mod, $indent = -1, $sr = null) { // Hide/Show/Available/Unavailable. if (has_capability('moodle/course:activityvisibility', $modcontext)) { - $allowstealth = !empty($CFG->allowstealth) && $courseformat->allow_stealth_module_visibility($mod, $sectioninfo); - - $sectionvisible = $sectioninfo->visible; - // The module on the course page may be in one of the following states: - // - Available and displayed on the course page ($displayedoncoursepage); - // - Not available and not displayed on the course page ($unavailable); - // - Available but not displayed on the course page ($stealth) - this can also be a visible activity in a hidden section. - $displayedoncoursepage = $mod->visible && $mod->visibleoncoursepage && $sectionvisible; - $unavailable = !$mod->visible; - $stealth = $mod->visible && (!$mod->visibleoncoursepage || !$sectionvisible); - if ($displayedoncoursepage) { - $actions['hide'] = new action_menu_link_secondary( - new moodle_url($baseurl, array('hide' => $mod->id)), - new pix_icon('t/hide', '', 'moodle', array('class' => 'iconsmall')), - $str->modhide, - [ - 'class' => 'editing_hide', - 'data-action' => ($usecomponents) ? 'cmHide' : 'hide', - 'data-id' => $mod->id, - ] - ); - } else if (!$displayedoncoursepage && $sectionvisible) { - // Offer to "show" only if the section is visible. - $actions['show'] = new action_menu_link_secondary( - new moodle_url($baseurl, array('show' => $mod->id)), - new pix_icon('t/show', '', 'moodle', array('class' => 'iconsmall')), - $str->modshow, - [ - 'class' => 'editing_show', - 'data-action' => ($usecomponents) ? 'cmShow' : 'show', - 'data-id' => $mod->id, - // Title is needed mostly for behat tests. Otherwise it will follow any link with "show". - 'title' => $str->modshowcmtitle, - ] - ); - } - - if ($stealth) { - // When making the "stealth" module unavailable we perform the same action as hiding the visible module. - $actions['hide'] = new action_menu_link_secondary( - new moodle_url($baseurl, array('hide' => $mod->id)), - new pix_icon('t/unblock', '', 'moodle', array('class' => 'iconsmall')), - $str->makeunavailable, - [ - 'class' => 'editing_makeunavailable', - 'data-action' => ($usecomponents) ? 'cmHide' : 'hide', - 'data-sectionreturn' => $sr, - 'data-id' => $mod->id, - ] - ); - } else if ($unavailable && (!$sectionvisible || $allowstealth) && $mod->has_view()) { - // Allow to make visually hidden module available in gradebook and other reports by making it a "stealth" module. - // When the section is hidden it is an equivalent of "showing" the module. - // Activities without the link (i.e. labels) can not be made available but hidden on course page. - $action = $sectionvisible ? 'stealth' : 'show'; - if ($usecomponents) { - $action = 'cm' . ucfirst($action); - } - $actions[$action] = new action_menu_link_secondary( - new moodle_url($baseurl, array('stealth' => $mod->id)), - new pix_icon('t/block', '', 'moodle', array('class' => 'iconsmall')), - $str->makeavailable, - [ - 'class' => 'editing_makeavailable', - 'data-action' => $action, - 'data-sectionreturn' => $sr, - 'data-id' => $mod->id, - // Title is needed mostly for behat tests. Otherwise it will follow any link with "make available". - 'title' => $str->makeavailablecmtitle, - ] + $availabilityclass = $courseformat->get_output_classname('content\\cm\\visibility'); + /** @var core_courseformat\output\local\content\cm\visibility */ + $availability = new $availabilityclass($courseformat, $sectioninfo, $mod); + $availabilitychoice = $availability->get_choice_list(); + if ($availabilitychoice->count_options() > 1) { + $actions['availability'] = new action_menu_subpanel( + $str->availability, + $availabilitychoice, + ['class' => 'editing_availability'], + new pix_icon('t/hide', '', 'moodle', array('class' => 'iconsmall')) ); } } diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index a030d457c9f..8e825710064 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -84,3 +84,5 @@ backpackemail,core_badges backpackemail_help,core_badges editcoursecompletionsettings,core_completion clicktochangeinbrackets,core +modshowcmtitle,core +makeavailablecmtitle,core diff --git a/lang/en/moodle.php b/lang/en/moodle.php index bcf6fbb02ff..c4dceb445f9 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1248,7 +1248,6 @@ $string['mailteachers'] = 'Inform teachers'; $string['maincoursepage'] = 'Main course page'; $string['makeafolder'] = 'Create folder'; $string['makeavailable'] = 'Make available'; -$string['makeavailablecmtitle'] = 'Make activity available'; $string['makeeditable'] = 'If you make \'{$a}\' editable by the web server process (eg apache) then you could edit this file directly from this page'; $string['makethismyhome'] = 'Make this my home page'; $string['makeunavailable'] = 'Make unavailable'; @@ -1350,7 +1349,6 @@ $string['missingusername'] = 'Missing username'; $string['moddoesnotsupporttype'] = 'Module {$a->modname} does not support uploads of type {$a->type}'; $string['modhide'] = 'Hide'; $string['modshow'] = 'Show'; -$string['modshowcmtitle'] = 'Show activity'; $string['modvisible'] = 'Availability'; $string['modvisible_help'] = '* Show on course page: Available to students (subject to any access restrictions which may be set). * Hide on course page: Not available to students.'; @@ -2452,3 +2450,5 @@ $string['supplyinfo'] = 'More details'; // Deprecated since Moodle 4.3. $string['clicktochangeinbrackets'] = '{$a} (Click to change)'; +$string['modshowcmtitle'] = 'Show activity'; +$string['makeavailablecmtitle'] = 'Make activity available'; diff --git a/lib/classes/output/choicelist.php b/lib/classes/output/choicelist.php index 6f3a032507c..ae38e786329 100644 --- a/lib/classes/output/choicelist.php +++ b/lib/classes/output/choicelist.php @@ -83,6 +83,14 @@ class choicelist implements renderable, named_templatable { } } + /** + * Get the number of options added to the choice list. + * @return int + */ + public function count_options(): int { + return count($this->options); + } + /** * Set the selected option. *