From 869c41d6efc253b569cc59ad87095b447339149d Mon Sep 17 00:00:00 2001 From: Michal Chruscielski Date: Mon, 2 Sep 2024 14:59:56 +0200 Subject: [PATCH] group-memberadd for moodle 4.1^ fix #443 --- .../Command/Moodle41/Group/GroupMemberAdd.php | 167 ++++++++++++++++++ moosh.php | 12 +- www/commands/index.md | 34 +++- 3 files changed, 202 insertions(+), 11 deletions(-) create mode 100644 Moosh/Command/Moodle41/Group/GroupMemberAdd.php diff --git a/Moosh/Command/Moodle41/Group/GroupMemberAdd.php b/Moosh/Command/Moodle41/Group/GroupMemberAdd.php new file mode 100644 index 00000000..12707489 --- /dev/null +++ b/Moosh/Command/Moodle41/Group/GroupMemberAdd.php @@ -0,0 +1,167 @@ +} + * + * @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 + */ +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"); + } + } +} diff --git a/moosh.php b/moosh.php index 04b42990..4dc90c77 100755 --- a/moosh.php +++ b/moosh.php @@ -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(); } @@ -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. diff --git a/www/commands/index.md b/www/commands/index.md index efd2ba8b..d6369e3c 100755 --- a/www/commands/index.md +++ b/www/commands/index.md @@ -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 ---------------