Merge branch 'MDL-62899-search-icons-master' of https://github.com/dmitriim/moodle
[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);
501801a2 31// Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
db48207e 32
69d66020 33$context = context_system::instance();
db48207e 34$pagetitle = get_string('globalsearch', 'search');
69d66020 35$PAGE->set_context($context);
db48207e
DM
36$PAGE->set_pagelayout('standard');
37$PAGE->set_title($pagetitle);
38$PAGE->set_heading($pagetitle);
39
40if (!empty($CFG->forcelogin)) {
41 require_login();
42}
43
69d66020
DM
44require_capability('moodle/search:query', $context);
45
db48207e
DM
46$searchrenderer = $PAGE->get_renderer('core_search');
47
48if (\core_search\manager::is_global_search_enabled() === false) {
49 $PAGE->set_url(new moodle_url('/search/index.php'));
50 echo $OUTPUT->header();
51 echo $OUTPUT->heading($pagetitle);
52 echo $searchrenderer->render_search_disabled();
53 echo $OUTPUT->footer();
54 exit;
55}
56
73fd5666 57$search = \core_search\manager::instance(true);
db48207e 58
7f9fb2c8 59// Set up custom data for form.
60$customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
61if ($contextid) {
62 // When a context is supplied, check if it's within course level. If so, show dropdown.
63 $context = context::instance_by_id($contextid);
64 $coursecontext = $context->get_course_context(false);
65 if ($coursecontext) {
66 $searchwithin = [];
67 $searchwithin[''] = get_string('everywhere', 'search');
68 $searchwithin['course'] = $coursecontext->get_context_name();
3c49b0e6 69 if ($context->contextlevel != CONTEXT_COURSE) {
7f9fb2c8 70 $searchwithin['context'] = $context->get_context_name();
3c49b0e6 71 if ($context->contextlevel == CONTEXT_MODULE) {
72 $customdata['withincmid'] = $context->instanceid;
73 }
7f9fb2c8 74 }
75 $customdata['searchwithin'] = $searchwithin;
3c49b0e6 76 $customdata['withincourseid'] = $coursecontext->instanceid;
7f9fb2c8 77 }
fc440796 78
7f9fb2c8 79}
784ed31e
MP
80// Get available ordering options from search engine.
81$customdata['orderoptions'] = $search->get_engine()->get_supported_orders($context);
82
7f9fb2c8 83$mform = new \core_search\output\form\search(null, $customdata);
db48207e
DM
84
85$data = $mform->get_data();
86if (!$data && $q) {
87 // Data can also come from the URL.
88
89 $data = new stdClass();
90 $data->q = $q;
91 $data->title = $title;
501801a2
EM
92 $areaids = optional_param('areaids', '', PARAM_RAW);
93 if (!empty($areaids)) {
94 $areaids = explode(',', $areaids);
95 $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
96 }
427e3cbc
EM
97 $courseids = optional_param('courseids', '', PARAM_RAW);
98 if (!empty($courseids)) {
99 $courseids = explode(',', $courseids);
100 $data->courseids = clean_param_array($courseids, PARAM_INT);
101 }
db48207e
DM
102 $data->timestart = optional_param('timestart', 0, PARAM_INT);
103 $data->timeend = optional_param('timeend', 0, PARAM_INT);
7f9fb2c8 104
105 $data->context = $contextid;
106
db48207e
DM
107 $mform->set_data($data);
108}
109
7f9fb2c8 110// Convert the 'search within' option, if used, to course or context restrictions.
111if ($data && !empty($data->searchwithin)) {
112 switch ($data->searchwithin) {
113 case 'course':
114 $data->courseids = [$coursecontext->instanceid];
115 break;
116 case 'context':
117 $data->courseids = [$coursecontext->instanceid];
118 $data->contextids = [$context->id];
119 break;
120 }
121}
122
fc440796 123// Inform search engine about source context.
124if (!empty($context) && $data) {
125 $data->context = $context;
126}
127
db48207e
DM
128// Set the page URL.
129$urlparams = array('page' => $page);
130if ($data) {
131 $urlparams['q'] = $data->q;
132 $urlparams['title'] = $data->title;
501801a2
EM
133 if (!empty($data->areaids)) {
134 $urlparams['areaids'] = implode(',', $data->areaids);
135 }
427e3cbc
EM
136 if (!empty($data->courseids)) {
137 $urlparams['courseids'] = implode(',', $data->courseids);
138 }
db48207e
DM
139 $urlparams['timestart'] = $data->timestart;
140 $urlparams['timeend'] = $data->timeend;
141}
142$url = new moodle_url('/search/index.php', $urlparams);
143$PAGE->set_url($url);
144
145// We are ready to render.
146echo $OUTPUT->header();
147echo $OUTPUT->heading($pagetitle);
148
149// Get the results.
150if ($data) {
053118a1 151 $results = $search->paged_search($data, $page);
db48207e
DM
152}
153
154if ($errorstr = $search->get_engine()->get_query_error()) {
155 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
8178b782 156} else if (empty($results->totalcount) && !empty($data)) {
db48207e
DM
157 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
158}
159
160$mform->display();
161
162if (!empty($results)) {
053118a1 163 echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url);
e71061a2
DM
164
165 \core_search\manager::trigger_search_results_viewed([
166 'q' => $data->q,
167 'page' => $page,
168 'title' => $data->title,
169 'areaids' => !empty($data->areaids) ? $data->areaids : array(),
170 'courseids' => !empty($data->courseids) ? $data->courseids : array(),
171 'timestart' => isset($data->timestart) ? $data->timestart : 0,
172 'timeend' => isset($data->timeend) ? $data->timeend : 0
173 ]);
174
db48207e
DM
175}
176
177echo $OUTPUT->footer();