Merge branch 'MDL-62899-search-icons-master' of https://github.com/dmitriim/moodle
[moodle.git] / search / classes / base_activity.php
CommitLineData
db48207e
DM
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 base class for activities.
19 *
20 * @package core_search
21 * @copyright 2016 Dan Poltawski
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
8fa1810c 25namespace core_search;
db48207e
DM
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Base implementation for activity modules.
31 *
32 * @package core_search
33 * @copyright 2016 Dan Poltawski
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36abstract class base_activity extends base_mod {
37
38 /**
39 * @var string The time modified field name.
40 *
41 * Activities not using timemodified as field name
42 * can overwrite this constant.
43 */
44 const MODIFIED_FIELD_NAME = 'timemodified';
45
091973db
EM
46 /**
47 * Activities with a time created field can overwrite this constant.
48 */
49 const CREATED_FIELD_NAME = '';
50
db48207e
DM
51 /**
52 * The context levels the search area is working on.
53 * @var array
54 */
55 protected static $levels = [CONTEXT_MODULE];
56
57 /**
427b7563 58 * Returns recordset containing all activities within the given context.
db48207e 59 *
427b7563 60 * @param \context|null $context Context
61 * @param int $modifiedfrom Return only records modified after this date
62 * @return \moodle_recordset|null Recordset, or null if no possible activities in given context
db48207e 63 */
427b7563 64 public function get_document_recordset($modifiedfrom = 0, \context $context = null) {
db48207e 65 global $DB;
427b7563 66 list ($contextjoin, $contextparams) = $this->get_context_restriction_sql(
67 $context, $this->get_module_name(), 'modtable');
68 if ($contextjoin === null) {
69 return null;
70 }
71 return $DB->get_recordset_sql('SELECT modtable.* FROM {' . $this->get_module_name() .
72 '} modtable ' . $contextjoin . ' WHERE modtable.' . static::MODIFIED_FIELD_NAME .
73 ' >= ? ORDER BY modtable.' . static::MODIFIED_FIELD_NAME . ' ASC',
74 array_merge($contextparams, [$modifiedfrom]));
db48207e
DM
75 }
76
77 /**
78 * Returns the document associated with this activity.
79 *
80 * This default implementation for activities sets the activity name to title and the activity intro to
81 * content. Any activity can overwrite this function if it is interested in setting other fields than the
82 * default ones, or to fill description optional fields with extra stuff.
83 *
84 * @param stdClass $record
091973db 85 * @param array $options
db48207e
DM
86 * @return \core_search\document
87 */
091973db 88 public function get_document($record, $options = array()) {
db48207e
DM
89
90 try {
91 $cm = $this->get_cm($this->get_module_name(), $record->id, $record->course);
92 $context = \context_module::instance($cm->id);
93 } catch (\dml_missing_record_exception $ex) {
94 // Notify it as we run here as admin, we should see everything.
95 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
96 $ex->getMessage(), DEBUG_DEVELOPER);
97 return false;
98 } catch (\dml_exception $ex) {
99 // Notify it as we run here as admin, we should see everything.
100 debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
101 return false;
102 }
103
104 // Prepare associative array with data from DB.
105 $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
505ce884 106 $doc->set('title', content_to_text($record->name, false));
69d66020 107 $doc->set('content', content_to_text($record->intro, $record->introformat));
db48207e 108 $doc->set('contextid', $context->id);
db48207e 109 $doc->set('courseid', $record->course);
f6b425e2 110 $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
db48207e
DM
111 $doc->set('modified', $record->{static::MODIFIED_FIELD_NAME});
112
091973db
EM
113 // Check if this document should be considered new.
114 if (isset($options['lastindexedtime'])) {
115 $createdfield = static::CREATED_FIELD_NAME;
116 if (!empty($createdfield) && ($options['lastindexedtime'] < $record->{$createdfield})) {
117 // If the document was created after the last index time, it must be new.
118 $doc->set_is_new(true);
119 }
120 }
121
db48207e
DM
122 return $doc;
123 }
124
125 /**
126 * Whether the user can access the document or not.
127 *
128 * @throws \dml_missing_record_exception
129 * @throws \dml_exception
130 * @param int $id The activity instance id.
131 * @return bool
132 */
133 public function check_access($id) {
134 global $DB;
135
db48207e 136 try {
54abd66a 137 $activity = $this->get_activity($id);
db48207e
DM
138 $cminfo = $this->get_cm($this->get_module_name(), $activity->id, $activity->course);
139 $cminfo->get_course_module_record();
140 } catch (\dml_missing_record_exception $ex) {
141 return \core_search\manager::ACCESS_DELETED;
142 } catch (\dml_exception $ex) {
143 return \core_search\manager::ACCESS_DENIED;
144 }
145
146 // Recheck uservisible although it should have already been checked in core_search.
147 if ($cminfo->uservisible === false) {
148 return \core_search\manager::ACCESS_DENIED;
149 }
150
151 return \core_search\manager::ACCESS_GRANTED;
152 }
153
154 /**
155 * Link to the module instance.
156 *
157 * @param \core_search\document $doc
158 * @return \moodle_url
159 */
4e921569 160 public function get_doc_url(\core_search\document $doc) {
db48207e
DM
161 return $this->get_context_url($doc);
162 }
163
164 /**
165 * Link to the module instance.
166 *
167 * @param \core_search\document $doc
168 * @return \moodle_url
169 */
170 public function get_context_url(\core_search\document $doc) {
171 $cminfo = $this->get_cm($this->get_module_name(), strval($doc->get('itemid')), $doc->get('courseid'));
172 return new \moodle_url('/mod/' . $this->get_module_name() . '/view.php', array('id' => $cminfo->id));
173 }
174
db48207e
DM
175 /**
176 * Returns an activity instance. Internally uses the class component to know which activity module should be retrieved.
177 *
178 * @param int $instanceid
179 * @return stdClass
180 */
181 protected function get_activity($instanceid) {
182 global $DB;
183
184 if (empty($this->activitiesdata[$this->get_module_name()][$instanceid])) {
185 $this->activitiesdata[$this->get_module_name()][$instanceid] = $DB->get_record($this->get_module_name(),
186 array('id' => $instanceid), '*', MUST_EXIST);
187 }
188 return $this->activitiesdata[$this->get_module_name()][$instanceid];
189
190 }
4e921569
MP
191
192 /**
193 * Return the context info required to index files for
194 * this search area.
195 *
196 * Should be onerridden by each search area.
197 *
198 * @return array
199 */
200 public function get_search_fileareas() {
201 $fileareas = array(
202 'intro' // Fileareas.
203 );
204
205 return $fileareas;
206 }
207
208 /**
209 * Files related to the current document are attached,
210 * to the document object ready for indexing by
211 * Global Search.
212 *
213 * The default implementation retrieves all files for
214 * the file areas returned by get_search_fileareas().
215 * If you need to filter files to specific items per
216 * file area, you will need to override this method
217 * and explicitly provide the items.
218 *
219 * @param document $document The current document
220 * @return void
221 */
222 public function attach_files($document) {
223 $fileareas = $this->get_search_fileareas();
224
225 if (!empty($fileareas)) {
226 $cm = $this->get_cm($this->get_module_name(), $document->get('itemid'), $document->get('courseid'));
227
228 $context = \context_module::instance($cm->id);
229 $contextid = $context->id;
230
231 $fs = get_file_storage();
232 $files = $fs->get_area_files($contextid, $this->get_component_name(), $fileareas, false, '', false);
233
234 foreach ($files as $file) {
235 $document->add_stored_file($file);
236 }
237 }
238
239 return;
240 }
db48207e 241}