MDL-81533 Admin: Availability restriction info default change

This commit is contained in:
Hieu Nguyen Dang 2024-07-15 14:09:15 +07:00
parent 7d7a871edd
commit adea726a9d
15 changed files with 284 additions and 80 deletions

View File

@ -0,0 +1,15 @@
issueNumber: MDL-81533
notes:
core:
- message: >
The function update_display_mode will update the eye icon (enabled/disabled) in the availability.
The $pluginname is represented to the plugin need to update.
The $displaymode is represented to the eye icon. Whether it enabled or disabled.
type: improved
core_admin:
- message: >
Add availability_management_table is a table which extends from plugin_management_table.
Create the availability_management_table can reusable the toggle button for enabled column.
type: improved

View File

@ -0,0 +1,85 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_admin\table;
use moodle_url;
use stdClass;
use html_writer;
use get_string_manager;
/**
* Availability admin settings.
*
* @package core_admin
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class availability_management_table extends plugin_management_table {
protected function get_table_id(): string {
return 'availabilityconditions_administration_table';
}
protected function get_plugintype(): string {
return 'availability';
}
protected function get_action_url(array $params = []): moodle_url {
return new moodle_url('/admin/tool/availabilityconditions/', $params);
}
public function guess_base_url(): void {
$this->define_baseurl(
new moodle_url('/admin/tool/availabilityconditions/')
);
}
protected function get_column_list(): array {
return [
'name' => get_string('plugin'),
'version' => get_string('version'),
'enabled' => get_string('enabled', 'admin'),
'defaultdisplaymode' => get_string('defaultdisplaymode', 'tool_availabilityconditions'),
];
}
public function setup(): void {
$this->set_attribute('id', 'availabilityconditions_administration_table');
$this->set_attribute('class', 'admintable generaltable');
parent::setup();
}
protected function col_name(stdClass $row): string {
return html_writer::span(
get_string('pluginname', 'availability_' . $row->plugininfo->name)
);
}
protected function col_defaultdisplaymode(stdClass $row): string {
global $OUTPUT, $CFG;
$displaymode = get_config('availability_' . $row->plugininfo->name, 'defaultdisplaymode') ? 'show' : 'hide';
$paramsdisplaymode = [
'sesskey' => sesskey(),
'plugin' => $row->plugininfo->name,
'displaymode' => $displaymode,
];
$urldisplaymode = new moodle_url('/' . $CFG->admin . '/tool/availabilityconditions/', $paramsdisplaymode);
return html_writer::link($urldisplaymode, $OUTPUT->pix_icon('t/' . $displaymode,
get_string($displaymode)), ['class' => 'display-mode-' . $row->plugininfo->name]);
}
}

View File

@ -14,7 +14,7 @@ Feature: I can apply presets
Given I navigate to "Plugins > Activity modules > Manage activities" in site administration
And I should see "Disable Database"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should exist in the "Restriction by grouping" "table_row"
And I should see "Disable Restriction by grouping"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And I should see "Disable Logged in user"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration
@ -107,7 +107,7 @@ Feature: I can apply presets
And I navigate to "Plugins > Activity modules > Manage activities" in site administration
And I should see "Enable Database"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should not exist in the "Restriction by grouping" "table_row"
And I should see "Enable Restriction by grouping"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And I should see "Enable Logged in user"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration

View File

@ -21,7 +21,7 @@ Feature: I can revert changes after a load
And I navigate to "Plugins > Activity modules > Manage activities" in site administration
And I should see "Enable Database"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should not exist in the "Restriction by grouping" "table_row"
And I should see "Enable Restriction by grouping"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And "Disable Logged in user" "icon" should not exist in the "Logged in user" "table_row"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration
@ -39,7 +39,7 @@ Feature: I can revert changes after a load
And I navigate to "Plugins > Activity modules > Manage activities" in site administration
And I should see "Disable Database"
And I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
And "Hide" "icon" should exist in the "Restriction by grouping" "table_row"
And I should see "Disable Restriction by grouping"
And I navigate to "Plugins > Blocks > Manage blocks" in site administration
And I should see "Disable Logged in user"
And I navigate to "Plugins > Course formats > Manage course formats" in site administration

View File

@ -31,7 +31,7 @@ require_once($CFG->libdir . '/tablelib.php');
admin_externalpage_setup('manageavailability');
// Get sorted list of all availability condition plugins.
$plugins = array();
$plugins = [];
foreach (core_component::get_plugin_list('availability') as $plugin => $plugindir) {
if (get_string_manager()->string_exists('pluginname', 'availability_' . $plugin)) {
$strpluginname = get_string('pluginname', 'availability_' . $plugin);
@ -44,20 +44,30 @@ core_collator::asort($plugins);
// Do plugin actions.
$pageurl = new moodle_url('/' . $CFG->admin . '/tool/availabilityconditions/');
$classavailability = \core_plugin_manager::resolve_plugininfo_class('availability');
if (($plugin = optional_param('plugin', '', PARAM_PLUGIN))) {
require_sesskey();
if (!array_key_exists($plugin, $plugins)) {
throw new \moodle_exception('invalidcomponent', 'error', $pageurl);
}
$action = required_param('action', PARAM_ALPHA);
switch ($action) {
$action = optional_param('action', '', PARAM_ALPHA);
if ($action === 'hide' && $classavailability::enable_plugin($plugin, 0)) {
\core\notification::add(
\core\notification::SUCCESS
);
} else if ($action === 'show' && $classavailability::enable_plugin($plugin, 1)) {
\core\notification::add(
\core\notification::SUCCESS
);
}
$displaymode = optional_param('displaymode', '', PARAM_ALPHA);
switch ($displaymode) {
case 'hide' :
$class = \core_plugin_manager::resolve_plugininfo_class('availability');
$class::enable_plugin($plugin, false);
$classavailability::update_display_mode($plugin, false);
break;
case 'show' :
$class = \core_plugin_manager::resolve_plugininfo_class('availability');
$class::enable_plugin($plugin, true);
$classavailability::update_display_mode($plugin, true);
break;
}
@ -68,48 +78,6 @@ if (($plugin = optional_param('plugin', '', PARAM_PLUGIN))) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('manageplugins', 'availability'));
// Show a table of installed availability conditions.
$table = new flexible_table('availabilityconditions_administration_table');
$table->define_columns(array('name', 'version', 'enable'));
$table->define_headers(array(get_string('plugin'),
get_string('version'), get_string('hide') . '/' . get_string('show')));
$table->define_baseurl($PAGE->url);
$table->set_attribute('id', 'availabilityconditions');
$table->set_attribute('class', 'admintable generaltable');
$table->setup();
$enabledlist = core\plugininfo\availability::get_enabled_plugins();
foreach ($plugins as $plugin => $name) {
// Get version or ? if unknown.
$version = get_config('availability_' . $plugin);
if (!empty($version->version)) {
$version = $version->version;
} else {
$version = '?';
}
// Get enabled status and use to grey out name if necessary.
$enabled = in_array($plugin, $enabledlist);
if ($enabled) {
$enabledaction = 'hide';
$enabledstr = get_string('hide');
$class = '';
} else {
$enabledaction = 'show';
$enabledstr = get_string('show');
$class = 'dimmed_text';
}
// Make enable control. This is a POST request (using a form control rather
// than just a link) because it makes a database change.
$params = array('sesskey' => sesskey(), 'plugin' => $plugin, 'action' => $enabledaction);
$url = new moodle_url('/' . $CFG->admin . '/tool/availabilityconditions/', $params);
$enablecontrol = html_writer::link($url, $OUTPUT->pix_icon('t/' . $enabledaction, $enabledstr));
$table->add_data([$name, $version, $enablecontrol], $class);
}
$table->print_html();
$table = new \core_admin\table\availability_management_table();
$table->out();
echo $OUTPUT->footer();

View File

@ -22,6 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['defaultdisplaymode'] = 'Default display mode';
$string['manageplugins'] = 'Manage restrictions';
$string['pluginname'] = 'Availability condition management';
$string['privacy:metadata'] = 'The Availability condition management plugin does not store any personal data.';

View File

@ -44,16 +44,16 @@ Feature: Manage availability conditions
# Check the icon is there (it should be a Hide icon, meaning is currently visible).
Then "Hide" "icon" should exist in the "Restriction by date" "table_row"
# Click the icon. It should toggle to hidden (title=Show).
And I click on "Hide" "icon" in the "Restriction by date" "table_row"
And "Show" "icon" should exist in the "Restriction by date" "table_row"
# Click the icon. It should toggle to disabled.
And I toggle the "Disable Restriction by date" admin switch "off"
And I should see "Restriction by date disabled."
# Toggle it back to visible (title=Hide).
And I click on "Show" "icon" in the "Restriction by date" "table_row"
And "Hide" "icon" should exist in the "Restriction by date" "table_row"
And I toggle the "Enable Restriction by date" admin switch "on"
And I should see "Restriction by date enabled."
# OK, toggling works. Set the grade one to Hide and we'll go see if it actually worked.
And I click on "Hide" "icon" in the "Restriction by grade" "table_row"
And I toggle the "Disable Restriction by grade" admin switch "off"
And I am on the "P1" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"

View File

@ -124,9 +124,12 @@ abstract class frontend {
$modules[] = 'moodle-' . $component . '-form';
// Get parameters for this plugin.
$componentparams->{$plugin} = array($component,
$frontend->allow_add($course, $cm, $section),
$frontend->get_javascript_init_params($course, $cm, $section));
$componentparams->{$plugin} = [
$component,
$frontend->allow_add($course, $cm, $section),
$frontend->get_javascript_init_params($course, $cm, $section),
get_config('availability_' . $plugin, 'defaultdisplaymode'),
];
// Include strings for this plugin.
$identifiers = $frontend->get_javascript_strings();

View File

@ -164,3 +164,20 @@ Feature: Display availability for activities and sections
# Section 2 should not be available at all
And I should not see "Section 2" in the "region-main" "region"
@javascript
Scenario: Change default display for in manage restriction, then check eye icon by add restriction access to activity.
Given I log in as "admin"
When I navigate to "Plugins > Availability restrictions > Manage restrictions" in site administration
# Change value for display eye.
And I click on "Hide" "icon" in the ".display-mode-date" "css_element"
And I am on the "Page 1" "page activity editing" page logged in as "teacher1"
And I expand all fieldsets
And I press "Add restriction..."
And I click on "Date" "button" in the "Add restriction..." "dialogue"
Then the "alt" attribute of ".availability-item .availability-eye img" "css_element" should contain "Hidden entirely"
And I click on ".availability-item .availability-delete img" "css_element"
And I should not see "Date" in the "Restrict access" "fieldset"
And I press "Add restriction..."
And I click on "Grade" "button" in the "Add restriction..." "dialogue"
And the "alt" attribute of ".availability-item .availability-eye img" "css_element" should contain "Displayed if student"

View File

@ -559,4 +559,62 @@ class info_test extends \advanced_testcase {
$CFG->enableavailability = 1;
$this->assertFalse($info->is_available_for_all());
}
/**
* Test update_display_mode function.
*
* @covers \core\plugininfo\availability::update_display_mode
* @dataProvider update_display_mode_provider
*
* @param string $plugin The plugin name.
* @param string $expected The expected data.
*/
public function test_update_display_mode(string $plugin, string $expected): void {
global $DB;
$this->resetAfterTest();
// Get default value for default display mode.
$availabilityvalue = $DB->get_field('config_plugins', 'value',
['name' => 'defaultdisplaymode', 'plugin' => "availability_$plugin"]);
$updatedisplaymode = \core\plugininfo\availability::update_display_mode($plugin, true);
// The default value is not inserted into the table.
// Or the display is updated but the display mode is the same value as the default.
$this->assertFalse($availabilityvalue);
$this->assertFalse($updatedisplaymode);
// Update display mode for plugins.
$updatedisplaymode = \core\plugininfo\availability::update_display_mode($plugin, false);
// The function should return true because the display mode value has changed.
$this->assertTrue($updatedisplaymode);
// Get the updated value for the default display mode.
$availabilityvalue = $DB->get_field('config_plugins', 'value',
['name' => 'defaultdisplaymode', 'plugin' => "availability_$plugin"]);
$this->assertEquals($expected, $availabilityvalue);
}
/**
* Data provider for test_update_display_mode().
*
* @return array
*/
public static function update_display_mode_provider(): array {
return [
'Update display mode for completion' => [
'completion',
'1',
],
'Update display mode for grade' => [
'grade',
'1',
],
'Update display mode for group' => [
'group',
'1',
],
];
}
}

