Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
$tags = optional_param_array('tags', [], PARAM_TEXT);
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
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
55
}
56
 
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
69
}
70
$starredonly = optional_param('starredonly', false, PARAM_BOOL); // Include only favourites.
71
 
72
$params = [
73
    'id' => $id,
74
    'perpage' => $perpage,
75
];
76
 
77
if ($search !== '') {
78
    $params['search'] = $search;
79
}
80
 
81
if ($page) {
82
    $params['page'] = $page;
83
}
84
 
85
if ($showform) {
86
    $params['showform'] = $showform;
87
}
88
 
89
if ($user !== '') {
90
    $params['user'] = $user;
91
}
92
 
93
if ($userid) {
94
    $params['userid'] = $userid;
95
}
96
 
97
if ($forumid) {
98
    $params['forumid'] = $forumid;
99
}
100
 
101
if ($subject !== '') {
102
    $params['subject'] = $subject;
103
}
104
 
105
if ($phrase !== '') {
106
    $params['phrase'] = $phrase;
107
}
108
 
109
if ($words !== '') {
110
    $params['words'] = $words;
111
}
112
 
113
if ($fullwords !== '') {
114
    $params['fullwords'] = $fullwords;
115
}
116
 
117
if ($notwords !== '') {
118
    $params['notwords'] = $notwords;
119
}
120
 
121
if ($timefromrestrict) {
122
    $params['timefromrestrict'] = $timefromrestrict;
123
}
124
 
125
if ($fromday) {
126
    $params['fromday'] = $fromday;
127
}
128
 
129
if ($fromhour) {
130
    $params['fromhour'] = $fromhour;
131
}
132
 
133
if ($fromminute) {
134
    $params['fromminute'] = $fromminute;
135
}
136
 
137
if ($frommonth) {
138
    $params['frommonth'] = $frommonth;
139
}
140
 
141
if ($fromyear) {
142
    $params['fromyear'] = $fromyear;
143
}
144
 
145
if ($datefrom) {
146
    $params['datefrom'] = $datefrom;
147
}
148
 
149
if ($timetorestrict) {
150
    $params['timetorestrict'] = $timetorestrict;
151
}
152
 
153
if ($today) {
154
    $params['today'] = $today;
155
}
156
 
157
if ($tohour) {
158
    $params['tohour'] = $tohour;
159
}
160
 
161
if ($tominute) {
162
    $params['tominute'] = $tominute;
163
}
164
 
165
if ($tomonth) {
166
    $params['tomonth'] = $tomonth;
167
}
168
 
169
if ($toyear) {
170
    $params['toyear'] = $toyear;
171
}
172
 
173
if ($dateto) {
174
    $params['dateto'] = $dateto;
175
}
176
 
177
if ($starredonly) {
178
    $params['starredonly'] = $starredonly;
179
}
180
 
181
$PAGE->set_pagelayout('standard');
182
$PAGE->set_url(new moodle_url('/mod/forum/search.php', $params));
183
$PAGE->set_secondary_active_tab("coursehome");
184
 
185
if (empty($search)) {   // Check the other parameters instead
186
    if (!empty($words)) {
187
        $search .= ' '.$words;
188
    }
189
    if (!empty($userid)) {
190
        $search .= ' userid:'.$userid;
191
    }
192
    if (!empty($forumid)) {
193
        $search .= ' forumid:'.$forumid;
194
    }
195
    if (!empty($user)) {
196
        $search .= ' '.forum_clean_search_terms($user, 'user:');
197
    }
198
    if (!empty($subject)) {
199
        $search .= ' '.forum_clean_search_terms($subject, 'subject:');
200
    }
201
    if (!empty($fullwords)) {
202
        $search .= ' '.forum_clean_search_terms($fullwords, '+');
203
    }
204
    if (!empty($notwords)) {
205
        $search .= ' '.forum_clean_search_terms($notwords, '-');
206
    }
207
    if (!empty($phrase)) {
208
        $search .= ' "'.$phrase.'"';
209
    }
210
    if (!empty($datefrom)) {
211
        $search .= ' datefrom:'.$datefrom;
212
    }
213
    if (!empty($dateto)) {
214
        $search .= ' dateto:'.$dateto;
215
    }
216
    if (!empty($tags)) {
217
        $search .= ' tags:' . implode(',', $tags);
218
    }
219
    if (!empty($starredonly)) {
220
        $search .= ' starredonly:on';
221
    }
222
    $individualparams = true;
223
} else {
224
    $individualparams = false;
225
}
226
 
227
if ($search) {
228
    $search = forum_clean_search_terms($search);
229
}
230
 
231
if (!$course = $DB->get_record('course', array('id'=>$id))) {
232
    throw new \moodle_exception('invalidcourseid');
233
}
234
 
235
require_course_login($course);
236
 
237
$params = array(
238
    'context' => $PAGE->context,
239
    'other' => array('searchterm' => $search)
240
);
241
 
242
$event = \mod_forum\event\course_searched::create($params);
243
$event->trigger();
244
 
245
$strforums = get_string("modulenameplural", "forum");
246
$strsearch = get_string("search", "forum");
247
$strsearchresults = get_string("searchresults", "forum");
248
$strpage = get_string("page");
249
 
