Skip to content

Commit

Permalink
Merge branch 'moodle:main' into solrrag
Browse files Browse the repository at this point in the history
  • Loading branch information
mhughes2k authored Mar 21, 2024
2 parents 8a6ce11 + 39b8e19 commit b425e48
Show file tree
Hide file tree
Showing 46 changed files with 656 additions and 183 deletions.
41 changes: 41 additions & 0 deletions admin/tool/mfa/classes/local/hooks/extend_bulk_user_actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 tool_mfa\local\hooks;

/**
* Extend user bulk actions menu
*
* @package tool_mfa
* @copyright 2024 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class extend_bulk_user_actions {

/**
* Add action to reset MFA factors
*
* @param \core_user\hook\extend_bulk_user_actions $hook
*/
public static function callback(\core_user\hook\extend_bulk_user_actions $hook): void {
if (has_capability('moodle/site:config', \context_system::instance())) {
$hook->add_action('tool_mfa_reset_factors', new \action_link(
new \moodle_url('/admin/tool/mfa/reset_factor.php'),
get_string('resetfactor', 'tool_mfa')
));
}
}
}
33 changes: 33 additions & 0 deletions admin/tool/mfa/db/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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/>.

/**
* Hook callbacks for Multi-factor authentication
*
* @package tool_mfa
* @copyright 2024 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$callbacks = [
[
'hook' => core_user\hook\extend_bulk_user_actions::class,
'callback' => 'tool_mfa\local\hooks\extend_bulk_user_actions::callback',
'priority' => 0,
],
];
17 changes: 0 additions & 17 deletions admin/tool/mfa/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,6 @@ function tool_mfa_after_config(): void {
}
}

/**
* Any plugin typically an admin tool can add new bulk user actions
*
* @return array
*/
function tool_mfa_bulk_user_actions(): array {
if (!has_capability('moodle/site:config', context_system::instance())) {
return [];
}
return [
'tool_mfa_reset_factors' => new action_link(
new moodle_url('/admin/tool/mfa/reset_factor.php'),
get_string('resetfactor', 'tool_mfa')
),
];
}

/**
* Serves any files for the guidance page.
*
Expand Down
63 changes: 46 additions & 17 deletions admin/user/user_bulk_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,33 @@ class user_bulk_action_form extends moodleform {
/** @var bool */
protected $hasbulkactions = false;

/** @var array|null */
protected $actions = null;

/**
* Returns an array of action_link's of all bulk actions available for this user.
*
* @param bool $flatlist whether to return a flat list (for easier searching) or a list with
* option groups that can be used to build a select element
* @return array of action_link objects
*/
public function get_actions(): array {
public function get_actions(bool $flatlist = true): array {
if ($this->actions === null) {
$this->actions = $this->build_actions();
$this->hasbulkactions = !empty($this->actions);
}
if ($flatlist) {
return array_reduce($this->actions, fn($carry, $item) => $carry + $item, []);
}
return $this->actions;
}

/**
* Builds the list of bulk user actions available for this user.
*
* @return array
*/
protected function build_actions(): array {

global $CFG;

Expand Down Expand Up @@ -89,28 +110,36 @@ public function get_actions(): array {
get_string('bulkadd', 'core_cohort'));
}

// Any plugin can append actions to this list by implementing a callback
// <component>_bulk_user_actions() which returns an array of action_link.
// Each new action's key should have a frankenstyle prefix to avoid clashes.
// See MDL-38511 for more details.
$moreactions = get_plugins_with_function('bulk_user_actions', 'lib.php');
// Collect all bulk user actions.
$hook = new \core_user\hook\extend_bulk_user_actions();

// Add actions from core.
foreach ($actions as $identifier => $action) {
$hook->add_action($identifier, $action);
}

// Add actions from the legacy callback 'bulk_user_actions'.
$moreactions = get_plugins_with_function('bulk_user_actions', 'lib.php', true, true);
foreach ($moreactions as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$actions += $pluginfunction();
$pluginactions = $pluginfunction();
foreach ($pluginactions as $identifier => $action) {
$hook->add_action($identifier, $action);
}
}
}

// Any plugin can append user bulk actions to this list by implementing a hook callback.
\core\hook\manager::get_instance()->dispatch($hook);

