[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/forum/ -> search.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package   mod_forum
  20   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  21   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  require_once('../../config.php');
  25  require_once ('lib.php');
  26  
  27  $id = required_param('id', PARAM_INT);                  // course id
  28  $search = trim(optional_param('search', '', PARAM_NOTAGS));  // search string
  29  $page = optional_param('page', 0, PARAM_INT);   // which page to show
  30  $perpage = optional_param('perpage', 10, PARAM_INT);   // how many per page
  31  $showform = optional_param('showform', 0, PARAM_INT);   // Just show the form
  32  
  33  $user    = trim(optional_param('user', '', PARAM_NOTAGS));    // Names to search for
  34  $userid  = trim(optional_param('userid', 0, PARAM_INT));      // UserID to search for
  35  $forumid = trim(optional_param('forumid', 0, PARAM_INT));      // ForumID to search for
  36  $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject
  37  $phrase  = trim(optional_param('phrase', '', PARAM_NOTAGS));  // Phrase
  38  $words   = trim(optional_param('words', '', PARAM_NOTAGS));   // Words
  39  $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words
  40  $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS));   // Words we don't want
  41  
  42  $timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
  43  $fromday = optional_param('fromday', 0, PARAM_INT);      // Starting date
  44  $frommonth = optional_param('frommonth', 0, PARAM_INT);      // Starting date
  45  $fromyear = optional_param('fromyear', 0, PARAM_INT);      // Starting date
  46  $fromhour = optional_param('fromhour', 0, PARAM_INT);      // Starting date
  47  $fromminute = optional_param('fromminute', 0, PARAM_INT);      // Starting date
  48  if ($timefromrestrict) {
  49      $calendartype = \core_calendar\type_factory::get_calendar_instance();
  50      $gregorianfrom = $calendartype->convert_to_gregorian($fromyear, $frommonth, $fromday);
  51      $datefrom = make_timestamp($gregorianfrom['year'], $gregorianfrom['month'], $gregorianfrom['day'], $fromhour, $fromminute);
  52  } else {
  53      $datefrom = optional_param('datefrom', 0, PARAM_INT);      // Starting date
  54  }
  55  
  56  $timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
  57  $today = optional_param('today', 0, PARAM_INT);      // Ending date
  58  $tomonth = optional_param('tomonth', 0, PARAM_INT);      // Ending date
  59  $toyear = optional_param('toyear', 0, PARAM_INT);      // Ending date
  60  $tohour = optional_param('tohour', 0, PARAM_INT);      // Ending date
  61  $tominute = optional_param('tominute', 0, PARAM_INT);      // Ending date
  62  if ($timetorestrict) {
  63      $calendartype = \core_calendar\type_factory::get_calendar_instance();
  64      $gregorianto = $calendartype->convert_to_gregorian($toyear, $tomonth, $today);
  65      $dateto = make_timestamp($gregorianto['year'], $gregorianto['month'], $gregorianto['day'], $tohour, $tominute);
  66  } else {
  67      $dateto = optional_param('dateto', 0, PARAM_INT);      // Ending date
  68  }
  69  
  70  $PAGE->set_pagelayout('standard');
  71  $PAGE->set_url($FULLME); //TODO: this is very sloppy --skodak
  72  
  73  if (empty($search)) {   // Check the other parameters instead
  74      if (!empty($words)) {
  75          $search .= ' '.$words;
  76      }
  77      if (!empty($userid)) {
  78          $search .= ' userid:'.$userid;
  79      }
  80      if (!empty($forumid)) {
  81          $search .= ' forumid:'.$forumid;
  82      }
  83      if (!empty($user)) {
  84          $search .= ' '.forum_clean_search_terms($user, 'user:');
  85      }
  86      if (!empty($subject)) {
  87          $search .= ' '.forum_clean_search_terms($subject, 'subject:');
  88      }
  89      if (!empty($fullwords)) {
  90          $search .= ' '.forum_clean_search_terms($fullwords, '+');
  91      }
  92      if (!empty($notwords)) {
  93          $search .= ' '.forum_clean_search_terms($notwords, '-');
  94      }
  95      if (!empty($phrase)) {
  96          $search .= ' "'.$phrase.'"';
  97      }
  98      if (!empty($datefrom)) {
  99          $search .= ' datefrom:'.$datefrom;
 100      }
 101      if (!empty($dateto)) {
 102          $search .= ' dateto:'.$dateto;
 103      }
 104      $individualparams = true;
 105  } else {
 106      $individualparams = false;
 107  }
 108  
 109  if ($search) {
 110      $search = forum_clean_search_terms($search);
 111  }
 112  
 113  if (!$course = $DB->get_record('course', array('id'=>$id))) {
 114      print_error('invalidcourseid');
 115  }
 116  
 117  require_course_login($course);
 118  
 119  $params = array(
 120      'context' => $PAGE->context,
 121      'other' => array('searchterm' => $search)
 122  );
 123  
 124  $event = \mod_forum\event\course_searched::create($params);
 125  $event->trigger();
 126  
 127  $strforums = get_string("modulenameplural", "forum");
 128  $strsearch = get_string("search", "forum");
 129  $strsearchresults = get_string("searchresults", "forum");
 130  $strpage = get_string("page");
 131  
 132  if (!$search || $showform) {
 133  
 134      $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id)));
 135      $PAGE->navbar->add(get_string('advancedsearch', 'forum'));
 136  
 137      $PAGE->set_title($strsearch);
 138      $PAGE->set_heading($course->fullname);
 139      echo $OUTPUT->header();
 140  
 141      forum_print_big_search_form($course);
 142      echo $OUTPUT->footer();
 143      exit;
 144  }
 145  
 146  /// We need to do a search now and print results
 147  
 148  $searchterms = str_replace('forumid:', 'instance:', $search);
 149  $searchterms = explode(' ', $searchterms);
 150  
 151  $searchform = forum_search_form($course, $search);
 152  
 153  $PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
 154  $PAGE->navbar->add($strsearchresults);
 155  if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
 156      $PAGE->set_title($strsearchresults);
 157      $PAGE->set_heading($course->fullname);
 158      echo $OUTPUT->header();
 159      echo $OUTPUT->heading($strforums, 2);
 160      echo $OUTPUT->heading($strsearchresults, 3);
 161      echo $OUTPUT->heading(get_string("noposts", "forum"), 4);
 162  
 163      if (!$individualparams) {
 164          $words = $search;
 165      }
 166  
 167      forum_print_big_search_form($course);
 168  
 169      echo $OUTPUT->footer();
 170      exit;
 171  }
 172  
 173  //including this here to prevent it being included if there are no search results
 174  require_once($CFG->dirroot.'/rating/lib.php');
 175  
 176  //set up the ratings information that will be the same for all posts
 177  $ratingoptions = new stdClass();
 178  $ratingoptions->component = 'mod_forum';
 179  $ratingoptions->ratingarea = 'post';
 180  $ratingoptions->userid = $USER->id;
 181  $ratingoptions->returnurl = $PAGE->url->out(false);
 182  $rm = new rating_manager();
 183  
 184  $PAGE->set_title($strsearchresults);
 185  $PAGE->set_heading($course->fullname);
 186  $PAGE->set_button($searchform);
 187  echo $OUTPUT->header();
 188  echo '<div class="reportlink">';
 189  echo '<a href="search.php?id='.$course->id.
 190                           '&amp;user='.urlencode($user).
 191                           '&amp;userid='.$userid.
 192                           '&amp;forumid='.$forumid.
 193                           '&amp;subject='.urlencode($subject).
 194                           '&amp;phrase='.urlencode($phrase).
 195                           '&amp;words='.urlencode($words).
 196                           '&amp;fullwords='.urlencode($fullwords).
 197                           '&amp;notwords='.urlencode($notwords).
 198                           '&amp;dateto='.$dateto.
 199                           '&amp;datefrom='.$datefrom.
 200                           '&amp;showform=1'.
 201                           '">'.get_string('advancedsearch','forum').'...</a>';
 202  echo '</div>';
 203  
 204  echo $OUTPUT->heading($strforums, 2);
 205  echo $OUTPUT->heading("$strsearchresults: $totalcount", 3);
 206  
 207  $url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage));
 208  echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
 209  
 210  //added to implement highlighting of search terms found only in HTML markup
 211  //fiedorow - 9/2/2005
 212  $strippedsearch = str_replace('user:','',$search);
 213  $strippedsearch = str_replace('subject:','',$strippedsearch);
 214  $strippedsearch = str_replace('&quot;','',$strippedsearch);
 215  $searchterms = explode(' ', $strippedsearch);    // Search for words independently
 216  foreach ($searchterms as $key => $searchterm) {
 217      if (preg_match('/^\-/',$searchterm)) {
 218          unset($searchterms[$key]);
 219      } else {
 220          $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
 221      }
 222  }
 223  $strippedsearch = implode(' ', $searchterms);    // Rebuild the string
 224  
 225  foreach ($posts as $post) {
 226  
 227      // Replace the simple subject with the three items forum name -> thread name -> subject
 228      // (if all three are appropriate) each as a link.
 229      if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
 230          print_error('invaliddiscussionid', 'forum');
 231      }
 232      if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) {
 233          print_error('invalidforumid', 'forum');
 234      }
 235  
 236      if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
 237          print_error('invalidcoursemodule');
 238      }
 239  
 240      $post->subject = highlight($strippedsearch, $post->subject);
 241      $discussion->name = highlight($strippedsearch, $discussion->name);
 242  
 243      $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
 244      if ($forum->type != 'single') {
 245          $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
 246          if ($post->parent != 0) {
 247              $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
 248          }
 249      }
 250  
 251      $post->subject = $fullsubject;
 252      $post->subjectnoformat = true;
 253  
 254      //add the ratings information to the post
 255      //Unfortunately seem to have do this individually as posts may be from different forums
 256      if ($forum->assessed != RATING_AGGREGATE_NONE) {
 257          $modcontext = context_module::instance($cm->id);
 258          $ratingoptions->context = $modcontext;
 259          $ratingoptions->items = array($post);
 260          $ratingoptions->aggregate = $forum->assessed;//the aggregation method
 261          $ratingoptions->scaleid = $forum->scale;
 262          $ratingoptions->assesstimestart = $forum->assesstimestart;
 263          $ratingoptions->assesstimefinish = $forum->assesstimefinish;
 264          $postswithratings = $rm->get_ratings($ratingoptions);
 265  
 266          if ($postswithratings && count($postswithratings)==1) {
 267              $post = $postswithratings[0];
 268          }
 269      }
 270  
 271      // Identify search terms only found in HTML markup, and add a warning about them to
 272      // the start of the message text. However, do not do the highlighting here. forum_print_post
 273      // will do it for us later.
 274      $missing_terms = "";
 275  
 276      $options = new stdClass();
 277      $options->trusted = $post->messagetrust;
 278      $post->message = highlight($strippedsearch,
 279                      format_text($post->message, $post->messageformat, $options, $course->id),
 280                      0, '<fgw9sdpq4>', '</fgw9sdpq4>');
 281  
 282      foreach ($searchterms as $searchterm) {
 283          if (preg_match("/$searchterm/i",$post->message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$post->message)) {
 284              $missing_terms .= " $searchterm";
 285          }
 286      }
 287  
 288      $post->message = str_replace('<fgw9sdpq4>', '<span class="highlight">', $post->message);
 289      $post->message = str_replace('</fgw9sdpq4>', '</span>', $post->message);
 290  
 291      if ($missing_terms) {
 292          $strmissingsearchterms = get_string('missingsearchterms','forum');
 293          $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
 294      }
 295  
 296      // Prepare a link to the post in context, to be displayed after the forum post.
 297      $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
 298  
 299      // Message is now html format.
 300      if ($post->messageformat != FORMAT_HTML) {
 301          $post->messageformat = FORMAT_HTML;
 302      }
 303  
 304      // Now pring the post.
 305      forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false,
 306              $fulllink, '', -99, false);
 307  }
 308  
 309  echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
 310  
 311  echo $OUTPUT->footer();
 312  
 313  
 314   /**
 315    * Print a full-sized search form for the specified course.
 316    *
 317    * @param stdClass $course The Course that will be searched.
 318    * @return void The function prints the form.
 319    */
 320  function forum_print_big_search_form($course) {
 321      global $CFG, $DB, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $PAGE, $OUTPUT;
 322  
 323      echo $OUTPUT->box(get_string('searchforumintro', 'forum'), 'searchbox boxaligncenter', 'intro');
 324  
 325      echo $OUTPUT->box_start('generalbox boxaligncenter');
 326  
 327      echo html_writer::script('', $CFG->wwwroot.'/mod/forum/forum.js');
 328  
 329      echo '<form id="searchform" action="search.php" method="get">';
 330      echo '<table cellpadding="10" class="searchbox" id="form">';
 331  
 332      echo '<tr>';
 333      echo '<td class="c0"><label for="words">'.get_string('searchwords', 'forum').'</label>';
 334      echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
 335      echo '<td class="c1"><input type="text" size="35" name="words" id="words"value="'.s($words, true).'" alt="" /></td>';
 336      echo '</tr>';
 337  
 338      echo '<tr>';
 339      echo '<td class="c0"><label for="phrase">'.get_string('searchphrase', 'forum').'</label></td>';
 340      echo '<td class="c1"><input type="text" size="35" name="phrase" id="phrase" value="'.s($phrase, true).'" alt="" /></td>';
 341      echo '</tr>';
 342  
 343      echo '<tr>';
 344      echo '<td class="c0"><label for="notwords">'.get_string('searchnotwords', 'forum').'</label></td>';
 345      echo '<td class="c1"><input type="text" size="35" name="notwords" id="notwords" value="'.s($notwords, true).'" alt="" /></td>';
 346      echo '</tr>';
 347  
 348      if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') {
 349          echo '<tr>';
 350          echo '<td class="c0"><label for="fullwords">'.get_string('searchfullwords', 'forum').'</label></td>';
 351          echo '<td class="c1"><input type="text" size="35" name="fullwords" id="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
 352          echo '</tr>';
 353      }
 354  
 355      echo '<tr>';
 356      echo '<td class="c0">'.get_string('searchdatefrom', 'forum').'</td>';
 357      echo '<td class="c1">';
 358      if (empty($datefrom)) {
 359          $datefromchecked = '';
 360          $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
 361      }else{
 362          $datefromchecked = 'checked="checked"';
 363      }
 364  
 365      echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '.  $datefromchecked . ' /> ';
 366      $selectors = html_writer::select_time('days', 'fromday', $datefrom)
 367                 . html_writer::select_time('months', 'frommonth', $datefrom)
 368                 . html_writer::select_time('years', 'fromyear', $datefrom)
 369                 . html_writer::select_time('hours', 'fromhour', $datefrom)
 370                 . html_writer::select_time('minutes', 'fromminute', $datefrom);
 371      echo $selectors;
 372      echo '<input type="hidden" name="hfromday" value="0" />';
 373      echo '<input type="hidden" name="hfrommonth" value="0" />';
 374      echo '<input type="hidden" name="hfromyear" value="0" />';
 375      echo '<input type="hidden" name="hfromhour" value="0" />';
 376      echo '<input type="hidden" name="hfromminute" value="0" />';
 377  
 378      echo '</td>';
 379      echo '</tr>';
 380  
 381      echo '<tr>';
 382      echo '<td class="c0">'.get_string('searchdateto', 'forum').'</td>';
 383      echo '<td class="c1">';
 384      if (empty($dateto)) {
 385          $datetochecked = '';
 386          $dateto = time()+3600;
 387      }else{
 388          $datetochecked = 'checked="checked"';
 389      }
 390  
 391      echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
 392      $selectors = html_writer::select_time('days', 'today', $dateto)
 393                 . html_writer::select_time('months', 'tomonth', $dateto)
 394                 . html_writer::select_time('years', 'toyear', $dateto)
 395                 . html_writer::select_time('hours', 'tohour', $dateto)
 396                 . html_writer::select_time('minutes', 'tominute', $dateto);
 397      echo $selectors;
 398  
 399      echo '<input type="hidden" name="htoday" value="0" />';
 400      echo '<input type="hidden" name="htomonth" value="0" />';
 401      echo '<input type="hidden" name="htoyear" value="0" />';
 402      echo '<input type="hidden" name="htohour" value="0" />';
 403      echo '<input type="hidden" name="htominute" value="0" />';
 404  
 405      echo '</td>';
 406      echo '</tr>';
 407  
 408      echo '<tr>';
 409      echo '<td class="c0"><label for="menuforumid">'.get_string('searchwhichforums', 'forum').'</label></td>';
 410      echo '<td class="c1">';
 411      echo html_writer::select(forum_menu_list($course), 'forumid', '', array(''=>get_string('allforums', 'forum')));
 412      echo '</td>';
 413      echo '</tr>';
 414  
 415      echo '<tr>';
 416      echo '<td class="c0"><label for="subject">'.get_string('searchsubject', 'forum').'</label></td>';
 417      echo '<td class="c1"><input type="text" size="35" name="subject" id="subject" value="'.s($subject, true).'" alt="" /></td>';
 418      echo '</tr>';
 419  
 420      echo '<tr>';
 421      echo '<td class="c0"><label for="user">'.get_string('searchuser', 'forum').'</label></td>';
 422      echo '<td class="c1"><input type="text" size="35" name="user" id="user" value="'.s($user, true).'" alt="" /></td>';
 423      echo '</tr>';
 424  
 425      echo '<tr>';
 426      echo '<td class="submit" colspan="2" align="center">';
 427      echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
 428      echo '</tr>';
 429  
 430      echo '</table>';
 431      echo '</form>';
 432  
 433      echo html_writer::script(js_writer::function_call('lockoptions_timetoitems'));
 434      echo html_writer::script(js_writer::function_call('lockoptions_timefromitems'));
 435  
 436      echo $OUTPUT->box_end();
 437  }
 438  
 439  /**
 440   * This function takes each word out of the search string, makes sure they are at least
 441   * two characters long and returns an string of the space-separated search
 442   * terms.
 443   *
 444   * @param string $words String containing space-separated strings to search for.
 445   * @param string $prefix String to prepend to the each token taken out of $words.
 446   * @return string The filtered search terms, separated by spaces.
 447   * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter.
 448   */
 449  function forum_clean_search_terms($words, $prefix='') {
 450      $searchterms = explode(' ', $words);
 451      foreach ($searchterms as $key => $searchterm) {
 452          if (strlen($searchterm) < 2) {
 453              unset($searchterms[$key]);
 454          } else if ($prefix) {
 455              $searchterms[$key] = $prefix.$searchterm;
 456          }
 457      }
 458      return trim(implode(' ', $searchterms));
 459  }
 460  
 461   /**
 462    * Retrieve a list of the forums that this user can view.
 463    *
 464    * @param stdClass $course The Course to use.
 465    * @return array A set of formatted forum names stored against the forum id.
 466    */
 467  function forum_menu_list($course)  {
 468      $menu = array();
 469  
 470      $modinfo = get_fast_modinfo($course);
 471      if (empty($modinfo->instances['forum'])) {
 472          return $menu;
 473      }
 474  
 475      foreach ($modinfo->instances['forum'] as $cm) {
 476          if (!$cm->uservisible) {
 477              continue;
 478          }
 479          $context = context_module::instance($cm->id);
 480          if (!has_capability('mod/forum:viewdiscussion', $context)) {
 481              continue;
 482          }
 483          $menu[$cm->instance] = format_string($cm->name);
 484      }
 485  
 486      return $menu;
 487  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1