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