Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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   * Search results viewed.
  19   *
  20   * @package    core
  21   * @copyright  2016 David Monllao {@link http://www.davidmonllao.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Search results viewed.
  31   *
  32   * @property-read array $other {
  33   *      Extra information about event.
  34   *      - string q: (required) The search query.
  35   *      - int page: (required) The page number.
  36   *      - string title: (optional) Title filter.
  37   *      - string[] areaids: (optional) Search areas filter.
  38   *      - int[] courseids: (optional) Courses filter.
  39   *      - int timestart: (optional) Results from timestamp.
  40   *      - int timeend: (optional) Results to timestamp.
  41   * }
  42   *
  43   * @package    core
  44   * @copyright  2016 David Monllao {@link http://www.davidmonllao.com}
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class search_results_viewed extends base {
  48  
  49      /**
  50       * Init method.
  51       *
  52       * @return void
  53       */
  54      protected function init() {
  55          $this->data['crud'] = 'r';
  56          $this->data['edulevel'] = self::LEVEL_OTHER;
  57      }
  58  
  59      /**
  60       * Returns localised general event name.
  61       *
  62       * @return string
  63       */
  64      public static function get_name() {
  65          return get_string('eventsearchresultsviewed');
  66      }
  67  
  68      /**
  69       * Returns description of what happened.
  70       *
  71       * @return string
  72       */
  73      public function get_description() {
  74          return "The user with id '$this->userid' viewed page '{$this->other['page']}' of " .
  75              "'{$this->other['q']}' search results";
  76      }
  77  
  78      /**
  79       * Returns relevant URL.
  80       *
  81       * @return \moodle_url
  82       */
  83      public function get_url() {
  84          $params = $this->other;
  85  
  86          // Skip search area and course filters (MDL-33188).
  87          if (isset($params['areaids'])) {
  88              unset($params['areaids']);
  89          }
  90          if (isset($params['courseids'])) {
  91              unset($params['courseids']);
  92          }
  93          return new \moodle_url('/search/index.php', $params);
  94      }
  95  
  96      /**
  97       * Custom validations.
  98       *
  99       * @throws \coding_exception
 100       * @return void
 101       */
 102      protected function validate_data() {
 103          parent::validate_data();
 104  
 105          if (!isset($this->other['q'])) {
 106              throw new \coding_exception('\'other\'[\'q\'] must be set.');
 107          }
 108  
 109          if (!isset($this->other['page'])) {
 110              throw new \coding_exception('\'other\'[\'page\'] must be set.');
 111          }
 112  
 113      }
 114  }