$(document).ready( function() {
    var i;
	for (i=0; i<$(".actions").length; i++) {
		if ($(".actions:eq("+i+")").html()=="
" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
			$(".actions:eq("+i+")").hide();
		}
	}
});
 ';
$nameTools = get_lang('GroupManagement');
$course_id = api_get_course_int_id();
/*
 * Self-registration and un-registration
 */
$my_group_id = isset($_GET['group_id']) ? intval($_GET['group_id']) : null;
$my_msg	= isset($_GET['msg']) ? Security::remove_XSS($_GET['msg']) : null;
$my_group = isset($_REQUEST['group']) ? Security::remove_XSS($_REQUEST['group']) : null;
$my_get_id1 = isset($_GET['id1']) ? Security::remove_XSS($_GET['id1']) : null;
$my_get_id2 = isset($_GET['id2']) ? Security::remove_XSS($_GET['id2']) : null;
$my_get_id  = isset($_GET['id']) ? Security::remove_XSS($_GET['id']) : null;
$currentUrl = api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq();
if (isset($_GET['action']) && $is_allowed_in_course) {
    switch ($_GET['action']) {
        case 'set_visible':
            if (api_is_allowed_to_edit()) {
                GroupManager::setVisible($my_get_id);
                Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
                header("Location: $currentUrl");
                exit;
            }
            break;
        case 'set_invisible':
            if (api_is_allowed_to_edit()) {
                GroupManager::setInvisible($my_get_id);
                Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
                header("Location: $currentUrl");
                exit;
            }
            break;
        case 'self_reg':
            if (GroupManager::is_self_registration_allowed($userId, $my_group_id)) {
                GroupManager::subscribe_users($userId, $my_group_id);
                Display::addFlash(Display::return_message(get_lang('GroupNowMember')));
                header("Location: $currentUrl");
                exit;
            }
            break;
        case 'self_unreg':
            if (GroupManager::is_self_unregistration_allowed($userId, $my_group_id)) {
                GroupManager::unsubscribe_users($userId, $my_group_id);
                Display::addFlash(Display::return_message(get_lang('StudentDeletesHimself')));
                header("Location: $currentUrl");
                exit;
            }
            break;
    }
}
/*
 * Group-admin functions
 */
