Merge branch 'MDL-61102-master' of https://github.com/sammarshallou/moodle
[moodle.git] / search / classes / output / form / search.php
index c386790..2aba87e 100644 (file)
@@ -36,7 +36,7 @@ class search extends \moodleform {
      * @return void
      */
     function definition() {
-        global $CFG;
+        global $USER;
 
         $mform =& $this->_form;
         $mform->disable_form_change_checker();
@@ -93,9 +93,42 @@ class search extends \moodleform {
         $mform->addElement('course', 'courseids', get_string('courses', 'core'), $options);
         $mform->setType('courseids', PARAM_INT);
 
-        // Course options should be hidden if we choose to search within a specific location.
         if (!empty($this->_customdata['searchwithin'])) {
+            // Course options should be hidden if we choose to search within a specific location.
             $mform->hideIf('courseids', 'searchwithin', 'ne', '');
+
+            // Get groups on course (we don't show group selector if there aren't any).
+            $courseid = $this->_customdata['withincourseid'];
+            $allgroups = groups_get_all_groups($courseid);
+            if ($allgroups && $search->get_engine()->supports_group_filtering()) {
+                $groupnames = [];
+                foreach ($allgroups as $group) {
+                    $groupnames[$group->id] = $group->name;
+                }
+
+                // Create group autocomplete option.
+                $options = array(
+                        'multiple' => true,
+                        'noselectionstring' => get_string('allgroups'),
+                );
+                $mform->addElement('autocomplete', 'groupids', get_string('groups'), $groupnames, $options);
+
+                // Is the second 'search within' option a cm?
+                if (!empty($this->_customdata['withincmid'])) {
+                    // Find out if the cm supports groups.
+                    $modinfo = get_fast_modinfo($courseid);
+                    $cm = $modinfo->get_cm($this->_customdata['withincmid']);
+                    if ($cm->effectivegroupmode != NOGROUPS) {
+                        // If it does, group ids are available when you have course or module selected.
+                        $mform->hideIf('groupids', 'searchwithin', 'eq', '');
+                    } else {
+                        // Group ids are only available if you have course selected.
+                        $mform->hideIf('groupids', 'searchwithin', 'ne', 'course');
+                    }
+                } else {
+                    $mform->hideIf('groupids', 'searchwithin', 'eq', '');
+                }
+            }
         }
 
         $mform->addElement('date_time_selector', 'timestart', get_string('fromtime', 'search'), array('optional' => true));