index.php 5.48 KB
Newer Older
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
<?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/>.

/**
 * Global Search index page for entering queries and display of results
 *
 * @package   core_search
 * @copyright Prateek Sachan {@link http://prateeksachan.com}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once(__DIR__ . '/../config.php');

$page = optional_param('page', 0, PARAM_INT);
$q = optional_param('q', '', PARAM_NOTAGS);
$title = optional_param('title', '', PARAM_NOTAGS);
30
$contextid = optional_param('context', 0, PARAM_INT);
31
// Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
32

33
$context = context_system::instance();
34
$pagetitle = get_string('globalsearch', 'search');
35
$PAGE->set_context($context);
36
37
38
39
40
41
42
43
$PAGE->set_pagelayout('standard');
$PAGE->set_title($pagetitle);
$PAGE->set_heading($pagetitle);

if (!empty($CFG->forcelogin)) {
    require_login();
}

44
45
require_capability('moodle/search:query', $context);

46
47
48
49
50
51
52
53
54
55
56
57
58
$searchrenderer = $PAGE->get_renderer('core_search');

if (\core_search\manager::is_global_search_enabled() === false) {
    $PAGE->set_url(new moodle_url('/search/index.php'));
    echo $OUTPUT->header();
    echo $OUTPUT->heading($pagetitle);
    echo $searchrenderer->render_search_disabled();
    echo $OUTPUT->footer();
    exit;
}

$search = \core_search\manager::instance();

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Set up custom data for form.
$customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
if ($contextid) {
    // When a context is supplied, check if it's within course level. If so, show dropdown.
    $context = context::instance_by_id($contextid);
    $coursecontext = $context->get_course_context(false);
    if ($coursecontext) {
        $searchwithin = [];
        $searchwithin[''] = get_string('everywhere', 'search');
        $searchwithin['course'] = $coursecontext->get_context_name();
        if ($context->contextlevel !== CONTEXT_COURSE) {
            $searchwithin['context'] = $context->get_context_name();
        }
        $customdata['searchwithin'] = $searchwithin;
    }
}
$mform = new \core_search\output\form\search(null, $customdata);
76
77
78
79
80
81
82
83

$data = $mform->get_data();
if (!$data && $q) {
    // Data can also come from the URL.

    $data = new stdClass();
    $data->q = $q;
    $data->title = $title;
84
85
86
87
88
    $areaids = optional_param('areaids', '', PARAM_RAW);
    if (!empty($areaids)) {
        $areaids = explode(',', $areaids);
        $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
    }
89
90
91
92
93
    $courseids = optional_param('courseids', '', PARAM_RAW);
    if (!empty($courseids)) {
        $courseids = explode(',', $courseids);
        $data->courseids = clean_param_array($courseids, PARAM_INT);
    }
94
95
    $data->timestart = optional_param('timestart', 0, PARAM_INT);
    $data->timeend = optional_param('timeend', 0, PARAM_INT);
96
97
98

    $data->context = $contextid;

99
100
101
    $mform->set_data($data);
}

102
103
104
105
106
107
108
109
110
111
112
113
114
// Convert the 'search within' option, if used, to course or context restrictions.
if ($data && !empty($data->searchwithin)) {
    switch ($data->searchwithin) {
        case 'course':
            $data->courseids = [$coursecontext->instanceid];
            break;
        case 'context':
            $data->courseids = [$coursecontext->instanceid];
            $data->contextids = [$context->id];
            break;
    }
}

115
116
117
118
119
// Set the page URL.
$urlparams = array('page' => $page);
if ($data) {
    $urlparams['q'] = $data->q;
    $urlparams['title'] = $data->title;
120
121
122
    if (!empty($data->areaids)) {
        $urlparams['areaids'] = implode(',', $data->areaids);
    }
123
124
125
    if (!empty($data->courseids)) {
        $urlparams['courseids'] = implode(',', $data->courseids);
    }
126
127
128
129
130
131
132
133
134
135
136
137
    $urlparams['timestart'] = $data->timestart;
    $urlparams['timeend'] = $data->timeend;
}
$url = new moodle_url('/search/index.php', $urlparams);
$PAGE->set_url($url);

// We are ready to render.
echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle);

// Get the results.
if ($data) {
138
    $results = $search->paged_search($data, $page);
139
140
141
142
}

if ($errorstr = $search->get_engine()->get_query_error()) {
    echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
143
} else if (empty($results->totalcount) && !empty($data)) {
144
145
146
147
148
149
    echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
}

$mform->display();

if (!empty($results)) {
150
    echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url);
151
152
153
154
155
156
157
158
159
160
161

    \core_search\manager::trigger_search_results_viewed([
        'q' => $data->q,
        'page' => $page,
        'title' => $data->title,
        'areaids' => !empty($data->areaids) ? $data->areaids : array(),
        'courseids' => !empty($data->courseids) ? $data->courseids : array(),
        'timestart' => isset($data->timestart) ? $data->timestart : 0,
        'timeend' => isset($data->timeend) ? $data->timeend : 0
    ]);

162
163
164
}

echo $OUTPUT->footer();