MDL-61102 core_search: Restore search manager instance
[moodle.git] / search / classes / output / form / search.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 search form definition
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
25namespace core_search\output\form;
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->libdir . '/formslib.php');
30
31class search extends \moodleform {
32
33 /**
34 * Form definition.
35 *
36 * @return void
37 */
38 function definition() {
3c49b0e6 39 global $USER;
db48207e
DM
40
41 $mform =& $this->_form;
e11127b4 42 $mform->disable_form_change_checker();
db48207e
DM
43 $mform->addElement('header', 'search', get_string('search', 'search'));
44
45 // Help info depends on the selected search engine.
46 $mform->addElement('text', 'q', get_string('enteryoursearchquery', 'search'));
47 $mform->addHelpButton('q', 'searchinfo', $this->_customdata['searchengine']);
48 $mform->setType('q', PARAM_TEXT);
49 $mform->addRule('q', get_string('required'), 'required', null, 'client');
50
7f9fb2c8 51 // Show the 'search within' option if the user came from a particular context.
52 if (!empty($this->_customdata['searchwithin'])) {
53 $mform->addElement('select', 'searchwithin', get_string('searchwithin', 'search'),
54 $this->_customdata['searchwithin']);
55 $mform->setDefault('searchwithin', '');
56 }
57
fc440796 58 // If the search engine provides multiple ways to order results, show options.
59 if (!empty($this->_customdata['orderoptions']) &&
60 count($this->_customdata['orderoptions']) > 1) {
61
62 $mform->addElement('select', 'order', get_string('order', 'search'),
63 $this->_customdata['orderoptions']);
64 $mform->setDefault('order', 'relevance');
65 }
66
db48207e
DM
67 $mform->addElement('header', 'filtersection', get_string('filterheader', 'search'));
68 $mform->setExpanded('filtersection', false);
69
70 $mform->addElement('text', 'title', get_string('title', 'search'));
71 $mform->setType('title', PARAM_TEXT);
72
8000eac4
DM
73 $search = \core_search\manager::instance(true);
74
db48207e 75 $searchareas = \core_search\manager::get_search_areas_list(true);
501801a2 76 $areanames = array();
db48207e
DM
77 foreach ($searchareas as $areaid => $searcharea) {
78 $areanames[$areaid] = $searcharea->get_visible_name();
79 }
a02fe723
EM
80
81 // Sort the array by the text.
82 \core_collator::asort($areanames);
83
501801a2
EM
84 $options = array(
85 'multiple' => true,
86 'noselectionstring' => get_string('allareas', 'search'),
87 );
88 $mform->addElement('autocomplete', 'areaids', get_string('searcharea', 'search'), $areanames, $options);
db48207e 89
427e3cbc
EM
90 $options = array(
91 'multiple' => true,
92 'limittoenrolled' => !is_siteadmin(),
93 'noselectionstring' => get_string('allcourses', 'search'),
94 );
95 $mform->addElement('course', 'courseids', get_string('courses', 'core'), $options);
96 $mform->setType('courseids', PARAM_INT);
97
7f9fb2c8 98 if (!empty($this->_customdata['searchwithin'])) {
3c49b0e6 99 // Course options should be hidden if we choose to search within a specific location.
7f9fb2c8 100 $mform->hideIf('courseids', 'searchwithin', 'ne', '');
3c49b0e6 101
102 // Get groups on course (we don't show group selector if there aren't any).
103 $courseid = $this->_customdata['withincourseid'];
104 $allgroups = groups_get_all_groups($courseid);
82f0ddc7 105 if ($allgroups && $search->get_engine()->supports_group_filtering()) {
3c49b0e6 106 $groupnames = [];
107 foreach ($allgroups as $group) {
108 $groupnames[$group->id] = $group->name;
109 }
110
111 // Create group autocomplete option.
112 $options = array(
113 'multiple' => true,
114 'noselectionstring' => get_string('allgroups'),
115 );
116 $mform->addElement('autocomplete', 'groupids', get_string('groups'), $groupnames, $options);
117
118 // Is the second 'search within' option a cm?
119 if (!empty($this->_customdata['withincmid'])) {
120 // Find out if the cm supports groups.
121 $modinfo = get_fast_modinfo($courseid);
122 $cm = $modinfo->get_cm($this->_customdata['withincmid']);
123 if ($cm->effectivegroupmode != NOGROUPS) {
124 // If it does, group ids are available when you have course or module selected.
125 $mform->hideIf('groupids', 'searchwithin', 'eq', '');
126 } else {
127 // Group ids are only available if you have course selected.
128 $mform->hideIf('groupids', 'searchwithin', 'ne', 'course');
129 }
130 } else {
131 $mform->hideIf('groupids', 'searchwithin', 'eq', '');
132 }
133 }
7f9fb2c8 134 }
135
db48207e
DM
136 $mform->addElement('date_time_selector', 'timestart', get_string('fromtime', 'search'), array('optional' => true));
137 $mform->setDefault('timestart', 0);
138
139 $mform->addElement('date_time_selector', 'timeend', get_string('totime', 'search'), array('optional' => true));
140 $mform->setDefault('timeend', 0);
141
7f9fb2c8 142 // Source context i.e. the page they came from when they clicked search.
143 $mform->addElement('hidden', 'context');
144 $mform->setType('context', PARAM_INT);
145
db48207e
DM
146 $this->add_action_buttons(false, get_string('search', 'search'));
147 }
148}