MDL-64820 forum: convert post.php to new rendering
[moodle.git] / mod / forum / search.php
CommitLineData
cd4e6b17 1<?php
2
8f685009
SH
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/**
01030f1b 19 * @package mod_forum
8f685009
SH
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
cd4e6b17 24require_once('../../config.php');
25require_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
d2ba493c 41$tags = optional_param_array('tags', [], PARAM_TEXT);
cd4e6b17 42
43$timefromrestrict = optional_param('timefromrestrict', 0, PARAM_INT); // Use starting date
44$fromday = optional_param('fromday', 0, PARAM_INT); // Starting date
45$frommonth = optional_param('frommonth', 0, PARAM_INT); // Starting date
46$fromyear = optional_param('fromyear', 0, PARAM_INT); // Starting date
47$fromhour = optional_param('fromhour', 0, PARAM_INT); // Starting date
48$fromminute = optional_param('fromminute', 0, PARAM_INT); // Starting date
49if ($timefromrestrict) {
e83bcc10
SR
50 $calendartype = \core_calendar\type_factory::get_calendar_instance();
51 $gregorianfrom = $calendartype->convert_to_gregorian($fromyear, $frommonth, $fromday);
52 $datefrom = make_timestamp($gregorianfrom['year'], $gregorianfrom['month'], $gregorianfrom['day'], $fromhour, $fromminute);
cd4e6b17 53} else {
54 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
55}
4e471fc6 56
cd4e6b17 57$timetorestrict = optional_param('timetorestrict', 0, PARAM_INT); // Use ending date
58$today = optional_param('today', 0, PARAM_INT); // Ending date
59$tomonth = optional_param('tomonth', 0, PARAM_INT); // Ending date
60$toyear = optional_param('toyear', 0, PARAM_INT); // Ending date
61$tohour = optional_param('tohour', 0, PARAM_INT); // Ending date
62$tominute = optional_param('tominute', 0, PARAM_INT); // Ending date
63if ($timetorestrict) {
e83bcc10
SR
64 $calendartype = \core_calendar\type_factory::get_calendar_instance();
65 $gregorianto = $calendartype->convert_to_gregorian($toyear, $tomonth, $today);
66 $dateto = make_timestamp($gregorianto['year'], $gregorianto['month'], $gregorianto['day'], $tohour, $tominute);
cd4e6b17 67} else {
68 $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
69}
4e471fc6 70
267aff7f 71$PAGE->set_pagelayout('standard');
f0202ae9 72$PAGE->set_url($FULLME); //TODO: this is very sloppy --skodak
e6ae4dc8 73
cd4e6b17 74if (empty($search)) { // Check the other parameters instead
75 if (!empty($words)) {
76 $search .= ' '.$words;
e6ae4dc8 77 }
cd4e6b17 78 if (!empty($userid)) {
79 $search .= ' userid:'.$userid;
8b9c7aa0 80 }
cd4e6b17 81 if (!empty($forumid)) {
82 $search .= ' forumid:'.$forumid;
83 }
84 if (!empty($user)) {
85 $search .= ' '.forum_clean_search_terms($user, 'user:');
86 }
87 if (!empty($subject)) {
88 $search .= ' '.forum_clean_search_terms($subject, 'subject:');
89 }
90 if (!empty($fullwords)) {
91 $search .= ' '.forum_clean_search_terms($fullwords, '+');
92 }
93 if (!empty($notwords)) {
94 $search .= ' '.forum_clean_search_terms($notwords, '-');
501cdbd8 95 }
cd4e6b17 96 if (!empty($phrase)) {
97 $search .= ' "'.$phrase.'"';
98 }
99 if (!empty($datefrom)) {
100 $search .= ' datefrom:'.$datefrom;
101 }
102 if (!empty($dateto)) {
103 $search .= ' dateto:'.$dateto;
104 }
d2ba493c
AH
105 if (!empty($tags)) {
106 $search .= ' tags:' . implode(',', $tags);
107 }
cd4e6b17 108 $individualparams = true;
109} else {
110 $individualparams = false;
111}
501cdbd8 112
cd4e6b17 113if ($search) {
114 $search = forum_clean_search_terms($search);
115}
501cdbd8 116
cd4e6b17 117if (!$course = $DB->get_record('course', array('id'=>$id))) {
118 print_error('invalidcourseid');
119}
501cdbd8 120
cd4e6b17 121require_course_login($course);
97485d07 122
22881392
DP
123$params = array(
124 'context' => $PAGE->context,
125 'other' => array('searchterm' => $search)
126);
127
128$event = \mod_forum\event\course_searched::create($params);
129$event->trigger();
65bcf17b 130
cd4e6b17 131$strforums = get_string("modulenameplural", "forum");
132$strsearch = get_string("search", "forum");
133$strsearchresults = get_string("searchresults", "forum");
134$strpage = get_string("page");
65bcf17b 135
cd4e6b17 136if (!$search || $showform) {
680afe2e 137
a6855934 138 $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id)));
b4c07395 139 $PAGE->navbar->add(get_string('advancedsearch', 'forum'));
501cdbd8 140
cd4e6b17 141 $PAGE->set_title($strsearch);
39790bd8 142 $PAGE->set_heading($course->fullname);
cd4e6b17 143 echo $OUTPUT->header();
e6ae4dc8 144
cd4e6b17 145 forum_print_big_search_form($course);
146 echo $OUTPUT->footer();
147 exit;
148}
e6ae4dc8 149
cd4e6b17 150/// We need to do a search now and print results
e6ae4dc8 151
cd4e6b17 152$searchterms = str_replace('forumid:', 'instance:', $search);
153$searchterms = explode(' ', $searchterms);
e6ae4dc8 154
cd4e6b17 155$searchform = forum_search_form($course, $search);
e6ae4dc8 156
a6855934 157$PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
b74c9a9f 158$PAGE->navbar->add($strsearchresults);
cd4e6b17 159if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
160 $PAGE->set_title($strsearchresults);
39790bd8 161 $PAGE->set_heading($course->fullname);
cd4e6b17 162 echo $OUTPUT->header();
66e2b9f8
AD
163 echo $OUTPUT->heading($strforums, 2);
164 echo $OUTPUT->heading($strsearchresults, 3);
165 echo $OUTPUT->heading(get_string("noposts", "forum"), 4);
77ffdf4b 166
cd4e6b17 167 if (!$individualparams) {
168 $words = $search;
e6ae4dc8 169 }
65bcf17b 170
cd4e6b17 171 forum_print_big_search_form($course);
172
173 echo $OUTPUT->footer();
174 exit;
175}
176
667f63fe
AD
177//including this here to prevent it being included if there are no search results
178require_once($CFG->dirroot.'/rating/lib.php');
179
180//set up the ratings information that will be the same for all posts
181$ratingoptions = new stdClass();
182$ratingoptions->component = 'mod_forum';
183$ratingoptions->ratingarea = 'post';
184$ratingoptions->userid = $USER->id;
185$ratingoptions->returnurl = $PAGE->url->out(false);
186$rm = new rating_manager();
187
cd4e6b17 188$PAGE->set_title($strsearchresults);
b4c07395 189$PAGE->set_heading($course->fullname);
cd4e6b17 190$PAGE->set_button($searchform);
191echo $OUTPUT->header();
192echo '<div class="reportlink">';
d2ba493c
AH
193
194$params = [
195 'id' => $course->id,
196 'user' => $user,
197 'userid' => $userid,
198 'forumid' => $forumid,
199 'subject' => $subject,
200 'phrase' => $phrase,
201 'words' => $words,
202 'fullwords' => $fullwords,
203 'notwords' => $notwords,
204 'dateto' => $dateto,
205 'datefrom' => $datefrom,
206 'showform' => 1
207];
208$url = new moodle_url("/mod/forum/search.php", $params);
209foreach ($tags as $tag) {
210 $url .= "&tags[]=$tag";
211}
212echo html_writer::link($url, get_string('advancedsearch', 'forum').'...');
213
cd4e6b17 214echo '</div>';
215
66e2b9f8
AD
216echo $OUTPUT->heading($strforums, 2);
217echo $OUTPUT->heading("$strsearchresults: $totalcount", 3);
cd4e6b17 218
1d67258e 219$url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage));
929d7a83 220echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
cd4e6b17 221
222//added to implement highlighting of search terms found only in HTML markup
223//fiedorow - 9/2/2005
224$strippedsearch = str_replace('user:','',$search);
225$strippedsearch = str_replace('subject:','',$strippedsearch);
226$strippedsearch = str_replace('&quot;','',$strippedsearch);
227$searchterms = explode(' ', $strippedsearch); // Search for words independently
228foreach ($searchterms as $key => $searchterm) {
229 if (preg_match('/^\-/',$searchterm)) {
230 unset($searchterms[$key]);
231 } else {
232 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
e6ae4dc8 233 }
cd4e6b17 234}
235$strippedsearch = implode(' ', $searchterms); // Rebuild the string
501cdbd8 236
cd4e6b17 237foreach ($posts as $post) {
e6ae4dc8 238
cd4e6b17 239 // Replace the simple subject with the three items forum name -> thread name -> subject
240 // (if all three are appropriate) each as a link.
241 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
242 print_error('invaliddiscussionid', 'forum');
243 }
244 if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) {
245 print_error('invalidforumid', 'forum');
246 }
cdea3a53 247
cd4e6b17 248 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
249 print_error('invalidcoursemodule');
250 }
65bcf17b 251
cd4e6b17 252 $post->subject = highlight($strippedsearch, $post->subject);
253 $discussion->name = highlight($strippedsearch, $discussion->name);
b800ac5a 254
cd4e6b17 255 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
256 if ($forum->type != 'single') {
257 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
258 if ($post->parent != 0) {
259 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
e6ae4dc8 260 }
cd4e6b17 261 }
501cdbd8 262
cd4e6b17 263 $post->subject = $fullsubject;
264 $post->subjectnoformat = true;
048ccc47 265
667f63fe
AD
266 //add the ratings information to the post
267 //Unfortunately seem to have do this individually as posts may be from different forums
268 if ($forum->assessed != RATING_AGGREGATE_NONE) {
bf0f06b1 269 $modcontext = context_module::instance($cm->id);
667f63fe
AD
270 $ratingoptions->context = $modcontext;
271 $ratingoptions->items = array($post);
272 $ratingoptions->aggregate = $forum->assessed;//the aggregation method
273 $ratingoptions->scaleid = $forum->scale;
274 $ratingoptions->assesstimestart = $forum->assesstimestart;
275 $ratingoptions->assesstimefinish = $forum->assesstimefinish;
276 $postswithratings = $rm->get_ratings($ratingoptions);
277
278 if ($postswithratings && count($postswithratings)==1) {
279 $post = $postswithratings[0];
280 }
281 }
282
cd4e6b17 283 // Identify search terms only found in HTML markup, and add a warning about them to
284 // the start of the message text. However, do not do the highlighting here. forum_print_post
285 // will do it for us later.
286 $missing_terms = "";
b343df86 287
39790bd8 288 $options = new stdClass();
cd4e6b17 289 $options->trusted = $post->messagetrust;
5da8e7d5 290 $post->message = highlight($strippedsearch,
cd4e6b17 291 format_text($post->message, $post->messageformat, $options, $course->id),
292 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
9044a387 293
cd4e6b17 294 foreach ($searchterms as $searchterm) {
5da8e7d5 295 if (preg_match("/$searchterm/i",$post->message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$post->message)) {
cd4e6b17 296 $missing_terms .= " $searchterm";
e6ae4dc8 297 }
cd4e6b17 298 }
501cdbd8 299
5da8e7d5
SH
300 $post->message = str_replace('<fgw9sdpq4>', '<span class="highlight">', $post->message);
301 $post->message = str_replace('</fgw9sdpq4>', '</span>', $post->message);
302
cd4e6b17 303 if ($missing_terms) {
304 $strmissingsearchterms = get_string('missingsearchterms','forum');
305 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
306 }
b800ac5a 307
cd4e6b17 308 // Prepare a link to the post in context, to be displayed after the forum post.
309 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".get_string("postincontext", "forum")."</a>";
f2b5d7e3 310
34edbe57
JSM
311 // Message is now html format.
312 if ($post->messageformat != FORMAT_HTML) {
313 $post->messageformat = FORMAT_HTML;
314 }
315
cd4e6b17 316 // Now pring the post.
5da8e7d5
SH
317 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false,
318 $fulllink, '', -99, false);
cd4e6b17 319}
501cdbd8 320
929d7a83 321echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
e6ae4dc8 322
cd4e6b17 323echo $OUTPUT->footer();
501cdbd8 324
e6ae4dc8 325
3a4ff0a8
AN
326 /**
327 * Print a full-sized search form for the specified course.
328 *
d9b196d9 329 * @param stdClass $course The Course that will be searched.
3a4ff0a8
AN
330 * @return void The function prints the form.
331 */
e6ae4dc8 332function forum_print_big_search_form($course) {
d2ba493c 333 global $PAGE, $words, $subject, $phrase, $user, $fullwords, $notwords, $datefrom, $dateto, $forumid, $tags;
10deddd3
FM
334
335 $renderable = new \mod_forum\output\big_search_form($course, $user);
336 $renderable->set_words($words);
337 $renderable->set_phrase($phrase);
338 $renderable->set_notwords($notwords);
339 $renderable->set_fullwords($fullwords);
340 $renderable->set_datefrom($datefrom);
341 $renderable->set_dateto($dateto);
342 $renderable->set_subject($subject);
343 $renderable->set_user($user);
1da58a1a 344 $renderable->set_forumid($forumid);
d2ba493c 345 $renderable->set_tags($tags);
10deddd3
FM
346
347 $output = $PAGE->get_renderer('mod_forum');
348 echo $output->render($renderable);
e6ae4dc8 349}
350
e9ff8e10 351/**
65bcf17b 352 * This function takes each word out of the search string, makes sure they are at least
3a4ff0a8
AN
353 * two characters long and returns an string of the space-separated search
354 * terms.
65bcf17b 355 *
3a4ff0a8
AN
356 * @param string $words String containing space-separated strings to search for.
357 * @param string $prefix String to prepend to the each token taken out of $words.
358 * @return string The filtered search terms, separated by spaces.
359 * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter.
e9ff8e10 360 */
e6ae4dc8 361function forum_clean_search_terms($words, $prefix='') {
362 $searchterms = explode(' ', $words);
363 foreach ($searchterms as $key => $searchterm) {
364 if (strlen($searchterm) < 2) {
365 unset($searchterms[$key]);
366 } else if ($prefix) {
367 $searchterms[$key] = $prefix.$searchterm;
368 }
369 }
370 return trim(implode(' ', $searchterms));
371}
372
3a4ff0a8
AN
373 /**
374 * Retrieve a list of the forums that this user can view.
375 *
d9b196d9 376 * @param stdClass $course The Course to use.
3a4ff0a8
AN
377 * @return array A set of formatted forum names stored against the forum id.
378 */
77ffdf4b 379function forum_menu_list($course) {
77ffdf4b 380 $menu = array();
77ffdf4b 381
debd3d62 382 $modinfo = get_fast_modinfo($course);
debd3d62 383 if (empty($modinfo->instances['forum'])) {
384 return $menu;
385 }
386
387 foreach ($modinfo->instances['forum'] as $cm) {
388 if (!$cm->uservisible) {
389 continue;
390 }
bf0f06b1 391 $context = context_module::instance($cm->id);
debd3d62 392 if (!has_capability('mod/forum:viewdiscussion', $context)) {
393 continue;
77ffdf4b 394 }
debd3d62 395 $menu[$cm->instance] = format_string($cm->name);
77ffdf4b 396 }
397
398 return $menu;
399}