diff --git a/block_groups.php b/block_groups.php index b6a6851..e59289f 100644 --- a/block_groups.php +++ b/block_groups.php @@ -187,4 +187,12 @@ public function build_grouping_array($allgroupings, $courseid) { } return $groupingsarray; } + + /** + * Tells moodle, that the groups block has a settings file. + * @return bool true + */ + public function has_config() { + return true; + } } diff --git a/classes/external/create_allgroups_output.php b/classes/external/create_allgroups_output.php index ad66ca3..a46a12c 100644 --- a/classes/external/create_allgroups_output.php +++ b/classes/external/create_allgroups_output.php @@ -76,31 +76,59 @@ public static function execute_returns() { * @return array */ public static function execute($groups) { - global $PAGE, $CFG, $DB; + global $PAGE, $DB; $params = self::validate_parameters(self::execute_parameters(), ['groups' => $groups]); - - require_once($CFG->dirroot . '/blocks/groups/locallib.php'); - $PAGE->set_context(context_course::instance($params['groups']['courseid'])); require_capability('moodle/course:managegroups', context_course::instance($params['groups']['courseid'])); + $groupsuitable = $DB->get_records('groups', ['courseid' => $params['groups']['courseid']], 'id ASC'); - // The Course has no groups therefore changing all is not possible. + // The Course has no groups therefore changing all is not possible. if (empty($groupsuitable)) { - $output['courseid'] = $params['groups']['courseid']; - $link = html_writer::link(new moodle_url( - '/group/index.php', - ['id' => $params['groups']['courseid']] - ), 'modify groups'); - $content = html_writer::div(get_string('nogroups', 'block_groups')) . $link; - $output['newelement'] = html_writer::div($content, ['class' => 'content']); - $output['visibility'] = '0'; - $output['changedgroups'] = []; - return $output; + return self::create_no_groups_response($params['groups']['courseid']); } - $groupsvisible = []; - $renderer = $PAGE->get_renderer('block_groups'); + $groupstochange = self::get_groups_to_change($params['groups']['action'], $groupsuitable); + $changedgroups = self::update_group_visibility($groupstochange, $params['groups']['courseid']); + $html = self::generate_groups_html($groupsuitable, $params['groups']['courseid'], $params['groups']['action']); + $statuscode = self::get_status_code($params['groups']['action'], $groupstochange); + + return [ + 'courseid' => $params['groups']['courseid'], + 'newelement' => $html, + 'visibility' => $statuscode, + 'changedgroups' => $changedgroups, + ]; + } + + /** + * Returns the response if the course has no groups. + * @param int $courseid + * @return array + */ + private static function create_no_groups_response($courseid) { + $output['courseid'] = $courseid; + $link = html_writer::link(new moodle_url( + '/group/index.php', + ['id' => $courseid] + ), 'modify groups'); + $content = html_writer::div(get_string('nogroups', 'block_groups')) . $link; + $output['newelement'] = html_writer::div($content, ['class' => 'content']); + $output['visibility'] = '0'; + $output['changedgroups'] = []; + return $output; + } + + /** + * Returns the groups which should be changed depending on the action. + * @param string $action + * @param array $groupsuitable + * @return array + */ + private static function get_groups_to_change($action, $groupsuitable) { + global $DB; + + $groupsvisible = []; foreach ($groupsuitable as $group) { $entry = $DB->get_records('block_groups_hide', ['id' => $group->id]); @@ -109,61 +137,80 @@ public static function execute($groups) { $groupsvisible[$group->id] = $group->id; } } - $groups = $groupsvisible; - $outputvisibility = '3'; - if ($params['groups']['action'] == "show") { - $outputvisibility = '4'; + + if ($action == "show") { $tempgroup = []; - if (!empty($groupsuitable)) { - foreach ($groupsuitable as $group) { - if (!empty($groupsvisible)) { - if (!(in_array($group->id, $groups))) { - $tempgroup[$group->id] = $group->id; - } - } else { - $tempgroup[$group->id] = $group->id; - } + foreach ($groupsuitable as $group) { + if (!in_array($group->id, $groupsvisible)) { + $tempgroup[$group->id] = $group->id; } } - $groups = $tempgroup; + return $tempgroup; } - $output['changedgroups'] = []; - if (!empty($groups)) { - foreach ($groups as $group) { - block_groups_db_transaction_change_visibility($group, $params['groups']['courseid']); - array_push($output['changedgroups'], ['groupid' => $group]); - } + + return $groupsvisible; + } + + /** + * Changes the visibility of the groups in the database. + * @param array $groups + * @param int $courseid + * @return array + */ + private static function update_group_visibility($groups, $courseid) { + global $CFG; + require_once($CFG->dirroot . '/blocks/groups/locallib.php'); + + $changedgroups = []; + foreach ($groups as $groupid) { + block_groups_db_transaction_change_visibility($groupid, $courseid); + $changedgroups[] = ['groupid' => $groupid]; } + return $changedgroups; + } + + /** + * Generates the html for all groups of the course. + * @param array $groupsuitable + * @param int $courseid + * @param string $action + * @return string + */ + private static function generate_groups_html($groupsuitable, $courseid, $action) { + global $PAGE; + + $renderer = $PAGE->get_renderer('block_groups'); + $groupsarray = []; + foreach ($groupsuitable as $group) { $fullgroup = groups_get_group($group->id); $href = new moodle_url( '/blocks/groups/changevisibility.php', - ['courseid' => $params['groups']['courseid'], 'groupid' => $group->id] + ['courseid' => $courseid, 'groupid' => $group->id] ); $countmembers = count(groups_get_members($group->id)); - if ($params['groups']['action'] == 'hide') { - $visibility = true; - } - if ($params['groups']['action'] == 'show') { - $visibility = false; - } - $groupsarray[] = $renderer->get_string_group($fullgroup, $href, $countmembers, $visibility); + $visibility = ($action == "show") ? false : true; + $groupsarray[] = $renderer->get_string_group( + $fullgroup, + $href, + $countmembers, + $visibility + ); } - $output['newelement'] = html_writer::alist($groupsarray, ['class' => 'wrapperlistgroup']); - $output['courseid'] = $params['groups']['courseid']; + return html_writer::alist($groupsarray, ['class' => 'wrapperlistgroup']); + } - if (empty($groups)) { - $output['visibility'] = $outputvisibility; - return $output; - } - // Parameter $outputvisibility 0->nogroups 1 -> hidden 2->visible 3-> all are hidden 4-> all are visible. - if ($params['groups']['action'] == 'hide') { - $outputvisibility = 1; - } - if ($params['groups']['action'] == 'show') { - $outputvisibility = 2; + /** + * Returns the status code for the response depending on the action and the groups which should be changed. + * @param string $action + * @param array $groupstochange + * @return int + */ + private static function get_status_code($action, $groupstochange) { + if (empty($groupstochange)) { + return ($action == "show") ? 4 : 3; } - $output['visibility'] = $outputvisibility; - return $output; + + return ($action == "hide") ? 1 : 2; } } diff --git a/classes/observers/group_observer.php b/classes/observers/group_observer.php new file mode 100644 index 0000000..b7c7b78 --- /dev/null +++ b/classes/observers/group_observer.php @@ -0,0 +1,54 @@ +. + +/** + * Event observers for the groups block. + * + * @package block_groups + * @copyright 2026 Lena Herfeldt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace block_groups\observers; + +/** + * Observer for group events + */ +class group_observer { + /** + * Handles the group_created event. + * Automatically adds new groups to block_groups_hide table if show_groups_default_setting is enabled. + * + * @param \core\event\group_created $event + */ + public static function group_created(\core\event\group_created $event) { + global $DB; + + // Checking if the setting is enabled. + $showalways = get_config('block_groups', 'show_groups_default_setting'); + + if (!$showalways) { + return; + } + + $groupid = $event->objectid; + + // Insert directly into the block_groups_hide table to make it visible from the start. + if (!$DB->record_exists('block_groups_hide', ['id' => $groupid])) { + $DB->insert_record_raw('block_groups_hide', ['id' => $groupid], true, false, true); + } + } +} diff --git a/classes/output/renderer.php b/classes/output/renderer.php index 67abb71..de2a007 100644 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -17,6 +17,8 @@ namespace block_groups\output; use plugin_renderer_base; +use html_writer; +use moodle_url; /** * Class of the block_groups renderer. diff --git a/db/events.php b/db/events.php new file mode 100644 index 0000000..7d90229 --- /dev/null +++ b/db/events.php @@ -0,0 +1,32 @@ +. + +/** + * Event observers for the groups block. + * + * @package block_groups + * @copyright 2026 Lena Herfeldt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$observers = [ + [ + 'eventname' => '\core\event\group_created', + 'callback' => '\block_groups\observers\group_observer::group_created', + ], +]; diff --git a/lang/en/block_groups.php b/lang/en/block_groups.php index e436a3f..3f50174 100644 --- a/lang/en/block_groups.php +++ b/lang/en/block_groups.php @@ -26,6 +26,7 @@ $string['allgroupsinstatevisible'] = 'All groups are already visible.'; $string['brackets'] = '({$a})'; $string['changeallgroups'] = 'Change all groups'; +$string['configshowgroupsandgroupings'] = 'The groups block will not show groups and groupings by default. By enabling this, admins/teachers choose to show groups/groupings immediately after the creation.'; $string['errorbutton'] = 'OK'; $string['errortitle'] = 'Synchronization error'; $string['group'] = 'Groups '; @@ -45,3 +46,4 @@ $string['pluginname2'] = 'Groups'; $string['privacy:metadata'] = 'The Group block only displays information about groups, but does not effect or store any personal data. The existing groups and groupings data are stored in other locations.'; $string['showgroup'] = 'show group'; +$string['showgroupsdefaultsetting'] = 'Show groups and groupings by default'; diff --git a/settings.php b/settings.php new file mode 100644 index 0000000..5b476ca --- /dev/null +++ b/settings.php @@ -0,0 +1,36 @@ +. + +/** + * Groups block settings + * + * @package block_groups + * @copyright 2026 Lena Herfeldt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +if ($ADMIN->fulltree) { + $settings->add( + new admin_setting_configcheckbox( + 'block_groups/show_groups_default_setting', + get_string('showgroupsdefaultsetting', 'block_groups'), + get_string('configshowgroupsandgroupings', 'block_groups'), + 0 + ) + ); +} diff --git a/tests/behat/behat_block_groups.php b/tests/behat/behat_block_groups.php index ec42bce..754c19c 100644 --- a/tests/behat/behat_block_groups.php +++ b/tests/behat/behat_block_groups.php @@ -95,4 +95,21 @@ public function i_click_in_the_groups_block_on_all_groups($action) { } $eyeicon->click(); } + + /** + * Sets the group default visibility setting to a specific value (0 or 1). + * This step allows tests to configure whether groups are visible or hidden by default. + * + * @Given /^the group default visibility setting is "(?P(?:[^"]|\\")*)"$/ + * + * @param string $value The value to set (0 for hidden, 1 for visible) + * @throws \Exception + */ + public function the_group_default_visibility_setting_is($value) { + if ($value !== '0' && $value !== '1') { + throw new \Exception('Invalid value for group default visibility setting: ' . $value . '. Must be 0 or 1.'); + } + + set_config('show_groups_default_setting', $value, 'block_groups'); + } } diff --git a/tests/behat/change_all.feature b/tests/behat/change_all.feature index 13a585b..6e1366d 100644 --- a/tests/behat/change_all.feature +++ b/tests/behat/change_all.feature @@ -7,33 +7,33 @@ Feature: Change all groups in a group block Background: Given the following "courses" exist: | fullname | shortname | category | - | Course 1 | C1 | 0 | + | Course 1 | C1 | 0 | And the following "users" exist: - | username | firstname | lastname | email | idnumber | - | teacher1 | Teacher | 1 | teacher1@example.com | T1 | - | student1 | Student | 1 | student1@example.com | S1 | - | student2 | Student | 2 | student2@example.com | S2 | - | student3 | Student | 3 | student3@example.com | S3 | + | username | firstname | lastname | email | idnumber | + | teacher1 | Teacher | 1 | teacher1@example.com | T1 | + | student1 | Student | 1 | student1@example.com | S1 | + | student2 | Student | 2 | student2@example.com | S2 | + | student3 | Student | 3 | student3@example.com | S3 | And the following "course enrolments" exist: - | user | course | role | - | teacher1 | C1 | editingteacher | - | student1 | C1 | student | - | student2 | C1 | student | - | student3 | C1 | student | + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + | student3 | C1 | student | And the following "groups" exist: - | name | course | idnumber | - | Group 1 | C1 | G1 | - | Group 2 | C1 | G2 | - | Group 3 | C1 | G3 | + | name | course | idnumber | + | Group 1 | C1 | G1 | + | Group 2 | C1 | G2 | + | Group 3 | C1 | G3 | And the following "groupings" exist: - | name | course | idnumber | - | Grouping 1 | C1 | GG1 | - | Grouping 2 | C1 | GG2 | + | name | course | idnumber | + | Grouping 1 | C1 | GG1 | + | Grouping 2 | C1 | GG2 | And the following "group members" exist: - | user | group | - | student1 | G1 | - | student2 | G1 | - | student2 | G2 | + | user | group | + | student1 | G1 | + | student2 | G1 | + | student2 | G2 | And the following "grouping groups" exist: | grouping | group | | GG1 | G1 | diff --git a/tests/behat/default_settings.feature b/tests/behat/default_settings.feature new file mode 100644 index 0000000..7a73130 --- /dev/null +++ b/tests/behat/default_settings.feature @@ -0,0 +1,49 @@ +@block @block_groups @groups_default_settings +Feature: Group block default visibility settings + In order to test that group visibility settings work properly + As a teacher + I need to verify that groups are shown to students according to the default visibility setting + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "teacher1" + And I am on "Course 1" course homepage with editing mode on + And I add the "Groups and Groupings" block + And I log out + + @javascript + Scenario: Default setting is 0 - student should not see hidden group + Given the group default visibility setting is "0" + And the following "groups" exist: + | name | course | idnumber | + | Group 1 | C1 | G1 | + And the following "group members" exist: + | user | group | + | student1 | G1 | + When I log in as "student1" + And I am on "Course 1" course homepage + Then "Groups" "block" should not exist + + @javascript + Scenario: Default setting is 1 - student should directly see group + Given the group default visibility setting is "1" + And the following "groups" exist: + | name | course | idnumber | + | Group 1 | C1 | G1 | + And the following "group members" exist: + | user | group | + | student1 | G1 | + When I log in as "student1" + And I am on "Course 1" course homepage + Then "Groups" "block" should exist + And I should see "Group 1" in the "Groups" "block" diff --git a/tests/behat/hide_group.feature b/tests/behat/hide_group.feature index f475e95..8197ab7 100644 --- a/tests/behat/hide_group.feature +++ b/tests/behat/hide_group.feature @@ -7,33 +7,33 @@ Feature: Hide a group in a group block Background: Given the following "courses" exist: | fullname | shortname | category | - | Course 1 | C1 | 0 | + | Course 1 | C1 | 0 | And the following "users" exist: - | username | firstname | lastname | email | idnumber | - | teacher1 | Teacher | 1 | teacher1@example.com | T1 | - | student1 | Student | 1 | student1@example.com | S1 | - | student2 | Student | 2 | student2@example.com | S2 | - | student3 | Student | 3 | student3@example.com | S3 | + | username | firstname | lastname | email | idnumber | + | teacher1 | Teacher | 1 | teacher1@example.com | T1 | + | student1 | Student | 1 | student1@example.com | S1 | + | student2 | Student | 2 | student2@example.com | S2 | + | student3 | Student | 3 | student3@example.com | S3 | And the following "course enrolments" exist: - | user | course | role | - | teacher1 | C1 | editingteacher | - | student1 | C1 | student | - | student2 | C1 | student | - | student3 | C1 | student | + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + | student3 | C1 | student | And the following "groups" exist: - | name | course | idnumber | - | Group 1 | C1 | G1 | - | Group 2 | C1 | G2 | - | Group 3 | C1 | G3 | + | name | course | idnumber | + | Group 1 | C1 | G1 | + | Group 2 | C1 | G2 | + | Group 3 | C1 | G3 | And the following "groupings" exist: - | name | course | idnumber | - | Grouping 1 | C1 | GG1 | - | Grouping 2 | C1 | GG2 | + | name | course | idnumber | + | Grouping 1 | C1 | GG1 | + | Grouping 2 | C1 | GG2 | And the following "group members" exist: - | user | group | - | student1 | G1 | - | student2 | G1 | - | student2 | G2 | + | user | group | + | student1 | G1 | + | student2 | G1 | + | student2 | G2 | And the following "grouping groups" exist: | grouping | group | | GG1 | G1 | diff --git a/tests/behat/show_group.feature b/tests/behat/show_group.feature index ade5915..661bdda 100644 --- a/tests/behat/show_group.feature +++ b/tests/behat/show_group.feature @@ -7,33 +7,33 @@ Feature: Make a group visible in a group block Background: Given the following "courses" exist: | fullname | shortname | category | - | Course 1 | C1 | 0 | + | Course 1 | C1 | 0 | And the following "users" exist: - | username | firstname | lastname | email | idnumber | - | teacher1 | Teacher | 1 | teacher1@example.com | T1 | - | student1 | Student | 1 | student1@example.com | S1 | - | student2 | Student | 2 | student2@example.com | S2 | - | student3 | Student | 3 | student3@example.com | S3 | + | username | firstname | lastname | email | idnumber | + | teacher1 | Teacher | 1 | teacher1@example.com | T1 | + | student1 | Student | 1 | student1@example.com | S1 | + | student2 | Student | 2 | student2@example.com | S2 | + | student3 | Student | 3 | student3@example.com | S3 | And the following "course enrolments" exist: - | user | course | role | - | teacher1 | C1 | editingteacher | - | student1 | C1 | student | - | student2 | C1 | student | - | student3 | C1 | student | + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + | student3 | C1 | student | And the following "groups" exist: - | name | course | idnumber | - | Group 1 | C1 | 1 | - | Group 2 | C1 | 2 | + | name | course | idnumber | + | Group 1 | C1 | 1 | + | Group 2 | C1 | 2 | And the following "groupings" exist: - | name | course | idnumber | - | Grouping 1 | C1 | GG1 | - | Grouping 2 | C1 | GG2 | + | name | course | idnumber | + | Grouping 1 | C1 | GG1 | + | Grouping 2 | C1 | GG2 | And the following "group members" exist: - | user | group | - | student1 | 1 | - | student2 | 1 | - | student3 | 2 | - | teacher1 | 1 | + | user | group | + | student1 | 1 | + | student2 | 1 | + | student3 | 2 | + | teacher1 | 1 | And the following "grouping groups" exist: | grouping | group | | GG1 | 1 | diff --git a/version.php b/version.php index fb698e4..70d5e4a 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2026030400; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2026052001; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2022112800; // Requires 4.1+ Moodle version. $plugin->component = 'block_groups'; // Full name of the plugin (used for diagnostics). $plugin->release = 'v5.0-r1';