Merge branch 'MDL-56673-fix' of git://github.com/danpoltawski/moodle
[moodle.git] / search / index.php
blob9c67e3dfde4b3b32275d8fcc00af09453d4799ab
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 // Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
32 $context = context_system::instance();
33 $pagetitle = get_string('globalsearch', 'search');
34 $PAGE->set_context($context);
35 $PAGE->set_pagelayout('standard');
36 $PAGE->set_title($pagetitle);
37 $PAGE->set_heading($pagetitle);
39 if (!empty($CFG->forcelogin)) {
40 require_login();
43 require_capability('moodle/search:query', $context);
45 $searchrenderer = $PAGE->get_renderer('core_search');
47 if (\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;
56 $search = \core_search\manager::instance();
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()));
61 $data = $mform->get_data();
62 if (!$data && $q) {
63 // Data can also come from the URL.
65 $data = new stdClass();
66 $data->q = $q;
67 $data->title = $title;
68 $areaids = optional_param('areaids', '', PARAM_RAW);
69 if (!empty($areaids)) {
70 $areaids = explode(',', $areaids);
71 $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
73 $courseids = optional_param('courseids', '', PARAM_RAW);
74 if (!empty($courseids)) {
75 $courseids = explode(',', $courseids);
76 $data->courseids = clean_param_array($courseids, PARAM_INT);
78 $data->timestart = optional_param('timestart', 0, PARAM_INT);
79 $data->timeend = optional_param('timeend', 0, PARAM_INT);
80 $mform->set_data($data);
83 // Set the page URL.
84 $urlparams = array('page' => $page);
85 if ($data) {
86 $urlparams['q'] = $data->q;
87 $urlparams['title'] = $data->title;
88 if (!empty($data->areaids)) {
89 $urlparams['areaids'] = implode(',', $data->areaids);
91 if (!empty($data->courseids)) {
92 $urlparams['courseids'] = implode(',', $data->courseids);
94 $urlparams['timestart'] = $data->timestart;
95 $urlparams['timeend'] = $data->timeend;
97 $url = new moodle_url('/search/index.php', $urlparams);
98 $PAGE->set_url($url);
100 // We are ready to render.
101 echo $OUTPUT->header();
102 echo $OUTPUT->heading($pagetitle);
104 // Get the results.
105 if ($data) {
106 $results = $search->paged_search($data, $page);
109 if ($errorstr = $search->get_engine()->get_query_error()) {
110 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
111 } else if (empty($results->totalcount) && !empty($data)) {
112 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
115 $mform->display();
117 if (!empty($results)) {
118 echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url);
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
132 echo $OUTPUT->footer();