MDL-60880 core_search: Allow search of specific context (back-end)
[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);
501801a2 30// Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
db48207e 31
69d66020 32$context = context_system::instance();
db48207e 33$pagetitle = get_string('globalsearch', 'search');
69d66020 34$PAGE->set_context($context);
db48207e
DM
35$PAGE->set_pagelayout('standard');
36$PAGE->set_title($pagetitle);
37$PAGE->set_heading($pagetitle);
38
39if (!empty($CFG->forcelogin)) {
40 require_login();
41}
42
69d66020
DM
43require_capability('moodle/search:query', $context);
44
db48207e
DM
45$searchrenderer = $PAGE->get_renderer('core_search');
46
47if (\core_search\manager::is_global_search_enabled() === false) {
48 $PAGE->set_url(new moodle_url('/search/index.php'));
49 echo $OUTPUT->header();
50 echo $OUTPUT->heading($pagetitle);
51 echo $searchrenderer->render_search_disabled();
52 echo $OUTPUT->footer();
53 exit;
54}
55
56$search = \core_search\manager::instance();
57
58// We first get the submitted data as we want to set it all in the page URL.
59$mform = new \core_search\output\form\search(null, array('searchengine' => $search->get_engine()->get_plugin_name()));
60
61$data = $mform->get_data();
62if (!$data && $q) {
63 // Data can also come from the URL.
64
65 $data = new stdClass();
66 $data->q = $q;
67 $data->title = $title;
501801a2
EM
68 $areaids = optional_param('areaids', '', PARAM_RAW);
69 if (!empty($areaids)) {
70 $areaids = explode(',', $areaids);
71 $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
72 }
427e3cbc
EM
73 $courseids = optional_param('courseids', '', PARAM_RAW);
74 if (!empty($courseids)) {
75 $courseids = explode(',', $courseids);
76 $data->courseids = clean_param_array($courseids, PARAM_INT);
77 }
db48207e
DM
78 $data->timestart = optional_param('timestart', 0, PARAM_INT);
79 $data->timeend = optional_param('timeend', 0, PARAM_INT);
80 $mform->set_data($data);
81}
82
83// Set the page URL.
84$urlparams = array('page' => $page);
85if ($data) {
86 $urlparams['q'] = $data->q;
87 $urlparams['title'] = $data->title;
501801a2
EM
88 if (!empty($data->areaids)) {
89 $urlparams['areaids'] = implode(',', $data->areaids);
90 }
427e3cbc
EM
91 if (!empty($data->courseids)) {
92 $urlparams['courseids'] = implode(',', $data->courseids);
93 }
db48207e
DM
94 $urlparams['timestart'] = $data->timestart;
95 $urlparams['timeend'] = $data->timeend;
96}
97$url = new moodle_url('/search/index.php', $urlparams);
98$PAGE->set_url($url);
99
100// We are ready to render.
101echo $OUTPUT->header();
102echo $OUTPUT->heading($pagetitle);
103
104// Get the results.
105if ($data) {
053118a1 106 $results = $search->paged_search($data, $page);
db48207e
DM
107}
108
109if ($errorstr = $search->get_engine()->get_query_error()) {
110 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
8178b782 111} else if (empty($results->totalcount) && !empty($data)) {
db48207e
DM
112 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
113}
114
115$mform->display();
116
117if (!empty($results)) {
053118a1 118 echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url);
e71061a2
DM
119
120 \core_search\manager::trigger_search_results_viewed([
121 'q' => $data->q,
122 'page' => $page,
123 'title' => $data->title,
124 'areaids' => !empty($data->areaids) ? $data->areaids : array(),
125 'courseids' => !empty($data->courseids) ? $data->courseids : array(),
126 'timestart' => isset($data->timestart) ? $data->timestart : 0,
127 'timeend' => isset($data->timeend) ? $data->timeend : 0
128 ]);
129
db48207e
DM
130}
131
132echo $OUTPUT->footer();