MDL-61363 tag: allow tagging in different context to item
[moodle.git] / search / index.php
blobece070ca488d45fdca997706e65e150840344656
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 $contextid = optional_param('context', 0, PARAM_INT);
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 // Set up custom data for form.
60 $customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
61 if ($contextid) {
62 // When a context is supplied, check if it's within course level. If so, show dropdown.
63 $context = context::instance_by_id($contextid);
64 $coursecontext = $context->get_course_context(false);
65 if ($coursecontext) {
66 $searchwithin = [];
67 $searchwithin[''] = get_string('everywhere', 'search');
68 $searchwithin['course'] = $coursecontext->get_context_name();
69 if ($context->contextlevel !== CONTEXT_COURSE) {
70 $searchwithin['context'] = $context->get_context_name();
72 $customdata['searchwithin'] = $searchwithin;
75 $mform = new \core_search\output\form\search(null, $customdata);
77 $data = $mform->get_data();
78 if (!$data && $q) {
79 // Data can also come from the URL.
81 $data = new stdClass();
82 $data->q = $q;
83 $data->title = $title;
84 $areaids = optional_param('areaids', '', PARAM_RAW);
85 if (!empty($areaids)) {
86 $areaids = explode(',', $areaids);
87 $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
89 $courseids = optional_param('courseids', '', PARAM_RAW);
90 if (!empty($courseids)) {
91 $courseids = explode(',', $courseids);
92 $data->courseids = clean_param_array($courseids, PARAM_INT);
94 $data->timestart = optional_param('timestart', 0, PARAM_INT);
95 $data->timeend = optional_param('timeend', 0, PARAM_INT);
97 $data->context = $contextid;
99 $mform->set_data($data);
102 // Convert the 'search within' option, if used, to course or context restrictions.
103 if ($data && !empty($data->searchwithin)) {
104 switch ($data->searchwithin) {
105 case 'course':
106 $data->courseids = [$coursecontext->instanceid];
107 break;
108 case 'context':
109 $data->courseids = [$coursecontext->instanceid];
110 $data->contextids = [$context->id];
111 break;
115 // Set the page URL.
116 $urlparams = array('page' => $page);
117 if ($data) {
118 $urlparams['q'] = $data->q;
119 $urlparams['title'] = $data->title;
120 if (!empty($data->areaids)) {
121 $urlparams['areaids'] = implode(',', $data->areaids);
123 if (!empty($data->courseids)) {
124 $urlparams['courseids'] = implode(',', $data->courseids);
126 $urlparams['timestart'] = $data->timestart;
127 $urlparams['timeend'] = $data->timeend;
129 $url = new moodle_url('/search/index.php', $urlparams);
130 $PAGE->set_url($url);
132 // We are ready to render.
133 echo $OUTPUT->header();
134 echo $OUTPUT->heading($pagetitle);
136 // Get the results.
137 if ($data) {
138 $results = $search->paged_search($data, $page);
141 if ($errorstr = $search->get_engine()->get_query_error()) {
142 echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
143 } else if (empty($results->totalcount) && !empty($data)) {
144 echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
147 $mform->display();
149 if (!empty($results)) {
150 echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url);
152 \core_search\manager::trigger_search_results_viewed([
153 'q' => $data->q,
154 'page' => $page,
155 'title' => $data->title,
156 'areaids' => !empty($data->areaids) ? $data->areaids : array(),
157 'courseids' => !empty($data->courseids) ? $data->courseids : array(),
158 'timestart' => isset($data->timestart) ? $data->timestart : 0,
159 'timeend' => isset($data->timeend) ? $data->timeend : 0
164 echo $OUTPUT->footer();