/**
 * Returns search results for users in a particular group
 *
 * The search term is applied against first and last names of the users in the group
 *
 * @param int    $group             The group to build results for
 * @param string $query             A search string to filter by
 * @param int    $offset            What result to start showing paginated results from
 * @param int    $limit             How many results to show
 * @param array  $membershiptype    User membershiptype
 * @param bool   $random            Set to true if you want the result to be ordered by random, default false
 * @param int    $friendof          Only return friends of this user
 *
 */
function get_group_user_search_results($group, $query, $offset, $limit, $membershiptype, $order = null, $friendof = null, $sortoptionidx = null)
{
    $plugin = get_config('searchplugin');
    safe_require('search', $plugin);
    $searchclass = generate_class_name('search', $plugin);
    $constraints = array();
    if (call_static_method($searchclass, 'can_process_raw_group_search_user_queries')) {
        // Pass the raw query string through to group_search_user; parsing of the
        // query depends on the plugin configuration.
        $queries = $query;
    } else {
        $queries = array();
        if (!empty($query)) {
            list($words, $fullnames) = parse_name_query($query);
            foreach ($words as $word) {
                $queries[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $word);
                $queries[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $word);
            }
            foreach ($fullnames as $n) {
                $constraints[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $n[0]);
                $constraints[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $n[1]);
            }
        }
    }
    $results = call_static_method($searchclass, 'group_search_user', $group, $queries, $constraints, $offset, $limit, $membershiptype, $order, $friendof, $sortoptionidx);
    if ($results['count']) {
        $userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']);
        $introductions = get_records_sql_assoc("SELECT \"owner\", title\n            FROM {artefact}\n            WHERE artefacttype = 'introduction'\n            AND \"owner\" IN (" . implode(',', db_array_to_ph($userids)) . ')', $userids);
        foreach ($results['data'] as &$result) {
            $result['name'] = display_name($result);
            $result['introduction'] = isset($introductions[$result['id']]) ? $introductions[$result['id']]->title : '';
            if (isset($result['jointime'])) {
                $result['jointime'] = format_date($result['jointime'], 'strftimedate');
            }
        }
    }
    return $results;
}
/**
 * Returns search results for users in a particular group
 *
 * The search term is applied against first and last names of the users in the group
 *
 * @param int    $group             The group to build results for
 * @param string $query             A search string to filter by
 * @param int    $offset            What result to start showing paginated results from
 * @param int    $limit             How many results to show
 * @param array  $membershiptype    User membershiptype
 * @param bool   $random            Set to true if you want the result to be ordered by random, default false
 *
 */
function get_group_user_search_results($group, $query, $offset, $limit, $membershiptype, $order = null)
{
    $queries = array();
    $constraints = array();
    if (!empty($query)) {
        list($words, $fullnames) = parse_name_query($query);
        foreach ($words as $word) {
            $queries[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $word);
            $queries[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $word);
        }
        foreach ($fullnames as $n) {
            $constraints[] = array('field' => 'firstname', 'type' => 'contains', 'string' => $n[0]);
            $constraints[] = array('field' => 'lastname', 'type' => 'contains', 'string' => $n[1]);
        }
    }
    $results = group_user_search($group, $queries, $constraints, $offset, $limit, $membershiptype, $order);
    if ($results['count']) {
        $userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']);
        $introductions = get_records_sql_assoc("SELECT \"owner\", title\n            FROM {artefact}\n            WHERE artefacttype = 'introduction'\n            AND \"owner\" IN (" . implode(',', db_array_to_ph($userids)) . ')', $userids);
        foreach ($results['data'] as &$result) {
            $result['name'] = display_name($result);
            $result['introduction'] = isset($introductions[$result['id']]) ? $introductions[$result['id']]->title : '';
            if (isset($result['jointime'])) {
                $result['jointime'] = strftime(get_string('strftimedate'), $result['jointime']);
            }
        }
    }
    return $results;
}