Skip to content

Commit

Permalink
group-memberadd for moodle 4.1^ fix #443
Browse files Browse the repository at this point in the history
  • Loading branch information
mi7chal committed Sep 2, 2024
1 parent 382418f commit 869c41d
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 11 deletions.
167 changes: 167 additions & 0 deletions Moosh/Command/Moodle41/Group/GroupMemberAdd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
/**
* moosh - Moodle Shell
*
* @copyright 2016 onwards Tomasz Muras
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace Moosh\Command\Moodle41\Group;

use Moosh\MooshCommand;

/**
* Adds member to the specified group. It may be done using member username or id. If course id is specified addition
* is based on usernames, otherwise on user ids.
* {@code moosh group-memberadd [-g, --group] [-c, --course] <username|id>}
*
* @example 1: Adds user with id 111 to the group with id 333.
* moosh group-memberadd -g 333 111
*
* @example 2: Adds users with ids 1, 2 and 3 to the group with id 333
* moosh group-memberadd -g 333 1 2 3
*
* @example 3: Adds user with username `example_username` enrolled in course with id 5 to the group with id 1
* moosh group-memberadd -g 1 -c 5 example_username
*
* @example 4: Adds users with usernames `example_username1`, `example_username2` enrolled in course with id 5
* to the group with id 1
* moosh group-memberadd -g 1 -c 5 example_username1, example_username2
*
* @package Moosh\Command\Moodle41\Group
* @author Michal Chruscielski <michalch775@gmail.com>
*/
class GroupMemberAdd extends MooshCommand
{
public function __construct()
{
parent::__construct('memberadd', 'group');

$this->addOption('g|group:', 'id of group');
$this->addOption('c|course:', 'id of course');

$this->addArgument('username');
$this->maxArguments = 255;

}

public function execute()
{
global $CFG, $DB;

require_once "$CFG->dirroot/group/lib.php";
require_once "$CFG->dirroot/user/lib.php";
require_once "$CFG->dirroot/enrol/externallib.php";

$options = $this->expandedOptions;
if (!empty($options['course'])) {
$courseId = $options['course'];

if($this->verbose) {
mtrace("Course id was specified. It means that users will be added by usernames.");
}
} else if($this->verbose) {
mtrace("Course id wasn't specified. It means that users will be added by ids.");
}

$groupId = $options['group'];

// we must now to which group we're adding
if(empty($groupId)) {
cli_error("Group id (--group) must not be empty!");
}

// Array with access to all loaded users by its usernames. Probably less readable than searching array,
// but more convenient and performant, probably.
$usersByUsername = array();

// If courseId provided we want to select users my their usernames, otherwise by ids.
if (!empty($courseId)) {
if($this->verbose) {
mtrace("Loading users for course id $courseId.");
}

try {
$users = \core_enrol_external::get_enrolled_users($courseId);
} catch(\dml_exception $e) {
if($e->errorcode === "invalidrecord") {
cli_error("Course with id $courseId does not exist.");
} else {
cli_error("Course users can't be found. Use --verbose for more info.");
}

// suppresses IDE warnings, exit breaks execution anyway
exit();
}

if($this->verbose) {
$usersSize = count($users);
mtrace("Loaded $usersSize users.");
}

foreach ($users as $user) {
$usersByUsername[$user['username']] = $user;
}
}

$addedUsers = [];
foreach ($this->arguments as $argument) {

$this->expandOptionsManually(array($argument));

if (!empty($courseId) && count($usersByUsername) > 0) {
$expectedUsername = $argument;

if(!array_key_exists($expectedUsername, $usersByUsername)) {
print("User with username $expectedUsername do not belong to course with id $courseId. Skipping.\n");
continue;
}

if($this->verbose) {
mtrace("User with username $expectedUsername belongs to selected course. Attempting addition to group.");
}

$user = $usersByUsername[$expectedUsername];

$userAdded = \groups_add_member($groupId, $user["id"]);
if ($userAdded) {
$addedUsers[] = $user;
$username = $user["username"];
print("User with username: $username successfully added.\n");
} else {
print("User with username: $expectedUsername wasn't added to the selected group. Skipping.\n");
}
} else {
try {
$user = $DB->get_record('user', array('id' => $argument), '*', MUST_EXIST);
} catch(\Exception $e) {
print("User with id $argument can't be found. Skipping.\n");
continue;
}

if($this->verbose) {
mtrace("User with id $argument found. Attempting addition to the group.");
}

$userAdded = \groups_add_member($groupId, $argument);

if ($userAdded) {
print("User with id: $argument successfully added.\n");
$addedUsers[] = $user;
} else {
print("User with id: $argument wasn't added to the selected group. Skipping.\n");
}
}
}

$addedUsersCount = count($addedUsers);

// Adding a bit of space
print("\n");
if($addedUsersCount > 0) {
print("Added $addedUsersCount users to group with id $groupId.\n");
} else {
print("No user was added to the selected group. Run command with --verbose for more info.\n");
}
}
}
12 changes: 6 additions & 6 deletions moosh.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ function cli_error($text, $errorcode = 1) {
$subcommand = NULL;
$possible_matches = array();

if (!$parser->isEnd()) {
$subcommand = $parser->advance();
if (!@$parser->isEnd()) {
$subcommand = @$parser->advance();
}


Expand Down Expand Up @@ -153,16 +153,16 @@ function cli_error($text, $errorcode = 1) {
exit(10);
}

$parser->setSpecs($subcommand_specs[$subcommand]);
@$parser->setSpecs($subcommand_specs[$subcommand]);
try {
$subcommand_options[$subcommand] = $parser->continueParse();
$subcommand_options[$subcommand] = @$parser->continueParse();
} catch (Exception $e) {
echo $e->getMessage() . "\n";
die("Moosh global options should be passed before command not after it.\n");
}

while (!$parser->isEnd()) {
$arguments[] = $parser->advance();
while (!@$parser->isEnd()) {
$arguments[] = @$parser->advance();
}

// Read config file if available.
Expand Down
34 changes: 29 additions & 5 deletions www/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1657,16 +1657,40 @@ Example 2:
group-memberadd
---------------

Add a member to a group.
Add member to a specified group. It may be done using member username or id. If course id is specified, addition
is based on username(s), otherwise on user id(s).

Example 1:

moosh group-memberadd -c courseid -g groupid membername1 [membername2] ...
Available options:

Example 2:
| Option | Description |
|--------------|-------------|
| -g, --group | Group id. |
| -c, --course | Course id. |

Command syntax (by user id):

moosh group-memberadd -g groupid memberid1 [memberid2] ...

Command syntax (by user username)

moosh group-memberadd -g groupid memberid1 [memberid2] ...

Example 1: Add user with id 111 to the group with id 333.

moosh group-memberadd -g 333 111

Example 2: Add users with ids 1, 2 and 3 to the group with id 333

moosh group-memberadd -g 333 1 2 3

Example 3: Add user with username `example_username` enrolled in course with id 5 to the group with id 1

moosh group-memberadd -g 1 -c 5 example_username

Example 4: Add users with usernames `example_username1`, `example_username2` enrolled in course with id 5
to the group with id 1

moosh group-memberadd -g 1 -c 5 example_username1, example_username2

grouping-create
---------------
Expand Down

0 comments on commit 869c41d

Please sign in to comment.