MDL-62384 privacy: Modify user contexts query for auth_oauth2
[moodle.git] / mod / forum / search.php
blob9cfed3b66c3be45c8242f423494cf805eca6430a
1 <?php
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.
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/>.
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
24 require_once('../../config.php');
25 require_once('lib.php');
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
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 $tags = optional_param_array('tags', [], PARAM_TEXT);
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
49 if ($timefromrestrict) {
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);
53 } else {
54 $datefrom = optional_param('datefrom', 0, PARAM_INT); // Starting date
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
63 if ($timetorestrict) {
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);
67 } else {
68 $dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
71 $PAGE->set_pagelayout('standard');
72 $PAGE->set_url($FULLME); //TODO: this is very sloppy --skodak
74 if (empty($search)) { // Check the other parameters instead
75 if (!empty($words)) {
76 $search .= ' '.$words;
78 if (!empty($userid)) {
79 $search .= ' userid:'.$userid;
81 if (!empty($forumid)) {
82 $search .= ' forumid:'.$forumid;
84 if (!empty($user)) {
85 $search .= ' '.forum_clean_search_terms($user, 'user:');
87 if (!empty($subject)) {
88 $search .= ' '.forum_clean_search_terms($subject, 'subject:');
90 if (!empty($fullwords)) {
91 $search .= ' '.forum_clean_search_terms($fullwords, '+');
93 if (!empty($notwords)) {
94 $search .= ' '.forum_clean_search_terms($notwords, '-');
96 if (!empty($phrase)) {
97 $search .= ' "'.$phrase.'"';
99 if (!empty($datefrom)) {
100 $search .= ' datefrom:'.$datefrom;
102 if (!empty($dateto)) {
103 $search .= ' dateto:'.$dateto;
105 if (!empty($tags)) {
106 $search .= ' tags:' . implode(',', $tags);
108 $individualparams = true;
109 } else {
110 $individualparams = false;
113 if ($search) {
114 $search = forum_clean_search_terms($search);
117 if (!$course = $DB->get_record('course', array('id'=>$id))) {
118 print_error('invalidcourseid');
121 require_course_login($course);
123 $params = array(
124 'context' => $PAGE->context,
125 'other' => array('searchterm' => $search)
128 $event = \mod_forum\event\course_searched::create($params);
129 $event->trigger();
131 $strforums = get_string("modulenameplural", "forum");
132 $strsearch = get_string("search", "forum");
133 $strsearchresults = get_string("searchresults", "forum");
134 $strpage = get_string("page");
136 if (!$search || $showform) {
138 $PAGE->navbar->add($strforums, new moodle_url('/mod/forum/index.php', array('id'=>$course->id)));
139 $PAGE->navbar->add(get_string('advancedsearch', 'forum'));
141 $PAGE->set_title($strsearch);
142 $PAGE->set_heading($course->fullname);
143 echo $OUTPUT->header();
145 forum_print_big_search_form($course);
146 echo $OUTPUT->footer();
147 exit;
150 /// We need to do a search now and print results
152 $searchterms = str_replace('forumid:', 'instance:', $search);
153 $searchterms = explode(' ', $searchterms);
155 $searchform = forum_search_form($course, $search);
157 $PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
158 $PAGE->navbar->add($strsearchresults);
159 if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
160 $PAGE->set_title($strsearchresults);
161 $PAGE->set_heading($course->fullname);
162 echo $OUTPUT->header();
163 echo $OUTPUT->heading($strforums, 2);
164 echo $OUTPUT->heading($strsearchresults, 3);
165 echo $OUTPUT->heading(get_string("noposts", "forum"), 4);
167 if (!$individualparams) {
168 $words = $search;
171 forum_print_big_search_form($course);
173 echo $OUTPUT->footer();
174 exit;
177 //including this here to prevent it being included if there are no search results
178 require_once($CFG->dirroot.'/rating/lib.php');
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();
188 $PAGE->set_title($strsearchresults);
189 $PAGE->set_heading($course->fullname);
190 $PAGE->set_button($searchform);
191 echo $OUTPUT->header();
192 echo '<div class="reportlink">';
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
208 $url = new moodle_url("/mod/forum/search.php", $params);
209 foreach ($tags as $tag) {
210 $url .= "&tags[]=$tag";
212 echo html_writer::link($url, get_string('advancedsearch', 'forum').'...');
214 echo '</div>';
216 echo $OUTPUT->heading($strforums, 2);
217 echo $OUTPUT->heading("$strsearchresults: $totalcount", 3);
219 $url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage));
220 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
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
228 foreach ($searchterms as $key => $searchterm) {
229 if (preg_match('/^\-/',$searchterm)) {
230 unset($searchterms[$key]);
231 } else {
232 $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
235 $strippedsearch = implode(' ', $searchterms); // Rebuild the string
237 foreach ($posts as $post) {
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');
244 if (! $forum = $DB->get_record('forum', array('id' => "$discussion->forum"))) {
245 print_error('invalidforumid', 'forum');
248 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
249 print_error('invalidcoursemodule');
252 $post->subject = highlight($strippedsearch, $post->subject);
253 $discussion->name = highlight($strippedsearch, $discussion->name);
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>";
263 $post->subject = $fullsubject;
264 $post->subjectnoformat = true;
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) {
269 $modcontext = context_module::instance($cm->id);
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);
278 if ($postswithratings && count($postswithratings)==1) {
279 $post = $postswithratings[0];
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 = "";
288 $options = new stdClass();
289 $options->trusted = $post->messagetrust;
290 $post->message = highlight($strippedsearch,
291 format_text($post->message, $post->messageformat, $options, $course->id),
292 0, '<fgw9sdpq4>', '</fgw9sdpq4>');
294 foreach ($searchterms as $searchterm) {
295 if (preg_match("/$searchterm/i",$post->message) && !preg_match('/<fgw9sdpq4>'.$searchterm.'<\/fgw9sdpq4>/i',$post->message)) {
296 $missing_terms .= " $searchterm";
300 $post->message = str_replace('<fgw9sdpq4>', '<span class="highlight">', $post->message);
301 $post->message = str_replace('</fgw9sdpq4>', '</span>', $post->message);
303 if ($missing_terms) {
304 $strmissingsearchterms = get_string('missingsearchterms','forum');
305 $post->message = '<p class="highlight2">'.$strmissingsearchterms.' '.$missing_terms.'</p>'.$post->message;
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>";
311 // Message is now html format.
312 if ($post->messageformat != FORMAT_HTML) {
313 $post->messageformat = FORMAT_HTML;
316 // Now pring the post.
317 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false,
318 $fulllink, '', -99, false);
321 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
323 echo $OUTPUT->footer();
327 * Print a full-sized search form for the specified course.
329 * @param stdClass $course The Course that will be searched.
330 * @return void The function prints the form.
332 function forum_print_big_search_form($course) {
333 global $PAGE, $words, $subject, $phrase, $user, $fullwords, $notwords, $datefrom, $dateto, $forumid, $tags;
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);
344 $renderable->set_forumid($forumid);
345 $renderable->set_tags($tags);
347 $output = $PAGE->get_renderer('mod_forum');
348 echo $output->render($renderable);
352 * This function takes each word out of the search string, makes sure they are at least
353 * two characters long and returns an string of the space-separated search
354 * terms.
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.
361 function 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;
370 return trim(implode(' ', $searchterms));
374 * Retrieve a list of the forums that this user can view.
376 * @param stdClass $course The Course to use.
377 * @return array A set of formatted forum names stored against the forum id.
379 function forum_menu_list($course) {
380 $menu = array();
382 $modinfo = get_fast_modinfo($course);
383 if (empty($modinfo->instances['forum'])) {
384 return $menu;
387 foreach ($modinfo->instances['forum'] as $cm) {
388 if (!$cm->uservisible) {
389 continue;
391 $context = context_module::instance($cm->id);
392 if (!has_capability('mod/forum:viewdiscussion', $context)) {
393 continue;
395 $menu[$cm->instance] = format_string($cm->name);
398 return $menu;