[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/search/ -> index.php (source)

   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  $areaid = optional_param('areaid', false, PARAM_ALPHANUMEXT);
  31  // Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.
  32  
  33  $context = context_system::instance();
  34  $pagetitle = get_string('globalsearch', 'search');
  35  $PAGE->set_context($context);
  36  $PAGE->set_pagelayout('standard');
  37  $PAGE->set_title($pagetitle);
  38  $PAGE->set_heading($pagetitle);
  39  
  40  if (!empty($CFG->forcelogin)) {
  41      require_login();
  42  }
  43  
  44  require_capability('moodle/search:query', $context);
  45  
  46  $searchrenderer = $PAGE->get_renderer('core_search');
  47  
  48  if (\core_search\manager::is_global_search_enabled() === false) {
  49      $PAGE->set_url(new moodle_url('/search/index.php'));
  50      echo $OUTPUT->header();
  51      echo $OUTPUT->heading($pagetitle);
  52      echo $searchrenderer->render_search_disabled();
  53      echo $OUTPUT->footer();
  54      exit;
  55  }
  56  
  57  $search = \core_search\manager::instance();
  58  
  59  // We first get the submitted data as we want to set it all in the page URL.
  60  $mform = new \core_search\output\form\search(null, array('searchengine' => $search->get_engine()->get_plugin_name()));
  61  
  62  $data = $mform->get_data();
  63  if (!$data && $q) {
  64      // Data can also come from the URL.
  65  
  66      $data = new stdClass();
  67      $data->q = $q;
  68      $data->title = $title;
  69      $areaids = optional_param('areaids', '', PARAM_RAW);
  70      if (!empty($areaids)) {
  71          $areaids = explode(',', $areaids);
  72          $data->areaids = clean_param_array($areaids, PARAM_ALPHANUMEXT);
  73      }
  74      $courseids = optional_param('courseids', '', PARAM_RAW);
  75      if (!empty($courseids)) {
  76          $courseids = explode(',', $courseids);
  77          $data->courseids = clean_param_array($courseids, PARAM_INT);
  78      }
  79      $data->timestart = optional_param('timestart', 0, PARAM_INT);
  80      $data->timeend = optional_param('timeend', 0, PARAM_INT);
  81      $mform->set_data($data);
  82  }
  83  
  84  // Set the page URL.
  85  $urlparams = array('page' => $page);
  86  if ($data) {
  87      $urlparams['q'] = $data->q;
  88      $urlparams['title'] = $data->title;
  89      if (!empty($data->areaids)) {
  90          $urlparams['areaids'] = implode(',', $data->areaids);
  91      }
  92      if (!empty($data->courseids)) {
  93          $urlparams['courseids'] = implode(',', $data->courseids);
  94      }
  95      $urlparams['timestart'] = $data->timestart;
  96      $urlparams['timeend'] = $data->timeend;
  97  }
  98  $url = new moodle_url('/search/index.php', $urlparams);
  99  $PAGE->set_url($url);
 100  
 101  // We are ready to render.
 102  echo $OUTPUT->header();
 103  echo $OUTPUT->heading($pagetitle);
 104  
 105  // Get the results.
 106  if ($data) {
 107      $results = $search->paged_search($data, $page);
 108  }
 109  
 110  if ($errorstr = $search->get_engine()->get_query_error()) {
 111      echo $OUTPUT->notification(get_string('queryerror', 'search', $errorstr), 'notifyproblem');
 112  } else if (empty($results->totalcount) && !empty($data)) {
 113      echo $OUTPUT->notification(get_string('noresults', 'search'), 'notifymessage');
 114  }
 115  
 116  $mform->display();
 117  
 118  if (!empty($results)) {
 119      echo $searchrenderer->render_results($results->results, $results->actualpage, $results->totalcount, $url);
 120  }
 121  
 122  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1