*/
$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
api_block_anonymous_users();
if (api_get_setting('allow_social_tool') !== 'true') {
    api_not_allowed();
}
$join_url = '';
$this_section = SECTION_SOCIAL;
$allowed_views = array('mygroups', 'newest', 'pop');
$content = null;
if (isset($_GET['view']) && in_array($_GET['view'], $allowed_views)) {
    if ($_GET['view'] == 'mygroups') {
        $interbreadcrumb[] = array('url' =>'groups.php', 'name' => get_lang('Groups'));
        $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('MyGroups'));
    } else if ($_GET['view'] == 'newest') {
        $interbreadcrumb[] = array('url' =>'groups.php', 'name' => get_lang('Groups'));
        $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('Newest'));
    } else {
        $interbreadcrumb[] = array('url' =>'groups.php', 'name' => get_lang('Groups'));
        $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('Popular'));
    }
} else {
    $interbreadcrumb[] = array('url' =>'groups.php', 'name' => get_lang('Groups'));
    if (!isset($_GET['id'])) {
        $interbreadcrumb[] = array('url' =>'#', 'name' => get_lang('GroupList'));
    }
}
// getting group information
$relation_group_title = '';
$my_group_role = 0;
$usergroup = new UserGroup();
$create_thread_link = '';
$show_menu = 'browse_groups';
if (isset($_GET['view']) && $_GET['view'] == 'mygroups') {
    $show_menu = $_GET['view'];
}
$social_right_content = null;
// My groups
$results = $usergroup->get_groups_by_user(api_get_user_id(), 0);
$grid_my_groups = array();
$my_group_list = array();
if (is_array($results) && count($results) > 0) {
    foreach ($results as $result) {
        $id = $result['id'];
        $result['name'] = Security::remove_XSS($result['name'], STUDENT, true);
        $result['description'] = Security::remove_XSS($result['description'], STUDENT, true);
        $my_group_list[] = $id;
        $name = cut($result['name'], GROUP_TITLE_LENGTH, true);
        if ($result['relation_type'] == GROUP_USER_PERMISSION_ADMIN) {
            $name .= ' '.Display::return_icon('social_group_admin.png', get_lang('Admin'), array('style'=>'vertical-align:middle'));
        } elseif ($result['relation_type'] == GROUP_USER_PERMISSION_MODERATOR) {
            $name .= ' '.Display::return_icon('social_group_moderator.png', get_lang('Moderator'), array('style'=>'vertical-align:middle'));
        }
        $url = ''.$name.'';
        $count_users_group = count(
            $usergroup->get_users_by_group(
                $id,
                false,
                array(
                    GROUP_USER_PERMISSION_ADMIN,
                    GROUP_USER_PERMISSION_READER,
                    GROUP_USER_PERMISSION_MODERATOR,
                ),
                0,
                1000
            )
        );
        if ($count_users_group == 1) {
            $count_users_group = $count_users_group.' '.get_lang('Member');
        } else {
            $count_users_group = $count_users_group.' '.get_lang('Members');
        }
        $picture = $usergroup->get_picture_group(
            $result['id'],
            $result['picture'],
            80
        );
        $result['picture'] = '
';
        $members = Display::returnFontAwesomeIcon('user').$count_users_group;
        $html = '
';
        $html .= '
';
        $html .= $result['picture'];
        $html .= '
';
        $html .= '
';
        $html .= '
';
        $html .= Display::tag('h5', $url);
        $html .= '
';
        $html .= '
'.$members.'
';
        if ($result['description'] != '') {
            $html .= '
'.cut($result['description'], 100, true).'
';
        } else {
            $html .= '';
        }
        $html .= '
';
        $html .= '
 ';
        $grid_item_2 = $html;
        $grid_my_groups[] = array($grid_item_2);
    }
}
// Newest groups
$results = $usergroup->get_groups_by_age(4, false);
$grid_newest_groups = array();
foreach ($results as $result) {
    $result['name'] = Security::remove_XSS($result['name'], STUDENT, true);
    $result['description'] = Security::remove_XSS($result['description'], STUDENT, true);
    $id = $result['id'];
    $name = cut($result['name'], GROUP_TITLE_LENGTH, true);
    $count_users_group = count($usergroup->get_users_by_group($id, false, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_READER, GROUP_USER_PERMISSION_MODERATOR), 0, 1000));
    if ($count_users_group == 1) {
        $count_users_group = $count_users_group.' '.get_lang('Member');
    } else {
        $count_users_group = $count_users_group.' '.get_lang('Members');
    }
    $url = ''.$name.'';
    $picture = $usergroup->get_picture_group($result['id'], $result['picture'], 80);
    $result['picture'] = '
';
    $members = Display::returnFontAwesomeIcon('user').$count_users_group;
    $html = '';
    $html .= '
';
    $html .= $result['picture'];
    $html .= '
';
    $html .= '
';
    $html .= '
';
    $html .= Display::tag('h5', $url);
    $html .= '
';
    $html .= '
'.$members.'
';
    if ($result['description'] != '') {
        $html .= '
'.cut($result['description'], 100, true).'
';
    } else {
        $html .= '';
    }
    //Avoiding my groups
    if (!in_array($id, $my_group_list)) {
        $html .= '
'.get_lang('JoinGroup').' ';
    }
    $html .= '
'.$join_url.'
';
    $html .= '
