[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/course/classes/search/ -> mycourse.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 Moodle courses I can access.
  19   *
  20   * @package    core_course
  21   * @copyright  2016 Skylar Kelty <S.Kelty@kent.ac.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_course\search;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Search area for Moodle courses I can access.
  30   *
  31   * @package    core_course
  32   * @copyright  2016 Skylar Kelty <S.Kelty@kent.ac.uk>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class mycourse extends \core_search\base {
  36  
  37      /**
  38       * The context levels the search implementation is working on.
  39       *
  40       * @var array
  41       */
  42      protected static $levels = [CONTEXT_COURSE];
  43  
  44      /**
  45       * Returns recordset containing required data for indexing courses.
  46       *
  47       * @param int $modifiedfrom timestamp
  48       * @return \moodle_recordset
  49       */
  50      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  51          global $DB;
  52          return $DB->get_recordset_select('course', 'timemodified >= ?', array($modifiedfrom), 'timemodified ASC');
  53      }
  54  
  55      /**
  56       * Returns the document associated with this course.
  57       *
  58       * @param stdClass $record
  59       * @param array    $options
  60       * @return \core_search\document
  61       */
  62      public function get_document($record, $options = array()) {
  63          try {
  64              $context = \context_course::instance($record->id);
  65          } catch (\moodle_exception $ex) {
  66              // Notify it as we run here as admin, we should see everything.
  67              debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
  68                  $ex->getMessage(), DEBUG_DEVELOPER);
  69              return false;
  70          }
  71          // Prepare associative array with data from DB.
  72          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  73          $doc->set('title', content_to_text($record->fullname, false));
  74          $doc->set('content', content_to_text($record->summary, $record->summaryformat));
  75          $doc->set('contextid', $context->id);
  76          $doc->set('courseid', $record->id);
  77          $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
  78          $doc->set('modified', $record->timemodified);
  79          $doc->set('description1', $record->shortname);
  80  
  81          // Check if this document should be considered new.
  82          if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) {
  83              // If the document was created after the last index time, it must be new.
  84              $doc->set_is_new(true);
  85          }
  86  
  87          return $doc;
  88      }
  89  
  90      /**
  91       * Whether the user can access the document or not.
  92       *
  93       * @param int $id The course instance id.
  94       * @return int
  95       */
  96      public function check_access($id) {
  97          global $DB;
  98          $course = $DB->get_record('course', array('id' => $id));
  99          if (!$course) {
 100              return \core_search\manager::ACCESS_DELETED;
 101          }
 102          if (can_access_course($course)) {
 103              return \core_search\manager::ACCESS_GRANTED;
 104          }
 105          return \core_search\manager::ACCESS_DENIED;
 106      }
 107  
 108      /**
 109       * Link to the course.
 110       *
 111       * @param \core_search\document $doc
 112       * @return \moodle_url
 113       */
 114      public function get_doc_url(\core_search\document $doc) {
 115          return $this->get_context_url($doc);
 116      }
 117  
 118      /**
 119       * Link to the course.
 120       *
 121       * @param \core_search\document $doc
 122       * @return \moodle_url
 123       */
 124      public function get_context_url(\core_search\document $doc) {
 125          return new \moodle_url('/course/view.php', array('id' => $doc->get('courseid')));
 126      }
 127  }


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