Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions block_groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
165 changes: 106 additions & 59 deletions classes/external/create_allgroups_output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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;
}
}
54 changes: 54 additions & 0 deletions classes/observers/group_observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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);
}
}
}
2 changes: 2 additions & 0 deletions classes/output/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace block_groups\output;

use plugin_renderer_base;
use html_writer;
use moodle_url;

/**
* Class of the block_groups renderer.
Expand Down
32 changes: 32 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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',
],
];
2 changes: 2 additions & 0 deletions lang/en/block_groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ';
Expand All @@ -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';
36 changes: 36 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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
)
);
}
17 changes: 17 additions & 0 deletions tests/behat/behat_block_groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<value_string>(?:[^"]|\\")*)"$/
*
* @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');
}
}
Loading
Loading