aboutsummaryrefslogtreecommitdiff
path: root/mod/autosubscribegroup/start.php
blob: 69f4472679eb884ba5a36590973abe21ffbdaf43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

/**
 * Elgg autosubscribegroup plugin
 * This plugin allows new users to get joined to groups automatically when they register.
 *
 * @package autosubscribegroups
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
 * @author RONNEL Jérémy
 * @copyright (c) Elbee 2008
 * @link /www.notredeco.com
 *
 * for Elgg 1.8 by iionly (iionly@gmx.de)
 */

/**
 * Init
 *
 */
function autosubscribegroup_init() {
    //group plugin disabled : no need to go further...
    if (!elgg_is_active_plugin("groups")) {
        return;
    }

    //Listen to user registration
    elgg_register_event_handler('create', 'user', 'autosubscribegroup_join', 502);
}

/**
 * auto join group define in plugin settings
 *
 */
function autosubscribegroup_join($event, $object_type, $object) {

    if (($object instanceof ElggUser) && ($event == 'create') && ($object_type == 'user')) {
        //auto submit relationships between user & groups
        //retrieve groups ids from plugin
        $groups = elgg_get_plugin_setting('systemgroups');
        $groups = split(',', $groups);

        //for each group ids
        foreach($groups as $groupId) {
            //if group exist : submit to group
            if ($groupEnt = get_entity($groupId)) {
                //join group succeed ?
                if ($groupEnt->join($object)) {
                    // Remove any invite or join request flags
                    elgg_delete_metadata(array('guid' => $object->guid, 'metadata_name' => 'group_invite', 'metadata_value' => $groupEnt->guid, 'limit' => 0));
                    elgg_delete_metadata(array('guid' => $object->guid, 'metadata_name' => 'group_join_request', 'metadata_value' => $groupEnt->guid, 'limit' => 0));
                }
            }
        }
    }
}

elgg_register_event_handler('init', 'system', 'autosubscribegroup_init');