[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/glossary/classes/search/ -> entry.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   * Glossary entries search.
  19   *
  20   * @package    mod_glossary
  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_glossary\search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/mod/glossary/lib.php');
  30  
  31  /**
  32   * Glossary entries search.
  33   *
  34   * @package    mod_glossary
  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 entry extends \core_search\base_mod {
  39  
  40      /**
  41       * @var array Internal quick static cache.
  42       */
  43      protected $entriesdata = array();
  44  
  45      /**
  46       * Returns recordset containing required data for indexing glossary entries.
  47       *
  48       * @param int $modifiedfrom timestamp
  49       * @return moodle_recordset
  50       */
  51      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  52          global $DB;
  53  
  54          $sql = "SELECT ge.*, g.course FROM {glossary_entries} ge
  55                    JOIN {glossary} g ON g.id = ge.glossaryid
  56                  WHERE ge.timemodified >= ? ORDER BY ge.timemodified ASC";
  57          return $DB->get_recordset_sql($sql, array($modifiedfrom));
  58      }
  59  
  60      /**
  61       * Returns the documents associated with this glossary entry id.
  62       *
  63       * @param stdClass $entry glossary entry.
  64       * @param array    $options
  65       * @return \core_search\document
  66       */
  67      public function get_document($entry, $options = array()) {
  68          global $DB;
  69  
  70          $keywords = array();
  71          if ($aliases = $DB->get_records('glossary_alias', array('entryid' => $entry->id))) {
  72              foreach ($aliases as $alias) {
  73                  $keywords[] = $alias->alias;
  74              }
  75          }
  76  
  77          try {
  78              $cm = $this->get_cm('glossary', $entry->glossaryid, $entry->course);
  79              $context = \context_module::instance($cm->id);
  80          } catch (\dml_missing_record_exception $ex) {
  81              // Notify it as we run here as admin, we should see everything.
  82              debugging('Error retrieving mod_glossary ' . $entry->id . ' document, not all required data is available: ' .
  83                  $ex->getMessage(), DEBUG_DEVELOPER);
  84              return false;
  85          } catch (\dml_exception $ex) {
  86              // Notify it as we run here as admin, we should see everything.
  87              debugging('Error retrieving mod_glossary' . $entry->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
  88              return false;
  89          }
  90  
  91          // Prepare associative array with data from DB.
  92          $doc = \core_search\document_factory::instance($entry->id, $this->componentname, $this->areaname);
  93          $doc->set('title', content_to_text($entry->concept, false));
  94          $doc->set('content', content_to_text($entry->definition, $entry->definitionformat));
  95          $doc->set('contextid', $context->id);
  96          $doc->set('courseid', $entry->course);
  97          $doc->set('userid', $entry->userid);
  98          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
  99          $doc->set('modified', $entry->timemodified);
 100  
 101          // Check if this document should be considered new.
 102          if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $entry->timecreated)) {
 103              // If the document was created after the last index time, it must be new.
 104              $doc->set_is_new(true);
 105          }
 106  
 107          // Adding keywords as extra info.
 108          if ($keywords) {
 109              // No need to pass through content_to_text here as this is just a list of keywords.
 110              $doc->set('description1', implode(' ' , $keywords));
 111          }
 112  
 113          return $doc;
 114      }
 115  
 116      /**
 117       * Whether the user can access the document or not.
 118       *
 119       * @throws \dml_missing_record_exception
 120       * @throws \dml_exception
 121       * @param int $id Glossary entry id
 122       * @return bool
 123       */
 124      public function check_access($id) {
 125          global $USER;
 126  
 127          try {
 128              $entry = $this->get_entry($id);
 129              $cminfo = $this->get_cm('glossary', $entry->glossaryid, $entry->course);
 130          } catch (\dml_missing_record_exception $ex) {
 131              return \core_search\manager::ACCESS_DELETED;
 132          } catch (\dml_exception $ex) {
 133              return \core_search\manager::ACCESS_DENIED;
 134          }
 135  
 136          if (!glossary_can_view_entry($entry, $cminfo)) {
 137              return \core_search\manager::ACCESS_DENIED;
 138          }
 139  
 140          return \core_search\manager::ACCESS_GRANTED;
 141      }
 142  
 143      /**
 144       * Link to glossary entry.
 145       *
 146       * @param \core_search\document $doc
 147       * @return \moodle_url
 148       */
 149      public function get_doc_url(\core_search\document $doc) {
 150          global $USER;
 151  
 152          // The post is already in static cache, we fetch it in self::search_access.
 153          $entry = $this->get_entry($doc->get('itemid'));
 154          $contextmodule = \context::instance_by_id($doc->get('contextid'));
 155  
 156          if ($entry->approved == false && $entry->userid != $USER->id) {
 157              // The URL should change when the entry is not approved and it was not created by the user.
 158              $docparams = array('id' => $contextmodule->instanceid, 'mode' => 'approval');
 159          } else {
 160              $docparams = array('id' => $contextmodule->instanceid, 'mode' => 'entry', 'hook' => $doc->get('itemid'));
 161  
 162          }
 163          return new \moodle_url('/mod/glossary/view.php', $docparams);
 164      }
 165  
 166      /**
 167       * Link to the glossary.
 168       *
 169       * @param \core_search\document $doc
 170       * @return \moodle_url
 171       */
 172      public function get_context_url(\core_search\document $doc) {
 173          $contextmodule = \context::instance_by_id($doc->get('contextid'));
 174          return new \moodle_url('/mod/glossary/view.php', array('id' => $contextmodule->instanceid));
 175      }
 176  
 177      /**
 178       * Returns the specified glossary entry checking the internal cache.
 179       *
 180       * Store minimal information as this might grow.
 181       *
 182       * @throws \dml_exception
 183       * @param int $entryid
 184       * @return stdClass
 185       */
 186      protected function get_entry($entryid) {
 187          global $DB;
 188  
 189          if (empty($this->entriesdata[$entryid])) {
 190              $this->entriesdata[$entryid] = $DB->get_record_sql("SELECT ge.*, g.course, g.defaultapproval FROM {glossary_entries} ge
 191                                                                    JOIN {glossary} g ON g.id = ge.glossaryid
 192                                                                  WHERE ge.id = ?", array('id' => $entryid), MUST_EXIST);
 193          }
 194          return $this->entriesdata[$entryid];
 195      }
 196  }


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