250
if (!$search || $showform) {
251
 
252
    $url = new moodle_url('/mod/forum/index.php', array('id' => $course->id));
253
    $PAGE->navbar->add($strforums, $url);
254
    $url = new moodle_url('/mod/forum/search.php', array('id' => $course->id));
255
    $PAGE->navbar->add(get_string('advancedsearch', 'forum'), $url);
256
 
257
    $PAGE->set_title($strsearch);
258
    $PAGE->set_heading($course->fullname);
259
    echo $OUTPUT->header();
260
 
261
    forum_print_big_search_form($course);
262
    echo $OUTPUT->footer();
263
    exit;
264
}
265
 
266
/// We need to do a search now and print results
267
 
268
$searchterms = str_replace('forumid:', 'instance:', $search);
269
$searchterms = explode(' ', $searchterms);
270
 
271
$searchform = forum_search_form($course, $search);
272
 
273
$PAGE->navbar->add($strsearch, new moodle_url('/mod/forum/search.php', array('id'=>$course->id)));
274
$PAGE->navbar->add($strsearchresults);
275
if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
276
    $PAGE->set_title($strsearchresults);
277
    $PAGE->set_heading($course->fullname);
278
    echo $OUTPUT->header();
279
    if (!$PAGE->has_secondary_navigation()) {
280
        echo $OUTPUT->heading($strforums, 2);
281
    }
282
    echo $OUTPUT->heading($strsearchresults, 3);
283
    echo $OUTPUT->heading(get_string("noposts", "forum"), 4);
284
 
285
    if (!$individualparams) {
286
        $words = $search;
287
    }
288
 
289
    forum_print_big_search_form($course);
290
 
291
    echo $OUTPUT->footer();
292
    exit;
293
}
294
 
295
//including this here to prevent it being included if there are no search results
296
require_once($CFG->dirroot.'/rating/lib.php');
297
 
298
//set up the ratings information that will be the same for all posts
299
$ratingoptions = new stdClass();
300
$ratingoptions->component = 'mod_forum';
301
$ratingoptions->ratingarea = 'post';
302
$ratingoptions->userid = $USER->id;
303
$ratingoptions->returnurl = $PAGE->url->out(false);
304
$rm = new rating_manager();
305
 
306
$PAGE->set_title($strsearchresults);
307
$PAGE->set_heading($course->fullname);
308
$PAGE->add_header_action($searchform);
309
echo $OUTPUT->header();
310
echo '<div class="reportlink">';
311
 
312
$params = [
313
    'id'        => $course->id,
314
    'user'      => $user,
315
    'userid'    => $userid,
316
    'forumid'   => $forumid,
317
    'subject'   => $subject,
318
    'phrase'    => $phrase,
319
    'words'     => $words,
320
    'fullwords' => $fullwords,
321
    'notwords'  => $notwords,
322
    'dateto'    => $dateto,
323
    'datefrom'  => $datefrom,
324
    'showform'  => 1,
325
    'starredonly' => $starredonly
326
];
327
$url    = new moodle_url("/mod/forum/search.php", $params);
328
foreach ($tags as $tag) {
329
    $url .= "&tags[]=$tag";
330
}
331
echo html_writer::link($url, get_string('advancedsearch', 'forum').'...');
332
 
333
echo '</div>';
334
 
335
echo $OUTPUT->heading($strforums, 2);
336
 
337
echo $OUTPUT->heading("$strsearchresults: $totalcount", 3);
338
 
339
$url = new moodle_url('search.php', array('search' => $search, 'id' => $course->id, 'perpage' => $perpage));
340
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
341
 
342
//added to implement highlighting of search terms found only in HTML markup
343
//fiedorow - 9/2/2005
344
$strippedsearch = str_replace('user:','',$search);
345
$strippedsearch = str_replace('subject:','',$strippedsearch);
346
$strippedsearch = str_replace('&quot;','',$strippedsearch);
347
$searchterms = explode(' ', $strippedsearch);    // Search for words independently
348
foreach ($searchterms as $key => $searchterm) {
349
    if (preg_match('/^\-/',$searchterm)) {
350
        unset($searchterms[$key]);
351
    } else {
352
        $searchterms[$key] = preg_replace('/^\+/','',$searchterm);
353
    }
354
}
355
$strippedsearch = implode(' ', $searchterms);    // Rebuild the string
356
$entityfactory = mod_forum\local\container::get_entity_factory();
357
$vaultfactory = mod_forum\local\container::get_vault_factory();
358
$rendererfactory = mod_forum\local\container::get_renderer_factory();
359
$managerfactory = mod_forum\local\container::get_manager_factory();
360
$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
361
$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
362
 
363
$discussionvault = $vaultfactory->get_discussion_vault();
364
$discussionids = array_keys(array_reduce($posts, function($carry, $post) {
365
    $carry[$post->discussion] = true;
366
    return $carry;
367
}, []));
368
$discussions = $discussionvault->get_from_ids($discussionids);
369
$discussionsbyid = array_reduce($discussions, function($carry, $discussion) {
370
    $carry[$discussion->get_id()] = $discussion;
371
    return $carry;
372
}, []);
373
 
