Skip to content

Commit 5c4b309

Browse files
committed
Issue #66: Add comments and version bump
1 parent f40d50c commit 5c4b309

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

classes/search/cmsfield.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
5656
return null;
5757
}
5858

59-
$sql = "SELECT mc.id, mc.course AS courseid, mc.typeid, mcf.name AS fieldname, mcd.id dataid,
60-
mcd.value AS value, mcd.valueformat AS valueformat,
59+
// Search area is from customfield_data, but if the record is missing from activity, use default value.
60+
$sql = "SELECT mc.id, mc.course AS courseid, mc.typeid, mcf.name AS fieldname, mcf.type,
61+
mcd.id dataid, mcd.value AS value, mcd.valueformat AS valueformat,
6162
mcd.timecreated AS timecreated, mcd.timemodified AS timemodified
6263
FROM {cms} mc
6364
JOIN {customfield_data} mcd ON mc.id = mcd.instanceid
@@ -100,16 +101,15 @@ public function get_document($record, $options = []) {
100101
return false;
101102
}
102103

104+
// Get default value for cms custom field.
103105
if (is_null($this->defaultvalues)) {
104106
$defaultvalues = [];
105-
// Get default value for cms custom field.
106107
$sql = "SELECT mct.id typeid, mcf.configdata, mcf.name fieldname
107108
FROM {cms_types} mct
108109
JOIN {customfield_category} mcc ON mcc.itemid = mct.id
109110
JOIN {customfield_field} mcf ON mcf.categoryid = mcc.id
110111
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcf.type IN ('textarea', 'text')";
111112
$cmstypes = $DB->get_records_sql($sql);
112-
$defaultvalues = [];
113113
foreach ($cmstypes as $cmstype) {
114114
$data = new \stdClass();
115115
$configdata = json_decode($cmstype->configdata);
@@ -123,9 +123,17 @@ public function get_document($record, $options = []) {
123123

124124
// Check if it's default value or not.
125125
if (empty($record->dataid)) {
126-
$title = $this->defaultvalues[$record->typeid]->fieldname ?? 'Default title';
127-
$value = $this->defaultvalues[$record->typeid]->value ?? 'Default value';
128-
$valueformat = $this->defaultvalues[$record->typeid]->valueformat ?? 'Default valueformat';
126+
$title = $this->defaultvalues[$record->typeid]->fieldname ?? '';
127+
$value = $this->defaultvalues[$record->typeid]->value ?? '';
128+
if (isset($this->defaultvalues[$record->typeid]->valueformat)) {
129+
$valueformat = $this->defaultvalues[$record->typeid]->valueformat;
130+
} else {
131+
if ($record->type == 'textarea') {
132+
$valueformat = FORMAT_HTML;
133+
} else {
134+
$valueformat = FORMAT_PLAIN;
135+
}
136+
}
129137
} else {
130138
$title = $record->fieldname;
131139
$value = $record->value;
@@ -216,10 +224,7 @@ protected function get_data($id) {
216224
FROM {customfield_data} mcd
217225
JOIN {cms} mc ON mc.id = mcd.instanceid
218226
WHERE mcd.id = :id";
219-
$this->cmsdata[$id] = $DB->get_record_sql($sql, ['id' => $id]);
220-
if (!$this->cmsdata[$id]) {
221-
throw new \dml_missing_record_exception('cms_data');
222-
}
227+
$this->cmsdata[$id] = $DB->get_record_sql($sql, ['id' => $id], MUST_EXIST);
223228
}
224229
return $this->cmsdata[$id];
225230
}

tests/search/search_test.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function setUp(): void {
7878
// Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
7979
$search = \testable_core_search::instance();
8080

81+
// Name for cms activity is using from "title_mustache".
8182
$cmstype = new cms_types();
8283
$cmstype->set('name', 'Overview')
8384
->set('idnumber', 'overview')
@@ -134,7 +135,7 @@ public function test_get_document_recordset(): void {
134135

135136
$course = self::getDataGenerator()->create_course();
136137

137-
// Name for cms is coming from "title_mustache" in cms_type.
138+
// The name of cms activity is from cms_type, so we do not set when creating the activity.
138139
$generator = self::getDataGenerator()->get_plugin_generator('mod_cms');
139140
$record = new \stdClass();
140141
$record->course = $course->id;
@@ -219,7 +220,6 @@ public function test_default_content(): void {
219220

220221
$course = self::getDataGenerator()->create_course();
221222

222-
// Name for cms is coming from "title_mustache" in cms_type.
223223
$generator = self::getDataGenerator()->get_plugin_generator('mod_cms');
224224
$record = new \stdClass();
225225
$record->course = $course->id;

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

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

28-
$plugin->version = 2024082700;
28+
$plugin->version = 2024082900;
2929
$plugin->requires = 2020061500; // Moodle 3.9.0 and above.
3030
$plugin->supported = [39, 401]; // Moodle 3.9 to 4.1 inclusive.
3131
$plugin->component = 'mod_cms';

0 commit comments

Comments
 (0)