Merge branch 'MDL-61102-master' of https://github.com/sammarshallou/moodle
[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');
31 class search extends \moodleform {
33     /**
34      * Form definition.
35      *
36      * @return void
37      */
38     function definition() {
39         global $USER;
41         $mform =& $this->_form;
42         $mform->disable_form_change_checker();
43         $mform->addElement('header', 'search', get_string('search', 'search'));
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');
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         }
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) {
62             $mform->addElement('select', 'order', get_string('order', 'search'),
63                     $this->_customdata['orderoptions']);
64             $mform->setDefault('order', 'relevance');
65         }
67         $mform->addElement('header', 'filtersection', get_string('filterheader', 'search'));
68         $mform->setExpanded('filtersection', false);
70         $mform->addElement('text', 'title', get_string('title', 'search'));
71         $mform->setType('title', PARAM_TEXT);
73         $searchareas = \core_search\manager::get_search_areas_list(true);
74         $areanames = array();
75         foreach ($searchareas as $areaid => $searcharea) {
76             $areanames[$areaid] = $searcharea->get_visible_name();
77         }
79         // Sort the array by the text.
80         \core_collator::asort($areanames);
82         $options = array(
83             'multiple' => true,
84             'noselectionstring' => get_string('allareas', 'search'),
85         );
86         $mform->addElement('autocomplete', 'areaids', get_string('searcharea', 'search'), $areanames, $options);
88         $options = array(
89             'multiple' => true,
90             'limittoenrolled' => !is_siteadmin(),
91             'noselectionstring' => get_string('allcourses', 'search'),
92         );
93         $mform->addElement('course', 'courseids', get_string('courses', 'core'), $options);
94         $mform->setType('courseids', PARAM_INT);
96         if (!empty($this->_customdata['searchwithin'])) {
97             // Course options should be hidden if we choose to search within a specific location.
98             $mform->hideIf('courseids', 'searchwithin', 'ne', '');
100             // Get groups on course (we don't show group selector if there aren't any).
101             $courseid = $this->_customdata['withincourseid'];
102             $allgroups = groups_get_all_groups($courseid);
103             if ($allgroups && $search->get_engine()->supports_group_filtering()) {
104                 $groupnames = [];
105                 foreach ($allgroups as $group) {
106                     $groupnames[$group->id] = $group->name;
107                 }
109                 // Create group autocomplete option.
110                 $options = array(
111                         'multiple' => true,
112                         'noselectionstring' => get_string('allgroups'),
113                 );
114                 $mform->addElement('autocomplete', 'groupids', get_string('groups'), $groupnames, $options);
116                 // Is the second 'search within' option a cm?
117                 if (!empty($this->_customdata['withincmid'])) {
118                     // Find out if the cm supports groups.
119                     $modinfo = get_fast_modinfo($courseid);
120                     $cm = $modinfo->get_cm($this->_customdata['withincmid']);
121                     if ($cm->effectivegroupmode != NOGROUPS) {
122                         // If it does, group ids are available when you have course or module selected.
123                         $mform->hideIf('groupids', 'searchwithin', 'eq', '');
124                     } else {
125                         // Group ids are only available if you have course selected.
126                         $mform->hideIf('groupids', 'searchwithin', 'ne', 'course');
127                     }
128                 } else {
129                     $mform->hideIf('groupids', 'searchwithin', 'eq', '');
130                 }
131             }
132         }
134         $mform->addElement('date_time_selector', 'timestart', get_string('fromtime', 'search'), array('optional' => true));
135         $mform->setDefault('timestart', 0);
137         $mform->addElement('date_time_selector', 'timeend', get_string('totime', 'search'), array('optional' => true));
138         $mform->setDefault('timeend', 0);
140         // Source context i.e. the page they came from when they clicked search.
141         $mform->addElement('hidden', 'context');
142         $mform->setType('context', PARAM_INT);
144         $this->add_action_buttons(false, get_string('search', 'search'));
145     }