2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Global Search index page for entering queries and display of results
20 * @package core_search
21 * @copyright Prateek Sachan {@link http://prateeksachan.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../config.php');
27 $page = optional_param('page', 0, PARAM_INT
);
28 $q = optional_param('q', '', PARAM_NOTAGS
);
29 $title = optional_param('title', '', PARAM_NOTAGS
);
30 $contextid = optional_param('context', 0, PARAM_INT
);
31 $cat = optional_param('cat', '', PARAM_NOTAGS
);
32 $mycoursesonly = optional_param('mycoursesonly', 0, PARAM_INT
);
34 if (\core_search\manager
::is_search_area_categories_enabled()) {
35 $cat = \core_search\manager
::get_search_area_category_by_name($cat);
38 // Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
40 $context = context_system
::instance();
41 $pagetitle = get_string('globalsearch', 'search');
42 $PAGE->set_context($context);
43 $PAGE->set_pagelayout('standard');
44 $PAGE->set_title($pagetitle);
45 $PAGE->set_heading($pagetitle);
47 if (!empty($CFG->forcelogin
)) {
51 require_capability('moodle/search:query', $context);
53 $searchrenderer = $PAGE->get_renderer('core_search');
55 if (\core_search\manager
::is_global_search_enabled() === false) {
56 $PAGE->set_url(new moodle_url('/search/index.php'));
57 echo $OUTPUT->header();
58 echo $OUTPUT->heading($pagetitle);
59 echo $searchrenderer->render_search_disabled();
60 echo $OUTPUT->footer();
64 $search = \core_search\manager
::instance(true);
66 // Set up custom data for form.
67 $customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
69 // When a context is supplied, check if it's within course level. If so, show dropdown.
70 $context = context
::instance_by_id($contextid);
71 $coursecontext = $context->get_course_context(false);
74 $searchwithin[''] = get_string('everywhere', 'search');
75 $searchwithin['course'] = $coursecontext->get_context_name();
76 if ($context->contextlevel
!= CONTEXT_COURSE
) {
77 $searchwithin['context'] = $context->get_context_name();
78 if ($context->contextlevel
== CONTEXT_MODULE
) {
79 $customdata['withincmid'] = $context->instanceid
;
82 $customdata['searchwithin'] = $searchwithin;
83 $customdata['withincourseid'] = $coursecontext->instanceid
;
87 // Get available ordering options from search engine.
88 $customdata['orderoptions'] = $search->get_engine()->get_supported_orders($context);
90 if ($cat instanceof \core_search\area_category
) {
91 $customdata['cat'] = $cat->get_name();
94 $mform = new \core_search\output\form\
search(null, $customdata);
96 $data = $mform->get_data();
98 // Data can also come from the URL.
100 $data = new stdClass();
102 $data->title
= $title;
103 $areaids = optional_param('areaids', '', PARAM_RAW
);
104 if (!empty($areaids)) {
105 $areaids = explode(',', $areaids);
106 $data->areaids
= clean_param_array($areaids, PARAM_ALPHANUMEXT
);
108 $courseids = optional_param('courseids', '', PARAM_RAW
);
109 if (!empty($courseids)) {
110 $courseids = explode(',', $courseids);
111 $data->courseids
= clean_param_array($courseids, PARAM_INT
);
113 $data->timestart
= optional_param('timestart', 0, PARAM_INT
);
114 $data->timeend
= optional_param('timeend', 0, PARAM_INT
);
116 $data->context
= $contextid;
117 $data->mycoursesonly
= $mycoursesonly;
119 $mform->set_data($data);
122 // Convert the 'search within' option, if used, to course or context restrictions.
123 if ($data && !empty($data->searchwithin
)) {
124 switch ($data->searchwithin
) {
126 $data->courseids
= [$coursecontext->instanceid
];
129 $data->courseids
= [$coursecontext->instanceid
];
130 $data->contextids
= [$context->id
];
135 // Inform search engine about source context.
136 if (!empty($context) && $data) {
137 $data->context
= $context;
140 if ($data && $cat instanceof \core_search\area_category
) {
141 $data->cat
= $cat->get_name();
145 $urlparams = array('page' => $page);
147 $urlparams['q'] = $data->q
;
148 $urlparams['title'] = $data->title
;
149 if (!empty($data->areaids
)) {
150 $urlparams['areaids'] = implode(',', $data->areaids
);
152 if (!empty($data->courseids
)) {
153 $urlparams['courseids'] = implode(',', $data->courseids
);
155 $urlparams['timestart'] = $data->timestart
;
156 $urlparams['timeend'] = $data->timeend
;
157 $urlparams['mycoursesonly'] = isset($data->mycoursesonly
) ?
$data->mycoursesonly
: 0;
160 if ($cat instanceof \core_search\area_category
) {
161 $urlparams['cat'] = $cat->get_name();
164 $url = new moodle_url('/search/index.php', $urlparams);
165 $PAGE->set_url($url);
167 // We are ready to render.
168 echo $OUTPUT->header();
169 echo $OUTPUT->heading($pagetitle);
173 $results = $search->paged_search($data, $page);
176 if ($errorstr = $search->get_engine()->get_query_error()) {
177 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
178 } else if (empty($results->totalcount
) && !empty($data)) {
179 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
184 if (!empty($results)) {
185 echo $searchrenderer->render_results($results->results
, $results->actualpage
, $results->totalcount
, $url, $cat);
187 \core_search\manager
::trigger_search_results_viewed([
190 'title' => $data->title
,
191 'areaids' => !empty($data->areaids
) ?
$data->areaids
: array(),
192 'courseids' => !empty($data->courseids
) ?
$data->courseids
: array(),
193 'timestart' => isset($data->timestart
) ?
$data->timestart
: 0,
194 'timeend' => isset($data->timeend
) ?
$data->timeend
: 0
199 echo $OUTPUT->footer();