registerFunction('search');
// 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'));
// Setting the name of the tool.
$tool_name = get_lang('SubscribeClassToCourses');
$add_type = 'multiple';
if (isset($_REQUEST['add_type']) && $_REQUEST['add_type']!=''){
    $add_type = Security::remove_XSS($_REQUEST['add_type']);
}
$add = isset($_GET['add']) ? Security::remove_XSS($_GET['add']) : null;
$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
$htmlHeadXtra[] = '';
$form_sent  = 0;
$errorMsg   = '';
$sessions = array();
$usergroup = new UserGroup();
$id = intval($_GET['id']);
if (isset($_POST['form_sent']) && $_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) {
        $usergroup->subscribe_courses_to_usergroup($id, $elements_posted);
        header('Location: usergroups.php');
        exit;
    }
}
// Filters
$filters = array(
    array('type' => 'text', 'name' => 'code', 'label' => get_lang('CourseCode')),
    array('type' => 'text', 'name' => 'title', 'label' => get_lang('Title')),
    /*array('type' => 'text', 'name' => 'lastname', 'label' => get_lang('LastName')),
    array('type' => 'text', 'name' => 'official_code', 'label' => get_lang('OfficialCode')),
    array('type' => 'text', 'name' => 'email', 'label' => get_lang('Email'))*/
);
$searchForm = new FormValidator('search', 'get', api_get_self().'?id='.$id);
$searchForm->addHeader(get_lang('AdvancedSearch'));
$renderer =& $searchForm->defaultRenderer();
$searchForm->addElement('hidden', 'id', $id);
foreach ($filters as $param) {
    $searchForm->addElement($param['type'], $param['name'], $param['label']);
}
$searchForm->addElement('button', 'submit', get_lang('Search'));
$filterData = array();
if ($searchForm->validate()) {
    $filterData = $searchForm->getSubmitValues();
}
$conditions = array();
if (!empty($filters) && !empty($filterData)) {
    foreach ($filters as $filter) {
        if (isset($filter['name']) && isset($filterData[$filter['name']])) {
            $value = $filterData[$filter['name']];
            if (!empty($value)) {
                $conditions[$filter['name']] = $value;
            }
        }
    }
}
$data = $usergroup->get($id);
$course_list_in = $usergroup->get_courses_by_usergroup($id, true);
$course_list = CourseManager::get_courses_list(0, 0, 'title', 'asc', -1, null, api_get_current_access_url_id(), false, $conditions);
$elements_not_in = $elements_in = array();
foreach ($course_list_in as $course) {
    $elements_in[$course['id']] = $course['title']." (".$course['visual_code'].")";
}
if (!empty($course_list)) {
    foreach ($course_list as $item) {
        $elements_not_in[$item['id']] = $item['title']." (".$item['visual_code'].")";
    }
}
$ajax_search = $add_type == 'unique' ? true : false;
//checking for extra field with filter on
function search($needle,$type)
{
    global $elements_in;
    $xajax_response = new xajaxResponse();
    $return = '';
    if (!empty($needle) && !empty($type)) {
        // xajax send utf8 datas... datas in db can be non-utf8 datas
        $charset = api_get_system_encoding();
        $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 {
            $list = CourseManager::get_courses_list(0, 0, 2, 'ASC', -1, $needle);
        }
        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 '