MDL-57660 mod_forum: Preserve forum ID selection after form submission
[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
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
48if ($timefromrestrict) {
e83bcc10
SR
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);
cd4e6b17 52} else {
53 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
54}
4e471fc6 55
cd4e6b17 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
62if ($timetorestrict) {
e83bcc10
SR
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);
cd4e6b17 66} else {
67 $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
68}
4e471fc6 69
267aff7f 70$PAGE->set_pagelayout('standard');
f0202ae9 71$PAGE->set_url($FULLME); //TODO: this is very sloppy --skodak
e6ae4dc8 72
cd4e6b17 73if (empty($search)) { // Check the other parameters instead
74 if (!empty($words)) {
75 $search .= ' '.$words;
e6ae4dc8 76 }
cd4e6b17 77 if (!empty($userid)) {
78 $search .= ' userid:'.$userid;
8b9c7aa0 79 }
cd4e6b17 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, '-');
501cdbd8 94 }
cd4e6b17 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}
501cdbd8 108
cd4e6b17 109if ($search) {
110 $search = forum_clean_search_terms($search);
111}
501cdbd8 112
cd4e6b17 113if (!$course = $DB->get_record('course', array('id'=>$id))) {
114 print_error('invalidcourseid');
115}
501cdbd8 116
cd4e6b17 117require_course_login($course);
97485d07 118
22881392
DP
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();
65bcf17b 126
cd4e6b17 127$strforums = get_string("modulenameplural", "forum");
128$strsearch = get_string("search", "forum");
129$strsearchresults = get_string("searchresults", "forum");
130$strpage = get_string("page");
65bcf17b 131
cd4e6b17 132if (!$search || $showform) {
680afe2e 133
a6855934 134 $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id)));
b4c07395 135 $PAGE->navbar->add(get_string('advancedsearch', 'forum'));
501cdbd8 136
cd4e6b17 137 $PAGE->set_title($strsearch);
39790bd8 138 $PAGE->set_heading($course->fullname);
cd4e6b17 139 echo $OUTPUT->header();
e6ae4dc8 140
cd4e6b17 141 forum_print_big_search_form($course);
142 echo $OUTPUT->footer();
143 exit;
144}
e6ae4dc8 145
cd4e6b17 146/// We need to do a search now and print results
e6ae4dc8 147
cd4e6b17 148$searchterms = str_replace('forumid:', 'instance:', $search);
149$searchterms = explode(' ', $searchterms);
e6ae4dc8 150
cd4e6b17 151$searchform = forum_search_form($course, $search);
e6ae4dc8 152
a6855934 153$PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
b74c9a9f 154$PAGE->navbar->add($strsearchresults);
cd4e6b17 155if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
156 $PAGE->set_title($strsearchresults);
39790bd8 157 $PAGE->set_heading($course->fullname);
cd4e6b17 158 echo $OUTPUT->header();
66e2b9f8
AD
159 echo $OUTPUT->heading($strforums, 2);
160 echo $OUTPUT->heading($strsearchresults, 3);
161 echo $OUTPUT->heading(get_string("noposts", "forum"), 4);
77ffdf4b 162
cd4e6b17 163 if (!$individualparams) {
164 $words = $search;
e6ae4dc8 165 }
65bcf17b 166
cd4e6b17 167 forum_print_big_search_form($course);
168
169 echo $OUTPUT->footer();
170 exit;
171}
172
667f63fe
AD
173//including this here to prevent it being included if there are no search results
174require_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
cd4e6b17 184$PAGE->set_title($strsearchresults);
b4c07395 185$PAGE->set_heading($course->fullname);
cd4e6b17 186$PAGE->set_button($searchform);
187echo $OUTPUT->header();
188echo '<div class="reportlink">';
189echo '<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>';
202echo '</div>';
203
66e2b9f8
AD
204echo $OUTPUT->heading($strforums, 2);
205echo $OUTPUT->heading("$strsearchresults: $totalcount", 3);
cd4e6b17 206
1d67258e 207$url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage));
929d7a83 208echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
cd4e6b17 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
216foreach ($searchterms as $key => $searchterm) {
217 if (preg_match('/^\-/',$searchterm)) {
218 unset($searchterms[$key]);
219 } else {
220 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
e6ae4dc8 221 }
cd4e6b17 222}
223$strippedsearch = implode(' ', $searchterms); // Rebuild the string
501cdbd8 224
cd4e6b17 225foreach ($posts as $post) {
e6ae4dc8 226
cd4e6b17 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 }
cdea3a53 235
cd4e6b17 236 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
237 print_error('invalidcoursemodule');
238 }
65bcf17b 239
cd4e6b17 240 $post->subject = highlight($strippedsearch, $post->subject);
241 $discussion->name = highlight($strippedsearch, $discussion->name);
b800ac5a 242
cd4e6b17 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>";
e6ae4dc8 248 }
cd4e6b17 249 }
501cdbd8 250
cd4e6b17 251 $post->subject = $fullsubject;
252 $post->subjectnoformat = true;
048ccc47 253
667f63fe
AD
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) {
bf0f06b1 257 $modcontext = context_module::instance($cm->id);
667f63fe
AD
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
cd4e6b17 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 = "";
b343df86 275
39790bd8 276 $options = new stdClass();
cd4e6b17 277 $options->trusted = $post->messagetrust;
5da8e7d5 278 $post->message = highlight($strippedsearch,
cd4e6b17 279 format_text($post->message, $post->messageformat, $options, $course->id),
280 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
9044a387 281
cd4e6b17 282 foreach ($searchterms as $searchterm) {
5da8e7d5 283 if (preg_match("/$searchterm/i",$post->message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$post->message)) {
cd4e6b17 284 $missing_terms .= " $searchterm";
e6ae4dc8 285 }
cd4e6b17 286 }
501cdbd8 287
5da8e7d5
SH
288 $post->message = str_replace('<fgw9sdpq4>', '<span class="highlight">', $post->message);
289 $post->message = str_replace('</fgw9sdpq4>', '</span>', $post->message);
290
cd4e6b17 291 if ($missing_terms) {
292 $strmissingsearchterms = get_string('missingsearchterms','forum');
293 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
294 }
b800ac5a 295
cd4e6b17 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>";
f2b5d7e3 298
34edbe57
JSM
299 // Message is now html format.
300 if ($post->messageformat != FORMAT_HTML) {
301 $post->messageformat = FORMAT_HTML;
302 }
303
cd4e6b17 304 // Now pring the post.
5da8e7d5
SH
305 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false,
306 $fulllink, '', -99, false);
cd4e6b17 307}
501cdbd8 308
929d7a83 309echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
e6ae4dc8 310
cd4e6b17 311echo $OUTPUT->footer();
501cdbd8 312
e6ae4dc8 313
3a4ff0a8
AN
314 /**
315 * Print a full-sized search form for the specified course.
316 *
d9b196d9 317 * @param stdClass $course The Course that will be searched.
3a4ff0a8
AN
318 * @return void The function prints the form.
319 */
e6ae4dc8 320function forum_print_big_search_form($course) {
1da58a1a 321 global $PAGE, $words, $subject, $phrase, $user, $fullwords, $notwords, $datefrom, $dateto, $forumid;
10deddd3
FM
322
323 $renderable = new \mod_forum\output\big_search_form($course, $user);
324 $renderable->set_words($words);
325 $renderable->set_phrase($phrase);
326 $renderable->set_notwords($notwords);
327 $renderable->set_fullwords($fullwords);
328 $renderable->set_datefrom($datefrom);
329 $renderable->set_dateto($dateto);
330 $renderable->set_subject($subject);
331 $renderable->set_user($user);
1da58a1a 332 $renderable->set_forumid($forumid);
10deddd3
FM
333
334 $output = $PAGE->get_renderer('mod_forum');
335 echo $output->render($renderable);
e6ae4dc8 336}
337
e9ff8e10 338/**
65bcf17b 339 * This function takes each word out of the search string, makes sure they are at least
3a4ff0a8
AN
340 * two characters long and returns an string of the space-separated search
341 * terms.
65bcf17b 342 *
3a4ff0a8
AN
343 * @param string $words String containing space-separated strings to search for.
344 * @param string $prefix String to prepend to the each token taken out of $words.
345 * @return string The filtered search terms, separated by spaces.
346 * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter.
e9ff8e10 347 */
e6ae4dc8 348function forum_clean_search_terms($words, $prefix='') {
349 $searchterms = explode(' ', $words);
350 foreach ($searchterms as $key => $searchterm) {
351 if (strlen($searchterm) < 2) {
352 unset($searchterms[$key]);
353 } else if ($prefix) {
354 $searchterms[$key] = $prefix.$searchterm;
355 }
356 }
357 return trim(implode(' ', $searchterms));
358}
359
3a4ff0a8
AN
360 /**
361 * Retrieve a list of the forums that this user can view.
362 *
d9b196d9 363 * @param stdClass $course The Course to use.
3a4ff0a8
AN
364 * @return array A set of formatted forum names stored against the forum id.
365 */
77ffdf4b 366function forum_menu_list($course) {
77ffdf4b 367 $menu = array();
77ffdf4b 368
debd3d62 369 $modinfo = get_fast_modinfo($course);
debd3d62 370 if (empty($modinfo->instances['forum'])) {
371 return $menu;
372 }
373
374 foreach ($modinfo->instances['forum'] as $cm) {
375 if (!$cm->uservisible) {
376 continue;
377 }
bf0f06b1 378 $context = context_module::instance($cm->id);
debd3d62 379 if (!has_capability('mod/forum:viewdiscussion', $context)) {
380 continue;
77ffdf4b 381 }
debd3d62 382 $menu[$cm->instance] = format_string($cm->name);
77ffdf4b 383 }
384
385 return $menu;
386}