if (api_is_allowed_to_edit(false, true)) {
    // Post-actions
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'delete_selected':
                if (is_array($_POST['group'])) {
                    GroupManager::delete_groups($my_group);
                    Display::addFlash(Display::return_message(get_lang('SelectedGroupsDeleted')));
                    header("Location: $currentUrl");
                    exit;
                }
                break;
            case 'empty_selected':
                if (is_array($_POST['group'])) {
                    GroupManager :: unsubscribe_all_users($my_group);
                    Display::addFlash(Display::return_message(get_lang('SelectedGroupsEmptied')));
                    header("Location: $currentUrl");
                    exit;
                }
                break;
            case 'fill_selected':
                if (is_array($_POST['group'])) {
                    GroupManager :: fill_groups($my_group);
                    Display::addFlash(Display::return_message(get_lang('SelectedGroupsFilled')));
                    header("Location: $currentUrl");
                    exit;
                }
                break;
        }
    }
    // Get-actions
    if (isset($_GET['action'])) {
        switch ($_GET['action']) {
            case 'swap_cat_order':
                GroupManager :: swap_category_order($my_get_id1, $my_get_id2);
                Display::addFlash(Display::return_message(get_lang('CategoryOrderChanged')));
                header("Location: $currentUrl");
                exit;
                break;
            case 'delete_one':
                GroupManager :: delete_groups($my_get_id);
                Display::addFlash(Display::return_message(get_lang('GroupDel')));
                header("Location: $currentUrl");
                exit;
                break;
            case 'fill_one':
                GroupManager :: fill_groups($my_get_id);
                Display::addFlash(Display::return_message(get_lang('GroupFilledGroups')));
                header("Location: $currentUrl");
                exit;
                break;
            case 'delete_category':
                GroupManager :: delete_category($my_get_id);
                Display::addFlash(Display::return_message(get_lang('CategoryDeleted')));
                header("Location: $currentUrl");
                exit;
                break;
        }
    }
}
/*	Header */
Display::display_header(get_lang('Groups'));
// Tool introduction
Display::display_introduction_section(TOOL_GROUP);
$actionsLeft = '';
if (api_is_allowed_to_edit(false, true)) {
    $actionsLeft .= ''.
        Display::return_icon('add.png', get_lang('NewGroupCreate'), '', ICON_SIZE_MEDIUM).'';
    if (api_get_setting('allow_group_categories') == 'true') {
        $actionsLeft .=  ''.
            Display::return_icon('new_folder.png', get_lang('AddCategory'), '', ICON_SIZE_MEDIUM).'';
    } else {
        $actionsLeft .= ''.
            Display::return_icon('settings.png', get_lang('PropModify'), '', ICON_SIZE_MEDIUM).'';
    }
    $actionsLeft .= ''.
        Display::return_icon('import_csv.png', get_lang('Import'), '', ICON_SIZE_MEDIUM).'';
    $actionsLeft .= ''.
        Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), '', ICON_SIZE_MEDIUM).'';
    $actionsLeft .= ''.
        Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), '', ICON_SIZE_MEDIUM).'';
    $actionsLeft .= ''.
        Display::return_icon('pdf.png', get_lang('ExportToPDF'), '', ICON_SIZE_MEDIUM).'';
    $actionsLeft .= ''.
        Display::return_icon('group_summary.png', get_lang('GroupOverview'), '', ICON_SIZE_MEDIUM).'';
}
$actionsRight = GroupManager::getSearchForm();
$toolbar = Display::toolbarAction('toolbar-groups', $content = array( 0 => $actionsLeft, 1 => $actionsRight ));
$group_cats = GroupManager::get_categories(api_get_course_id());
echo $toolbar;
echo UserManager::getUserSubscriptionTab(3);
/*  List all categories */
if (api_get_setting('allow_group_categories') == 'true') {
    $defaultCategory = [
        'id' => 0,
        'iid' => 0,
        'description' => '',
        'title' => get_lang('DefaultGroupCategory')
    ];
    $group_cats = array_merge([$defaultCategory], $group_cats);
    foreach ($group_cats as $index => $category) {
        $categoryId = $category['id'];
        $group_list = GroupManager::get_group_list($categoryId);
        $groupToShow = GroupManager::process_groups($group_list, $categoryId);
        if (empty($groupToShow)) {
            continue;
        }
        $label = Display::label(count($group_list).' '.get_lang('ExistingGroups'), 'info');
        $actions = null;
        if (api_is_allowed_to_edit(false, true) && !empty($categoryId)) {
            $actions .= ''.
                Display::return_icon('edit.png', get_lang('EditGroup'),'',ICON_SIZE_SMALL).'';
            $actions .=
                Display::url(
                    Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL),
                    'group.php?'.api_get_cidreq().'&action=delete_category&id='.$categoryId,
                    array(
                        'onclick' => 'javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;'
                    )
                );
            if ($index != 0) {
                $actions .=  ' '.
                    Display::return_icon('up.png',' ','',ICON_SIZE_SMALL).'';
            }
            if ($index != count($group_cats) - 1) {
                $actions .= ' '.
                    Display::return_icon('down.png',' ','',ICON_SIZE_SMALL).'';
            }
        }
        echo Display::page_header(
            Security::remove_XSS($category['title'].' '. $label.' ').$actions,
            null,
            'h4',
            false
        );
        echo $category['description'];
        echo $groupToShow;
    }
} else {
    $group_list = GroupManager::get_group_list();
    echo GroupManager::process_groups($group_list);
}
if (!isset($_GET['origin']) || $_GET['origin'] != 'learnpath') {
    Display::display_footer();
}
$_SESSION['_gid'] = 0;