id, "forum", "search", "search.php?id=$course->id&search=".urlencode($search), $search); $strforums = get_string("modulenameplural", "forum"); $strsearch = get_string("search", "forum"); $strsearchresults = get_string("searchresults", "forum"); $strpage = get_string("page"); if (!$search || $showform) { $navlinks = array(); $navlinks[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity'); $navlinks[] = array('name' => $strsearch, 'link' => '', 'type' => 'title'); $navigation = build_navigation($navlinks); print_header_simple("$strsearch", "", $navigation, 'search.words', "", "", " ", navmenu($course)); forum_print_big_search_form($course); print_footer($course); exit; } /// We need to do a search now and print results $searchterms = str_replace('forumid:', 'instance:', $search); $searchterms = explode(' ', $searchterms); $searchform = forum_search_form($course, $search); $navlinks = array(); $navlinks[] = array('name' => $strsearch, 'link' => "search.php?id=$course->id", 'type' => 'activityinstance'); $navlinks[] = array('name' => s($search, true), 'link' => '', 'type' => 'link'); $navigation = build_navigation($navlinks); if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) { print_header_simple("$strsearchresults", "", $navigation, 'search.words', "", "", " ", navmenu($course)); print_heading(get_string("nopostscontaining", "forum", $search)); if (!$individualparams) { $words = $search; } forum_print_big_search_form($course); print_footer($course); exit; } print_header_simple("$strsearchresults", "", $navigation, '', "", "", $searchform, navmenu($course)); echo ''; print_heading("$strsearchresults: $totalcount"); print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&id=$course->id&perpage=$perpage&"); //added to implement highlighting of search terms found only in HTML markup //fiedorow - 9/2/2005 $strippedsearch = str_replace('user:','',$search); $strippedsearch = str_replace('subject:','',$strippedsearch); $strippedsearch = str_replace('"','',$strippedsearch); $searchterms = explode(' ', $strippedsearch); // Search for words independently foreach ($searchterms as $key => $searchterm) { if (preg_match('/^\-/',$searchterm)) { unset($searchterms[$key]); } else { $searchterms[$key] = preg_replace('/^\+/','',$searchterm); } } $strippedsearch = implode(' ', $searchterms); // Rebuild the string foreach ($posts as $post) { // Replace the simple subject with the three items forum name -> thread name -> subject // (if all three are appropriate) each as a link. if (! $discussion = get_record('forum_discussions', 'id', $post->discussion)) { error('Discussion ID was incorrect'); } if (! $forum = get_record('forum', 'id', "$discussion->forum")) { error("Could not find forum $discussion->forum"); } if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) { error('Course Module ID was incorrect'); } $post->subject = highlight($strippedsearch, $post->subject); $discussion->name = highlight($strippedsearch, $discussion->name); $fullsubject = "id\">".format_string($forum->name,true).""; if ($forum->type != 'single') { $fullsubject .= " -> id\">".format_string($discussion->name,true).""; if ($post->parent != 0) { $fullsubject .= " -> discussion&parent=$post->id\">".format_string($post->subject,true).""; } } $post->subject = $fullsubject; $post->subjectnoformat = true; // Identify search terms only found in HTML markup, and add a warning about them to // the start of the message text. However, do not do the highlighting here. forum_print_post // will do it for us later. $missing_terms = ""; $options = new object(); $options->trusttext = true; $message = highlight($strippedsearch, format_text($post->message, $post->format, $options, $course->id), 0, '', ''); foreach ($searchterms as $searchterm) { if (preg_match("/$searchterm/i",$message) && !preg_match('/'.$searchterm.'<\/fgw9sdpq4>/i',$message)) { $missing_terms .= " $searchterm"; } } if ($missing_terms) { $strmissingsearchterms = get_string('missingsearchterms','forum'); $post->message = '

'.$strmissingsearchterms.' '.$missing_terms.'

'.$post->message; } // Prepare a link to the post in context, to be displayed after the forum post. $fulllink = "discussion#p$post->id\">".get_string("postincontext", "forum").""; // Now pring the post. forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, false, $fulllink, $strippedsearch, -99, false); } print_paging_bar($totalcount, $page, $perpage, "search.php?search=".urlencode(stripslashes($search))."&id=$course->id&perpage=$perpage&"); print_footer($course); /** * @todo Document this function */ function forum_print_big_search_form($course) { global $CFG, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto; print_simple_box(get_string('searchforumintro', 'forum'), 'center', '', '', 'searchbox', 'intro'); print_simple_box_start("center"); echo "\n"; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') { echo ''; echo ''; echo ''; echo ''; } echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo "\n"; print_simple_box_end(); } /** * This function takes each word out of the search string, makes sure they are at least * two characters long and returns an array containing every good word. * * @param string $words String containing space-separated strings to search for * @param string $prefix String to prepend to the each token taken out of $words * @returns array * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter */ function forum_clean_search_terms($words, $prefix='') { $searchterms = explode(' ', $words); foreach ($searchterms as $key => $searchterm) { if (strlen($searchterm) < 2) { unset($searchterms[$key]); } else if ($prefix) { $searchterms[$key] = $prefix.$searchterm; } } return trim(implode(' ', $searchterms)); } /** * @todo Document this function */ function forum_menu_list($course) { $menu = array(); $modinfo = get_fast_modinfo($course); if (empty($modinfo->instances['forum'])) { return $menu; } foreach ($modinfo->instances['forum'] as $cm) { if (!$cm->uservisible) { continue; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!has_capability('mod/forum:viewdiscussion', $context)) { continue; } $menu[$cm->instance] = format_string($cm->name); } return $menu; } ?>