. /** * @package mod-forum * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once('../../config.php'); require_once('lib.php'); $id = required_param('id', PARAM_INT); // course id $search = trim(optional_param('search', '', PARAM_NOTAGS)); // search string $page = optional_param('page', 0, PARAM_INT); // which page to show $perpage = optional_param('perpage', 10, PARAM_INT); // how many per page $showform = optional_param('showform', 0, PARAM_INT); // Just show the form $user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for $userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for $forumid = trim(optional_param('forumid', 0, PARAM_INT)); // ForumID to search for $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject $phrase = trim(optional_param('phrase', '', PARAM_NOTAGS)); // Phrase $words = trim(optional_param('words', '', PARAM_NOTAGS)); // Words $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS)); // Words we don't want $timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date $fromday = optional_param('fromday', 0, PARAM_INT); // Starting date $frommonth = optional_param('frommonth', 0, PARAM_INT); // Starting date $fromyear = optional_param('fromyear', 0, PARAM_INT); // Starting date $fromhour = optional_param('fromhour', 0, PARAM_INT); // Starting date $fromminute = optional_param('fromminute', 0, PARAM_INT); // Starting date if ($timefromrestrict) { $datefrom = make_timestamp($fromyear, $frommonth, $fromday, $fromhour, $fromminute); } else { $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date } $timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date $today = optional_param('today', 0, PARAM_INT); // Ending date $tomonth = optional_param('tomonth', 0, PARAM_INT); // Ending date $toyear = optional_param('toyear', 0, PARAM_INT); // Ending date $tohour = optional_param('tohour', 0, PARAM_INT); // Ending date $tominute = optional_param('tominute', 0, PARAM_INT); // Ending date if ($timetorestrict) { $dateto = make_timestamp($toyear, $tomonth, $today, $tohour, $tominute); } else { $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date } $PAGE->set_pagelayout('standard'); $PAGE->set_url($FULLME); if (empty($search)) { // Check the other parameters instead if (!empty($words)) { $search .= ' '.$words; } if (!empty($userid)) { $search .= ' userid:'.$userid; } if (!empty($forumid)) { $search .= ' forumid:'.$forumid; } if (!empty($user)) { $search .= ' '.forum_clean_search_terms($user, 'user:'); } if (!empty($subject)) { $search .= ' '.forum_clean_search_terms($subject, 'subject:'); } if (!empty($fullwords)) { $search .= ' '.forum_clean_search_terms($fullwords, '+'); } if (!empty($notwords)) { $search .= ' '.forum_clean_search_terms($notwords, '-'); } if (!empty($phrase)) { $search .= ' "'.$phrase.'"'; } if (!empty($datefrom)) { $search .= ' datefrom:'.$datefrom; } if (!empty($dateto)) { $search .= ' dateto:'.$dateto; } $individualparams = true; } else { $individualparams = false; } if ($search) { $search = forum_clean_search_terms($search); } if (!$course = $DB->get_record('course', array('id'=>$id))) { print_error('invalidcourseid'); } require_course_login($course); add_to_log($course->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) { $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id))); $PAGE->navbar->add(get_string('advancedsearch', 'forum')); $PAGE->set_title($strsearch); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); forum_print_big_search_form($course); echo $OUTPUT->footer(); 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); $PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id))); $PAGE->navbar->add(s($search, true)); if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) { $PAGE->set_title($strsearchresults); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string("nopostscontaining", "forum", $search)); if (!$individualparams) { $words = $search; } forum_print_big_search_form($course); echo $OUTPUT->footer(); exit; } //including this here to prevent it being included if there are no search results require_once($CFG->dirroot.'/rating/lib.php'); //set up the ratings information that will be the same for all posts $ratingoptions = new stdClass(); $ratingoptions->component = 'mod_forum'; $ratingoptions->ratingarea = 'post'; $ratingoptions->userid = $USER->id; $ratingoptions->returnurl = $PAGE->url->out(false); $rm = new rating_manager(); $PAGE->set_title($strsearchresults); $PAGE->set_heading($course->fullname); $PAGE->set_button($searchform); echo $OUTPUT->header(); echo ''; echo $OUTPUT->heading("$strsearchresults: $totalcount"); $url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage)); echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url); //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 = $DB->get_record('forum_discussions', array('id' => $post->discussion))) { print_error('invaliddiscussionid', 'forum'); } if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) { print_error('invalidforumid', 'forum'); } if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) { print_error('invalidcoursemodule'); } $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; //add the ratings information to the post //Unfortunately seem to have do this individually as posts may be from different forums if ($forum->assessed != RATING_AGGREGATE_NONE) { $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); $ratingoptions->context = $modcontext; $ratingoptions->items = array($post); $ratingoptions->aggregate = $forum->assessed;//the aggregation method $ratingoptions->scaleid = $forum->scale; $ratingoptions->assesstimestart = $forum->assesstimestart; $ratingoptions->assesstimefinish = $forum->assesstimefinish; $postswithratings = $rm->get_ratings($ratingoptions); if ($postswithratings && count($postswithratings)==1) { $post = $postswithratings[0]; } } // 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 stdClass(); $options->trusted = $post->messagetrust; $post->message = highlight($strippedsearch, format_text($post->message, $post->messageformat, $options, $course->id), 0, '', ''); foreach ($searchterms as $searchterm) { if (preg_match("/$searchterm/i",$post->message) && !preg_match('/'.$searchterm.'<\/fgw9sdpq4>/i',$post->message)) { $missing_terms .= " $searchterm"; } } $post->message = str_replace('', '', $post->message); $post->message = str_replace('', '', $post->message); 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, $fulllink, '', -99, false); } echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url); echo $OUTPUT->footer(); /** * @todo Document this function */ function forum_print_big_search_form($course) { global $CFG, $DB, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $PAGE, $OUTPUT; echo $OUTPUT->box(get_string('searchforumintro', 'forum'), 'searchbox boxaligncenter', 'intro'); echo $OUTPUT->box_start('generalbox boxaligncenter'); echo html_writer::script('', $CFG->wwwroot.'/mod/forum/forum.js'); echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; if ($DB->get_dbfamily() == 'mysql' || $DB->get_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 html_writer::script(js_writer::function_call('lockoptions_timetoitems')); echo html_writer::script(js_writer::function_call('lockoptions_timefromitems')); echo $OUTPUT->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; }