[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/search/tests/fixtures/ -> mock_search_area.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  namespace core_mocksearch\search;
  18  
  19  /**
  20   * Component implementing search for testing purposes.
  21   *
  22   * @package   core_search
  23   * @category  phpunit
  24   * @copyright David Monllao {@link http://www.davidmonllao.com}
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  class mock_search_area extends \core_search\base {
  31  
  32      /**
  33       * Multiple context level so we can test get_areas_user_accesses.
  34       * @var int[]
  35       */
  36      protected static $levels = [CONTEXT_SYSTEM, CONTEXT_USER];
  37  
  38      /**
  39       * To make things easier, base class required config stuff.
  40       *
  41       * @return bool
  42       */
  43      public function is_enabled() {
  44          return true;
  45      }
  46  
  47      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  48          global $DB;
  49  
  50          $sql = "SELECT * FROM {temp_mock_search_area} WHERE timemodified >= ? ORDER BY timemodified ASC";
  51          return $DB->get_recordset_sql($sql, array($modifiedfrom));
  52      }
  53  
  54  
  55      /**
  56       * A helper function that will turn a record into 'data array', for use with document building.
  57       */
  58      public function convert_record_to_doc_array($record) {
  59          $docdata = (array)unserialize($record->info);
  60          $docdata['areaid'] = $this->get_area_id();
  61          $docdata['itemid'] = $record->id;
  62          $docdata['modified'] = $record->timemodified;
  63  
  64          return $docdata;
  65      }
  66  
  67      public function get_document($record, $options = array()) {
  68          global $USER;
  69  
  70          $info = unserialize($record->info);
  71  
  72          // Prepare associative array with data from DB.
  73          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  74          $doc->set('title', $info->title);
  75          $doc->set('content', $info->content);
  76          $doc->set('description1', $info->description1);
  77          $doc->set('description1', $info->description2);
  78          $doc->set('contextid', $info->contextid);
  79          $doc->set('courseid', $info->courseid);
  80          $doc->set('userid', $info->userid);
  81          $doc->set('owneruserid', $info->owneruserid);
  82          $doc->set('modified', $record->timemodified);
  83  
  84          return $doc;
  85      }
  86  
  87      public function attach_files($document) {
  88          global $DB;
  89  
  90          if (!$record = $DB->get_record('temp_mock_search_area', array('id' => $document->get('itemid')))) {
  91              return;
  92          }
  93  
  94          $info = unserialize($record->info);
  95          foreach ($info->attachfileids as $fileid) {
  96              $document->add_stored_file($fileid);
  97          }
  98      }
  99  
 100      public function uses_file_indexing() {
 101          return true;
 102      }
 103  
 104      public function check_access($id) {
 105          global $DB, $USER;
 106  
 107          if ($record = $DB->get_record('temp_mock_search_area', array('id' => $id))) {
 108              $info = unserialize($record->info);
 109  
 110              if (in_array($USER->id, $info->denyuserids)) {
 111                  return \core_search\manager::ACCESS_DENIED;
 112              }
 113              return \core_search\manager::ACCESS_GRANTED;
 114          }
 115          return \core_search\manager::ACCESS_DELETED;
 116      }
 117  
 118      public function get_doc_url(\core_search\document $doc) {
 119          return new \moodle_url('/index.php');
 120      }
 121  
 122      public function get_context_url(\core_search\document $doc) {
 123          return new \moodle_url('/index.php');
 124      }
 125  }


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