Skip to content

Commit

Permalink
added: option to make all site users a member of an auto join group
Browse files Browse the repository at this point in the history
changed: moved some functions
  • Loading branch information
jeabakker committed Oct 17, 2011
1 parent d6886c0 commit 916cd11
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 167 deletions.
60 changes: 60 additions & 0 deletions actions/fix_auto_join.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

global $CONFIG;
global $GROUP_TOOLS_ACL;

set_time_limit(0);

admin_gatekeeper();

$group_guid = (int) get_input("group_guid");

if(!empty($group_guid)){
if(($group = get_entity($group_guid)) && ($group instanceof ElggGroup)){
// set counters
$already = 0;
$new = 0;
$failure = 0;

$options = array(
"type" => "user",
"relationship" => "member_of_site",
"relationship_guid" => $CONFIG->site_guid,
"inverse_relationship" => true,
"limit" => false,
"callback" => "group_tools_guid_only_callback"
);

if($user_guids = elgg_get_entities_from_relationship($options)){
// make sure we can add user to group acl
register_plugin_hook("access:collections:write", "user", "group_tools_add_user_acl_hook");
$GROUP_TOOLS_ACL = $group->group_acl;

foreach($user_guids as $user_guid){
if(!is_group_member($group->getGUID(), $user_guid)){
if(join_group($group->getGUID(), $user_guid)){
$new++;
} else {
$failure++;
}
} else {
$already++;
}

// cleanup cache, to be sure
invalidate_cache_for_entity($user_guid);
}

// cleanup hook
unregister_plugin_hook("access:collections:write", "user", "group_tools_add_user_acl_hook");
}

system_message(sprintf(elgg_echo("group_tools:action:fix_auto_join:success"), $new, $already, $failure));
} else {
register_error(elgg_echo("group_tools:action:error:entity"));
}
} else {
register_error(elgg_echo("group_tools:action:error:input"));
}

forward(REFERER);
4 changes: 4 additions & 0 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
// auto join form
'group_tools:auto_join:add' => "%sAdd this group%s to the auto join groups. This will mean that new users are automaticly added to this group on registration.",
'group_tools:auto_join:remove' => "%sRemove this group%s from the auto join groups. This will mean that new users will no longer automaticly join this group on registration.",
'group_tools:auto_join:fix' => "To make all site members a member of this group, please %sclick here%s.",

// group admins
'group_tools:multiple_admin:group_admins' => "Group admins",
Expand Down Expand Up @@ -198,6 +199,9 @@
'group_tools:action:toggle_auto_join:error:save' => "An error occured while saving the new settings",
'group_tools:action:toggle_auto_join:success' => "The new settings were saved successfully",

// group fix auto_join
'group_tools:action:fix_auto_join:success' => "Group membership fixed: %s new members, %s were already a member and %s failures",

);

add_translation("en", $english);
Expand Down
232 changes: 117 additions & 115 deletions languages/nl.php

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion lib/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,21 @@ function group_tools_join_site_handler($event, $type, $relationship){
elgg_set_ignore_access($ia);
}
}
}
}

function group_tools_multiple_admin_group_leave($event, $type, $params){

if(!empty($params) && is_array($params)){
if(array_key_exists("group", $params) && array_key_exists("user", $params)){
$entity = $params["group"];
$user = $params["user"];

if(($entity instanceof ElggGroup) && ($user instanceof ElggUser)){
if(check_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID())){
return remove_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID());
}
}
}
}
}

35 changes: 35 additions & 0 deletions lib/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

function group_tools_add_user_acl_hook($hook, $type, $return_value, $params){
global $GROUP_TOOLS_ACL;

$result = $return_value;

if(!empty($GROUP_TOOLS_ACL) && is_array($result)){
if(!array_key_exists($GROUP_TOOLS_ACL, $result)){
$result[$GROUP_TOOLS_ACL] = "temp group";
}
}

return $result;
}

function group_tools_multiple_admin_can_edit_hook($hook, $type, $return_value, $params){
$result = $return_value;

if(!empty($params) && is_array($params) && !$result){
if(array_key_exists("entity", $params) && array_key_exists("user", $params)){
$entity = $params["entity"];
$user = $params["user"];

if(($entity instanceof ElggGroup) && ($user instanceof ElggUser)){
if($entity->isMember($user) && check_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID())){
$result = true;
}
}
}
}

return $result;
}