// This method may be called from 'Bulk actions' and 'Browse user list' pages. Some actions
// may be irrelevant in one of the contexts and they can be excluded by specifying the
// 'excludeactions' customdata.
$excludeactions = $this->_customdata['excludeactions'] ?? [];
foreach ($excludeactions as $excludeaction) {
unset($actions[$excludeaction]);
$hook->add_action($excludeaction, null);
}

$this->hasbulkactions = !empty($actions);
return $actions;

return $hook->get_actions();
}

/**
Expand All @@ -131,13 +160,13 @@ public function definition() {
$mform->addElement('hidden', 'userids');
$mform->setType('userids', PARAM_SEQUENCE);

$actions = [0 => get_string('choose') . '...'];
$bulkactions = $this->get_actions();
foreach ($bulkactions as $key => $action) {
$actions[$key] = $action->text;
$actions = ['' => [0 => get_string('choose') . '...']];
$bulkactions = $this->get_actions(false);
foreach ($bulkactions as $category => $categoryactions) {
$actions[$category] = array_map(fn($action) => $action->text, $categoryactions);
}
$objs = array();
$objs[] = $selectel = $mform->createElement('select', 'action', get_string('userbulk', 'admin'), $actions);
$objs[] = $selectel = $mform->createElement('selectgroups', 'action', get_string('userbulk', 'admin'), $actions);
$selectel->setHiddenLabel(true);
if (empty($this->_customdata['hidesubmit'])) {
$objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));
Expand Down
39 changes: 38 additions & 1 deletion badges/classes/reportbuilder/local/entities/badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
use moodle_url;
use stdClass;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\filters\{select, text};
use core_reportbuilder\local\filters\{date, select, text};
use core_reportbuilder\local\helpers\database;
use core_reportbuilder\local\report\{column, filter};

defined('MOODLE_INTERNAL') or die;
Expand Down Expand Up @@ -283,6 +284,8 @@ protected function get_all_columns(): array {
* @return filter[]
*/
protected function get_all_filters(): array {
global $DB;

$badgealias = $this->get_table_alias('badge');

// Name.
Expand All @@ -295,6 +298,16 @@ protected function get_all_filters(): array {
))
->add_joins($this->get_joins());

// Version.
$filters[] = (new filter(
text::class,
'version',
new lang_string('version', 'core_badges'),
$this->get_entity_name(),
"{$badgealias}.version"
))
->add_joins($this->get_joins());