View File

@ -274,13 +274,17 @@ M.core_availability.plugin = {
* overridden by child plugin.
*
* @method init
* @param {String} component Component name e.g. 'availability_date'
* @param {String} component Component name e.g. 'availability_date'.
* @param {boolean} allowAdd Indicates whether adding new instances of the plugin is permitted.
* @param {Object} params Additional parameters.
* @param {boolean} displayMode Whether the eye icon is show or hide. True for "Hide", false for "Show".
*/
init: function(component, allowAdd, params) {
init: function(component, allowAdd, params, displayMode) {
var name = component.replace(/^availability_/, '');
this.allowAdd = allowAdd;
M.core_availability.form.plugins[name] = this;
this.initInner.apply(this, params);
this.displayMode = displayMode;
},
/**
@ -778,12 +782,17 @@ M.core_availability.List.prototype.clickAdd = function() {
M.core_availability.List.prototype.getAddHandler = function(type, dialogRef) {
return function() {
var newItem;
var displayMode = true;
// Check if we have changed the eye icon in the manage restriction to hidden.
if (type && M.core_availability.form.plugins[type].displayMode) {
displayMode = false;
}
if (type) {
// Create an Item object to represent the child.
newItem = new M.core_availability.Item({type: type, creating: true}, this.root);
newItem = new M.core_availability.Item({type: type, creating: true, showc: displayMode}, this.root);
} else {
// Create a new List object to represent the child.
newItem = new M.core_availability.List({c: [], showc: true}, false, this.root);
newItem = new M.core_availability.List({c: [], showc: displayMode}, false, this.root);
}
// Add to list.
this.addChild(newItem);

File diff suppressed because one or more lines are too long

View File

@ -274,13 +274,17 @@ M.core_availability.plugin = {
* overridden by child plugin.
*
* @method init
* @param {String} component Component name e.g. 'availability_date'
* @param {String} component Component name e.g. 'availability_date'.
* @param {boolean} allowAdd Indicates whether adding new instances of the plugin is permitted.
* @param {Object} params Additional parameters.
* @param {boolean} displayMode Whether the eye icon is show or hide. True for "Hide", false for "Show".
*/
init: function(component, allowAdd, params) {
init: function(component, allowAdd, params, displayMode) {
var name = component.replace(/^availability_/, '');
this.allowAdd = allowAdd;
M.core_availability.form.plugins[name] = this;
this.initInner.apply(this, params);
this.displayMode = displayMode;
},
/**
@ -778,12 +782,17 @@ M.core_availability.List.prototype.clickAdd = function() {
M.core_availability.List.prototype.getAddHandler = function(type, dialogRef) {
return function() {
var newItem;
var displayMode = true;
// Check if we have changed the eye icon in the manage restriction to hidden.
if (type && M.core_availability.form.plugins[type].displayMode) {
displayMode = false;
}
if (type) {
// Create an Item object to represent the child.
newItem = new M.core_availability.Item({type: type, creating: true}, this.root);
newItem = new M.core_availability.Item({type: type, creating: true, showc: displayMode}, this.root);
} else {
// Create a new List object to represent the child.
newItem = new M.core_availability.List({c: [], showc: true}, false, this.root);
newItem = new M.core_availability.List({c: [], showc: displayMode}, false, this.root);
}
// Add to list.
this.addChild(newItem);

View File

@ -272,13 +272,17 @@ M.core_availability.plugin = {
* overridden by child plugin.
*
* @method init
* @param {String} component Component name e.g. 'availability_date'
* @param {String} component Component name e.g. 'availability_date'.
* @param {boolean} allowAdd Indicates whether adding new instances of the plugin is permitted.
* @param {Object} params Additional parameters.
* @param {boolean} displayMode Whether the eye icon is show or hide. True for "Hide", false for "Show".
*/
init: function(component, allowAdd, params) {
init: function(component, allowAdd, params, displayMode) {
var name = component.replace(/^availability_/, '');
this.allowAdd = allowAdd;
M.core_availability.form.plugins[name] = this;
this.initInner.apply(this, params);
this.displayMode = displayMode;
},
/**
@ -776,12 +780,17 @@ M.core_availability.List.prototype.clickAdd = function() {
M.core_availability.List.prototype.getAddHandler = function(type, dialogRef) {
return function() {
var newItem;
var displayMode = true;
// Check if we have changed the eye icon in the manage restriction to hidden.
if (type && M.core_availability.form.plugins[type].displayMode) {
displayMode = false;
}
if (type) {
// Create an Item object to represent the child.
newItem = new M.core_availability.Item({type: type, creating: true}, this.root);
newItem = new M.core_availability.Item({type: type, creating: true, showc: displayMode}, this.root);
} else {
// Create a new List object to represent the child.
newItem = new M.core_availability.List({c: [], showc: true}, false, this.root);
newItem = new M.core_availability.List({c: [], showc: displayMode}, false, this.root);
}
// Add to list.
this.addChild(newItem);

View File

@ -82,6 +82,36 @@ class availability extends base {
return $haschanged;
}
/**
* Update the display mode for a specific plugin based on `$displaymode` parameter.
*
* @param string $pluginname The plugin name.
* @param bool $displaymode whether the eye icon of display mode is enabled or disabled.
* @return bool Returns true if the configuration has been changed, false otherwise.
*/
public static function update_display_mode(string $pluginname, bool $displaymode): bool {
$haschanged = false;
$plugin = 'availability_' . $pluginname;
$oldvalue = get_config($plugin, 'defaultdisplaymode');
$disabled = !$displaymode;
// Only set value if there is no config setting or if the value is different from the previous one.
if ($oldvalue == false && $disabled) {
set_config('defaultdisplaymode', $disabled, $plugin);
$haschanged = true;
} else if ($oldvalue != false && !$disabled) {
unset_config('defaultdisplaymode', $plugin);
$haschanged = true;
}
if ($haschanged) {
add_to_config_log('defaultdisplaymode', $oldvalue, $disabled, $plugin);
\core_plugin_manager::reset_caches();
}
return $haschanged;
}
/**
* Defines if there should be a way to uninstall the plugin via the administration UI.
*