Skip to content

Commit 780587d

Browse files
committed
WIP: index rendered activity
1 parent cb3a746 commit 780587d

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

classes/search/activity.php

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
namespace mod_cms\search;
18+
19+
use stdClass;
20+
use mod_cms\local\model\cms;
21+
use mod_cms\local\renderer;
22+
23+
/**
24+
* Search area for mod_cms activities.
25+
*
26+
* @package mod_cms
27+
* @copyright 2024 Catalyst IT
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
*/
30+
class activity extends \core_search\base_activity {
31+
32+
/**
33+
* Returns the document associated with this activity.
34+
*
35+
* Overwriting base_activity method as page contents field is required,
36+
* description field is not.
37+
*
38+
* @param stdClass $record
39+
* @param array $options
40+
* @return bool|\core_search\document
41+
*/
42+
public function get_document($record, $options = []) {
43+
44+
try {
45+
$cm = $this->get_cm($this->get_module_name(), $record->id, $record->course);
46+
$context = \context_module::instance($cm->id);
47+
} catch (\dml_missing_record_exception $ex) {
48+
// Notify it as we run here as admin, we should see everything.
49+
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' .
50+
$ex->getMessage(), DEBUG_DEVELOPER);
51+
52+
return false;
53+
} catch (\dml_exception $ex) {
54+
// Notify it as we run here as admin, we should see everything.
55+
debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER);
56+
57+
return false;
58+
}
59+
60+
// Prepare associative array with data from DB.
61+
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
62+
$doc->set('title', content_to_text($record->name, false));
63+
64+
$cms = new cms($cm->instance);
65+
$renderer = new renderer($cms);
66+
$doc->set('content', content_to_text($renderer->get_html(), FORMAT_HTML));
67+
$doc->set('contextid', $context->id);
68+
$doc->set('courseid', $record->course);
69+
$doc->set('owneruserid', \core_search\manager::NO_OWNER_ID);
70+
$doc->set('modified', $record->timemodified);
71+
72+
return $doc;
73+
}
74+
75+
/**
76+
* Returns true if this area uses file indexing.
77+
*
78+
* @return bool
79+
*/
80+
public function uses_file_indexing() {
81+
return false;
82+
}
83+
84+
/**
85+
* Return the context info required to index files for
86+
* this search area.
87+
*
88+
* @return array
89+
*/
90+
public function get_search_fileareas() {
91+
return ['content'];
92+
}
93+
94+
/**
95+
* Add the cms file attachments.
96+
*
97+
* @param \core_search\document $document The current document
98+
*/
99+
public function attach_files($document) {
100+
global $DB;
101+
102+
$fileareas = $this->get_search_fileareas();
103+
// File is in "customfield_file" for component, "value" for filearea, and for customfield data id for itemid.
104+
$contextid = \context_system::instance()->id;
105+
$component = 'customfield_file';
106+
$cmsid = $document->get('itemid');
107+
108+
// Search customfield data from cms record.
109+
$sql = "SELECT mcd.id
110+
FROM {cms} mc
111+
JOIN {customfield_data} mcd ON mc.id = mcd.instanceid
112+
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
113+
JOIN {customfield_category} mcc ON mcf.categoryid = mcc.id
114+
WHERE mc.id = ? AND mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcf.type = 'file'";
115+
$param = [$cmsid];
116+
$filedata = $DB->get_records_sql($sql, $param);
117+
118+
foreach ($fileareas as $filearea) {
119+
foreach ($filedata as $data) {
120+
$fs = get_file_storage();
121+
$files = $fs->get_area_files($contextid, $component, $filearea, $data->id, '', false);
122+
123+
foreach ($files as $file) {
124+
$document->add_stored_file($file);
125+
}
126+
}
127+
}
128+
}
129+
}

lang/en/cms.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
$string['error:no_config_hash'] = 'Module {$a} has no config hash.';
105105

106106
// Search strings.
107-
$string['search:cmsfield'] = 'CMS';
107+
$string['search:cmsfield'] = 'CMS fields';
108+
$string['search:activity'] = 'CMS activity';
108109

109110
// Site datasource strings.
110111
$string['site:displayname'] = 'Site Info';

0 commit comments

Comments
 (0)