if ($allforums) {
         // Search all forums.
         // NOTE: I think this code branch is no longer used as we removed
         // the 'all forums' facility to the resources_search block, but
         // perhaps it may be used in standard Moodle installs somehow.
         $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(local_ousearch_search::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 != mod_forumng::NO_GROUPS) {
             $result->set_group_id($groupid);
         }
     }
     // Pass necessary data to filter function using ugly global.
     global $forumngfilteroptions;
     $forumngfilteroptions = (object) array('author' => trim($data->author), 'datefrom' => $data->datefrom, 'dateto' => $data->dateto, 'asmoderator' => !empty($data->asmoderator));
     $result->set_filter('forumng_exclude_words_filter');
     print $result->display_results($url, $searchtitle);
 } else {
     // Searching without free text using author and/or date range.
 /**
  * Do a coursewide search as the current user on the given course, through
  * all forums.
  *
  * @param stdClass $course Moodle course object
  * @param string $search Search query
  * @return array Array of titles of results (empty array if none)
  */
 protected function coursewide_search($course, $search)
 {
     global $USER;
     // Based on the code in blocks/resources_search/search.class.php.
     $query = new local_ousearch_search($search);
     $query->set_course_id($course->id);
     $query->set_plugin('mod_forumng');
     $query->set_visible_modules_in_course($course, 'forumng');
     $groups = groups_get_all_groups($course->id, $USER->id);
     $groupids = array();
     foreach ($groups as $group) {
         $groupids[] = $group->id;
     }
     $query->set_group_ids($groupids);
     $query->set_group_exceptions($query->get_group_exceptions($course->id));
     $query->set_user_id($USER->id);
     $results = $query->query(0, 10);
     $out = array();
     if ($results->success) {
         foreach ($results->results as $result) {
             $out[] = strip_tags($result->title);
         }
     }
     sort($out);
     return $out;
 }