Пример #1
0
/**
 * Search with filter and build base array avoiding repeated terms
 * @param array $filter XapianQuery array
 * @param array $specific_fields
 * @return array $sf_terms
 */
function get_usual_sf_terms($filter, $specific_fields)
{
    $sf_terms = array();
    $dkterms = chamilo_query_simple_query('', 0, 1000, $filter);
    if (is_array($dkterms) && is_array($dkterms[1])) {
        foreach ($specific_fields as $specific_field) {
            foreach ($dkterms[1] as $obj) {
                foreach ($obj['sf-' . $specific_field['code']] as $raw_term) {
                    if (count($raw_term['name']) > 1) {
                        $normal_term = substr($raw_term['name'], 1);
                        $sf_terms[$specific_field['code']][$normal_term] = $normal_term;
                    }
                }
            }
        }
    }
    return $sf_terms;
}
Пример #2
0
/**
 * Show the search widget
 *
 * The form will post to index.php by default, you can pass a value to
 * $action to use a custom action.
 * IMPORTANT: you have to call search_widget_prepare() before calling this
 * function or otherwise the form will not behave correctly.
 *
 * @param   string $action     Just in case your action is not
 * index.php
 */
function search_widget_show($action = 'index.php')
{
    require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloQuery.php';
    // TODO: load images dinamically when they're avalaible from specific field ui to add
    $groupId = api_get_group_id();
    $sf_terms = array();
    $specific_fields = get_specific_field_list();
    $url_params = array();
    if (($cid = api_get_course_id()) != -1) {
        // with cid
        // get search engine terms
        $course_filter = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID . $cid);
        $dkterms = chamilo_query_simple_query('', 0, 1000, array($course_filter));
        //prepare specific fields names (and also get possible URL param names)
        foreach ($specific_fields as $specific_field) {
            $temp = array();
            if (is_array($dkterms) && count($dkterms) > 0) {
                foreach ($dkterms[1] as $obj) {
                    $temp = array_merge($obj['sf-' . $specific_field['code']], $temp);
                }
            }
            $sf_terms[$specific_field['code']] = $temp;
            $url_params[] = 'sf_' . $specific_field['code'];
            unset($temp);
        }
    } else {
        // without cid
        // prepare specific fields names (and also get possible URL param names)
        foreach ($specific_fields as $specific_field) {
            //get Xapian terms for a specific term prefix, in ISO, apparently
            $sf_terms[$specific_field['code']] = xapian_get_all_terms(1000, $specific_field['code']);
            $url_params[] = 'sf_' . $specific_field['code'];
        }
    }
    echo '<h2>' . get_lang('Search') . '</h2>';
    // Tool introduction
    // TODO: Settings for the online editor to be checked (insert an image for example). Probably this is a special case here.
    if (api_get_course_id() !== -1) {
        if (!empty($groupId)) {
            Display::display_introduction_section(TOOL_SEARCH . $groupId);
        } else {
            Display::display_introduction_section(TOOL_SEARCH);
        }
    }
    $op = 'or';
    if (!empty($_REQUEST['operator']) && in_array($op, array('or', 'and'))) {
        $op = $_REQUEST['operator'];
    }
    //check if URL params are defined (to see if we show the thesaurus or not)
    $show_thesaurus = false;
    foreach ($url_params as $param) {
        if (isset($_REQUEST[$param]) && is_array($_REQUEST[$param])) {
            $thesaurus_decided = FALSE;
            foreach ($_REQUEST[$param] as $term) {
                if (!empty($term)) {
                    $show_thesaurus = true;
                    $thesaurus_decided = TRUE;
                    break;
                }
            }
            if ($thesaurus_decided) {
                break;
            }
        }
    }
    // create the form
    // TODO: use FormValidator
    display_search_form($action, $show_thesaurus, $sf_terms, $op);
}