Skip to content

Commit db5f98d

Browse files
committed
Issue #66: Add file search API
1 parent cd334d5 commit db5f98d

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

classes/search/cmsfield.php

+59-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
7272
JOIN {customfield_category} mcc ON mcf.categoryid = mcc.id
7373
$contextjoin
7474
WHERE mcd.timemodified >= ? AND mcc.component = 'mod_cms' AND mcc.area = 'cmsfield'
75-
AND mcf.type IN ('textarea', 'text')
75+
AND mcf.type IN ('textarea', 'text', 'file')
7676
GROUP BY mc.id
7777
) cdata ON ccms.id = cdata.id
7878
JOIN {customfield_field} cmcf ON cmcf.id = cdata.fieldid
@@ -81,10 +81,11 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
8181
null AS dataid, null AS value, null AS valueformat,
8282
mc.timecreated timecreated, mc.timemodified timemodified
8383
FROM {cms} mc
84+
$contextjoin
8485
LEFT JOIN {customfield_data} mcd ON mc.id = mcd.instanceid
8586
WHERE mcd.id IS NULL AND mc.timecreated >= ?
8687
ORDER BY timemodified ASC";
87-
return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom, $modifiedfrom]));
88+
return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom], $contextparams, [$modifiedfrom]));
8889
}
8990

9091
/**
@@ -253,4 +254,60 @@ protected function get_data($id) {
253254
}
254255
return $this->cmsdata[$id];
255256
}
257+
258+
/**
259+
* Returns true if this area uses file indexing.
260+
*
261+
* @return bool
262+
*/
263+
public function uses_file_indexing() {
264+
return true;
265+
}
266+
267+
/**
268+
* Return the context info required to index files for
269+
* this search area.
270+
*
271+
* @return array
272+
*/
273+
public function get_search_fileareas() {
274+
return ['value'];
275+
}
276+
277+
/**
278+
* Add the forum post attachments.
279+
*
280+
* @param document $document The current document
281+
* @return null
282+
*/
283+
public function attach_files($document) {
284+
global $DB;
285+
286+
$fileareas = $this->get_search_fileareas();
287+
// File is in "customfield_file" for component, "value" for filearea, and for customfield data id for itemid.
288+
$contextid = \context_system::instance()->id;
289+
$component = 'customfield_file';
290+
$cmsid = $document->get('itemid');
291+
292+
// Search customfield data from cms record.
293+
$sql = "SELECT mcd.id
294+
FROM {cms} mc
295+
JOIN {customfield_data} mcd ON mc.id = mcd.instanceid
296+
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
297+
JOIN {customfield_category} mcc ON mcf.categoryid = mcc.id
298+
WHERE mc.id = ? AND mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcf.type = 'file'";
299+
$param = [$cmsid];
300+
$filedata = $DB->get_records_sql($sql, $param);
301+
302+
foreach ($fileareas as $filearea) {
303+
foreach ($filedata as $data) {
304+
$fs = get_file_storage();
305+
$files = $fs->get_area_files($contextid, $component, $filearea, $data->id, '', false);
306+
307+
foreach ($files as $file) {
308+
$document->add_stored_file($file);
309+
}
310+
}
311+
}
312+
}
256313
}

0 commit comments

Comments
 (0)