// Status.
$filters[] = (new filter(
select::class,
Expand All @@ -312,6 +325,30 @@ protected function get_all_filters(): array {
BADGE_STATUS_ARCHIVED => new lang_string('badgestatus_4', 'core_badges'),
]);

// Expiry date/period.
[$parammaxint, $paramtime] = database::generate_param_names(2);
$filters[] = (new filter(
date::class,
'expiry',
new lang_string('expirydate', 'core_badges'),
$this->get_entity_name(),
"CASE WHEN {$badgealias}.expiredate IS NULL AND {$badgealias}.expireperiod IS NULL
THEN " . $DB->sql_cast_char2int(":{$parammaxint}") . "
ELSE COALESCE({$badgealias}.expiredate, {$badgealias}.expireperiod + :{$paramtime})
END",
[$parammaxint => 2147483647, $paramtime => time()]
))
->add_joins($this->get_joins())
->set_limited_operators([
date::DATE_ANY,
date::DATE_RANGE,
date::DATE_LAST,
date::DATE_CURRENT,
date::DATE_NEXT,
date::DATE_PAST,
date::DATE_FUTURE,
]);

// Type.
$filters[] = (new filter(
select::class,
Expand Down
3 changes: 3 additions & 0 deletions badges/classes/reportbuilder/local/systemreports/badges.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function add_columns(badge $badgeentity): void {
$columns = [
'badge:image',
'badge:namewithlink',
'badge:version',
'badge:status',
'badge:criteria',
];
Expand Down Expand Up @@ -148,7 +149,9 @@ public function add_columns(badge $badgeentity): void {
protected function add_filters(): void {
$filters = [
'badge:name',
'badge:version',
'badge:status',
'badge:expiry',
];
$this->add_filters_from_entities($filters);
}
Expand Down
36 changes: 29 additions & 7 deletions badges/tests/behat/manage_badges.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Feature: Manage badges
Given the following "core_badges > Badge" exists:
| name | Badge #1 |
| status | 0 |
| version | 1 |
| version | 1.0 |
| language | en |
| description | Test badge description |
| image | badges/tests/behat/badge.png |
Expand All @@ -23,20 +23,22 @@ Feature: Manage badges
And I press "Save changes"
And I click on "Back" "button"
Then the following should exist in the "reportbuilder-table" table:
| Name | Badge status |
| Badge #1 | Not available |
| Copy of Badge #1 | Not available |
| Name | Version | Badge status |
| Badge #1 | 1.0 | Not available |
| Copy of Badge #1 | 1.0 | Not available |

Scenario: Edit a badge
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
And I press "Edit" action in the "Badge #1" report row
And I set the field "Name" to "New Badge #1"
And I set the following fields to these values:
| Name | New Badge #1 |
| Version | 1.1 |
And I press "Save changes"
And I click on "Back" "button"
Then the following should exist in the "reportbuilder-table" table:
| Name | Badge status |
| New Badge #1 | Not available |
| Name | Version | Badge status |
| New Badge #1 | 1.1 | Not available |

Scenario: Delete a badge
Given I log in as "admin"
Expand All @@ -45,6 +47,26 @@ Feature: Manage badges
And I press "Delete and remove existing issued badges"
Then I should see "There are currently no badges available for users to earn"

Scenario Outline: Filter managed badges
Given the following "core_badges > Badges" exist:
| name | status | version |
| Badge #2 | 1 | 2.0 |
And I log in as "admin"
When I navigate to "Badges > Manage badges" in site administration
And I click on "Filters" "button"
And I set the following fields in the "<filter>" "core_reportbuilder > Filter" to these values:
| <filter> operator | Is equal to |
| <filter> value | <value> |
And I click on "Apply" "button" in the "[data-region='report-filters']" "css_element"
Then I should see "Filters applied"
And I should see "Badge #1" in the "Badges" "table"
And I should not see "Badge #2" in the "Badges" "table"
Examples:
| filter | value |
| Name | Badge #1 |
| Version | 1.0 |
| Badge status | Not available |

Scenario: Enable and disable access to a badge
Given I log in as "admin"
And I navigate to "Badges > Manage badges" in site administration
Expand Down
17 changes: 17 additions & 0 deletions badges/tests/reportbuilder/datasource/badges_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ public function datasource_filters_provider(): array {
'badge:name_operator' => text::IS_EQUAL_TO,
'badge:name_value' => 'Other badge',
], false],
'Filter badge version' => ['badge:version', [
'badge:version_operator' => text::IS_EQUAL_TO,
'badge:version_value' => '2.0',
], true],
'Filter badge version (no match)' => ['badge:version', [
'badge:version_operator' => text::IS_EQUAL_TO,
'badge:version_value' => '1.0',
], false],
'Filter badge status' => ['badge:status', [
'badge:status_operator' => select::EQUAL_TO,
'badge:status_value' => BADGE_STATUS_ACTIVE_LOCKED,
Expand All @@ -224,6 +232,14 @@ public function datasource_filters_provider(): array {
'badge:status_operator' => select::EQUAL_TO,
'badge:status_value' => BADGE_STATUS_ACTIVE,
], false],
'Filter badge expiry' => ['badge:expiry', [
'badge:expiry_operator' => date::DATE_RANGE,
'badge:expiry_from' => 1622502000,
], true],
'Filter badge expiry (no match)' => ['badge:expiry', [
'badge:expiry_operator' => date::DATE_RANGE,
'badge:expiry_to' => 1622502000,
], false],
'Filter badge type' => ['badge:type', [
'badge:type_operator' => select::EQUAL_TO,
'badge:type_value' => BADGE_TYPE_COURSE,
Expand Down Expand Up @@ -307,6 +323,7 @@ public function test_datasource_filters(string $filtername, array $filtervalues,
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
$badge = $generator->create_badge([
'name' => 'Course badge',
'version' => '2.0',
'type' => BADGE_TYPE_COURSE,
'courseid' => $course->id,
'expireperiod' => HOURSECS,
Expand Down
Loading

0 comments on commit b425e48

Please sign in to comment.