Merge branch 'MDL-61102-master' of https://github.com/sammarshallou/moodle
authorDavid Monllao <davidm@moodle.com>
Thu, 19 Apr 2018 08:37:30 +0000 (10:37 +0200)
committerDavid Monllao <davidm@moodle.com>
Thu, 19 Apr 2018 08:37:30 +0000 (10:37 +0200)
1  2 
search/classes/output/form/search.php
search/index.php

@@@ -36,7 -36,7 +36,7 @@@ class search extends \moodleform 
       * @return void
       */
      function definition() {
 -        global $CFG;
 +        global $USER;
  
          $mform =& $this->_form;
          $mform->disable_form_change_checker();
@@@ -70,8 -70,6 +70,6 @@@
          $mform->addElement('text', 'title', get_string('title', 'search'));
          $mform->setType('title', PARAM_TEXT);
  
-         $search = \core_search\manager::instance();
          $searchareas = \core_search\manager::get_search_areas_list(true);
          $areanames = array();
          foreach ($searchareas as $areaid => $searcharea) {
          $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));
diff --combined search/index.php
@@@ -54,7 -54,7 +54,7 @@@ if (\core_search\manager::is_global_sea
      exit;
  }
  
- $search = \core_search\manager::instance();
+ $search = \core_search\manager::instance(true);
  
  // Set up custom data for form.
  $customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
@@@ -66,14 -66,10 +66,14 @@@ if ($contextid) 
          $searchwithin = [];
          $searchwithin[''] = get_string('everywhere', 'search');
          $searchwithin['course'] = $coursecontext->get_context_name();
 -        if ($context->contextlevel !== CONTEXT_COURSE) {
 +        if ($context->contextlevel != CONTEXT_COURSE) {
              $searchwithin['context'] = $context->get_context_name();
 +            if ($context->contextlevel == CONTEXT_MODULE) {
 +                $customdata['withincmid'] = $context->instanceid;
 +            }
          }
          $customdata['searchwithin'] = $searchwithin;
 +        $customdata['withincourseid'] = $coursecontext->instanceid;
      }
  
      // Get available ordering options from search engine.