In Totara - User Enrolment into Course as well as corresponding Group

Author - Pawandeep Kaur
27.09.2019
|
0 Comments
||

Totara – How to programmatically Enroll User into Course along with the corresponding Group

As a trainer or a manager, if you go to Gradebook screen you will see the dropdown of group names there, on which you can filter the gradebook records by selecting a group. Each group here corresponds to one course and on filtering on it, only those members are displayed in the grade book who are enrolled into the course associated with the group.

Now, if you want to programmatically create a custom group for a Course with a custom name, and want to add members to this group as well as enroll the group members into the course, how will you achieve it? This post explains the solution for this problem.

To add users to a course group, you need to enroll them in the same course. Totara does not allow course group privilege to users outside the course. So if you need to add users in a group, you need to enroll them first in the same course.

Only enrolled users may be group members. Therefore, Grades are stored only for enrolled users.

Enrollment Methods for enrolling the users into the course from front-end:

  • Manual enrolment – The administrator or course teacher adds users manually from Settings > Course administration > Users > Enrolled users.
  • Self enrolment – A user can enroll him/herself into a course by going through Settings > Course administration > “\enrol me in this course”
  • Course meta link – In this method, all users enrolled in one course to be automatically enrolled in one or more other courses.
  • Guest access – If users get enrolled by this method, he/she can view the contents of a course, not participate in the activities of a course.
  • Category enrolments – In which users get enrolled in all courses in a category.

Following is the code to enroll a user in the course and then add the same user in the group:

$context = \context_course::instance( < courseid > ); //get course context
if (!is_enrolled($context, $userid)) { //check if user is not enrolled
    $pluginins = $DB - > get_record("enrol", array('courseid' => < courseid > , 'enrol' => 'manual')); //get the enrollment method
    $enrolplugin = enrol_get_plugin('manual');
    if ($enrolplugin != null) { //if enroll method exists
        //enroll the user
        $plugin - > enrol_user($pluginins, , < userid > , $pluginins > roleid);
    } else {
        return false;
    }
}

For using group API functions, we will have to include the “group/lib.php” file.

1. Firstly, we will create the group by using the following code:

$data = new stdClass();
   	 $data->courseid = <courseid>;
   	 $data->name = <set_group_name>;
   	 $groupid = groups_create_group($data); //courseid and group name params are mandatory.

2. Now, we will add the users/members into groups by calling the following method with valid parameters(groupid and userid).

groups_add_member(<groupid>, <userid>);

3. We can check the user is a member of the given group by calling the following method:

groups_is_member($groupid, $userid)

4. If you want to remove the existing members, the call these functions:

groups_remove_member(<groupid>, <userid>);

The above function will delete the specific user from the group.

groups_delete_group_members(<groupid>);

The above function will delete all the users from the group.

5. You can delete the entire group with all their members by invoking the following function:

groups_delete_group(<groupid>);
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *