if ($current && $current->permission > 0) {
                    $coursemodules[$current->id] = $current;
                }
                $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);
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package ouwiki
 */
/** */
require 'basicpage.php';
if (class_exists('ouflags')) {
    require_once '../../local/mobile/ou_lib.php';
    global $OUMOBILESUPPORT;
    $OUMOBILESUPPORT = true;
    ou_set_is_mobile(ou_get_is_mobile_from_cookies());
    if (ou_get_is_mobile()) {
        ou_mobile_configure_theme();
    }
}
ouwiki_print_start($ouwiki, $cm, $course, $subwiki, get_string('searchresults'), $context);
require_once '../../blocks/ousearch/searchlib.php';
$querytext = stripslashes(required_param('query', PARAM_RAW));
$query = new ousearch_search($querytext);
$query->set_coursemodule($cm);
if ($subwiki->groupid) {
    $query->set_group_id($subwiki->groupid);
}
if ($subwiki->userid) {
    $query->set_user_id($subwiki->userid);
}
$foundsomething = ousearch_display_results($query, 'search.php?' . ouwiki_display_wiki_parameters(null, $subwiki, $cm));
// Footer
ouwiki_print_footer($course, $cm, $subwiki, null, 'search.php?query=' . urlencode($querytext), $foundsomething ? null : 'searchfailure', $querytext);
?>

// Lets users search everything (that uses ousearch) on a course.
require_once '../../config.php';
require_once 'searchlib.php';
// User must be logged in
$courseid = required_param('course', PARAM_INT);
$plugin = optional_param('plugin', '', PARAM_RAW);
// User must be logged in
$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
require_login($courseid);
$extranavigation = array();
$extranavigation[] = array('name' => get_string('searchresults'), 'type' => 'misc');
$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);
        $average = round($score->time / $score->count, 2);
        print "<div>{$selects} queries ({$results->dbstart} rows used, {$results->dbrows} read), average {$average} s</div><ul>";
        if ($results->success) {
            foreach ($results->results as $result) {
                if ($result->title === '') {
                    $result->title = '(Blank line title)';
                }
                print '<li><div style="background:#eee">' . str_replace('highlight>', 'strong>', $result->title) . '</div><div>' . str_replace('highlight>', 'strong>', $result->summary) . ' [<a href="' . htmlspecialchars($result->url) . '">View</a>]</div></li>';
            }
        } else {
            print '<li>Query failed: <strong>' . $results->problemword . '</strong></li>';
        }
        print '</ul>';
    }
}
$sh = new ousearch_search('"the time"');
$sh->set_plugin('test/test2');
$tests = array(new ousearch_search('a and'), new ousearch_search('23dfbsdg3456 and'), new ousearch_search('and 23dfbsdg3456'), new ousearch_search('virulent attack'), new ousearch_search('regiment moved'), new ousearch_search('adjutant galloping'), new ousearch_search('adjutant galloping -Napoleon'), new ousearch_search('adjutant galloping -"where Napoleon was standing"'), new ousearch_search('about an hour'), new ousearch_search('"about an hour"'), new ousearch_search('about french hour were an'), new ousearch_search('and'), new ousearch_search('"a and"'), new ousearch_search('"and maidens"'), new ousearch_search('"the time"'), $sh);
//$tests=array(new ousearch_search('"a and"'));
$results = array();
foreach ($tests as $test) {
    $blankresult = new StdClass();
    $blankresult->time = 0.0;
    $blankresult->count = 0;
    $results[] = $blankresult;
}
// Test loop
define('OUSEARCH_TESTLOOPS', 3);
print '<h1>Running tests ' . OUSEARCH_TESTLOOPS . ' times</h1>';
flush();
for ($i = 0; $i < OUSEARCH_TESTLOOPS; $i++) {
    $forum = forum::get_from_cmid($cmid, $cloneid);
    $cm = $forum->get_course_module();
    $course = $forum->get_course();
    $groupid = forum::get_activity_group($cm, true);
    $forum->require_view($groupid, 0, true);
    forum::search_installed();
    // Search form for header
    $buttontext = $forum->display_search_form($querytext);
    // Display header
    $navigation = array();
    $navigation[] = array('name' => get_string('searchfor', 'block_ousearch', $querytext), 'type' => 'forumng');
    print_header_simple(format_string($forum->get_name()), '', build_navigation($navigation, $cm), '', '', true, $buttontext, navmenu($course, $cm));
    // Display group selector if required
    groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/forumng/search.php?' . $forum->get_link_params(forum::PARAM_HTML) . '&amp;query=' . rawurlencode($querytext));
    $searchurl = 'search.php?' . $forum->get_link_params(forum::PARAM_PLAIN);
    $query = new ousearch_search($querytext);
    $query->set_coursemodule($forum->get_course_module(true));
    if ($groupid && $groupid != forum::NO_GROUPS) {
        $query->set_group_id($groupid);
    }
    ousearch_display_results($query, $searchurl);
    //Print advanced search link
    $options = $forum->get_link_params(forum::PARAM_HTML);
    $options .= '&amp;action=0';
    $options .= $querytext ? '&amp;query=' . rawurlencode($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>";
    print_footer($course);
} catch (forum_exception $e) {
    forum_utils::handle_exception($e);
 function set_query($query)
 {
     $this->querytext = $query;
     // Clear the existing arrays
     $this->terms = array();
     $this->negativeterms = array();
     // Refill those arrays from the query text
     $words = ousearch_document::split_words($query, true);
     $currentquote = array();
     $sign = false;
     $inquote = false;
     $tl = textlib_get_instance();
     foreach ($words as $word) {
         // Clean word to get rid of +, ", and - except if it's in the middle
         $cleaned = preg_replace('/(^-)|(-$)/', '', preg_replace('/[+"]/', '', $word));
         // Shorten word if necessary to db length
         $cleaned = $tl->substr($cleaned, 0, OUSEARCH_DB_MAX_WORD_LENGTH);
         if ($inquote) {
             // Handle hyphenated words
             if (strpos($cleaned, '-') !== false) {
                 foreach (explode('-', $cleaned) as $subword) {
                     $currentquote[] = $subword;
                 }
             } else {
                 $currentquote[] = $cleaned;
             }
             if (substr($word, strlen($word) - 1, 1) == '"') {
                 $term = new StdClass();
                 $term->words = $currentquote;
                 switch ($sign) {
                     case '+':
                         $term->required = true;
                         $this->terms[] = $term;
                         break;
                     case '':
                         $term->required = false;
                         $this->terms[] = $term;
                         break;
                     case '-':
                         $term->required = true;
                         $this->negativeterms[] = $term;
                         break;
                 }
                 $inquote = false;
             }
         } else {
             $firstchar = substr($word, 0, 1);
             $secondchar = strlen($word) > 1 ? substr($word, 1, 1) : false;
             if ($firstchar == '"') {
                 // "a phrase"
                 $currentquote = ousearch_search::internal_hyphenated_array($cleaned);
                 $inquote = true;
                 $sign = '';
             } else {
                 if ($firstchar == '+' && $secondchar == '"') {
                     // +"a phrase"
                     $currentquote = ousearch_search::internal_hyphenated_array($cleaned);
                     $inquote = true;
                     $sign = '+';
                 } else {
                     if ($firstchar == '-' && $secondchar == '"') {
                         // -"a phrase"
                         $currentquote = ousearch_search::internal_hyphenated_array($cleaned);
                         $inquote = true;
                         $sign = '-';
                     } else {
                         if ($firstchar == '+') {
                             // +cat
                             $term = new StdClass();
                             $term->words = ousearch_search::internal_hyphenated_array($cleaned);
                             $term->required = true;
                             $this->terms[] = $term;
                         } else {
                             if ($firstchar == '-') {
                                 // -cat
                                 $term = new StdClass();
                                 $term->words = ousearch_search::internal_hyphenated_array($cleaned);
                                 $this->negativeterms[] = $term;
                             } else {
                                 $term = new StdClass();
                                 $term->words = ousearch_search::internal_hyphenated_array($cleaned);
                                 $term->required = false;
                                 $this->terms[] = $term;
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 $searchtitle = forumng_get_search_results_title(stripslashes($query), stripslashes($author), $daterangefrom, $daterangeto);
 $urloptions = $query ? '&query=' . rawurlencode($query) : '';
 $urloptions = $author ? '&author=' . rawurlencode($author) : '';
 $urloptions .= $daterangefrom ? '&datefrom=' . $daterangefrom : '';
 $urloptions .= $daterangeto ? '&dateto=' . $daterangeto : '';
 if (!$allforums) {
     // Display group selector if required
     groups_print_activity_menu($cm, $url . $urloptions);
     $groupid = forum::get_activity_group($cm, true);
     $forum->require_view($groupid, 0, true);
     print '<br/><br/>';
 }
 $editform->display();
 // 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 {