1 |
efrain |
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.
|
|
|
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.
|
|
|
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 course 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 |
* @param \context|null $context Restriction context
|
|
|
49 |
* @return \moodle_recordset|null Recordset or null if no change possible
|
|
|
50 |
*/
|
|
|
51 |
public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
|
|
|
52 |
global $DB;
|
|
|
53 |
|
|
|
54 |
list ($contextjoin, $contextparams) = $this->get_course_level_context_restriction_sql(
|
|
|
55 |
$context, 'c');
|
|
|
56 |
if ($contextjoin === null) {
|
|
|
57 |
return null;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
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]));
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Returns the document associated with this course.
|
|
|
70 |
*
|
|
|
71 |
* @param \stdClass $record
|
|
|
72 |
* @param array $options
|
|
|
73 |
* @return \core_search\document
|
|
|
74 |
*/
|
|
|
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;
|
|
|
83 |
}
|
|
|
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);
|
|
|
93 |
|
|
|
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);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
return $doc;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Whether the user can access the document or not.
|
|
|
105 |
*
|
|
|
106 |
* @param int $id The course instance id.
|
|
|
107 |
* @return int
|
|
|
108 |
*/
|
|
|
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;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if (\core_course_category::can_view_course_info($course)) {
|
|
|
117 |
return \core_search\manager::ACCESS_GRANTED;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return \core_search\manager::ACCESS_DENIED;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Link to the course.
|
|
|
125 |
*
|
|
|
126 |
* @param \core_search\document $doc
|
|
|
127 |
* @return \moodle_url
|
|
|
128 |
*/
|
|
|
129 |
public function get_doc_url(\core_search\document $doc) {
|
|
|
130 |
return $this->get_context_url($doc);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Link to the course.
|
|
|
135 |
*
|
|
|
136 |
* @param \core_search\document $doc
|
|
|
137 |
* @return \moodle_url
|
|
|
138 |
*/
|
|
|
139 |
public function get_context_url(\core_search\document $doc) {
|
|
|
140 |
return new \moodle_url('/course/view.php', array('id' => $doc->get('courseid')));
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Returns true if this area uses file indexing.
|
|
|
145 |
*
|
|
|
146 |
* @return bool
|
|
|
147 |
*/
|
|
|
148 |
public function uses_file_indexing() {
|
|
|
149 |
return true;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Return the context info required to index files for
|
|
|
154 |
* this search area.
|
|
|
155 |
*
|
|
|
156 |
* Should be overridden by each search area.
|
|
|
157 |
*
|
|
|
158 |
* @return array
|
|
|
159 |
*/
|
|
|
160 |
public function get_search_fileareas() {
|
|
|
161 |
$fileareas = array(
|
|
|
162 |
'overviewfiles',
|
|
|
163 |
'summary'// Fileareas.
|
|
|
164 |
);
|
|
|
165 |
|
|
|
166 |
return $fileareas;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Returns the moodle component name.
|
|
|
171 |
*
|
|
|
172 |
* It might be the plugin name (whole frankenstyle name) or the core subsystem name.
|
|
|
173 |
*
|
|
|
174 |
* @return string
|
|
|
175 |
*/
|
|
|
176 |
public function get_component_name() {
|
|
|
177 |
return 'course';
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Returns an icon instance for the document.
|
|
|
182 |
*
|
|
|
183 |
* @param \core_search\document $doc
|
|
|
184 |
* @return \core_search\document_icon
|
|
|
185 |
*/
|
|
|
186 |
public function get_doc_icon(\core_search\document $doc): \core_search\document_icon {
|
|
|
187 |
return new \core_search\document_icon('i/course');
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Returns a list of category names associated with the area.
|
|
|
192 |
*
|
|
|
193 |
* @return array
|
|
|
194 |
*/
|
|
|
195 |
public function get_category_names() {
|
|
|
196 |
return [\core_search\manager::SEARCH_AREA_CATEGORY_COURSES];
|
|
|
197 |
}
|
|
|
198 |
}
|