[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/forum/classes/search/ -> post.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   * Forum posts search area
  19   *
  20   * @package    mod_forum
  21   * @copyright  2015 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 mod_forum\search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/mod/forum/lib.php');
  30  
  31  /**
  32   * Forum posts search area.
  33   *
  34   * @package    mod_forum
  35   * @copyright  2015 David Monllao {@link http://www.davidmonllao.com}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class post extends \core_search\base_mod {
  39  
  40      /**
  41       * @var array Internal quick static cache.
  42       */
  43      protected $forumsdata = array();
  44  
  45      /**
  46       * @var array Internal quick static cache.
  47       */
  48      protected $discussionsdata = array();
  49  
  50      /**
  51       * @var array Internal quick static cache.
  52       */
  53      protected $postsdata = array();
  54  
  55      /**
  56       * Returns recordset containing required data for indexing forum posts.
  57       *
  58       * @param int $modifiedfrom timestamp
  59       * @return moodle_recordset
  60       */
  61      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  62          global $DB;
  63  
  64          $sql = 'SELECT fp.*, f.id AS forumid, f.course AS courseid
  65                    FROM {forum_posts} fp
  66                    JOIN {forum_discussions} fd ON fd.id = fp.discussion
  67                    JOIN {forum} f ON f.id = fd.forum
  68                   WHERE fp.modified >= ? ORDER BY fp.modified ASC';
  69          return $DB->get_recordset_sql($sql, array($modifiedfrom));
  70      }
  71  
  72      /**
  73       * Returns the document associated with this post id.
  74       *
  75       * @param stdClass $record Post info.
  76       * @param array    $options
  77       * @return \core_search\document
  78       */
  79      public function get_document($record, $options = array()) {
  80  
  81          try {
  82              $cm = $this->get_cm('forum', $record->forumid, $record->courseid);
  83              $context = \context_module::instance($cm->id);
  84          } catch (\dml_missing_record_exception $ex) {
  85              // Notify it as we run here as admin, we should see everything.
  86              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  87                  $ex->getMessage(), DEBUG_DEVELOPER);
  88              return false;
  89          } catch (\dml_exception $ex) {
  90              // Notify it as we run here as admin, we should see everything.
  91              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
  92              return false;
  93          }
  94  
  95          // Prepare associative array with data from DB.
  96          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  97          $doc->set('title', content_to_text($record->subject, false));
  98          $doc->set('content', content_to_text($record->message, $record->messageformat));
  99          $doc->set('contextid', $context->id);
 100          $doc->set('courseid', $record->courseid);
 101          $doc->set('userid', $record->userid);
 102          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 103          $doc->set('modified', $record->modified);
 104  
 105          // Check if this document should be considered new.
 106          if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->created)) {
 107              // If the document was created after the last index time, it must be new.
 108              $doc->set_is_new(true);
 109          }
 110  
 111          return $doc;
 112      }
 113  
 114      /**
 115       * Returns true if this area uses file indexing.
 116       *
 117       * @return bool
 118       */
 119      public function uses_file_indexing() {
 120          return true;
 121      }
 122  
 123      /**
 124       * Add the forum post attachments.
 125       *
 126       * @param document $document The current document
 127       * @return null
 128       */
 129      public function attach_files($document) {
 130          global $DB;
 131  
 132          $postid = $document->get('itemid');
 133  
 134          try {
 135              $post = $this->get_post($postid);
 136          } catch (\dml_missing_record_exception $e) {
 137              unset($this->postsdata[$postid]);
 138              debugging('Could not get record to attach files to '.$document->get('id'), DEBUG_DEVELOPER);
 139              return;
 140          }
 141  
 142          // Because this is used during indexing, we don't want to cache posts. Would result in memory leak.
 143          unset($this->postsdata[$postid]);
 144  
 145          $cm = $this->get_cm('forum', $post->forum, $document->get('courseid'));
 146          $context = \context_module::instance($cm->id);
 147  
 148          // Get the files and attach them.
 149          $fs = get_file_storage();
 150          $files = $fs->get_area_files($context->id, 'mod_forum', 'attachment', $postid, "filename", false);
 151          foreach ($files as $file) {
 152              $document->add_stored_file($file);
 153          }
 154      }
 155  
 156      /**
 157       * Whether the user can access the document or not.
 158       *
 159       * @throws \dml_missing_record_exception
 160       * @throws \dml_exception
 161       * @param int $id Forum post id
 162       * @return bool
 163       */
 164      public function check_access($id) {
 165          global $USER;
 166  
 167          try {
 168              $post = $this->get_post($id);
 169              $forum = $this->get_forum($post->forum);
 170              $discussion = $this->get_discussion($post->discussion);
 171              $cminfo = $this->get_cm('forum', $forum->id, $forum->course);
 172              $cm = $cminfo->get_course_module_record();
 173          } catch (\dml_missing_record_exception $ex) {
 174              return \core_search\manager::ACCESS_DELETED;
 175          } catch (\dml_exception $ex) {
 176              return \core_search\manager::ACCESS_DENIED;
 177          }
 178  
 179          // Recheck uservisible although it should have already been checked in core_search.
 180          if ($cminfo->uservisible === false) {
 181              return \core_search\manager::ACCESS_DENIED;
 182          }
 183  
 184          if (!forum_user_can_see_post($forum, $discussion, $post, $USER, $cm)) {
 185              return \core_search\manager::ACCESS_DENIED;
 186          }
 187  
 188          return \core_search\manager::ACCESS_GRANTED;
 189      }
 190  
 191      /**
 192       * Link to the forum post discussion
 193       *
 194       * @param \core_search\document $doc
 195       * @return \moodle_url
 196       */
 197      public function get_doc_url(\core_search\document $doc) {
 198          // The post is already in static cache, we fetch it in self::search_access.
 199          $post = $this->get_post($doc->get('itemid'));
 200          return new \moodle_url('/mod/forum/discuss.php', array('d' => $post->discussion));
 201      }
 202  
 203      /**
 204       * Link to the forum.
 205       *
 206       * @param \core_search\document $doc
 207       * @return \moodle_url
 208       */
 209      public function get_context_url(\core_search\document $doc) {
 210          $contextmodule = \context::instance_by_id($doc->get('contextid'));
 211          return new \moodle_url('/mod/forum/view.php', array('id' => $contextmodule->instanceid));
 212      }
 213  
 214      /**
 215       * Returns the specified forum post from its internal cache.
 216       *
 217       * @throws \dml_missing_record_exception
 218       * @param int $postid
 219       * @return stdClass
 220       */
 221      protected function get_post($postid) {
 222          if (empty($this->postsdata[$postid])) {
 223              $this->postsdata[$postid] = forum_get_post_full($postid);
 224              if (!$this->postsdata[$postid]) {
 225                  throw new \dml_missing_record_exception('forum_posts');
 226              }
 227          }
 228          return $this->postsdata[$postid];
 229      }
 230  
 231      /**
 232       * Returns the specified forum checking the internal cache.
 233       *
 234       * Store minimal information as this might grow.
 235       *
 236       * @throws \dml_exception
 237       * @param int $forumid
 238       * @return stdClass
 239       */
 240      protected function get_forum($forumid) {
 241          global $DB;
 242  
 243          if (empty($this->forumsdata[$forumid])) {
 244              $this->forumsdata[$forumid] = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
 245          }
 246          return $this->forumsdata[$forumid];
 247      }
 248  
 249      /**
 250       * Returns the discussion checking the internal cache.
 251       *
 252       * @throws \dml_missing_record_exception
 253       * @param int $discussionid
 254       * @return stdClass
 255       */
 256      protected function get_discussion($discussionid) {
 257          global $DB;
 258  
 259          if (empty($this->discussionsdata[$discussionid])) {
 260              $this->discussionsdata[$discussionid] = $DB->get_record('forum_discussions',
 261                  array('id' => $discussionid), '*', MUST_EXIST);
 262          }
 263          return $this->discussionsdata[$discussionid];
 264      }
 265  }


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