2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
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
{
38 * The context levels the search implementation is working on.
42 protected static $levels = [CONTEXT_COURSE
];
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) {
54 list ($contextjoin, $contextparams) = $this->get_course_level_context_restriction_sql(
56 if ($contextjoin === null) {
60 return $DB->get_recordset_sql("
64 WHERE c.timemodified >= ?
65 ORDER BY c.timemodified ASC", array_merge($contextparams, [$modifiedfrom]));
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()) {
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
);
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);
104 * Whether the user can access the document or not.
106 * @param int $id The course instance id.
109 public function check_access($id) {
111 $course = $DB->get_record('course', array('id' => $id));
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.
148 public function uses_file_indexing() {
153 * Return the context info required to index files for
156 * Should be overridden by each search area.
160 public function get_search_fileareas() {
163 'summary'// Fileareas.
170 * Returns the moodle component name.
172 * It might be the plugin name (whole frankenstyle name) or the core subsystem name.
176 public function get_component_name() {
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.
195 public function get_category_names() {
196 return [\core_search\manager
::SEARCH_AREA_CATEGORY_COURSES
];