Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

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