1 |
efrain |
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 |
|
|
|
25 |
namespace core_search\output\form;
|
|
|
26 |
|
|
|
27 |
use core_search\manager;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die;
|
|
|
30 |
|
|
|
31 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
32 |
|
|
|
33 |
class search extends \moodleform {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Form definition.
|
|
|
37 |
*
|
|
|
38 |
* @return void
|
|
|
39 |
*/
|
|
|
40 |
function definition() {
|
|
|
41 |
global $USER, $DB, $OUTPUT;
|
|
|
42 |
|
|
|
43 |
$mform =& $this->_form;
|
|
|
44 |
|
|
|
45 |
if (\core_search\manager::is_search_area_categories_enabled() && !empty($this->_customdata['cat'])) {
|
|
|
46 |
$mform->addElement('hidden', 'cat');
|
|
|
47 |
$mform->setType('cat', PARAM_NOTAGS);
|
|
|
48 |
$mform->setDefault('cat', $this->_customdata['cat']);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$mform->disable_form_change_checker();
|
|
|
52 |
$mform->addElement('header', 'search', get_string('search', 'search'));
|
|
|
53 |
|
|
|
54 |
// Help info depends on the selected search engine.
|
|
|
55 |
$mform->addElement('text', 'q', get_string('enteryoursearchquery', 'search'));
|
|
|
56 |
$mform->addHelpButton('q', 'searchinfo', $this->_customdata['searchengine']);
|
|
|
57 |
$mform->setType('q', PARAM_TEXT);
|
|
|
58 |
$mform->addRule('q', get_string('required'), 'required', null, 'client');
|
|
|
59 |
|
|
|
60 |
// Show the 'search within' option if the user came from a particular context.
|
|
|
61 |
if (!empty($this->_customdata['searchwithin'])) {
|
|
|
62 |
$mform->addElement('select', 'searchwithin', get_string('searchwithin', 'search'),
|
|
|
63 |
$this->_customdata['searchwithin']);
|
|
|
64 |
$mform->setDefault('searchwithin', '');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// If the search engine provides multiple ways to order results, show options.
|
|
|
68 |
if (!empty($this->_customdata['orderoptions']) &&
|
|
|
69 |
count($this->_customdata['orderoptions']) > 1) {
|
|
|
70 |
|
|
|
71 |
$mform->addElement('select', 'order', get_string('order', 'search'),
|
|
|
72 |
$this->_customdata['orderoptions']);
|
|
|
73 |
$mform->setDefault('order', 'relevance');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$mform->addElement('header', 'filtersection', get_string('filterheader', 'search'));
|
|
|
77 |
$mform->setExpanded('filtersection', false);
|
|
|
78 |
|
|
|
79 |
$mform->addElement('text', 'title', get_string('title', 'search'));
|
|
|
80 |
$mform->setType('title', PARAM_TEXT);
|
|
|
81 |
|
|
|
82 |
$search = \core_search\manager::instance(true);
|
|
|
83 |
$enabledsearchareas = \core_search\manager::get_search_areas_list(true);
|
|
|
84 |
$areanames = array();
|
|
|
85 |
|
|
|
86 |
if (\core_search\manager::is_search_area_categories_enabled() && !empty($this->_customdata['cat'])) {
|
|
|
87 |
$searchareacategory = \core_search\manager::get_search_area_category_by_name($this->_customdata['cat']);
|
|
|
88 |
$searchareas = $searchareacategory->get_areas();
|
|
|
89 |
foreach ($searchareas as $areaid => $searcharea) {
|
|
|
90 |
if (key_exists($areaid, $enabledsearchareas)) {
|
|
|
91 |
$areanames[$areaid] = $searcharea->get_visible_name();
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
} else {
|
|
|
95 |
foreach ($enabledsearchareas as $areaid => $searcharea) {
|
|
|
96 |
$areanames[$areaid] = $searcharea->get_visible_name();
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// Sort the array by the text.
|
|
|
101 |
\core_collator::asort($areanames);
|
|
|
102 |
|
|
|
103 |
$options = array(
|
|
|
104 |
'multiple' => true,
|
|
|
105 |
'noselectionstring' => get_string('allareas', 'search'),
|
|
|
106 |
);
|
|
|
107 |
$mform->addElement('autocomplete', 'areaids', get_string('searcharea', 'search'), $areanames, $options);
|
|
|
108 |
|
|
|
109 |
if (is_siteadmin()) {
|
|
|
110 |
$limittoenrolled = false;
|
|
|
111 |
} else {
|
|
|
112 |
$limittoenrolled = !manager::include_all_courses();
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$options = array(
|
|
|
116 |
'multiple' => true,
|
|
|
117 |
'limittoenrolled' => $limittoenrolled,
|
|
|
118 |
'noselectionstring' => get_string('allcourses', 'search'),
|
|
|
119 |
);
|
|
|
120 |
$mform->addElement('course', 'courseids', get_string('courses', 'core'), $options);
|
|
|
121 |
$mform->setType('courseids', PARAM_INT);
|
|
|
122 |
|
|
|
123 |
if (manager::include_all_courses() || !empty(get_config('core', 'searchallavailablecourses'))) {
|
|
|
124 |
$mform->addElement('checkbox', 'mycoursesonly', get_string('mycoursesonly', 'search'));
|
|
|
125 |
$mform->setType('mycoursesonly', PARAM_INT);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// If the search engine can search by user, and the user is logged in (so we have
|
|
|
129 |
// permission to call the user-listing web service) then show the user selector.
|
|
|
130 |
if ($search->get_engine()->supports_users() && isloggedin()) {
|
|
|
131 |
$options = [
|
|
|
132 |
'ajax' => 'core_search/form-search-user-selector',
|
|
|
133 |
'multiple' => true,
|
|
|
134 |
'noselectionstring' => get_string('allusers', 'search'),
|
|
|
135 |
'valuehtmlcallback' => function($value) {
|
|
|
136 |
global $DB, $OUTPUT;
|
|
|
137 |
$user = $DB->get_record('user', ['id' => (int)$value], '*', IGNORE_MISSING);
|
|
|
138 |
if (!$user || !user_can_view_profile($user)) {
|
|
|
139 |
return false;
|
|
|
140 |
}
|
|
|
141 |
$details = user_get_user_details($user);
|
|
|
142 |
return $OUTPUT->render_from_template(
|
|
|
143 |
'core_search/form-user-selector-suggestion', $details);
|
|
|
144 |
}
|
|
|
145 |
];
|
|
|
146 |
if (!empty($this->_customdata['withincourseid'])) {
|
|
|
147 |
$options['withincourseid'] = $this->_customdata['withincourseid'];
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$mform->addElement('autocomplete', 'userids', get_string('users'), [], $options);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
if (!empty($this->_customdata['searchwithin'])) {
|
|
|
154 |
// Course options should be hidden if we choose to search within a specific location.
|
|
|
155 |
$mform->hideIf('courseids', 'searchwithin', 'ne', '');
|
|
|
156 |
|
|
|
157 |
// Get groups on course (we don't show group selector if there aren't any).
|
|
|
158 |
$courseid = $this->_customdata['withincourseid'];
|
|
|
159 |
$allgroups = groups_get_all_groups($courseid);
|
|
|
160 |
if ($allgroups && $search->get_engine()->supports_group_filtering()) {
|
|
|
161 |
$groupnames = [];
|
|
|
162 |
foreach ($allgroups as $group) {
|
|
|
163 |
$groupnames[$group->id] = $group->name;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
// Create group autocomplete option.
|
|
|
167 |
$options = array(
|
|
|
168 |
'multiple' => true,
|
|
|
169 |
'noselectionstring' => get_string('allgroups'),
|
|
|
170 |
);
|
|
|
171 |
$mform->addElement('autocomplete', 'groupids', get_string('groups'), $groupnames, $options);
|
|
|
172 |
|
|
|
173 |
// Is the second 'search within' option a cm?
|
|
|
174 |
if (!empty($this->_customdata['withincmid'])) {
|
|
|
175 |
// Find out if the cm supports groups.
|
|
|
176 |
$modinfo = get_fast_modinfo($courseid);
|
|
|
177 |
$cm = $modinfo->get_cm($this->_customdata['withincmid']);
|
|
|
178 |
if ($cm->effectivegroupmode != NOGROUPS) {
|
|
|
179 |
// If it does, group ids are available when you have course or module selected.
|
|
|
180 |
$mform->hideIf('groupids', 'searchwithin', 'eq', '');
|
|
|
181 |
} else {
|
|
|
182 |
// Group ids are only available if you have course selected.
|
|
|
183 |
$mform->hideIf('groupids', 'searchwithin', 'ne', 'course');
|
|
|
184 |
}
|
|
|
185 |
} else {
|
|
|
186 |
$mform->hideIf('groupids', 'searchwithin', 'eq', '');
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
$mform->addElement('date_time_selector', 'timestart', get_string('fromtime', 'search'), array('optional' => true));
|
|
|
192 |
$mform->setDefault('timestart', 0);
|
|
|
193 |
|
|
|
194 |
$mform->addElement('date_time_selector', 'timeend', get_string('totime', 'search'), array('optional' => true));
|
|
|
195 |
$mform->setDefault('timeend', 0);
|
|
|
196 |
|
|
|
197 |
// Source context i.e. the page they came from when they clicked search.
|
|
|
198 |
$mform->addElement('hidden', 'context');
|
|
|
199 |
$mform->setType('context', PARAM_INT);
|
|
|
200 |
|
|
|
201 |
$this->add_action_buttons(false, get_string('search', 'search'));
|
|
|
202 |
}
|
|
|
203 |
}
|