Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/forum/search.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00024 require_once('../../config.php');
00025 require_once('lib.php');
00026 
00027 $id = required_param('id', PARAM_INT);                  // course id
00028 $search = trim(optional_param('search', '', PARAM_NOTAGS));  // search string
00029 $page = optional_param('page', 0, PARAM_INT);   // which page to show
00030 $perpage = optional_param('perpage', 10, PARAM_INT);   // how many per page
00031 $showform = optional_param('showform', 0, PARAM_INT);   // Just show the form
00032 
00033 $user    = trim(optional_param('user', '', PARAM_NOTAGS));    // Names to search for
00034 $userid  = trim(optional_param('userid', 0, PARAM_INT));      // UserID to search for
00035 $forumid = trim(optional_param('forumid', 0, PARAM_INT));      // ForumID to search for
00036 $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject
00037 $phrase  = trim(optional_param('phrase', '', PARAM_NOTAGS));  // Phrase
00038 $words   = trim(optional_param('words', '', PARAM_NOTAGS));   // Words
00039 $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words
00040 $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS));   // Words we don't want
00041 
00042 $timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
00043 $fromday = optional_param('fromday', 0, PARAM_INT);      // Starting date
00044 $frommonth = optional_param('frommonth', 0, PARAM_INT);      // Starting date
00045 $fromyear = optional_param('fromyear', 0, PARAM_INT);      // Starting date
00046 $fromhour = optional_param('fromhour', 0, PARAM_INT);      // Starting date
00047 $fromminute = optional_param('fromminute', 0, PARAM_INT);      // Starting date
00048 if ($timefromrestrict) {
00049     $datefrom = make_timestamp($fromyear, $frommonth, $fromday, $fromhour, $fromminute);
00050 } else {
00051     $datefrom = optional_param('datefrom', 0, PARAM_INT);      // Starting date
00052 }
00053 
00054 $timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
00055 $today = optional_param('today', 0, PARAM_INT);      // Ending date
00056 $tomonth = optional_param('tomonth', 0, PARAM_INT);      // Ending date
00057 $toyear = optional_param('toyear', 0, PARAM_INT);      // Ending date
00058 $tohour = optional_param('tohour', 0, PARAM_INT);      // Ending date
00059 $tominute = optional_param('tominute', 0, PARAM_INT);      // Ending date
00060 if ($timetorestrict) {
00061     $dateto = make_timestamp($toyear, $tomonth, $today, $tohour, $tominute);
00062 } else {
00063     $dateto = optional_param('dateto', 0, PARAM_INT);      // Ending date
00064 }
00065 
00066 $PAGE->set_pagelayout('standard');
00067 $PAGE->set_url($FULLME);
00068 
00069 if (empty($search)) {   // Check the other parameters instead
00070     if (!empty($words)) {
00071         $search .= ' '.$words;
00072     }
00073     if (!empty($userid)) {
00074         $search .= ' userid:'.$userid;
00075     }
00076     if (!empty($forumid)) {
00077         $search .= ' forumid:'.$forumid;
00078     }
00079     if (!empty($user)) {
00080         $search .= ' '.forum_clean_search_terms($user, 'user:');
00081     }
00082     if (!empty($subject)) {
00083         $search .= ' '.forum_clean_search_terms($subject, 'subject:');
00084     }
00085     if (!empty($fullwords)) {
00086         $search .= ' '.forum_clean_search_terms($fullwords, '+');
00087     }
00088     if (!empty($notwords)) {
00089         $search .= ' '.forum_clean_search_terms($notwords, '-');
00090     }
00091     if (!empty($phrase)) {
00092         $search .= ' "'.$phrase.'"';
00093     }
00094     if (!empty($datefrom)) {
00095         $search .= ' datefrom:'.$datefrom;
00096     }
00097     if (!empty($dateto)) {
00098         $search .= ' dateto:'.$dateto;
00099     }
00100     $individualparams = true;
00101 } else {
00102     $individualparams = false;
00103 }
00104 
00105 if ($search) {
00106     $search = forum_clean_search_terms($search);
00107 }
00108 
00109 if (!$course = $DB->get_record('course', array('id'=>$id))) {
00110     print_error('invalidcourseid');
00111 }
00112 
00113 require_course_login($course);
00114 
00115 add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
00116 
00117 $strforums = get_string("modulenameplural", "forum");
00118 $strsearch = get_string("search", "forum");
00119 $strsearchresults = get_string("searchresults", "forum");
00120 $strpage = get_string("page");
00121 
00122 if (!$search || $showform) {
00123 
00124     $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id)));
00125     $PAGE->navbar->add(get_string('advancedsearch', 'forum'));
00126 
00127     $PAGE->set_title($strsearch);
00128     $PAGE->set_heading($course->fullname);
00129     echo $OUTPUT->header();
00130 
00131     forum_print_big_search_form($course);
00132     echo $OUTPUT->footer();
00133     exit;
00134 }
00135 
00137 
00138 $searchterms = str_replace('forumid:', 'instance:', $search);
00139 $searchterms = explode(' ', $searchterms);
00140 
00141 $searchform = forum_search_form($course, $search);
00142 
00143 $PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
00144 $PAGE->navbar->add(s($search, true));
00145 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
00146     $PAGE->set_title($strsearchresults);
00147     $PAGE->set_heading($course->fullname);
00148     echo $OUTPUT->header();
00149     echo $OUTPUT->heading(get_string("nopostscontaining", "forum", $search));
00150 
00151     if (!$individualparams) {
00152         $words = $search;
00153     }
00154 
00155     forum_print_big_search_form($course);
00156 
00157     echo $OUTPUT->footer();
00158     exit;
00159 }
00160 
00161 //including this here to prevent it being included if there are no search results
00162 require_once($CFG->dirroot.'/rating/lib.php');
00163 
00164 //set up the ratings information that will be the same for all posts
00165 $ratingoptions = new stdClass();
00166 $ratingoptions->component = 'mod_forum';
00167 $ratingoptions->ratingarea = 'post';
00168 $ratingoptions->userid = $USER->id;
00169 $ratingoptions->returnurl = $PAGE->url->out(false);
00170 $rm = new rating_manager();
00171 
00172 $PAGE->set_title($strsearchresults);
00173 $PAGE->set_heading($course->fullname);
00174 $PAGE->set_button($searchform);
00175 echo $OUTPUT->header();
00176 echo '<div class="reportlink">';
00177 echo '<a href="search.php?id='.$course->id.
00178                          '&amp;user='.urlencode($user).
00179                          '&amp;userid='.$userid.
00180                          '&amp;forumid='.$forumid.
00181                          '&amp;subject='.urlencode($subject).
00182                          '&amp;phrase='.urlencode($phrase).
00183                          '&amp;words='.urlencode($words).
00184                          '&amp;fullwords='.urlencode($fullwords).
00185                          '&amp;notwords='.urlencode($notwords).
00186                          '&amp;dateto='.$dateto.
00187                          '&amp;datefrom='.$datefrom.
00188                          '&amp;showform=1'.
00189                          '">'.get_string('advancedsearch','forum').'...</a>';
00190 echo '</div>';
00191 
00192 echo $OUTPUT->heading("$strsearchresults: $totalcount");
00193 
00194 $url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage));
00195 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
00196 
00197 //added to implement highlighting of search terms found only in HTML markup
00198 //fiedorow - 9/2/2005
00199 $strippedsearch = str_replace('user:','',$search);
00200 $strippedsearch = str_replace('subject:','',$strippedsearch);
00201 $strippedsearch = str_replace('&quot;','',$strippedsearch);
00202 $searchterms = explode(' ', $strippedsearch);    // Search for words independently
00203 foreach ($searchterms as $key => $searchterm) {
00204     if (preg_match('/^\-/',$searchterm)) {
00205         unset($searchterms[$key]);
00206     } else {
00207         $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
00208     }
00209 }
00210 $strippedsearch = implode(' ', $searchterms);    // Rebuild the string
00211 
00212 foreach ($posts as $post) {
00213 
00214     // Replace the simple subject with the three items forum name -> thread name -> subject
00215     // (if all three are appropriate) each as a link.
00216     if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
00217         print_error('invaliddiscussionid', 'forum');
00218     }
00219     if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) {
00220         print_error('invalidforumid', 'forum');
00221     }
00222 
00223     if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
00224         print_error('invalidcoursemodule');
00225     }
00226 
00227     $post->subject = highlight($strippedsearch, $post->subject);
00228     $discussion->name = highlight($strippedsearch, $discussion->name);
00229 
00230     $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
00231     if ($forum->type != 'single') {
00232         $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
00233         if ($post->parent != 0) {
00234             $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
00235         }
00236     }
00237 
00238     $post->subject = $fullsubject;
00239     $post->subjectnoformat = true;
00240 
00241     //add the ratings information to the post
00242     //Unfortunately seem to have do this individually as posts may be from different forums
00243     if ($forum->assessed != RATING_AGGREGATE_NONE) {
00244         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00245         $ratingoptions->context = $modcontext;
00246         $ratingoptions->items = array($post);
00247         $ratingoptions->aggregate = $forum->assessed;//the aggregation method
00248         $ratingoptions->scaleid = $forum->scale;
00249         $ratingoptions->assesstimestart = $forum->assesstimestart;
00250         $ratingoptions->assesstimefinish = $forum->assesstimefinish;
00251         $postswithratings = $rm->get_ratings($ratingoptions);
00252 
00253         if ($postswithratings && count($postswithratings)==1) {
00254             $post = $postswithratings[0];
00255         }
00256     }
00257 
00258     // Identify search terms only found in HTML markup, and add a warning about them to
00259     // the start of the message text. However, do not do the highlighting here. forum_print_post
00260     // will do it for us later.
00261     $missing_terms = "";
00262 
00263     $options = new stdClass();
00264     $options->trusted = $post->messagetrust;
00265     $post->message = highlight($strippedsearch,
00266                     format_text($post->message, $post->messageformat, $options, $course->id),
00267                     0, '<fgw9sdpq4>', '</fgw9sdpq4>');
00268 
00269     foreach ($searchterms as $searchterm) {
00270         if (preg_match("/$searchterm/i",$post->message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$post->message)) {
00271             $missing_terms .= " $searchterm";
00272         }
00273     }
00274 
00275     $post->message = str_replace('<fgw9sdpq4>', '<span class="highlight">', $post->message);
00276     $post->message = str_replace('</fgw9sdpq4>', '</span>', $post->message);
00277 
00278     if ($missing_terms) {
00279         $strmissingsearchterms = get_string('missingsearchterms','forum');
00280         $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
00281     }
00282 
00283     // Prepare a link to the post in context, to be displayed after the forum post.
00284     $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
00285 
00286     // Now pring the post.
00287     forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false,
00288             $fulllink, '', -99, false);
00289 }
00290 
00291 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
00292 
00293 echo $OUTPUT->footer();
00294 
00295 
00296 
00300 function forum_print_big_search_form($course) {
00301     global $CFG, $DB, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $PAGE, $OUTPUT;
00302 
00303     echo $OUTPUT->box(get_string('searchforumintro', 'forum'), 'searchbox boxaligncenter', 'intro');
00304 
00305     echo $OUTPUT->box_start('generalbox boxaligncenter');
00306 
00307     echo html_writer::script('', $CFG->wwwroot.'/mod/forum/forum.js');
00308 
00309     echo '<form id="searchform" action="search.php" method="get">';
00310     echo '<table cellpadding="10" class="searchbox" id="form">';
00311 
00312     echo '<tr>';
00313     echo '<td class="c0"><label for="words">'.get_string('searchwords', 'forum').'</label>';
00314     echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
00315     echo '<td class="c1"><input type="text" size="35" name="words" id="words"value="'.s($words, true).'" alt="" /></td>';
00316     echo '</tr>';
00317 
00318     echo '<tr>';
00319     echo '<td class="c0"><label for="phrase">'.get_string('searchphrase', 'forum').'</label></td>';
00320     echo '<td class="c1"><input type="text" size="35" name="phrase" id="phrase" value="'.s($phrase, true).'" alt="" /></td>';
00321     echo '</tr>';
00322 
00323     echo '<tr>';
00324     echo '<td class="c0"><label for="notwords">'.get_string('searchnotwords', 'forum').'</label></td>';
00325     echo '<td class="c1"><input type="text" size="35" name="notwords" id="notwords" value="'.s($notwords, true).'" alt="" /></td>';
00326     echo '</tr>';
00327 
00328     if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') {
00329         echo '<tr>';
00330         echo '<td class="c0"><label for="fullwords">'.get_string('searchfullwords', 'forum').'</label></td>';
00331         echo '<td class="c1"><input type="text" size="35" name="fullwords" id="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
00332         echo '</tr>';
00333     }
00334 
00335     echo '<tr>';
00336     echo '<td class="c0">'.get_string('searchdatefrom', 'forum').'</td>';
00337     echo '<td class="c1">';
00338     if (empty($datefrom)) {
00339         $datefromchecked = '';
00340         $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
00341     }else{
00342         $datefromchecked = 'checked="checked"';
00343     }
00344 
00345     echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '.  $datefromchecked . ' /> ';
00346     $selectors = html_writer::select_time('days', 'fromday', $datefrom)
00347                . html_writer::select_time('months', 'frommonth', $datefrom)
00348                . html_writer::select_time('years', 'fromyear', $datefrom)
00349                . html_writer::select_time('hours', 'fromhour', $datefrom)
00350                . html_writer::select_time('minutes', 'fromminute', $datefrom);
00351     echo $selectors;
00352     echo '<input type="hidden" name="hfromday" value="0" />';
00353     echo '<input type="hidden" name="hfrommonth" value="0" />';
00354     echo '<input type="hidden" name="hfromyear" value="0" />';
00355     echo '<input type="hidden" name="hfromhour" value="0" />';
00356     echo '<input type="hidden" name="hfromminute" value="0" />';
00357 
00358     echo '</td>';
00359     echo '</tr>';
00360 
00361     echo '<tr>';
00362     echo '<td class="c0">'.get_string('searchdateto', 'forum').'</td>';
00363     echo '<td class="c1">';
00364     if (empty($dateto)) {
00365         $datetochecked = '';
00366         $dateto = time()+3600;
00367     }else{
00368         $datetochecked = 'checked="checked"';
00369     }
00370 
00371     echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
00372     $selectors = html_writer::select_time('days', 'fromday', $dateto)
00373                . html_writer::select_time('months', 'frommonth', $dateto)
00374                . html_writer::select_time('years', 'fromyear', $dateto)
00375                . html_writer::select_time('hours', 'fromhour', $dateto)
00376                . html_writer::select_time('minutes', 'fromminute', $dateto);
00377     echo $selectors;
00378 
00379     echo '<input type="hidden" name="htoday" value="0" />';
00380     echo '<input type="hidden" name="htomonth" value="0" />';
00381     echo '<input type="hidden" name="htoyear" value="0" />';
00382     echo '<input type="hidden" name="htohour" value="0" />';
00383     echo '<input type="hidden" name="htominute" value="0" />';
00384 
00385     echo '</td>';
00386     echo '</tr>';
00387 
00388     echo '<tr>';
00389     echo '<td class="c0"><label for="menuforumid">'.get_string('searchwhichforums', 'forum').'</label></td>';
00390     echo '<td class="c1">';
00391     echo html_writer::select(forum_menu_list($course), 'forumid', '', array(''=>get_string('allforums', 'forum')));
00392     echo '</td>';
00393     echo '</tr>';
00394 
00395     echo '<tr>';
00396     echo '<td class="c0"><label for="subject">'.get_string('searchsubject', 'forum').'</label></td>';
00397     echo '<td class="c1"><input type="text" size="35" name="subject" id="subject" value="'.s($subject, true).'" alt="" /></td>';
00398     echo '</tr>';
00399 
00400     echo '<tr>';
00401     echo '<td class="c0"><label for="user">'.get_string('searchuser', 'forum').'</label></td>';
00402     echo '<td class="c1"><input type="text" size="35" name="user" id="user" value="'.s($user, true).'" alt="" /></td>';
00403     echo '</tr>';
00404 
00405     echo '<tr>';
00406     echo '<td class="submit" colspan="2" align="center">';
00407     echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
00408     echo '</tr>';
00409 
00410     echo '</table>';
00411     echo '</form>';
00412 
00413     echo html_writer::script(js_writer::function_call('lockoptions_timetoitems'));
00414     echo html_writer::script(js_writer::function_call('lockoptions_timefromitems'));
00415 
00416     echo $OUTPUT->box_end();
00417 }
00418 
00428 function forum_clean_search_terms($words, $prefix='') {
00429     $searchterms = explode(' ', $words);
00430     foreach ($searchterms as $key => $searchterm) {
00431         if (strlen($searchterm) < 2) {
00432             unset($searchterms[$key]);
00433         } else if ($prefix) {
00434             $searchterms[$key] = $prefix.$searchterm;
00435         }
00436     }
00437     return trim(implode(' ', $searchterms));
00438 }
00439 
00443 function forum_menu_list($course)  {
00444 
00445     $menu = array();
00446 
00447     $modinfo = get_fast_modinfo($course);
00448 
00449     if (empty($modinfo->instances['forum'])) {
00450         return $menu;
00451     }
00452 
00453     foreach ($modinfo->instances['forum'] as $cm) {
00454         if (!$cm->uservisible) {
00455             continue;
00456         }
00457         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00458         if (!has_capability('mod/forum:viewdiscussion', $context)) {
00459             continue;
00460         }
00461         $menu[$cm->instance] = format_string($cm->name);
00462     }
00463 
00464     return $menu;
00465 }
00466 
 All Data Structures Namespaces Files Functions Variables Enumerations