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