Skip to content

Added new commands: addrolemember and removerolemember #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions classes/command_groupsadd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?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/>.

/**
* Workflow script command to add role members to groups.
*
* @package block_workflow
* @copyright 2021 Takayuki Nagai, Center for Information Science, Kyoto Institute of Technology
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');

/**
* The command to add role members to a list of groups identified by idnumbers
*
* @package block
* @subpackage workflow
* @copyright 2021 Takayuki Nagai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
class block_workflow_command_groupsadd extends block_workflow_command {
/**
* Validate the syntax of this line, and ensure that it is correct for
* this context
* @param String args The list of arguments to the command
* @param stdClass state The state object for this step_state
* @throws block_workflow_invalid_command_exception
* @return stdClass data An object containing the validated data
* which will be used for execution
*
* Exceptions are thrown if:
* * an invalid role is specified for the newrole; or
* * an invalid role is specified for any of the role assignments.
*
* Note: No exception is thrown if there are no users with specified role.
*/
public function parse($args, $step, $state = null) {
// We'll return the components in an object.
$data = new stdClass();
$data->errors = array();

if ($state) {
$data->context = $state->context();
}

// Break down the line. It should be in the format
// role from idnumber1 idnumber2 ... idnumberN
// with any number of group idnumbers.
$line = preg_split('/[\s+]/', $args);

// Grab the role name.
$data->role = parent::require_role_exists(array_shift($line), $data->errors);

// Shift off the 'to' component.
$to = array_shift($line);
if ($to !== 'to') {
$data->errors[] = get_string('invalidsyntaxmissingto', 'block_workflow');
return $data;
}

// Check whether the specified groups exist and fill the list of target users.
$data->groups = array();
$data->users = array();

if($state) {
// Get users who has the specified role.
$thisrole = $data->role;
if ($thisrole) {
// We can only get the list of users if we've got a specific context.
$data->users = array_merge($data->users, parent::role_users($thisrole, $data->context));
}

$courseid = context::instance_by_id($state->contextid)->get_course_context()->instanceid;

// Check that each group with the specified idnumber exists
foreach ($line as $idnumber) {
// Check that the group exists.

// (courseid,idnumber) -> (group)
if($thisgroup = groups_get_group_by_idnumber($courseid,$idnumber)) {
$data->groups[] = $thisgroup;
} else {
$data->errors[] = get_string('invalididnumber', 'block_workflow');
}
}

// Check that some groups were specified.
if (count($data->groups) <= 0) {
$data->errors[] = get_string('nogroupsspecified', 'block_workflow');
return $data;
}
}

return $data;
}

/**
* Execute the command given the line of arguments and state of the
* step.
*
* Validation is automatically performed before continuing.
* @param String args The list of arguments to the command
* @param stdClass state The state object for this step_state
*/
public function execute($args, $state) {
global $CFG;
require_once("$CFG->dirroot/group/lib.php");

$data = $this->parse($args, $state->step(), $state);
foreach ($data->groups as $group) {
foreach ($data->users as $user){
groups_add_member($group->id,$user->id,'block_workflow', $state->id);
}
}
}
}
131 changes: 131 additions & 0 deletions classes/command_groupsremove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?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/>.

/**
* Workflow script command to remove role members from groups.
*
* @package block_workflow
* @copyright 2021 Takayuki Nagai, Center for Information Science, Kyoto Institute of Technology
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');

/**
* The command to remove role members from a list of groups identified by idnumbers
*
* @package block
* @subpackage workflow
* @copyright 2021 Takayuki Nagai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
class block_workflow_command_groupsremove extends block_workflow_command {
/**
* Validate the syntax of this line, and ensure that it is correct for
* this context
* @param String args The list of arguments to the command
* @param stdClass state The state object for this step_state
* @throws block_workflow_invalid_command_exception
* @return stdClass data An object containing the validated data
* which will be used for execution
*
* Exceptions are thrown if:
* * an invalid role is specified for the role; or
* * an invalid group is specified for any of the group assignments.
*
* Note: No exception is thrown if there are no users with specified role.
*/
public function parse($args, $step, $state = null) {
// We'll return the components in an object.
$data = new stdClass();
$data->errors = array();

if ($state) {
$data->context = $state->context();
}

// Break down the line. It should be in the format
// role from idnumber1 idnumber2 ... idnumberN
// with any number of group idnumbers.
$line = preg_split('/[\s+]/', $args);

// Grab the role name.
$data->role = parent::require_role_exists(array_shift($line), $data->errors);

// Shift off the 'from' component.
$from = array_shift($line);
if ($from !== 'from') {
$data->errors[] = get_string('invalidsyntaxmissingfrom', 'block_workflow');
return $data;
}

// Check whether the specified groups exist and fill the list of target users.
$data->groups = array();
$data->users = array();

if($state) {
// Get users who has the specified role.
$thisrole = $data->role;
if ($thisrole) {
// We can only get the list of users if we've got a specific context.
$data->users = array_merge($data->users, parent::role_users($thisrole, $data->context));
}

$courseid = context::instance_by_id($state->contextid)->get_course_context()->instanceid;

// Check that each group with the specified idnumber exists
foreach ($line as $idnumber) {
// Check that the group exists.

// (courseid,idnumber) -> (group)
if($thisgroup = groups_get_group_by_idnumber($courseid,$idnumber)) {
$data->groups[] = $thisgroup;
} else {
$data->errors[] = get_string('invalididnumber', 'block_workflow');
}
}

// Check that some groups were specified.
if (count($data->groups) <= 0) {
$data->errors[] = get_string('nogroupsspecified', 'block_workflow');
return $data;
}
}

return $data;
}

/**
* Execute the command given the line of arguments and state of the
* step.
*
* Validation is automatically performed before continuing.
* @param String args The list of arguments to the command
* @param stdClass state The state object for this step_state
*/
public function execute($args, $state) {
global $CFG;
require_once("$CFG->dirroot/group/lib.php");

$data = $this->parse($args, $state->step(), $state);
foreach ($data->groups as $group) {
foreach ($data->users as $user){
groups_remove_member($group->id,$user->id,'block_workflow', $state->id);
}
}
}
}
2 changes: 2 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
require_once($CFG->dirroot . '/blocks/workflow/classes/command.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_assignrole.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_email.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_groupsadd.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_groupsremove.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_override.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_setactivitysetting.php');
require_once($CFG->dirroot . '/blocks/workflow/classes/command_setactivityvisibility.php');
Expand Down