Merge branch 'MDL-79937_fixlessonmatching' of https://github.com/catalystfd/moodle
[moodle.git] / course / classes / search / course.php
blobb1bab831c08316d6c4a84914a59088fe70977ca6
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.
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/>.
17 /**
18 * Search area for Moodle courses.
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
24 namespace core_course\search;
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Search area for Moodle courses.
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
35 class course extends \core_search\base {
37 /**
38 * The context levels the search implementation is working on.
40 * @var array
42 protected static $levels = [CONTEXT_COURSE];
44 /**
45 * Returns recordset containing required data for indexing courses.
47 * @param int $modifiedfrom timestamp
48 * @param \context|null $context Restriction context
49 * @return \moodle_recordset|null Recordset or null if no change possible
51 public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
52 global $DB;
54 list ($contextjoin, $contextparams) = $this->get_course_level_context_restriction_sql(
55 $context, 'c');
56 if ($contextjoin === null) {
57 return null;
60 return $DB->get_recordset_sql("
61 SELECT c.*
62 FROM {course} c
63 $contextjoin
64 WHERE c.timemodified >= ?
65 ORDER BY c.timemodified ASC", array_merge($contextparams, [$modifiedfrom]));
68 /**
69 * Returns the document associated with this course.
71 * @param \stdClass $record
72 * @param array $options
73 * @return \core_search\document
75 public function get_document($record, $options = array()) {
76 try {
77 $context = \context_course::instance($record->id);
78 } catch (\moodle_exception $ex) {
79 // Notify it as we run here as admin, we should see everything.
80 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
81 $ex->getMessage(), DEBUG_DEVELOPER);
82 return false;
84 // Prepare associative array with data from DB.
85 $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
86 $doc->set('title', content_to_text($record->fullname, false));
87 $doc->set('content', content_to_text($record->summary, $record->summaryformat));
88 $doc->set('contextid', $context->id);
89 $doc->set('courseid', $record->id);
90 $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
91 $doc->set('modified', $record->timemodified);
92 $doc->set('description1', $record->shortname);
94 // Check if this document should be considered new.
95 if (isset($options['lastindexedtime']) && $options['lastindexedtime'] < $record->timecreated) {
96 // If the document was created after the last index time, it must be new.
97 $doc->set_is_new(true);
100 return $doc;
104 * Whether the user can access the document or not.
106 * @param int $id The course instance id.
107 * @return int
109 public function check_access($id) {
110 global $DB;
111 $course = $DB->get_record('course', array('id' => $id));
112 if (!$course) {
113 return \core_search\manager::ACCESS_DELETED;
116 if (\core_course_category::can_view_course_info($course)) {
117 return \core_search\manager::ACCESS_GRANTED;
120 return \core_search\manager::ACCESS_DENIED;
124 * Link to the course.
126 * @param \core_search\document $doc
127 * @return \moodle_url
129 public function get_doc_url(\core_search\document $doc) {
130 return $this->get_context_url($doc);
134 * Link to the course.
136 * @param \core_search\document $doc
137 * @return \moodle_url
139 public function get_context_url(\core_search\document $doc) {
140 return new \moodle_url('/course/view.php', array('id' => $doc->get('courseid')));
144 * Returns true if this area uses file indexing.
146 * @return bool
148 public function uses_file_indexing() {
149 return true;
153 * Return the context info required to index files for
154 * this search area.
156 * Should be overridden by each search area.
158 * @return array
160 public function get_search_fileareas() {
161 $fileareas = array(
162 'overviewfiles',
163 'summary'// Fileareas.
166 return $fileareas;
170 * Returns the moodle component name.
172 * It might be the plugin name (whole frankenstyle name) or the core subsystem name.
174 * @return string
176 public function get_component_name() {
177 return 'course';
181 * Returns an icon instance for the document.
183 * @param \core_search\document $doc
184 * @return \core_search\document_icon
186 public function get_doc_icon(\core_search\document $doc) : \core_search\document_icon {
187 return new \core_search\document_icon('i/course');
191 * Returns a list of category names associated with the area.
193 * @return array
195 public function get_category_names() {
196 return [\core_search\manager::SEARCH_AREA_CATEGORY_COURSES];