Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/search/ -> index.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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 index page for entering queries and display of results
  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  require_once(__DIR__ . '/../config.php');
  26  
  27  $page = optional_param('page', 0, PARAM_INT);
  28  $q = optional_param('q', '', PARAM_NOTAGS);
  29  $title = optional_param('title', '', PARAM_NOTAGS);
  30  $contextid = optional_param('context', 0, PARAM_INT);
  31  $cat = optional_param('cat', '', PARAM_NOTAGS);
  32  $mycoursesonly = optional_param('mycoursesonly', 0, PARAM_INT);
  33  
  34  if (\core_search\manager::is_search_area_categories_enabled()) {
  35      $cat = \core_search\manager::get_search_area_category_by_name($cat);
  36  }
  37  
  38  // Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
  39  
  40  $context = context_system::instance();
  41  $pagetitle = get_string('globalsearch', 'search');
  42  $PAGE->set_context($context);
  43  $PAGE->set_pagelayout('standard');
  44  $PAGE->set_title($pagetitle);
  45  $PAGE->set_heading($pagetitle);
  46  
  47  if (!empty($CFG->forcelogin)) {
  48      require_login();
  49  }
  50  
  51  // Unlock the session during a search.
  52  \core\session\manager::write_close();
  53  
  54  require_capability('moodle/search:query', $context);
  55  
  56  $searchrenderer = $PAGE->get_renderer('core_search');
  57  
  58  if (\core_search\manager::is_global_search_enabled() === false) {
  59      $PAGE->set_url(new moodle_url('/search/index.php'));
  60      echo $OUTPUT->header();
  61      echo $searchrenderer->render_search_disabled();
  62      echo $OUTPUT->footer();
  63      exit;
  64  }
  65  
  66  $search = \core_search\manager::instance(true, true);
  67  
  68  // Set up custom data for form.
  69  $customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
  70  if ($contextid) {
  71      // When a context is supplied, check if it's within course level. If so, show dropdown.
  72      $context = context::instance_by_id($contextid);
  73      $coursecontext = $context->get_course_context(false);
  74      if ($coursecontext) {
  75          $searchwithin = [];
  76          $searchwithin[''] = get_string('everywhere', 'search');
  77          $searchwithin['course'] = $coursecontext->get_context_name();
  78          if ($context->contextlevel != CONTEXT_COURSE) {
  79              $searchwithin['context'] = $context->get_context_name();
  80              if ($context->contextlevel == CONTEXT_MODULE) {
  81                  $customdata['withincmid'] = $context->instanceid;
  82              }
  83          }
  84          $customdata['searchwithin'] = $searchwithin;
  85          $customdata['withincourseid'] = $coursecontext->instanceid;
  86      }
  87  
  88  }
  89  // Get available ordering options from search engine.
  90  $customdata['orderoptions'] = $search->get_engine()->get_supported_orders($context);
  91  
  92  if ($cat instanceof \core_search\area_category) {
  93      $customdata['cat'] = $cat->get_name();
  94  }
  95  
  96  $mform = new \core_search\output\form\search(null, $customdata);
  97  
  98  $data = $mform->get_data();
  99  if (!$data && $q) {
 100      // Data can also come from the URL.
 101  
 102      $data = new stdClass();
 103      $data->q = $q;
 104      $data->title = $title;
 105      $areaids = optional_param('areaids', '', PARAM_RAW);
 106      if (!empty($areaids)) {
 107          $areaids = explode(',', $areaids);
 108          $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
 109      }
 110      $courseids = optional_param('courseids', '', PARAM_RAW);
 111      if (!empty($courseids)) {
 112          $courseids = explode(',', $courseids);
 113          $data->courseids = clean_param_array($courseids, PARAM_INT);
 114      }
 115      $data->timestart = optional_param('timestart', 0, PARAM_INT);
 116      $data->timeend = optional_param('timeend', 0, PARAM_INT);
 117  
 118      $data->context = $contextid;
 119      $data->mycoursesonly = $mycoursesonly;
 120  
 121      $mform->set_data($data);
 122  }
 123  
 124  // Convert the 'search within' option, if used, to course or context restrictions.
 125  if ($data && !empty($data->searchwithin)) {
 126      switch ($data->searchwithin) {
 127          case 'course':
 128              $data->courseids = [$coursecontext->instanceid];
 129              break;
 130          case 'context':
 131              $data->courseids = [$coursecontext->instanceid];
 132              $data->contextids = [$context->id];
 133              break;
 134      }
 135  }
 136  
 137  // Inform search engine about source context.
 138  if (!empty($context) && $data) {
 139      $data->context = $context;
 140  }
 141  
 142  if ($data && $cat instanceof \core_search\area_category) {
 143      $data->cat = $cat->get_name();
 144  }
 145  
 146  // Set the page URL.
 147  $urlparams = array('page' => $page);
 148  if ($data) {
 149      $urlparams['q'] = $data->q;
 150      $urlparams['title'] = $data->title;
 151      if (!empty($data->areaids)) {
 152          $urlparams['areaids'] = implode(',', $data->areaids);
 153      }
 154      if (!empty($data->courseids)) {
 155          $urlparams['courseids'] = implode(',', $data->courseids);
 156      }
 157      $urlparams['timestart'] = $data->timestart;
 158      $urlparams['timeend'] = $data->timeend;
 159      $urlparams['mycoursesonly'] = isset($data->mycoursesonly) ? $data->mycoursesonly : 0;
 160  }
 161  
 162  if ($cat instanceof \core_search\area_category) {
 163      $urlparams['cat'] = $cat->get_name();
 164  }
 165  
 166  $url = new moodle_url('/search/index.php', $urlparams);
 167  $PAGE->set_url($url);
 168  
 169  // We are ready to render.
 170  echo $OUTPUT->header();
 171  
 172  // Get the results.
 173  if ($data) {
 174      $results = $search->paged_search($data, $page);
 175  }
 176  
 177  // Show search information if configured by system administrator.
 178  if ($CFG->searchbannerenable && $CFG->searchbanner) {
 179      echo $OUTPUT->notification(format_text($CFG->searchbanner, FORMAT_HTML), 'notifywarning');
 180  }
 181  
 182  if ($errorstr = $search->get_engine()->get_query_error()) {
 183      echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
 184  } else if (empty($results->totalcount) && !empty($data)) {
 185      echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
 186  }
 187  
 188  $mform->display();
 189  
 190  if (!empty($results)) {
 191      $topresults = $search->search_top($data);
 192      if (!empty($topresults)) {
 193          echo $searchrenderer->render_top_results($topresults);
 194      }
 195      echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url, $cat);
 196  
 197      \core_search\manager::trigger_search_results_viewed([
 198          'q' => $data->q,
 199          'page' => $page,
 200          'title' => $data->title,
 201          'areaids' => !empty($data->areaids) ? $data->areaids : array(),
 202          'courseids' => !empty($data->courseids) ? $data->courseids : array(),
 203          'timestart' => isset($data->timestart) ? $data->timestart : 0,
 204          'timeend' => isset($data->timeend) ? $data->timeend : 0
 205      ]);
 206  
 207  }
 208  
 209  echo $OUTPUT->footer();