374
$forumvault = $vaultfactory->get_forum_vault();
375
$forumids = array_keys(array_reduce($discussions, function($carry, $discussion) {
376
    $carry[$discussion->get_forum_id()] = true;
377
    return $carry;
378
}, []));
379
$forums = $forumvault->get_from_ids($forumids);
380
$forumsbyid = array_reduce($forums, function($carry, $forum) {
381
    $carry[$forum->get_id()] = $forum;
382
    return $carry;
383
}, []);
384
 
385
$postids = array_map(function($post) {
386
    return $post->id;
387
}, $posts);
388
 
389
$poststorender = [];
390
 
391
foreach ($posts as $post) {
392
 
393
    // Replace the simple subject with the three items forum name -> thread name -> subject
394
    // (if all three are appropriate) each as a link.
395
    if (!isset($discussionsbyid[$post->discussion])) {
396
        throw new \moodle_exception('invaliddiscussionid', 'forum');
397
    }
398
 
399
    $discussion = $discussionsbyid[$post->discussion];
400
    if (!isset($forumsbyid[$discussion->get_forum_id()])) {
401
        throw new \moodle_exception('invalidforumid', 'forum');
402
    }
403
 
404
    $forum = $forumsbyid[$discussion->get_forum_id()];
405
    $capabilitymanager = $managerfactory->get_capability_manager($forum);
406
    $postentity = $entityfactory->get_post_from_stdclass($post);
407
 
408
    if (!$capabilitymanager->can_view_post($USER, $discussion, $postentity)) {
409
        // Don't render posts that the user can't view.
410
        continue;
411
    }
412
 
413
    if ($postentity->is_deleted()) {
414
        // Don't render deleted posts.
415
        continue;
416
    }
417
 
418
    $poststorender[] = $postentity;
419
}
420
 
421
$renderer = $rendererfactory->get_posts_search_results_renderer($searchterms);
422
echo $renderer->render(
423
    $USER,
424
    $forumsbyid,
425
    $discussionsbyid,
426
    $poststorender
427
);
428
 
429
echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $url);
430
 
431
echo $OUTPUT->footer();
432
 
433
 
434
 /**
435
  * Print a full-sized search form for the specified course.
436
  *
437
  * @param stdClass $course The Course that will be searched.
438
  * @return void The function prints the form.
439
  */
440
function forum_print_big_search_form($course) {
441
    global $PAGE, $words, $subject, $phrase, $user, $fullwords, $notwords, $datefrom,
442
           $dateto, $forumid, $tags, $starredonly;
443
 
444
    $renderable = new \mod_forum\output\big_search_form($course, $user);
445
    $renderable->set_words($words);
446
    $renderable->set_phrase($phrase);
447
    $renderable->set_notwords($notwords);
448
    $renderable->set_fullwords($fullwords);
449
    $renderable->set_datefrom($datefrom);
450
    $renderable->set_dateto($dateto);
451
    $renderable->set_subject($subject);
452
    $renderable->set_user($user);
453
    $renderable->set_forumid($forumid);
454
    $renderable->set_tags($tags);
455
    $renderable->set_starredonly($starredonly);
456
 
457
    $output = $PAGE->get_renderer('mod_forum');
458
    echo $output->render($renderable);
459
}
460
 
461
/**
462
 * This function takes each word out of the search string, makes sure they are at least
463
 * two characters long and returns an string of the space-separated search
464
 * terms.
465
 *
466
 * @param string $words String containing space-separated strings to search for.
467
 * @param string $prefix String to prepend to the each token taken out of $words.
468
 * @return string The filtered search terms, separated by spaces.
469
 * @todo Take the hardcoded limit out of this function and put it into a user-specified parameter.
470
 */
471
function forum_clean_search_terms($words, $prefix='') {
472
    $searchterms = explode(' ', $words);
473
    foreach ($searchterms as $key => $searchterm) {
474
        if (strlen($searchterm) < 2) {
475
            unset($searchterms[$key]);
476
        } else if ($prefix) {
477
            $searchterms[$key] = $prefix.$searchterm;
478
        }
479
    }
480
    return trim(implode(' ', $searchterms));
481
}
482
 
483
 /**
484
  * Retrieve a list of the forums that this user can view.
485
  *
486
  * @param stdClass $course The Course to use.
487
  * @return array A set of formatted forum names stored against the forum id.
488
  */
489
function forum_menu_list($course)  {
490
    $menu = array();
491
 
492
    $modinfo = get_fast_modinfo($course);
493
    if (empty($modinfo->instances['forum'])) {
494
        return $menu;
495
    }
496
 
497
    foreach ($modinfo->instances['forum'] as $cm) {
498
        if (!$cm->uservisible) {
499
            continue;
500
        }
501
        $context = context_module::instance($cm->id);
502
        if (!has_capability('mod/forum:viewdiscussion', $context)) {
503
            continue;
504
        }
505
        $menu[$cm->instance] = format_string($cm->name);
506
    }
507
 
508
    return $menu;
509
}