[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/wiki/classes/search/ -> collaborative_page.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 for mod_wiki collaborative pages.
  19   *
  20   * @package    mod_wiki
  21   * @copyright  2016 Eric Merrill {@link http://www.merrilldigital.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_wiki\search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/mod/wiki/locallib.php');
  30  
  31  /**
  32   * Search area for mod_wiki collaborative pages.
  33   *
  34   * @package    mod_wiki
  35   * @copyright  2016 Eric Merrill {@link http://www.merrilldigital.com}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class collaborative_page extends \core_search\base_mod {
  39      /**
  40       * @var array Cache of wiki records.
  41       */
  42      protected $wikiscache = array();
  43  
  44      /**
  45       * Returns a recordset with all required page information.
  46       *
  47       * @param int $modifiedfrom
  48       * @return moodle_recordset
  49       */
  50      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  51          global $DB;
  52  
  53          $sql = 'SELECT p.*, w.id AS wikiid, w.course AS courseid
  54                    FROM {wiki_pages} p
  55                    JOIN {wiki_subwikis} s ON s.id = p.subwikiid
  56                    JOIN {wiki} w ON w.id = s.wikiid
  57                   WHERE p.timemodified >= ?
  58                     AND w.wikimode = ?
  59                ORDER BY p.timemodified ASC';
  60          return $DB->get_recordset_sql($sql, array($modifiedfrom, 'collaborative'));
  61      }
  62  
  63      /**
  64       * Returns the document for a particular page.
  65       *
  66       * @param \stdClass $record A record containing, at least, the indexed document id and a modified timestamp
  67       * @param array     $options Options for document creation
  68       * @return \core_search\document
  69       */
  70      public function get_document($record, $options = array()) {
  71          try {
  72              $cm = $this->get_cm('wiki', $record->wikiid, $record->courseid);
  73              $context = \context_module::instance($cm->id);
  74          } catch (\dml_missing_record_exception $ex) {
  75              // Notify it as we run here as admin, we should see everything.
  76              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  77                  $ex->getMessage(), DEBUG_DEVELOPER);
  78              return false;
  79          } catch (\dml_exception $ex) {
  80              // Notify it as we run here as admin, we should see everything.
  81              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
  82              return false;
  83          }
  84  
  85          // Make a page object without extra fields.
  86          $page = clone $record;
  87          unset($page->courseid);
  88          unset($page->wikiid);
  89  
  90          // Conversion based wiki_print_page_content().
  91          // Check if we have passed the cache time.
  92          if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
  93              $content = wiki_refresh_cachedcontent($page);
  94              $page = $content['page'];
  95          }
  96          // Convert to text.
  97          $content = content_to_text($page->cachedcontent, FORMAT_MOODLE);
  98  
  99          // Prepare associative array with data from DB.
 100          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
 101          $doc->set('title', content_to_text($record->title, false));
 102          $doc->set('content', $content);
 103          $doc->set('contextid', $context->id);
 104          $doc->set('courseid', $record->courseid);
 105          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
 106          $doc->set('modified', $record->timemodified);
 107  
 108          // Check if this document should be considered new.
 109          if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->timecreated)) {
 110              // If the document was created after the last index time, it must be new.
 111              $doc->set_is_new(true);
 112          }
 113  
 114          return $doc;
 115      }
 116  
 117      /**
 118       * Can the current user see the document.
 119       *
 120       * @param int $id The internal search area entity id.
 121       * @return bool True if the user can see it, false otherwise
 122       */
 123      public function check_access($id) {
 124          global $DB;
 125  
 126          try {
 127              $page = $DB->get_record('wiki_pages', array('id' => $id), '*', MUST_EXIST);
 128              if (!isset($this->wikiscache[$page->subwikiid])) {
 129                  $sql = 'SELECT w.*
 130                            FROM {wiki_subwikis} s
 131                            JOIN {wiki} w ON w.id = s.wikiid
 132                           WHERE s.id = ?';
 133                  $this->wikiscache[$page->subwikiid] = $DB->get_record_sql($sql, array('id' => $page->subwikiid), MUST_EXIST);
 134              }
 135              $wiki = $this->wikiscache[$page->subwikiid];
 136              $cminfo = $this->get_cm('wiki', $wiki->id, $wiki->course);
 137          } catch (\dml_missing_record_exception $ex) {
 138              return \core_search\manager::ACCESS_DELETED;
 139          } catch (\dml_exception $ex) {
 140              return \core_search\manager::ACCESS_DENIED;
 141          }
 142  
 143          // Recheck uservisible although it should have already been checked in core_search.
 144          if ($cminfo->uservisible === false) {
 145              return \core_search\manager::ACCESS_DENIED;
 146          }
 147  
 148          $context = \context_module::instance($cminfo->id);
 149  
 150          if (!has_capability('mod/wiki:viewpage', $context)) {
 151              return \core_search\manager::ACCESS_DENIED;
 152          }
 153  
 154          return \core_search\manager::ACCESS_GRANTED;
 155      }
 156  
 157      /**
 158       * Returns a url to the page.
 159       *
 160       * @param \core_search\document $doc
 161       * @return \moodle_url
 162       */
 163      public function get_doc_url(\core_search\document $doc) {
 164          $params = array('pageid' => $doc->get('itemid'));
 165          return new \moodle_url('/mod/wiki/view.php', $params);
 166      }
 167  
 168      /**
 169       * Returns a url to the wiki.
 170       *
 171       * @param \core_search\document $doc
 172       * @return \moodle_url
 173       */
 174      public function get_context_url(\core_search\document $doc) {
 175          $contextmodule = \context::instance_by_id($doc->get('contextid'));
 176          return new \moodle_url('/mod/wiki/view.php', array('id' => $contextmodule->instanceid));
 177      }
 178  }


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