4 changes: 4 additions & 0 deletions manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ ToDo
====================
Version History
====================
x.x:
- added: option to make all site users a member of an auto join group
- changed: moved some functions
1.6 (2011-10-05):
- added: option to resend invites to already invited users
- changed: name of robots view to be less generic
Expand Down
53 changes: 3 additions & 50 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once(dirname(__FILE__) . "/lib/functions.php");
require_once(dirname(__FILE__) . "/lib/events.php");
require_once(dirname(__FILE__) . "/lib/hooks.php");

function group_tools_init(){

Expand Down Expand Up @@ -126,41 +127,6 @@ function group_tools_pagesetup(){

}

function group_tools_multiple_admin_can_edit_hook($hook, $type, $return_value, $params){
$result = $return_value;

if(!empty($params) && is_array($params) && !$result){
if(array_key_exists("entity", $params) && array_key_exists("user", $params)){
$entity = $params["entity"];
$user = $params["user"];

if(($entity instanceof ElggGroup) && ($user instanceof ElggUser)){
if($entity->isMember($user) && check_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID())){
$result = true;
}
}
}
}

return $result;
}

function group_tools_multiple_admin_group_leave($event, $type, $params){

if(!empty($params) && is_array($params)){
if(array_key_exists("group", $params) && array_key_exists("user", $params)){
$entity = $params["group"];
$user = $params["user"];

if(($entity instanceof ElggGroup) && ($user instanceof ElggUser)){
if(check_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID())){
return remove_entity_relationship($user->getGUID(), "group_admin", $entity->getGUID());
}
}
}
}
}

function group_tools_version_1_3(){
global $CONFIG, $GROUP_TOOLS_ACL;

Expand Down Expand Up @@ -192,20 +158,6 @@ function group_tools_version_1_3(){

}

function group_tools_add_user_acl_hook($hook, $type, $return_value, $params){
global $GROUP_TOOLS_ACL;

$result = $return_value;

if(!empty($GROUP_TOOLS_ACL) && is_array($result)){
if(!array_key_exists($GROUP_TOOLS_ACL, $result)){
$result[$GROUP_TOOLS_ACL] = "temp group";
}
}

return $result;
}

// default elgg event handlers
register_elgg_event_handler("init", "system", "group_tools_init");
register_elgg_event_handler("pagesetup", "system", "group_tools_pagesetup");
Expand All @@ -216,6 +168,7 @@ function group_tools_add_user_acl_hook($hook, $type, $return_value, $params){
register_action("group_tools/kick", false, dirname(__FILE__) . "/actions/kick.php");
register_action("group_tools/mail", false, dirname(__FILE__) . "/actions/mail.php");
register_action("group_tools/toggle_auto_join", false, dirname(__FILE__) . "/actions/toggle_auto_join.php", true);
register_action("group_tools/fix_auto_join", false, dirname(__FILE__) . "/actions/fix_auto_join.php", true);
register_action("groups/email_invitation", false, dirname(__FILE__) . "/actions/groups/email_invitation.php");

?>
25 changes: 24 additions & 1 deletion views/default/group_tools/forms/auto_join.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,36 @@

if(in_array($group->getGUID(), $auto_join_groups)){
$lang_key = "group_tools:auto_join:remove";

// check if all users are member of this group
$options = array(
"type" => "user",
"relationship" => "member_of_site",
"relationship_guid" => $group->site_guid,
"inverse_relationship" => true,
"count" => true
);

$user_count = elgg_get_entities_from_relationship($options);
$member_count = $group->getMembers(0, 0, true);

if($user_count != $member_count){

$link_start = "<a href='" . elgg_add_action_tokens_to_url($vars["url"] . "action/group_tools/fix_auto_join?group_guid=" . $group->getGUID()) . "'>";
$link_end = "</a>";

$fix_members = "<br /><br />" . sprintf(elgg_echo("group_tools:auto_join:fix"), $link_start, $link_end);
}
} else {
$lang_key = "group_tools:auto_join:add";
}

$link_start = "<a href='" . elgg_add_action_tokens_to_url($vars["url"] . "action/group_tools/toggle_auto_join?group_guid=" . $group->getGUID()) . "'>";
$link_end = "</a>";

echo elgg_view("page_elements/contentwrapper", array("body" => sprintf(elgg_echo($lang_key), $link_start, $link_end)));
$body = sprintf(elgg_echo($lang_key), $link_start, $link_end);
$body .= $fix_members;

echo elgg_view("page_elements/contentwrapper", array("body" => $body));
}
}

0 comments on commit 916cd11

Please sign in to comment.