Skip to content

Commit

Permalink
Update #886bwwnb0j - Refactor for Moodle 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentcornelis committed Mar 4, 2024
1 parent 8e08212 commit 1de7786
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 84 deletions.
36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Moodle-admin_tool_groupautoenrol

Version 3.9.0 (stable version) for Moodle 3.9.0 to 4.1
Version 4.2.0 (stable version) for Moodle 3.9.0 to 4.2

Plugin to randomly auto enrol students in Moodle courses groups when they are enrolled into the course (whatever the enrol methods : auto-enrol by key, cohorts sync or manual enrol)

Expand All @@ -14,7 +14,7 @@ Plugin to randomly auto enrol students in Moodle courses groups when they are en
- you can choose to auto-enrol students in all existing course or specific ones

## Compatibility :
- Tested with Moodle 3.9 and Moodle 4.0
- Tested with Moodle 3.9 and Moodle 4.2
- Another version of the plugin exist and works with Moodle 2.5 and 2.7 (I did not test it with the others versions but should work with all 2.x).
It's a local plugin because adding link into "Course administration" menu was not possible for admin tool before Moodle 3.0
You can get it here : https://github.com/pascal-my/moodle-local_groupautoenrol/tree/STABLE
Expand All @@ -40,3 +40,4 @@ https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Y5DQB9F8AJB
- 1.1.1 : stable version working as local plugin for Moodle 2.x (see https://github.com/pascal-my/moodle-local_groupautoenrol)
- 1.1.2 : stable version working as admin tool plugin for Moodle 3.x
- 3.9.0 : Moodle 3.9 & PHP7.2 version | Moodle 4.0 & PHP 7.4 version | Moodle 4.1 & PHP 8.0 version
- 4.2.0 : Moodle 4.2 & PHP 8.0 version
53 changes: 41 additions & 12 deletions classes/form/manage_auto_group_enrol_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,31 @@

namespace tool_groupautoenrol\form;

use html_writer;
use moodle_url;
use moodleform;

defined('MOODLE_INTERNAL') || die;

global $CFG;
require_once("$CFG->libdir/formslib.php");

/**
* manage_auto_group_enrol_form class
* Class manage_auto_group_enrol_form
*
* @package tool_groupautoenrol
* @copyright 2016 Pascal
* @author Pascal M - https://github.com/pascal-my
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manage_auto_group_enrol_form extends moodleform {

/**
* Definition
*
* @return void
*/
public function definition() : void {
public function definition(): void {
$this->auto_group_enrol_form();
$this->add_action_buttons();
}
Expand All @@ -50,8 +60,8 @@ public function definition() : void {
* @throws \coding_exception
* @throws \dml_exception
*/
public function auto_group_enrol_form() : void {
global $CFG, $DB;
public function auto_group_enrol_form(): void {
global $DB;
$mform = &$this->_form;
$course = $this->_customdata['course'];
$allgroupscourse = groups_get_all_groups($course->id);
Expand All @@ -60,25 +70,42 @@ public function auto_group_enrol_form() : void {

// Group(s) must be created first.
if (empty($allgroupscourse)) {
// @TODO Use Moodle url.
$mform->addElement('static', 'no_group_found', '', "<a href='" . $CFG->wwwroot . "/group/index.php?id=" . $course->id . "'>" .
get_string('auto_group_enrol_form_no_group_found', 'tool_groupautoenrol') . "</a>");

$groupurl = new moodle_url('/group/index.php', ['id' => $course->id]);
$link = html_writer::link(
$groupurl,
get_string('auto_group_enrol_form_no_group_found', 'tool_groupautoenrol')
);
$mform->addElement('static', 'no_group_found', '', $link);

return;
}

$instance = $DB->get_record('tool_groupautoenrol', ['courseid' => $course->id]);
$mform->addElement('checkbox', 'enable_enrol', get_string('auto_group_form_enable_enrol', 'tool_groupautoenrol'));

$mform->addElement('checkbox', 'use_groupslist', get_string('auto_group_form_usegroupslist', 'tool_groupautoenrol'));
$mform->addElement(
'checkbox',
'enable_enrol',
get_string('auto_group_form_enable_enrol', 'tool_groupautoenrol')
);

$mform->addElement(
'checkbox',
'use_groupslist',
get_string('auto_group_form_usegroupslist', 'tool_groupautoenrol')
);
$mform->disabledIf('use_groupslist', 'enable_enrol');

$fields = [];
foreach ($allgroupscourse as $group) {
$fields[$group->id] = $group->name;
}

$select = $mform->addElement('select', 'groupslist', get_string('auto_group_form_groupslist', 'tool_groupautoenrol'), $fields);
$select = $mform->addElement(
'select',
'groupslist',
get_string('auto_group_form_groupslist', 'tool_groupautoenrol'),
$fields
);
$select->setMultiple(true);

$mform->disabledIf('groupslist', 'enable_enrol');
Expand All @@ -87,5 +114,7 @@ public function auto_group_enrol_form() : void {
$mform->setDefault('use_groupslist', $instance->use_groupslist ?? 0);
$mform->setDefault('groupslist', explode(",", $instance->groupslist ?? ''));
$mform->setDefault('enable_enrol', $instance->enable_enrol ?? 0);

}
}

}
39 changes: 25 additions & 14 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

use core\event\user_enrolment_created;

defined('MOODLE_INTERNAL') || die();

/**
* Event observer for tool_groupautoenrol.
*
* @package tool_groupautoenrol
* @copyright 2016 Pascal
* @author Pascal M - https://github.com/pascal-my
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_groupautoenrol_observer {

Expand All @@ -42,7 +45,7 @@ class tool_groupautoenrol_observer {
* @throws coding_exception
* @throws dml_exception
*/
public static function user_is_enrolled(user_enrolment_created $event) : bool {
public static function user_is_enrolled(user_enrolment_created $event): bool {
global $CFG, $DB;
require_once($CFG->dirroot . '/group/lib.php');
$enroldata = $event->get_record_snapshot($event->objecttable, $event->objectid);
Expand All @@ -59,7 +62,7 @@ public static function user_is_enrolled(user_enrolment_created $event) : bool {
return true;
}

// Checking if user is not already into theses groups.
// Checking if user is not already into these groups.
if (self::user_is_group_member($groupstouse, $enroldata)) {
return true;
}
Expand All @@ -71,21 +74,24 @@ public static function user_is_enrolled(user_enrolment_created $event) : bool {
}

/**
* @param stdClass $groupautoenrol
* Get the groups to use for the course.
*
* @param stdClass $groupautoenrol
* @param user_enrolment_created $event
*
* @return array
*/
private static function get_course_groups(stdClass $groupautoenrol, user_enrolment_created $event) : array {
private static function get_course_groups(stdClass $groupautoenrol, user_enrolment_created $event): array {
$groupstouse = [];
if (!empty($groupautoenrol->use_groupslist)) {

// If use_groupslist == 1, we need to check.
// a) if the list is not empty.
// A) if the list is not empty.
if (!empty($groupautoenrol->groupslist)) {
$groupstemp = explode(",", $groupautoenrol->groupslist);

// b) if the listed groups still exists (because when a group is deleted, groupautoenrol table is not updated !).
// B) if the listed groups still exists
// (because when a group is deleted, groupautoenrol table is not updated !).
$allgroupscourse = groups_get_all_groups($event->courseid);

foreach ($groupstemp as $group) {
Expand All @@ -107,13 +113,15 @@ private static function get_course_groups(stdClass $groupautoenrol, user_enrolme
}

/**
* Add user to group.
*
* @param stdClass $groupautoenrol
* @param array $groupstouse
* @param array $groupstouse
* @param stdClass $enroldata
*
* @throws coding_exception
*/
private static function add_user_to_group(stdClass $groupautoenrol, array $groupstouse, stdClass $enroldata) : void {
private static function add_user_to_group(stdClass $groupautoenrol, array $groupstouse, stdClass $enroldata): void {
global $USER;

if (!empty($groupautoenrol->enrol_method)) {
Expand All @@ -124,24 +132,26 @@ private static function add_user_to_group(stdClass $groupautoenrol, array $group
if (($groupname[strlen($groupname) - 2] <= $USER->lastname[0])
&& ($groupname[strlen($groupname) - 1] >= $USER->lastname[0])) {
groups_add_member($group->id, $enroldata->userid);
break; // exit foreach (is it working ?)
break; // Exit foreach (is it working ?).
}
}
} else {
// array_rand return key not value !
// Array_rand return key not value!
$randkeys = array_rand($groupstouse);
$group2add = $groupstouse[$randkeys];
groups_add_member($group2add, $enroldata->userid);
}
}

/**
* @param array $groupstouse
* Check if user is already in one of the groups.
*
* @param array $groupstouse
* @param stdClass $enroldata
*
* @return bool
*/
private static function user_is_group_member(array $groupstouse, stdClass $enroldata) : bool {
private static function user_is_group_member(array $groupstouse, stdClass $enroldata): bool {

foreach ($groupstouse as $group) {
if (groups_is_member($group->id, $enroldata->userid)) {
Expand All @@ -151,4 +161,5 @@ private static function user_is_group_member(array $groupstouse, stdClass $enrol

return false;
}

}
11 changes: 5 additions & 6 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @package moodle-admin_tool_groupautoenrol
* @package tool_groupautoenrol
* @copyright 23/07/2020 Mfreak.nl | LdesignMedia.nl - Luuk Verhoeven
* @author Luuk Verhoeven
**/

namespace local_commander\privacy;

defined('MOODLE_INTERNAL') || die();

/**
* Class provider
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @package moodle-admin_tool_groupautoenrol
* @package tool_groupautoenrol
* @copyright 23/07/2020 Mfreak.nl | LdesignMedia.nl - Luuk Verhoeven
* @author Luuk Verhoeven
**/
Expand All @@ -44,7 +42,8 @@ class provider implements \core_privacy\local\metadata\null_provider {
*
* @return string
*/
public static function get_reason() : string {
public static function get_reason(): string {
return 'privacy:null_reason';
}
}

}
1 change: 1 addition & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @author Pascal M - https://github.com/pascal-my
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

$observers = [
Expand Down
10 changes: 10 additions & 0 deletions lang/en/tool_groupautoenrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* EN language file for tool_groupautoenrol
*
* @package tool_groupautoenrol
* @copyright 2016 Pascal
* @author Pascal M - https://github.com/pascal-my
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

$string['pluginname'] = 'Automatic enrolment in groups';
Expand Down
17 changes: 9 additions & 8 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Lib functions
*
* @package tool_groupautoenrol
* @copyright 2016 Pascal
* @author Pascal M - https://github.com/pascal-my
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;

/**
* @param $navigation
* @param $course
* @param $context
* Extend the navigation for course.
*
* @throws coding_exception
* @throws moodle_exception
* @param navigation_node $navigation
* @param object $course
* @param context $context
*
* @return void
*/
function tool_groupautoenrol_extend_navigation_course($navigation, $course, $context) {
function tool_groupautoenrol_extend_navigation_course(navigation_node $navigation, object $course, context $context): void {

if (!($context instanceof context_course || $context instanceof context_module) && empty($context->instanceid)) {
return;
Expand All @@ -45,7 +46,7 @@ function tool_groupautoenrol_extend_navigation_course($navigation, $course, $con
'/admin/tool/groupautoenrol/manage_auto_group_enrol.php',
['id' => $context->instanceid]
);

$usermenu = $navigation->get('users');

$usermenu->add(get_string('menu_auto_groups', 'tool_groupautoenrol'), $url);
}
Loading

0 comments on commit 1de7786

Please sign in to comment.