weekly release 3.1dev
[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
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
57$search = \core_search\manager::instance();
58
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()));
61
62$data = $mform->get_data();
63if (!$data && $q) {
64 // Data can also come from the URL.
65
66 $data = new stdClass();
67 $data->q = $q;
68 $data->title = $title;
69 $data->areaid = $areaid;
70 $data->timestart = optional_param('timestart', 0, PARAM_INT);
71 $data->timeend = optional_param('timeend', 0, PARAM_INT);
72 $mform->set_data($data);
73}
74
75// Set the page URL.
76$urlparams = array('page' => $page);
77if ($data) {
78 $urlparams['q'] = $data->q;
79 $urlparams['title'] = $data->title;
80 $urlparams['areaid'] = $data->areaid;
81 $urlparams['timestart'] = $data->timestart;
82 $urlparams['timeend'] = $data->timeend;
83}
84$url = new moodle_url('/search/index.php', $urlparams);
85$PAGE->set_url($url);
86
87// We are ready to render.
88echo $OUTPUT->header();
89echo $OUTPUT->heading($pagetitle);
90
91// Get the results.
92if ($data) {
93 $data->page = $page;
94 $results = $search->search($data);
95}
96
97if ($errorstr = $search->get_engine()->get_query_error()) {
98 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
99} else if (empty($results) && !empty($data)) {
100 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
101}
102
103$mform->display();
104
105if (!empty($results)) {
106 echo $searchrenderer->render_results($results, $page, $url);
107}
108
109echo $OUTPUT->footer();