debugOn();
$xajax->registerFunction('search_sessions');
// setting the section (for the tabs)
$this_section = SECTION_PLATFORM_ADMIN;
// Access restrictions
api_protect_admin_script(true);
// setting breadcrumbs
$interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin'));
$interbreadcrumb[]=array('url' => 'usergroups.php','name' => get_lang('Classes'));
// Database Table Definitions
// setting the name of the tool
$tool_name=get_lang('SubscribeClassToSessions');
$add_type = 'multiple';
if(isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){
    $add_type = Security::remove_XSS($_REQUEST['add_type']);
}
$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
$htmlHeadXtra[] = '
';
$form_sent  = 0;
$errorMsg   = '';
$sessions=array();
$usergroup = new UserGroup();
$id = intval($_GET['id']);
if($_POST['form_sent']) {
    $form_sent          = $_POST['form_sent'];    
    $elements_posted    = $_POST['elements_in_name'];     
    if (!is_array($elements_posted)) {
        $elements_posted = array();
    }
    if ($form_sent == 1) {
        //added a parameter to send emails when registering a user        
        $usergroup->subscribe_sessions_to_usergroup($id, $elements_posted);
        header('Location: usergroups.php');
        exit;        
    }
}
$data               = $usergroup->get($id);
$session_list_in    = $usergroup->get_sessions_by_usergroup($id);
$session_list       = SessionManager::get_sessions_list(array(), array('name'));
//api_display_tool_title($tool_name.' ('.$session_info['name'].')');
$elements_not_in = $elements_in= array();
if (!empty($session_list)) {
    foreach($session_list as $session) {        
        if (in_array($session['id'], $session_list_in)) {            
            $elements_in[$session['id']] = $session['name'];             
        } else {
            $elements_not_in[$session['id']] = $session['name'];
        }
    }
}
$ajax_search = $add_type == 'unique' ? true : false;
//checking for extra field with filter on
function search_sessions($needle,$type) {
    global $tbl_user,$elements_in;
    $xajax_response = new XajaxResponse();
    $return = '';
    if (isset($needle) && !empty($type)) {
        // xajax send utf8 datas... datas in db can be non-utf8 datas
        $charset = api_get_system_encoding();
        $needle  = Database::escape_string($needle);
        $needle  = api_convert_encoding($needle, $charset, 'utf-8');
        if ($type == 'single') {
            // search users where username or firstname or lastname begins likes $needle
          /*  $sql = 'SELECT user.user_id, username, lastname, firstname FROM '.$tbl_user.' user
                    WHERE (username LIKE "'.$needle.'%"
                    OR firstname LIKE "'.$needle.'%"
                OR lastname LIKE "'.$needle.'%") AND user.user_id<>"'.$user_anonymous.'"   AND user.status<>'.DRH.''.
                $order_clause.
                ' LIMIT 11';*/
        } else if ($type == 'searchbox') {
            $session_list = SessionManager::get_sessions_list(array('s.name LIKE' => "%$needle%"));
        }
        else {
            $session_list = SessionManager::get_sessions_list(array('s.name LIKE' => "$needle%"));
        }
        $i=0;        
        if ($type=='single') {
            /*
            while ($user = Database :: fetch_array($rs)) {
                $i++;
                if ($i<=10) {
                    $person_name = api_get_person_name($user['firstname'], $user['lastname']);
                    $return .= ''.$person_name.' ('.$user['username'].')
';
                } else {
                    $return .= '...
';
                }
            }
            $xajax_response -> addAssign('ajax_list_users_single','innerHTML',api_utf8_encode($return));*/
        } else {
            $return .= '';
            $xajax_response -> addAssign('ajax_list_multiple','innerHTML',api_utf8_encode($return));
        }
    }
    return $xajax_response;
}
$xajax -> processRequests();
Display::display_header($tool_name);
if ($add_type == 'multiple') {
    $link_add_type_unique = ''.Display::return_icon('single.gif').get_lang('SessionAddTypeUnique').'';
    $link_add_type_multiple = Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple');
} else {
    $link_add_type_unique = Display::return_icon('single.gif').get_lang('SessionAddTypeUnique');
    $link_add_type_multiple = ''.Display::return_icon('multiple.gif').get_lang('SessionAddTypeMultiple').'';
}
echo '