From 1de7786ec4c3619bb6fa3d9686438ce6bcf180ea Mon Sep 17 00:00:00 2001 From: Vincent Cornelis Date: Mon, 4 Mar 2024 13:09:01 +0100 Subject: [PATCH] Update #886bwwnb0j - Refactor for Moodle 4.2 --- .travis.yml | 36 ------------- README.md | 5 +- classes/form/manage_auto_group_enrol_form.php | 53 ++++++++++++++----- classes/observer.php | 39 +++++++++----- classes/privacy/provider.php | 11 ++-- db/events.php | 1 + lang/en/tool_groupautoenrol.php | 10 ++++ lib.php | 17 +++--- manage_auto_group_enrol.php | 14 +++-- version.php | 7 +-- 10 files changed, 109 insertions(+), 84 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index aaa3b86..0000000 --- a/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -language: php - -sudo: false - -cache: - directories: - - $HOME/.composer/cache - -php: - - 7.2 - -env: - matrix: - - DB=mysqli MOODLE_BRANCH=MOODLE_39_STABLE - -before_install: - - phpenv config-rm xdebug.ini - - cd ../.. - - composer selfupdate - - composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1 - - export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH" - -install: - - moodle-plugin-ci install - -script: - - moodle-plugin-ci phplint - - moodle-plugin-ci phpcpd - - moodle-plugin-ci phpmd - - moodle-plugin-ci codechecker - - moodle-plugin-ci csslint - - moodle-plugin-ci shifter - - moodle-plugin-ci jshint - - moodle-plugin-ci validate - - moodle-plugin-ci phpunit - - moodle-plugin-ci behat diff --git a/README.md b/README.md index 42f4916..8fc1d1c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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 diff --git a/classes/form/manage_auto_group_enrol_form.php b/classes/form/manage_auto_group_enrol_form.php index d9b71a0..9684e27 100644 --- a/classes/form/manage_auto_group_enrol_form.php +++ b/classes/form/manage_auto_group_enrol_form.php @@ -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(); } @@ -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); @@ -60,17 +70,29 @@ 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', '', "" . - get_string('auto_group_enrol_form_no_group_found', 'tool_groupautoenrol') . ""); + + $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 = []; @@ -78,7 +100,12 @@ public function auto_group_enrol_form() : void { $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'); @@ -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); + } -} \ No newline at end of file + +} diff --git a/classes/observer.php b/classes/observer.php index 6fe074d..f1df4aa 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -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 { @@ -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); @@ -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; } @@ -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) { @@ -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)) { @@ -124,11 +132,11 @@ 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); @@ -136,12 +144,14 @@ private static function add_user_to_group(stdClass $groupautoenrol, array $group } /** - * @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)) { @@ -151,4 +161,5 @@ private static function user_is_group_member(array $groupstouse, stdClass $enrol return false; } + } diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index bb14a9c..f7f584f 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -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 **/ @@ -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'; } -} \ No newline at end of file + +} diff --git a/db/events.php b/db/events.php index 4b4e579..b8ff06c 100644 --- a/db/events.php +++ b/db/events.php @@ -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 = [ diff --git a/lang/en/tool_groupautoenrol.php b/lang/en/tool_groupautoenrol.php index 0d60a03..fe6fbdc 100644 --- a/lang/en/tool_groupautoenrol.php +++ b/lang/en/tool_groupautoenrol.php @@ -13,6 +13,16 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . + +/** + * 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'; diff --git a/lib.php b/lib.php index ab422fb..2d66d08 100644 --- a/lib.php +++ b/lib.php @@ -14,23 +14,24 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . /** + * 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; @@ -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); } diff --git a/manage_auto_group_enrol.php b/manage_auto_group_enrol.php index ab961df..406ea2a 100644 --- a/manage_auto_group_enrol.php +++ b/manage_auto_group_enrol.php @@ -13,14 +13,18 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . + /** - * @file Params page for auto group enrollment as defined by Comete + * Manage auto group enrolment + * + * Params page for auto group enrollment as defined by Comete * * @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 */ + require_once('../../../config.php'); defined('MOODLE_INTERNAL') || die; @@ -46,7 +50,7 @@ ]); if ($form->is_cancelled()) { - redirect(new moodle_url("$CFG->wwwroot/course/view.php", ['id' => $course->id])); + redirect(new moodle_url('/course/view.php', ['id' => $course->id])); } else if ($data = $form->get_data()) { if (empty($data->enable_enrol)) { @@ -74,7 +78,11 @@ $DB->update_record('tool_groupautoenrol', $groupautoenrol); } - redirect(new moodle_url("$CFG->wwwroot/admin/tool/groupautoenrol/manage_auto_group_enrol.php", ['id' => $course->id])); + redirect( + new moodle_url('/admin/tool/groupautoenrol/manage_auto_group_enrol.php', + ['id' => $course->id] + ) + ); } echo $OUTPUT->header(); diff --git a/version.php b/version.php index 22e6187..2a48268 100644 --- a/version.php +++ b/version.php @@ -24,9 +24,10 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020072300; // The (date) version of this module + 2 extra digital for daily versions. -$plugin->requires = 2013040500; // Requires this Moodle version - at least 2.0. +$plugin->version = 2024030400; // The (date) version of this module + 2 extra digital for daily versions. +$plugin->requires = 2020061500; // Requires this Moodle version - at least 3.9. +$plugin->supported = [39, 402]; $plugin->cron = 0; $plugin->component = 'tool_groupautoenrol'; -$plugin->release = '3.9.0'; +$plugin->release = '4.2.0'; $plugin->maturity = MATURITY_STABLE;