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