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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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 area base class for activities.
  19   *
  20   * @package    core_search
  21   * @copyright  2016 Dan Poltawski
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Base implementation for activity modules.
  31   *
  32   * @package    core_search
  33   * @copyright  2016 Dan Poltawski
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  abstract class base_activity extends base_mod {
  37  
  38      /**
  39       * @var string The time modified field name.
  40       *
  41       * Activities not using timemodified as field name
  42       * can overwrite this constant.
  43       */
  44      const MODIFIED_FIELD_NAME = 'timemodified';
  45  
  46      /**
  47       * Activities with a time created field can overwrite this constant.
  48       */
  49      const CREATED_FIELD_NAME = '';
  50  
  51      /**
  52       * The context levels the search area is working on.
  53       * @var array
  54       */
  55      protected static $levels = [CONTEXT_MODULE];
  56  
  57      /** @var array activity data instance. */
  58      public $activitiesdata = [];
  59  
  60      /**
  61       * Returns recordset containing all activities within the given context.
  62       *
  63       * @param \context|null $context Context
  64       * @param int $modifiedfrom Return only records modified after this date
  65       * @return \moodle_recordset|null Recordset, or null if no possible activities in given context
  66       */
  67      public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
  68          global $DB;
  69          list ($contextjoin, $contextparams) = $this->get_context_restriction_sql(
  70                  $context, $this->get_module_name(), 'modtable');
  71          if ($contextjoin === null) {
  72              return null;
  73          }
  74          return $DB->get_recordset_sql('SELECT modtable.* FROM {' . $this->get_module_name() .
  75                  '} modtable ' . $contextjoin . ' WHERE modtable.' . static::MODIFIED_FIELD_NAME .
  76                  ' >= ? ORDER BY modtable.' . static::MODIFIED_FIELD_NAME . ' ASC',
  77                  array_merge($contextparams, [$modifiedfrom]));
  78      }
  79  
  80      /**
  81       * Returns the document associated with this activity.
  82       *
  83       * This default implementation for activities sets the activity name to title and the activity intro to
  84       * content. Any activity can overwrite this function if it is interested in setting other fields than the
  85       * default ones, or to fill description optional fields with extra stuff.
  86       *
  87       * @param \stdClass $record
  88       * @param array    $options
  89       * @return \core_search\document
  90       */
  91      public function get_document($record, $options = array()) {
  92  
  93          try {
  94              $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course);
  95              $context = \context_module::instance($cm->id);
  96          } catch (\dml_missing_record_exception $ex) {
  97              // Notify it as we run here as admin, we should see everything.
  98              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  99                  $ex->getMessage(), DEBUG_DEVELOPER);
 100              return false;
 101          } catch (\dml_exception $ex) {
 102              // Notify it as we run here as admin, we should see everything.
 103              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
 104              return false;
 105          }
 106  
 107          // Prepare associative array with data from DB.
 108          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
 109          $doc->set('title', content_to_text($record->name, false));
 110          $doc->set('content', content_to_text($record->intro, $record->introformat));
 111          $doc->set('contextid', $context->id);
 112          $doc->set('courseid', $record->course);
 113          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 114          $doc->set('modified', $record->{static::MODIFIED_FIELD_NAME});
 115  
 116          // Check if this document should be considered new.
 117          if (isset($options['lastindexedtime'])) {
 118              $createdfield = static::CREATED_FIELD_NAME;
 119              if (!empty($createdfield) && ($options['lastindexedtime'] < $record->{$createdfield})) {
 120                  // If the document was created after the last index time, it must be new.
 121                  $doc->set_is_new(true);
 122              }
 123          }
 124  
 125          return $doc;
 126      }
 127  
 128      /**
 129       * Whether the user can access the document or not.
 130       *
 131       * @throws \dml_missing_record_exception
 132       * @throws \dml_exception
 133       * @param int $id The activity instance id.
 134       * @return bool
 135       */
 136      public function check_access($id) {
 137          global $DB;
 138  
 139          try {
 140              $activity = $this->get_activity($id);
 141              $cminfo = $this->get_cm($this->get_module_name(), $activity->id, $activity->course);
 142              $cminfo->get_course_module_record();
 143          } catch (\dml_missing_record_exception $ex) {
 144              return \core_search\manager::ACCESS_DELETED;
 145          } catch (\dml_exception $ex) {
 146              return \core_search\manager::ACCESS_DENIED;
 147          }
 148  
 149          // Recheck uservisible although it should have already been checked in core_search.
 150          if ($cminfo->uservisible === false) {
 151              return \core_search\manager::ACCESS_DENIED;
 152          }
 153  
 154          return \core_search\manager::ACCESS_GRANTED;
 155      }
 156  
 157      /**
 158       * Link to the module instance.
 159       *
 160       * @param \core_search\document $doc
 161       * @return \moodle_url
 162       */
 163      public function get_doc_url(\core_search\document $doc) {
 164          return $this->get_context_url($doc);
 165      }
 166  
 167      /**
 168       * Link to the module instance.
 169       *
 170       * @param \core_search\document $doc
 171       * @return \moodle_url
 172       */
 173      public function get_context_url(\core_search\document $doc) {
 174          $cminfo = $this->get_cm($this->get_module_name(), strval($doc->get('itemid')), $doc->get('courseid'));
 175          return new \moodle_url('/mod/' . $this->get_module_name() . '/view.php', array('id' => $cminfo->id));
 176      }
 177  
 178      /**
 179       * Returns an activity instance. Internally uses the class component to know which activity module should be retrieved.
 180       *
 181       * @param int $instanceid
 182       * @return stdClass
 183       */
 184      protected function get_activity($instanceid) {
 185          global $DB;
 186  
 187          if (empty($this->activitiesdata[$this->get_module_name()][$instanceid])) {
 188              $this->activitiesdata[$this->get_module_name()][$instanceid] = $DB->get_record($this->get_module_name(),
 189                  array('id' => $instanceid), '*', MUST_EXIST);
 190          }
 191          return $this->activitiesdata[$this->get_module_name()][$instanceid];
 192  
 193      }
 194  
 195      /**
 196       * Return the context info required to index files for
 197       * this search area.
 198       *
 199       * Should be onerridden by each search area.
 200       *
 201       * @return array
 202       */
 203      public function get_search_fileareas() {
 204          $fileareas = array(
 205                  'intro' // Fileareas.
 206          );
 207  
 208          return $fileareas;
 209      }
 210  
 211      /**
 212       * Files related to the current document are attached,
 213       * to the document object ready for indexing by
 214       * Global Search.
 215       *
 216       * The default implementation retrieves all files for
 217       * the file areas returned by get_search_fileareas().
 218       * If you need to filter files to specific items per
 219       * file area, you will need to override this method
 220       * and explicitly provide the items.
 221       *
 222       * @param document $document The current document
 223       * @return void
 224       */
 225      public function attach_files($document) {
 226          $fileareas = $this->get_search_fileareas();
 227  
 228          if (!empty($fileareas)) {
 229              $cm = $this->get_cm($this->get_module_name(), $document->get('itemid'), $document->get('courseid'));
 230  
 231              $context = \context_module::instance($cm->id);
 232              $contextid = $context->id;
 233  
 234              $fs = get_file_storage();
 235              $files = $fs->get_area_files($contextid, $this->get_component_name(), $fileareas, false, '', false);
 236  
 237              foreach ($files as $file) {
 238                  $document->add_stored_file($file);
 239              }
 240          }
 241  
 242          return;
 243      }
 244  }