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.
   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  namespace mod_bigbluebuttonbn\search;
  18  
  19  use stdClass;
  20  
  21  /**
  22   * Search area for mod_bigbluebuttonbn tags.
  23   *
  24   * @package   mod_bigbluebuttonbn
  25   * @copyright 2010 onwards, Blindside Networks Inc
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class tags extends \core_search\base_activity {
  29  
  30      /**
  31       * Returns true if this area uses file indexing.
  32       *
  33       * @return bool
  34       */
  35      public function uses_file_indexing() {
  36          return false;
  37      }
  38  
  39      /**
  40       * Overwritting get_document_recordset()
  41       * In this search implementation, we need to re-index all instances (and not only the last modified) because we
  42       * are working with core tags and these can be removed from "manage tags" without change the timemodified in
  43       * BBB instances.
  44       * @param int $modifiedfrom
  45       * @param \context|null $context
  46       * @return \moodle_recordset|null
  47       */
  48      public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
  49          global $DB;
  50          [$contextjoin, $contextparams] = $this->get_context_restriction_sql($context, $this->get_module_name(), 'modtable');
  51          if ($contextjoin === null) {
  52              return null;
  53          }
  54  
  55          $result = $DB->get_recordset_sql(
  56              'SELECT modtable.* FROM {' . $this->get_module_name() .  '} modtable ' . $contextjoin,
  57              array_merge($contextparams)
  58          );
  59  
  60          return($result);
  61      }
  62  
  63      /**
  64       * Overriding method to index tags of module as string separated by comma.
  65       *
  66       * @param stdClass $record
  67       * @param array    $options
  68       * @return \core_search\document|bool
  69       */
  70      public function get_document($record, $options = []) {
  71  
  72          try {
  73              $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course);
  74              $context = \context_module::instance($cm->id);
  75  
  76              $tags = \core_tag_tag::get_tags_by_area_in_contexts("core", "course_modules", [$context]);
  77              $tagsstring = "";
  78              if (!empty($tags)) {
  79                  $res = [];
  80                  foreach ($tags as $t) {
  81                      $res[] = $t->name;
  82                  }
  83                  $tagsstring = implode(", ", $res);
  84              }
  85  
  86          } catch (\dml_missing_record_exception $ex) {
  87              // Notify it as we run here as admin, we should see everything.
  88              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  89                  $ex->getMessage(), DEBUG_DEVELOPER);
  90              return false;
  91          } catch (\dml_exception $ex) {
  92              // Notify it as we run here as admin, we should see everything.
  93              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
  94              return false;
  95          }
  96  
  97          // Prepare array with data from DB.
  98          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  99          $doc->set('title', content_to_text($record->name, false));
 100          $doc->set('content', $tagsstring);
 101          $doc->set('contextid', $context->id);
 102          $doc->set('courseid', $record->course);
 103          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 104          $doc->set('modified', $record->{static::MODIFIED_FIELD_NAME});
 105  
 106          // Check if this document should be considered new.
 107          if (isset($options['lastindexedtime'])) {
 108              $createdfield = static::CREATED_FIELD_NAME;
 109              if (!empty($createdfield) && ($options['lastindexedtime'] < $record->{$createdfield})) {
 110                  // If the document was created after the last index time, it must be new.
 111                  $doc->set_is_new(true);
 112              }
 113          }
 114  
 115          return $doc;
 116      }
 117  }