Skip to content

Commit

Permalink
Issue #88 Add new capabilities for mass functionalities (#94)
Browse files Browse the repository at this point in the history
* Issue #88 Add new capabilities for mass functionalities
  • Loading branch information
TomoTsuyuki authored and Syxton committed Dec 16, 2024
1 parent 5f2e1e8 commit 1e8b28a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 14 deletions.
19 changes: 16 additions & 3 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
require_login();

// Check capability.
$context = context_block::instance($instanceid);
require_capability('block/massaction:use', $context);
$blockcontext = context_block::instance($instanceid);
require_capability('block/massaction:use', $blockcontext);

$data = block_massaction\massactionutils::extract_modules_from_json($massactionrequest);
$modulerecords = $data->modulerecords;

$context = $context->get_course_context();
$context = $blockcontext->get_course_context();
// Dispatch the submitted action.

// Redirect to course by default.
Expand All @@ -53,22 +53,27 @@
switch ($data->action) {
case 'moveleft':
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:indent', $blockcontext);
block_massaction\actions::adjust_indentation($modulerecords, -1);
break;
case 'moveright':
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:indent', $blockcontext);
block_massaction\actions::adjust_indentation($modulerecords, 1);
break;
case 'hide':
require_capability('moodle/course:activityvisibility', $context);
require_capability('block/massaction:activityshowhide', $blockcontext);
block_massaction\actions::set_visibility($modulerecords, false);
break;
case 'show':
require_capability('moodle/course:activityvisibility', $context);
require_capability('block/massaction:activityshowhide', $blockcontext);
block_massaction\actions::set_visibility($modulerecords, true);
break;
case 'makeavailable':
require_capability('moodle/course:activityvisibility', $context);
require_capability('block/massaction:activityshowhide', $blockcontext);
if (empty($CFG->allowstealth)) {
throw new invalid_parameter_exception('The "makeavailable" action is deactivated.');
}
Expand All @@ -77,6 +82,7 @@
case 'duplicate':
require_capability('moodle/backup:backuptargetimport', $context);
require_capability('moodle/restore:restoretargetimport', $context);
require_capability('block/massaction:duplicate', $blockcontext);
if (get_config('block_massaction', 'duplicatemaxactivities') < count($modulerecords)) {
$duplicatetask = new duplicate_task();
$duplicatetask->set_userid($USER->id);
Expand All @@ -90,6 +96,7 @@
break;
case 'delete':
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:delete', $blockcontext);
if (!$deletionconfirmed) {
$redirect = false;
block_massaction\actions::print_deletion_confirmation($modulerecords, $massactionrequest, $instanceid, $returnurl);
Expand All @@ -99,21 +106,25 @@
break;
case 'showdescription':
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:descriptionshowhide', $blockcontext);
block_massaction\actions::show_description($modulerecords, true);
break;
case 'hidedescription':
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:descriptionshowhide', $blockcontext);
block_massaction\actions::show_description($modulerecords, false);
break;
case 'contentchangednotification':
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:sendcontentchangednotifications', $blockcontext);
block_massaction\actions::send_content_changed_notifications($modulerecords);
break;
case 'moveto':
if (!isset($data->moveToTarget)) {
throw new moodle_exception('missingparam', 'block_massaction');
}
require_capability('moodle/course:manageactivities', $context);
require_capability('block/massaction:movetosection', $blockcontext);
block_massaction\actions::perform_moveto($modulerecords, $data->moveToTarget);
break;
case 'duplicateto':
Expand All @@ -122,6 +133,7 @@
}
require_capability('moodle/backup:backuptargetimport', $context);
require_capability('moodle/restore:restoretargetimport', $context);
require_capability('block/massaction:movetosection', $blockcontext);
if (get_config('block_massaction', 'duplicatemaxactivities') < count($modulerecords)) {
$duplicatetask = new duplicate_task();
$duplicatetask->set_userid($USER->id);
Expand Down Expand Up @@ -157,6 +169,7 @@

require_capability('moodle/backup:backuptargetimport', $context);
require_capability('moodle/restore:restoretargetimport', context_course::instance($targetcourseid));
require_capability('block/massaction:duplicatetocourse', $blockcontext);

$sectionselectform = new block_massaction\form\section_select_form(null, $options);
if ($sectionselectform->is_cancelled()) {
Expand Down
32 changes: 21 additions & 11 deletions block_massaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function get_content(): stdClass {

if ($this->page->user_is_editing()) {
$blockcontext = context_block::instance($this->instance->id);

if (!has_capability('block/massaction:use', $blockcontext)) {
$this->content->text = get_string('nopermissions', 'error', get_string('massaction:use', 'block_massaction'));
return $this->content;
Expand Down Expand Up @@ -131,7 +132,8 @@ public function get_content(): stdClass {
$context = context_course::instance($COURSE->id);
// Actions to be rendered later on.
$actionicons = [];
if (has_capability('moodle/course:activityvisibility', $context)) {
if (has_capability('moodle/course:activityvisibility', $context)
&& has_capability('block/massaction:activityshowhide', $blockcontext)) {
// As we want to use this symbol for the *operation*, not the state, we switch the icons hide/show.
$actionicons['show'] = 't/hide';
$actionicons['hide'] = 't/show';
Expand All @@ -140,24 +142,30 @@ public function get_content(): stdClass {
}
}
if (has_capability('moodle/backup:backuptargetimport', $context)
&& has_capability('moodle/restore:restoretargetimport', $context)) {
&& has_capability('moodle/restore:restoretargetimport', $context)
&& has_capability('block/massaction:duplicate', $blockcontext)) {
$actionicons['duplicate'] = 't/copy';
}
if (has_capability('moodle/backup:backuptargetimport', $context)) {
if (has_capability('moodle/backup:backuptargetimport', $context)
&& has_capability('block/massaction:duplicatetocourse', $blockcontext)) {
$actionicons['duplicatetocourse'] = 't/copy';
}
if (has_capability('moodle/course:manageactivities', $context)) {
$actionicons['delete'] = 't/delete';
if (course_get_format($COURSE->id)->uses_indentation()) {
if (has_capability('block/massaction:delete', $blockcontext)) {
$actionicons['delete'] = 't/delete';
}
if (course_get_format($COURSE->id)->uses_indentation()
&& has_capability('block/massaction:indent', $blockcontext)) {
// From Moodle 4.0 on the course format has to declare if it supports indentation or not.
$actionicons['moveright'] = 't/right';
$actionicons['moveleft'] = 't/left';
}
$actionicons['showdescription'] = 't/more';
$actionicons['hidedescription'] = 't/less';
if (has_capability('block/massaction:descriptionshowhide', $blockcontext)) {
$actionicons['showdescription'] = 't/more';
$actionicons['hidedescription'] = 't/less';
}
}
if (has_capability('block/massaction:sendcontentchangednotifications',
context_block::instance($this->instance->id))) {
if (has_capability('block/massaction:sendcontentchangednotifications', $blockcontext)) {
$actionicons['contentchangednotification'] = 't/email';
}

Expand All @@ -171,9 +179,11 @@ public function get_content(): stdClass {
['actions' => $actions, 'formaction' => $CFG->wwwroot . '/blocks/massaction/action.php',
'instanceid' => $this->instance->id, 'requesturi' => $_SERVER['REQUEST_URI'],
'helpicon' => $OUTPUT->help_icon('usage', 'block_massaction'),
'show_moveto_select' => has_capability('moodle/course:manageactivities', $context),
'show_moveto_select' => (has_capability('moodle/course:manageactivities', $context)
&& has_capability('block/massaction:movetosection', $context)),
'show_duplicateto_select' => (has_capability('moodle/backup:backuptargetimport', $context) &&
has_capability('moodle/restore:restoretargetimport', $context)),
has_capability('moodle/restore:restoretargetimport', $context)
&& has_capability('block/massaction:movetosection', $context)),
'sectionselecthelpicon' => $OUTPUT->help_icon('sectionselect', 'block_massaction')
]);
}
Expand Down
77 changes: 77 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,72 @@
'clonepermissionsfrom' => 'moodle/site:manageblocks'
],

'block/massaction:activityshowhide' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:duplicate' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:duplicatetocourse' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:delete' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:indent' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:descriptionshowhide' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:sendcontentchangednotifications' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
Expand All @@ -56,4 +122,15 @@
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],

'block/massaction:movetosection' => [
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
],
'clonepermissionsfrom' => 'moodle/course:manageactivities'
],
];
9 changes: 9 additions & 0 deletions lang/en/block_massaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@
*/

$string['pluginname'] = 'Mass Actions';

$string['massaction:activityshowhide'] = 'MassAction: Show/Hide modules';
$string['massaction:addinstance'] = 'Add a new Mass Actions Block';
$string['massaction:delete'] = 'MassAction: Delete modules';
$string['massaction:descriptionshowhide'] = 'MassAction: Show/Hide Descriptions';
$string['massaction:duplicate'] = 'MassAction: Duplicate modules';
$string['massaction:duplicatetocourse'] = 'MassAction: Duplicate to Course';
$string['massaction:indent'] = 'MassAction: Indent modules';
$string['massaction:movetosection'] = 'MassAction: Move to Section';
$string['massaction:use'] = 'Use the Mass Actions block';
$string['massaction:sendcontentchangednotifications'] = 'Send content changed notifications';

$string['blockname'] = 'Mass Actions';
$string['blocktitle'] = 'Mass Actions';
$string['privacy:metadata'] = 'This block only offers the possibility to apply standard operations on multiple course modules at the same time.
Expand Down

0 comments on commit 1e8b28a

Please sign in to comment.