Skip to content

Commit 8f0cf15

Browse files
authored
Merge pull request #133 from catalyst/issue66-add-template
Issue #66: Add template for search
2 parents cd334d5 + 480aaa2 commit 8f0cf15

File tree

5 files changed

+198
-294
lines changed

5 files changed

+198
-294
lines changed

classes/search/activity.php

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

classes/search/cmsfield.php

-256
This file was deleted.

0 commit comments

Comments
 (0)