Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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 area for course sections (title and summary).
  19   *
  20   * @package core_course
  21   * @copyright 2018 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_course\search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Search area for course sections (title and summary).
  31   *
  32   * Note this does not include the activities within the section, as these have their own search
  33   * areas.
  34   *
  35   * @package core_course
  36   * @copyright 2018 The Open University
  37   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class section extends \core_search\base {
  40      /**
  41       * Sections are indexed at course context.
  42       *
  43       * @var array
  44       */
  45      protected static $levels = [CONTEXT_COURSE];
  46  
  47      /**
  48       * Returns recordset containing required data for indexing course sections.
  49       *
  50       * @param int $modifiedfrom timestamp
  51       * @param \context|null $context Restriction context
  52       * @return \moodle_recordset|null Recordset or null if no change possible
  53       */
  54      public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
  55          global $DB;
  56  
  57          list ($contextjoin, $contextparams) = $this->get_course_level_context_restriction_sql($context, 'c');
  58          if ($contextjoin === null) {
  59              return null;
  60          }
  61  
  62          $comparetext = $DB->sql_compare_text('cs.summary', 1);
  63  
  64          return $DB->get_recordset_sql("
  65                  SELECT cs.id,
  66                         cs.course,
  67                         cs.section,
  68                         cs.name,
  69                         cs.summary,
  70                         cs.summaryformat,
  71                         cs.timemodified
  72                    FROM {course_sections} cs
  73                    JOIN {course} c ON c.id = cs.course
  74            $contextjoin
  75                   WHERE cs.timemodified >= ?
  76                     AND (cs.name != ? OR $comparetext != ?)
  77                ORDER BY cs.timemodified ASC", array_merge($contextparams, [$modifiedfrom, '', '']));
  78      }
  79  
  80      /**
  81       * Returns the document associated with this section.
  82       *
  83       * @param \stdClass $record
  84       * @param array $options
  85       * @return \core_search\document
  86       */
  87      public function get_document($record, $options = array()) {
  88          global $CFG;
  89          require_once($CFG->dirroot . '/course/lib.php');
  90  
  91          // Get the context, modinfo, and section.
  92          try {
  93              $context = \context_course::instance($record->course);
  94          } catch (\moodle_exception $ex) {
  95              // Notify it as we run here as admin, we should see everything.
  96              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id .
  97                      ' document, not all required data is available: ' . $ex->getMessage(),
  98                      DEBUG_DEVELOPER);
  99              return false;
 100          }
 101  
 102          // Title - use default if none given.
 103          $title = get_section_name($record->course, $record->section);
 104  
 105          // Prepare associative array with data from DB.
 106          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
 107          $doc->set('title', content_to_text($title, false));
 108          $doc->set('content', content_to_text($record->summary, $record->summaryformat));
 109          $doc->set('contextid', $context->id);
 110          $doc->set('courseid', $record->course);
 111          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 112          $doc->set('modified', $record->timemodified);
 113  
 114          return $doc;
 115      }
 116  
 117      /**
 118       * Whether the user can access the section or not.
 119       *
 120       * @param int $id The course section id.
 121       * @return int One of the \core_search\manager:ACCESS_xx constants
 122       */
 123      public function check_access($id) {
 124          global $DB;
 125  
 126          // Check we can get the section and the course modinfo.
 127          $sectionrec = $DB->get_record('course_sections', ['id' => $id], '*', IGNORE_MISSING);
 128          if (!$sectionrec) {
 129              return \core_search\manager::ACCESS_DELETED;
 130          }
 131          try {
 132              $modinfo = get_fast_modinfo($sectionrec->course);
 133          } catch (\moodle_exception $e) {
 134              return \core_search\manager::ACCESS_DELETED;
 135          }
 136          $section = $modinfo->get_section_info($sectionrec->section, IGNORE_MISSING);
 137          if (!$section) {
 138              return \core_search\manager::ACCESS_DELETED;
 139          }
 140  
 141          // Check access to course and that the section is visible to current user.
 142          if (can_access_course($modinfo->get_course()) && $section->uservisible) {
 143              return \core_search\manager::ACCESS_GRANTED;
 144          }
 145  
 146          return \core_search\manager::ACCESS_DENIED;
 147      }
 148  
 149      /**
 150       * Gets a link to the section.
 151       *
 152       * @param \core_search\document $doc
 153       * @return \moodle_url
 154       */
 155      public function get_doc_url(\core_search\document $doc) {
 156          global $DB;
 157          $section = $DB->get_field('course_sections', 'section', ['id' => $doc->get('itemid')], MUST_EXIST);
 158          $format = course_get_format($doc->get('courseid'));
 159          return $format->get_view_url($section);
 160      }
 161  
 162      /**
 163       * Gets a link to the section.
 164       *
 165       * @param \core_search\document $doc
 166       * @return \moodle_url
 167       */
 168      public function get_context_url(\core_search\document $doc) {
 169          return $this->get_doc_url($doc);
 170      }
 171  
 172      /**
 173       * Returns true to include summary files in the index.
 174       *
 175       * @return bool True
 176       */
 177      public function uses_file_indexing() {
 178          return true;
 179      }
 180  
 181      /**
 182       * Return the file area that is used for summary files.
 183       *
 184       * @return array File area name
 185       */
 186      public function get_search_fileareas() {
 187          return ['section'];
 188      }
 189  
 190      /**
 191       * Returns the moodle component name, as used in the files table.
 192       *
 193       * @return string Component name
 194       */
 195      public function get_component_name() {
 196          return 'course';
 197      }
 198  
 199      /**
 200       * Returns an icon instance for the document.
 201       *
 202       * @param \core_search\document $doc
 203       * @return \core_search\document_icon
 204       */
 205      public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
 206          return new \core_search\document_icon('i/section');
 207      }
 208  
 209      /**
 210       * Returns a list of category names associated with the area.
 211       *
 212       * @return array
 213       */
 214      public function get_category_names() {
 215          return [\core_search\manager::SEARCH_AREA_CATEGORY_COURSE_CONTENT];
 216      }
 217  }