MDL-31989 search: Search API and search engine API
[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);
30$areaid = optional_param('areaid', false, PARAM_ALPHANUMEXT);
31// Moving timestart and timeend further down as they might come as an array if they come from the form.
32
33$pagetitle = get_string('globalsearch', 'search');
34$PAGE->set_context(context_system::instance());
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
43$searchrenderer = $PAGE->get_renderer('core_search');
44
45if (\core_search\manager::is_global_search_enabled() === false) {
46 $PAGE->set_url(new moodle_url('/search/index.php'));
47 echo $OUTPUT->header();
48 echo $OUTPUT->heading($pagetitle);
49 echo $searchrenderer->render_search_disabled();
50 echo $OUTPUT->footer();
51 exit;
52}
53
54$search = \core_search\manager::instance();
55
56// We first get the submitted data as we want to set it all in the page URL.
57$mform = new \core_search\output\form\search(null, array('searchengine' => $search->get_engine()->get_plugin_name()));
58
59$data = $mform->get_data();
60if (!$data && $q) {
61 // Data can also come from the URL.
62
63 $data = new stdClass();
64 $data->q = $q;
65 $data->title = $title;
66 $data->areaid = $areaid;
67 $data->timestart = optional_param('timestart', 0, PARAM_INT);
68 $data->timeend = optional_param('timeend', 0, PARAM_INT);
69 $mform->set_data($data);
70}
71
72// Set the page URL.
73$urlparams = array('page' => $page);
74if ($data) {
75 $urlparams['q'] = $data->q;
76 $urlparams['title'] = $data->title;
77 $urlparams['areaid'] = $data->areaid;
78 $urlparams['timestart'] = $data->timestart;
79 $urlparams['timeend'] = $data->timeend;
80}
81$url = new moodle_url('/search/index.php', $urlparams);
82$PAGE->set_url($url);
83
84// We are ready to render.
85echo $OUTPUT->header();
86echo $OUTPUT->heading($pagetitle);
87
88// Get the results.
89if ($data) {
90 $data->page = $page;
91 $results = $search->search($data);
92}
93
94if ($errorstr = $search->get_engine()->get_query_error()) {
95 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
96} else if (empty($results) && !empty($data)) {
97 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
98}
99
100$mform->display();
101
102if (!empty($results)) {
103 echo $searchrenderer->render_results($results, $page, $url);
104}
105
106echo $OUTPUT->footer();