[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/search/classes/ -> base_activity.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   * 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      /**
  58       * Returns recordset containing required data for indexing activities.
  59       *
  60       * @param int $modifiedfrom timestamp
  61       * @return \moodle_recordset
  62       */
  63      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  64          global $DB;
  65          return $DB->get_recordset_select($this->get_module_name(), static::MODIFIED_FIELD_NAME . ' >= ?', array($modifiedfrom),
  66                  static::MODIFIED_FIELD_NAME . ' ASC');
  67      }
  68  
  69      /**
  70       * Returns the document associated with this activity.
  71       *
  72       * This default implementation for activities sets the activity name to title and the activity intro to
  73       * content. Any activity can overwrite this function if it is interested in setting other fields than the
  74       * default ones, or to fill description optional fields with extra stuff.
  75       *
  76       * @param stdClass $record
  77       * @param array    $options
  78       * @return \core_search\document
  79       */
  80      public function get_document($record, $options = array()) {
  81  
  82          try {
  83              $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course);
  84              $context = \context_module::instance($cm->id);
  85          } catch (\dml_missing_record_exception $ex) {
  86              // Notify it as we run here as admin, we should see everything.
  87              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  88                  $ex->getMessage(), DEBUG_DEVELOPER);
  89              return false;
  90          } catch (\dml_exception $ex) {
  91              // Notify it as we run here as admin, we should see everything.
  92              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
  93              return false;
  94          }
  95  
  96          // Prepare associative array with data from DB.
  97          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  98          $doc->set('title', content_to_text($record->name, false));
  99          $doc->set('content', content_to_text($record->intro, $record->introformat));
 100          $doc->set('contextid', $context->id);
 101          $doc->set('courseid', $record->course);
 102          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 103          $doc->set('modified', $record->{static::MODIFIED_FIELD_NAME});
 104  
 105          // Check if this document should be considered new.
 106          if (isset($options['lastindexedtime'])) {
 107              $createdfield = static::CREATED_FIELD_NAME;
 108              if (!empty($createdfield) && ($options['lastindexedtime'] < $record->{$createdfield})) {
 109                  // If the document was created after the last index time, it must be new.
 110                  $doc->set_is_new(true);
 111              }
 112          }
 113  
 114          return $doc;
 115      }
 116  
 117      /**
 118       * Whether the user can access the document or not.
 119       *
 120       * @throws \dml_missing_record_exception
 121       * @throws \dml_exception
 122       * @param int $id The activity instance id.
 123       * @return bool
 124       */
 125      public function check_access($id) {
 126          global $DB;
 127  
 128          try {
 129              $activity = $this->get_activity($id);
 130              $cminfo = $this->get_cm($this->get_module_name(), $activity->id, $activity->course);
 131              $cminfo->get_course_module_record();
 132          } catch (\dml_missing_record_exception $ex) {
 133              return \core_search\manager::ACCESS_DELETED;
 134          } catch (\dml_exception $ex) {
 135              return \core_search\manager::ACCESS_DENIED;
 136          }
 137  
 138          // Recheck uservisible although it should have already been checked in core_search.
 139          if ($cminfo->uservisible === false) {
 140              return \core_search\manager::ACCESS_DENIED;
 141          }
 142  
 143          return \core_search\manager::ACCESS_GRANTED;
 144      }
 145  
 146      /**
 147       * Link to the module instance.
 148       *
 149       * @param \core_search\document $doc
 150       * @return \moodle_url
 151       */
 152      public function get_doc_url(\core_search\document $doc) {;
 153          return $this->get_context_url($doc);
 154      }
 155  
 156      /**
 157       * Link to the module instance.
 158       *
 159       * @param \core_search\document $doc
 160       * @return \moodle_url
 161       */
 162      public function get_context_url(\core_search\document $doc) {
 163          $cminfo = $this->get_cm($this->get_module_name(), strval($doc->get('itemid')), $doc->get('courseid'));
 164          return new \moodle_url('/mod/' . $this->get_module_name() . '/view.php', array('id' => $cminfo->id));
 165      }
 166  
 167      /**
 168       * Returns the module name.
 169       *
 170       * @return string
 171       */
 172      protected function get_module_name() {
 173          return substr($this->componentname, 4);
 174      }
 175  
 176      /**
 177       * Returns an activity instance. Internally uses the class component to know which activity module should be retrieved.
 178       *
 179       * @param int $instanceid
 180       * @return stdClass
 181       */
 182      protected function get_activity($instanceid) {
 183          global $DB;
 184  
 185          if (empty($this->activitiesdata[$this->get_module_name()][$instanceid])) {
 186              $this->activitiesdata[$this->get_module_name()][$instanceid] = $DB->get_record($this->get_module_name(),
 187                  array('id' => $instanceid), '*', MUST_EXIST);
 188          }
 189          return $this->activitiesdata[$this->get_module_name()][$instanceid];
 190  
 191      }
 192  }


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