Merge branch 'MDL-54956-310' of git://github.com/jleyva/moodle into MOODLE_310_STABLE
[moodle.git] / search / index.php
CommitLineData
db48207e
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Global Search index page for entering queries and display of results
19 *
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
23 */
24
25require_once(__DIR__ . '/../config.php');
26
27$page = optional_param('page', 0, PARAM_INT);
28$q = optional_param('q', '', PARAM_NOTAGS);
29$title = optional_param('title', '', PARAM_NOTAGS);
7f9fb2c8 30$contextid = optional_param('context', 0, PARAM_INT);
e9074ee3 31$cat = optional_param('cat', '', PARAM_NOTAGS);
2085e860 32$mycoursesonly = optional_param('mycoursesonly', 0, PARAM_INT);
e9074ee3
DM
33
34if (\core_search\manager::is_search_area_categories_enabled()) {
35 $cat = \core_search\manager::get_search_area_category_by_name($cat);
36}
37
501801a2 38// Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
db48207e 39
69d66020 40$context = context_system::instance();
db48207e 41$pagetitle = get_string('globalsearch', 'search');
69d66020 42$PAGE->set_context($context);
db48207e
DM
43$PAGE->set_pagelayout('standard');
44$PAGE->set_title($pagetitle);
45$PAGE->set_heading($pagetitle);
46
47if (!empty($CFG->forcelogin)) {
48 require_login();
49}
50
5bd72dd6
KR
51// Unlock the session during a search.
52\core\session\manager::write_close();
53
69d66020
DM
54require_capability('moodle/search:query', $context);
55
db48207e
DM
56$searchrenderer = $PAGE->get_renderer('core_search');
57
58if (\core_search\manager::is_global_search_enabled() === false) {
59 $PAGE->set_url(new moodle_url('/search/index.php'));
60 echo $OUTPUT->header();
61 echo $OUTPUT->heading($pagetitle);
62 echo $searchrenderer->render_search_disabled();
63 echo $OUTPUT->footer();
64 exit;
65}
66
679e8d8b 67$search = \core_search\manager::instance(true, true);
db48207e 68
7f9fb2c8 69// Set up custom data for form.
70$customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
71if ($contextid) {
72 // When a context is supplied, check if it's within course level. If so, show dropdown.
73 $context = context::instance_by_id($contextid);
74 $coursecontext = $context->get_course_context(false);
75 if ($coursecontext) {
76 $searchwithin = [];
77 $searchwithin[''] = get_string('everywhere', 'search');
78 $searchwithin['course'] = $coursecontext->get_context_name();
3c49b0e6 79 if ($context->contextlevel != CONTEXT_COURSE) {
7f9fb2c8 80 $searchwithin['context'] = $context->get_context_name();
3c49b0e6 81 if ($context->contextlevel == CONTEXT_MODULE) {
82 $customdata['withincmid'] = $context->instanceid;
83 }
7f9fb2c8 84 }
85 $customdata['searchwithin'] = $searchwithin;
3c49b0e6 86 $customdata['withincourseid'] = $coursecontext->instanceid;
7f9fb2c8 87 }
fc440796 88
7f9fb2c8 89}
784ed31e
MP
90// Get available ordering options from search engine.
91$customdata['orderoptions'] = $search->get_engine()->get_supported_orders($context);
92
e9074ee3
DM
93if ($cat instanceof \core_search\area_category) {
94 $customdata['cat'] = $cat->get_name();
95}
96
7f9fb2c8 97$mform = new \core_search\output\form\search(null, $customdata);
db48207e
DM
98
99$data = $mform->get_data();
100if (!$data && $q) {
101 // Data can also come from the URL.
102
103 $data = new stdClass();
104 $data->q = $q;
105 $data->title = $title;
501801a2
EM
106 $areaids = optional_param('areaids', '', PARAM_RAW);
107 if (!empty($areaids)) {
108 $areaids = explode(',', $areaids);
109 $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
110 }
427e3cbc
EM
111 $courseids = optional_param('courseids', '', PARAM_RAW);
112 if (!empty($courseids)) {
113 $courseids = explode(',', $courseids);
114 $data->courseids = clean_param_array($courseids, PARAM_INT);
115 }
db48207e
DM
116 $data->timestart = optional_param('timestart', 0, PARAM_INT);
117 $data->timeend = optional_param('timeend', 0, PARAM_INT);
7f9fb2c8 118
119 $data->context = $contextid;
2085e860 120 $data->mycoursesonly = $mycoursesonly;
7f9fb2c8 121
db48207e
DM
122 $mform->set_data($data);
123}
124
7f9fb2c8 125// Convert the 'search within' option, if used, to course or context restrictions.
126if ($data && !empty($data->searchwithin)) {
127 switch ($data->searchwithin) {
128 case 'course':
129 $data->courseids = [$coursecontext->instanceid];
130 break;
131 case 'context':
132 $data->courseids = [$coursecontext->instanceid];
133 $data->contextids = [$context->id];
134 break;
135 }
136}
137
fc440796 138// Inform search engine about source context.
139if (!empty($context) && $data) {
140 $data->context = $context;
141}
142
e9074ee3
DM
143if ($data && $cat instanceof \core_search\area_category) {
144 $data->cat = $cat->get_name();
145}
146
db48207e
DM
147// Set the page URL.
148$urlparams = array('page' => $page);
149if ($data) {
150 $urlparams['q'] = $data->q;
151 $urlparams['title'] = $data->title;
501801a2
EM
152 if (!empty($data->areaids)) {
153 $urlparams['areaids'] = implode(',', $data->areaids);
154 }
427e3cbc
EM
155 if (!empty($data->courseids)) {
156 $urlparams['courseids'] = implode(',', $data->courseids);
157 }
db48207e
DM
158 $urlparams['timestart'] = $data->timestart;
159 $urlparams['timeend'] = $data->timeend;
2085e860 160 $urlparams['mycoursesonly'] = isset($data->mycoursesonly) ? $data->mycoursesonly : 0;
db48207e 161}
e9074ee3
DM
162
163if ($cat instanceof \core_search\area_category) {
164 $urlparams['cat'] = $cat->get_name();
165}
166
db48207e
DM
167$url = new moodle_url('/search/index.php', $urlparams);
168$PAGE->set_url($url);
169
170// We are ready to render.
171echo $OUTPUT->header();
172echo $OUTPUT->heading($pagetitle);
173
174// Get the results.
175if ($data) {
053118a1 176 $results = $search->paged_search($data, $page);
db48207e
DM
177}
178
679e8d8b 179// Show search information if configured by system administrator.
180if ($CFG->searchbannerenable && $CFG->searchbanner) {
181 echo $OUTPUT->notification(format_text($CFG->searchbanner, FORMAT_HTML), 'notifywarning');
182}
183
db48207e
DM
184if ($errorstr = $search->get_engine()->get_query_error()) {
185 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
8178b782 186} else if (empty($results->totalcount) && !empty($data)) {
db48207e
DM
187 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
188}
189
190$mform->display();
191
192if (!empty($results)) {
e9074ee3 193 echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url, $cat);
e71061a2
DM
194
195 \core_search\manager::trigger_search_results_viewed([
196 'q' => $data->q,
197 'page' => $page,
198 'title' => $data->title,
199 'areaids' => !empty($data->areaids) ? $data->areaids : array(),
200 'courseids' => !empty($data->courseids) ? $data->courseids : array(),
201 'timestart' => isset($data->timestart) ? $data->timestart : 0,
202 'timeend' => isset($data->timeend) ? $data->timeend : 0
203 ]);
204
db48207e
DM
205}
206
207echo $OUTPUT->footer();