$navigation = build_navigation($extranavigation);
print_header_simple(get_string('searchresults'), "", $navigation);
$querytext = stripslashes(required_param('query', PARAM_RAW));
$query = new ousearch_search($querytext);
if (strpos($plugin, 'mod/') === 0) {
    $modname = substr($plugin, 4);
} else {
    $modname = null;
}
$query->set_visible_modules_in_course($COURSE, $modname);
// Restrict them to the groups they belong to
if (!isset($USER->groupmember[$courseid])) {
    $query->set_group_ids(array());
} else {
    $query->set_group_ids($USER->groupmember[$courseid]);
}
// Add exceptions where they can see other groups
$query->set_group_exceptions(ousearch_get_group_exceptions($courseid));
$query->set_user_id($USER->id);
$query->set_plugin($plugin);
ousearch_display_results($query, 'search.php?course=' . $courseid . '&plugin=' . $plugin);
//Print advanced search link
if ($plugin == 'mod/forumng') {
    $querytext = rawurlencode($querytext);
    $options = "course={$courseid}&query={$querytext}";
    $url = $CFG->wwwroot . '/mod/forumng/advancedsearch.php?' . $options;
    $strlink = get_string('moresearchoptions', 'forumng');
    print "<div class='advanced-search-link'>\n            <a href=\"{$url}\">{$strlink}</a></div>";
}
// Footer
print_footer();
                }
                $current = $rec;
            }
        }
    }
    // Don't forget the last one
    if ($current && $current->permission > 0) {
        $coursemodules[$current->id] = $current;
    }
    rs_close($rs);
    return $coursemodules;
}
// Set up basic search with specified query
$search = new ousearch_search($query);
// Set up list of accessible groups and user ID
$groupids = array();
$rs = get_recordset_sql("\nSELECT\n  gm.groupid\nFROM \n  {$CFG->prefix}groups_members gm\nWHERE\n  gm.userid={$userid}  \n");
while ($rec = rs_fetch_next_record($rs)) {
    $groupids[] = $rec->id;
}
rs_close($rs);
$search->set_group_ids($groupids);
$search->set_user_id($userid);
// Get array of course-module info
$accessible = get_all_accessible_modules_of_type('ouwiki', 'mod/ouwiki:view', $userid);
$search->set_coursemodule_array($accessible);
// Get exceptions where user can access all groups
$allgroups = get_all_accessible_modules_of_type('ouwiki', 'moodle/site:accessallgroups', $userid, 'cm.groupmode<>0');
$search->set_group_exceptions($allgroups);
$results = $search->query(0, OUWIKI_MAXRESULTS);
ousearch_display_remote_results($results, $first, OUWIKI_RESULTSPERPAGE);
 // Searching for free text with or without filtering author and date range
 if ($query) {
     $result = new ousearch_search(stripslashes($query));
     // Search all forums
     if ($allforums) {
         $result->set_plugin('mod/forumng');
         $result->set_course_id($courseid);
         $result->set_visible_modules_in_course($COURSE);
         // Restrict them to the groups they belong to
         if (!isset($USER->groupmember[$courseid])) {
             $result->set_group_ids(array());
         } else {
             $result->set_group_ids($USER->groupmember[$courseid]);
         }
         // Add exceptions where they can see other groups
         $result->set_group_exceptions(ousearch_get_group_exceptions($courseid));
         $result->set_user_id($USER->id);
     } else {
         // Search this forum
         $result->set_coursemodule($forum->get_course_module(true));
         if ($groupid && $groupid != forum::NO_GROUPS) {
             $result->set_group_id($groupid);
         }
     }
     $result->set_filter('forumng_exclude_words_filter');
     ousearch_display_results($result, $url . $urloptions, $searchtitle);
     // Searching without free text uding author and/or date range
 } elseif ($action) {
     $page = optional_param('page', 0, PARAM_INT);
     $prevpage = $page - FORUMNG_SEARCH_RESULTSPERPAGE;
     $prevrange = $page - FORUMNG_SEARCH_RESULTSPERPAGE + 1 . ' - ' . $page;