Moodle Hack: Site-wide Groups

Moodle Hack: Site-wide Groups

by Dakota Duff -
Number of replies: 13

Our organization uses groups to share courses among multiple sites. However, we wanted an easy way to create and maintain identical groups in multiple classes, so this script was created.

In our setup we wanted group names and settings, as well as group members (the teachers) to be identical in all courses where we were using groups. Our students enroll in courses and are assigned to the appropriate group by using the group password.

The script is a cron addition that first looks for courses with Group Mode set to "Separate Groups" and modifies these courses to have the same group settings as the Template Course (including removing any groups not found in the Template Course).

The script does NOT remove any users from existing groups, but does add them. This prevents our students from being removed from groups, since they're actually entered into groups on a course-by-course basis, rather than a site-wide basis.

Because the script can delete groups, please backup before running.

Installation is simple, though. Just define the Template Course's ID on line 4 of cron.php and place the file at [site root]/local/cron.php (you may have to create the "local" directory). Each course with groupmode = 1 (Group Mode = Separate Groups) will have the groups modified to match the Template Course's groups the next time your cron job runs.

I wrote this for Moodle 1.9 and haven't tested it in anything prior.

If you have any questions, please email me.

Average of ratings: -
In reply to Dakota Duff

Re: Moodle Hack: Site-wide Groups

by Dakota Duff -

One thing this script doesn't address (yet) is group pictures. It doesn't duplicate group pictures.

In reply to Dakota Duff

Re: Moodle Hack: Site-wide Groups

by Nicolas Sicard -
Hi,

Good idea ! I didn't try the script on my production Moodle but I sure will on our testing install.

What would be very useful is to be able to use different source courses. We could use an associated array where each entry links one source course (identified by their ids) with a list of target courses (also identified by their ids).

Then, groups from each source course would be replicated into their repective target courses. No need to check for groupmode.

I would definitly use such a script to simplify group enrollement in our Moodle.

Cheers !

Nicolas.
In reply to Nicolas Sicard

Re: Moodle Hack: Site-wide Groups

by Dakota Duff -

Thanks Nicolas.


If anyone has basic PHP/MySQL knowledge you can customize this to check whatever course variable you set and sync those courses to your template course.

I did come up with a version that copies group pictures. I think it might only work with Apache, though. Basically, it creates symbolic links to the template course's group images.
In reply to Dakota Duff

Re: Moodle Hack: Site-wide Groups

by Dakota Duff -

Fixed some bugs with image copying, sped up queries, fixed a bug where an invisible user was being added to groups, and added an option to reassign users to their template group.

In reply to Dakota Duff

Re: Moodle Hack: Site-wide Groups

by Dakota Duff -

Finally got around to fixing the deletion of non-template groups and reassigning of users to template groups. Also change the symbolic link creation to a straight copy to prevent a problem where servers would get confused chasing symbolic links.

Average of ratings: Useful (1)
In reply to Dakota Duff

Re: Moodle Hack: Site-wide Groups

by Sergei Yegorov -
Hello, I tried to run your script but I get an error message "Warning: mysql_unbuffered_query() [function.mysql-unbuffered-query]: Access denied for user 'nobody'@'localhost' (using password: NO)"

What is going wrong?
In reply to Sergei Yegorov

Re: Moodle Hack: Site-wide Groups

by Sergei Yegorov -
SOLVED! Added

require_once(dirname(__FILE__) . '/../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/gradelib.php');

in your cron.php file. Thank you for a great hack!
In reply to Sergei Yegorov

Re: Moodle Hack: Site-wide Groups

by Dakota Duff -

If you place the script in "/moodle/local/cron.php" then it will run at the same time as your Moodle maintenance CRON and will pick up the required variables and functions from there. If you want to run it as a separate CRON, though, you do have to add the includes (as you already figured out). smile

In reply to Dakota Duff

Re: Moodle Hack: Site-wide Groups

by Melanie Scott -
Small question...we want the same groups in several courses but sometimes there are people in the original group who are not required to take sub-courses because they did very well in the original course. When this copies the groups into the courses, does it also enroll users in the original groups, if they aren't enrolled, or do they get left off?

Some of my courses I would want the groups but not necessarily the participants in the groups...
In reply to Melanie Scott

Re: Moodle Hack: Site-wide Groups

by Dakota Duff -
Good question. This script only copies group assignments — it does not automatically enroll a user. So it sounds like this functions exactly as you need it to.