Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #66: Add template for search #133

Merged
merged 9 commits into from
Oct 29, 2024
37 changes: 36 additions & 1 deletion classes/search/cmsfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

defined('MOODLE_INTERNAL') || die();

use mod_cms\local\model\cms;
use mod_cms\local\renderer;

require_once($CFG->dirroot . '/mod/cms/lib.php');

/**
Expand Down Expand Up @@ -95,7 +98,6 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
* @return \core_search\document
*/
public function get_document($record, $options = []) {
global $DB;
try {
$cm = $this->get_cm('cms', $record->id, $record->courseid);
$context = \context_module::instance($cm->id);
Expand Down Expand Up @@ -131,6 +133,26 @@ public function get_document($record, $options = []) {
$valueformat = $record->valueformat;
}

// Add mustache template to value.
if (!empty($defaultvalues[$record->typeid]->mustache)) {
$cms = new cms($cm->instance);
$renderer = new renderer($cms);
ob_start();
try {
// Indexer uses "Empty" session, it may get an error from rendering.
$value .= $renderer->get_html();
} catch (\Exception $e) {
// Use template when an error occurs.
$value .= ' ' . $defaultvalues[$record->typeid]->mustache;
}
// Do not show any errors from rendering.
ob_end_clean();
if (empty($title)) {
$title = $defaultvalues[$record->typeid]->name;
}
$valueformat = FORMAT_HTML;
}

// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', content_to_text($title, false));
Expand Down Expand Up @@ -179,6 +201,19 @@ protected function get_default_values() {
}
$defaultvalues[$cmstype->typeid] = $data;
}

// Add mustache template for default value.
$sql = "SELECT mct.id, mct.name, mct.mustache
FROM {cms_types} mct";
$mustaches = $DB->get_records_sql($sql);
foreach ($mustaches as $mustache) {
if (empty($defaultvalues[$mustache->id])) {
$defaultvalues[$mustache->id] = new \stdClass();
}
$defaultvalues[$mustache->id]->name = $mustache->name;
$defaultvalues[$mustache->id]->mustache = $mustache->mustache;
}

$this->defaultvalues = $defaultvalues;
}
return $this->defaultvalues;
Expand Down
14 changes: 5 additions & 9 deletions tests/search/search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function setUp(): void {
$cmstype = new cms_types();
$cmstype->set('name', 'Overview')
->set('idnumber', 'overview')
->set('mustache', 'Template doc')
->set('title_mustache', 'Overview');
$cmstype->save();
$fieldcategory = self::getDataGenerator()->create_custom_field_category([
Expand Down Expand Up @@ -187,13 +188,8 @@ public function test_get_document_recordset(): void {
$this->assertEquals($course->id, $doc->get('courseid'));
$this->assertEquals($context->id, $doc->get('contextid'));
$this->assertEquals($this->field->get('name'), $doc->get('title'));
$this->assertEquals($data->value, $doc->get('content'));

// Static caches are working.
$dbreads = $DB->perf_get_reads();
$doc = $searcharea->get_document($record);
$this->assertEquals($dbreads, $DB->perf_get_reads());
$this->assertInstanceOf('\core_search\document', $doc);
$this->assertStringContainsString($data->value, $doc->get('content'));
$this->assertStringContainsString($this->cmstype->get('mustache'), $doc->get('content'));
$count++;
}
$this->assertEquals(1, $count);
Expand Down Expand Up @@ -238,7 +234,7 @@ public function test_default_content(): void {
$doc = $searcharea->get_document($record);
$this->assertInstanceOf('\core_search\document', $doc);
// Confirm the content is from defaultvalue from cms fieldtype.
$this->assertEquals('Default Text Overview', $doc->get('content'));
$this->assertStringContainsString('Default Text Overview', $doc->get('content'));
$count++;
}
$this->assertEquals(1, $count);
Expand All @@ -253,7 +249,7 @@ public function test_default_content(): void {
foreach ($recordset as $record) {
$this->assertInstanceOf('stdClass', $record);
$doc = $searcharea->get_document($record);
$this->assertEquals('Update test 1', $doc->get('content'));
$this->assertStringContainsString('Update test 1', $doc->get('content'));
$count++;
}
$this->assertEquals(1, $count);
Expand Down
Loading