';
    $html .= '
 ';
    $grid_item_2 = $html;
    $grid_newest_groups[] = array($grid_item_2);
}
// Pop groups
$results = $usergroup->get_groups_by_popularity(4, false);
$grid_pop_groups = array();
if (is_array($results) && count($results) > 0) {
    foreach ($results as $result) {
        $result['name'] = Security::remove_XSS($result['name'], STUDENT, true);
        $result['description'] = Security::remove_XSS($result['description'], STUDENT, true);
        $id = $result['id'];
        $name = cut($result['name'], GROUP_TITLE_LENGTH, true);
        $count_users_group = count(
            $usergroup->get_users_by_group(
                $id,
                false,
                array(
                    GROUP_USER_PERMISSION_ADMIN,
                    GROUP_USER_PERMISSION_READER,
                    GROUP_USER_PERMISSION_MODERATOR,
                ),
                0,
                1000
            )
        );
        if ($count_users_group == 1) {
            $count_users_group = $count_users_group.' '.get_lang('Member');
        } else {
            $count_users_group = $count_users_group.' '.get_lang('Members');
        }
        $url = ''.$name.'';
        $picture = $usergroup->get_picture_group($result['id'], $result['picture'], 80);
        $result['picture'] = '
';
        $html = '';
        $html .= '
';
        $html .= $result['picture'];
        $html .= '
';
        $html .= '
';
        $html .= '
';
        $html .= Display::tag('h5', $url);
        $html .= '
';
        $html .= '
'.$members.'
';
        if ($result['description'] != '') {
            $html .= '
'.cut($result['description'], 100, true).'
';
        } else {
            $html .= '';
        }
        //Avoiding my groups
        if (!in_array($id, $my_group_list)) {
            $html .= '
'.get_lang('JoinGroup').' ';
        }
        $html .= '
'.$join_url.'
';
        $html .= '
';
        $html .= '
 ';
        $grid_item_2 = $html;
        $grid_pop_groups[] = array($grid_item_2);
    }
}
// Display groups (newest, mygroups, pop)
$query_vars = array();
$newest_content = $popular_content = $my_group_content = null;
if (isset($_GET['view']) && in_array($_GET['view'], $allowed_views)) {
    $view_group = $_GET['view'];
    switch ($view_group) {
        case 'mygroups':
            if (count($grid_my_groups) > 0) {
                $my_group_content = Display::return_sortable_grid(
                    'mygroups',
                    array(),
                    $grid_my_groups,
                    array('hide_navigation' => true, 'per_page' => 2),
                    $query_vars,
                    false,
                    array(true, true, true, false)
                );
            }
            if (api_get_setting('allow_students_to_create_groups_in_social') == 'true') {
                $create_group_item = ''.
                    get_lang('CreateASocialGroup').'';
            } else {
                if (api_is_allowed_to_edit(null, true)) {
                    $create_group_item = ''.
                        get_lang('CreateASocialGroup').'';
                }
            }
            break;
        case 'newest':
            if (count($grid_newest_groups) > 0) {
                $newest_content = Display::return_sortable_grid('newest', array(), $grid_newest_groups, array('hide_navigation'=>true, 'per_page' => 100), $query_vars, false, array(true, true, true, false));
            }
            break;
        default:
            if (count($grid_pop_groups) > 0) {
                $popular_content = Display::return_sortable_grid('popular', array(), $grid_pop_groups, array('hide_navigation'=>true, 'per_page' => 100), $query_vars, false, array(true, true, true, true, true));
            }
            break;
    }
} else {
    $my_group_content = null;
    if (count($grid_my_groups) > 0) {
        $my_group_content = Display::return_sortable_grid('mygroups', array(), $grid_my_groups, array('hide_navigation'=>true, 'per_page' => 2), $query_vars, false, array(true, true, true, false));
    } else {
        $my_group_content = ''.get_lang('GroupNone').'';
    }
    if (api_get_setting('allow_students_to_create_groups_in_social') == 'true') {
        $create_group_item = ''.
            get_lang('CreateASocialGroup').'';
    } else {
        if (api_is_allowed_to_edit(null, true)) {
            $create_group_item = ''.get_lang('CreateASocialGroup').'';
        }
    }
    if (count($grid_newest_groups) > 0) {
        $newest_content = Display::return_sortable_grid('mygroups', array(), $grid_newest_groups, array('hide_navigation'=>true, 'per_page' => 100), $query_vars, false, array(true, true, true, false));
    } else {
        $newest_content = ''.get_lang('GroupNone').'
';
    }
    if (count($grid_pop_groups) > 0) {
        $popular_content = Display::return_sortable_grid('mygroups', array(), $grid_pop_groups, array('hide_navigation'=>true, 'per_page' => 100), $query_vars, false, array(true, true, true, true, true));
    } else {
        $popular_content = ''.get_lang('GroupNone').'
';
    }
}
if (!empty($create_group_item)) {
    $social_right_content .= Display::page_subheader($create_group_item);
}
$headers = array(get_lang('Newest'), get_lang('Popular'), get_lang('MyGroups'));
$social_right_content .= Display::tabs($headers, array($newest_content, $popular_content, $my_group_content), 'tab_browse');
$tpl = new Template(null);
// Block Social Avatar
SocialManager::setSocialUserBlock($tpl, api_get_user_id(), $show_menu);
$show_menu = 'browse_groups';
if (isset($_GET['view']) && $_GET['view'] == 'mygroups') {
    $show_menu = $_GET['view'];
}
$social_menu_block = SocialManager::show_social_menu($show_menu);
$templateName = 'social/groups.tpl';
$tpl->setHelp('Groups');
$tpl->assign('social_menu_block', $social_menu_block);
$tpl->assign('social_right_content', $social_right_content);
$social_layout = $tpl->get_template($templateName);
$tpl->display($social_layout);