MDL-53632 competency: Consistently use the terms frameworks, plans, ...
[moodle.git] / search / index.php
blob0c27c24ebc47aef4f10dee01c3789ca3085ff142
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.
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/>.
17 /**
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 $areaid = optional_param('areaid', false, PARAM_ALPHANUMEXT);
31 // Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
33 $context = context_system::instance();
34 $pagetitle = get_string('globalsearch', 'search');
35 $PAGE->set_context($context);
36 $PAGE->set_pagelayout('standard');
37 $PAGE->set_title($pagetitle);
38 $PAGE->set_heading($pagetitle);
40 if (!empty($CFG->forcelogin)) {
41 require_login();
44 require_capability('moodle/search:query', $context);
46 $searchrenderer = $PAGE->get_renderer('core_search');
48 if (\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;
57 $search = \core_search\manager::instance();
59 // We first get the submitted data as we want to set it all in the page URL.
60 $mform = new \core_search\output\form\search(null, array('searchengine' => $search->get_engine()->get_plugin_name()));
62 $data = $mform->get_data();
63 if (!$data && $q) {
64 // Data can also come from the URL.
66 $data = new stdClass();
67 $data->q = $q;
68 $data->title = $title;
69 $areaids = optional_param('areaids', '', PARAM_RAW);
70 if (!empty($areaids)) {
71 $areaids = explode(',', $areaids);
72 $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
74 $courseids = optional_param('courseids', '', PARAM_RAW);
75 if (!empty($courseids)) {
76 $courseids = explode(',', $courseids);
77 $data->courseids = clean_param_array($courseids, PARAM_INT);
79 $data->timestart = optional_param('timestart', 0, PARAM_INT);
80 $data->timeend = optional_param('timeend', 0, PARAM_INT);
81 $mform->set_data($data);
84 // Set the page URL.
85 $urlparams = array('page' => $page);
86 if ($data) {
87 $urlparams['q'] = $data->q;
88 $urlparams['title'] = $data->title;
89 if (!empty($data->areaids)) {
90 $urlparams['areaids'] = implode(',', $data->areaids);
92 if (!empty($data->courseids)) {
93 $urlparams['courseids'] = implode(',', $data->courseids);
95 $urlparams['timestart'] = $data->timestart;
96 $urlparams['timeend'] = $data->timeend;
98 $url = new moodle_url('/search/index.php', $urlparams);
99 $PAGE->set_url($url);
101 // We are ready to render.
102 echo $OUTPUT->header();
103 echo $OUTPUT->heading($pagetitle);
105 // Get the results.
106 if ($data) {
107 $data->page = $page;
108 $results = $search->search($data);
111 if ($errorstr = $search->get_engine()->get_query_error()) {
112 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
113 } else if (empty($results) && !empty($data)) {
114 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
117 $mform->display();
119 if (!empty($results)) {
120 echo $searchrenderer->render_results($results, $page, $url);
123 echo $OUTPUT->footer();