Skip to content

Commit 55a6f70

Browse files
committed
Issue #68: CMS name is derived from the CMS type config
1 parent 71ae55f commit 55a6f70

17 files changed

+217
-22
lines changed

classes/form/cms_types_form.php

+24
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ protected function definition() {
6161

6262
$this->add_datasource_select_element();
6363

64+
$mform->addElement('header', 'instance_heading', get_string('instance:header', 'mod_cms'));
65+
66+
$mform->addElement('text', 'title_mustache', get_string('instance:name', 'mod_cms'));
67+
$mform->addRule('title_mustache', get_string('required'), 'required', null, 'client');
68+
6469
$mform->addElement(
6570
'textarea',
6671
'mustache',
@@ -92,6 +97,25 @@ protected function definition() {
9297
$this->add_action_buttons();
9398
}
9499

100+
/**
101+
* Extra validataion on the data.
102+
*
103+
* @param \stdClass $data
104+
* @param array $files
105+
* @param array $errors
106+
* @return array
107+
*/
108+
public function extra_validation($data, $files, array &$errors) {
109+
$errors = parent::extra_validation($data, $files, $errors);
110+
111+
$valid = renderer::validate_template($data->title_mustache);
112+
if ($valid !== true) {
113+
$errors['title_mustache'] = $valid;
114+
}
115+
116+
return $errors;
117+
}
118+
95119
/**
96120
* Put together the datasource selection mechanism using checkboxes.
97121
*/

classes/local/datasource/roles.php

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public static function get_displayname(): string {
4545
* @return \stdClass
4646
*/
4747
public function get_data(): \stdClass {
48+
global $CFG;
49+
4850
$data = new \stdClass();
4951

5052
$showuser = 'all'; // List user under each first role.
@@ -86,6 +88,8 @@ public function get_data(): \stdClass {
8688
(object) ['shortname' => 'editingteacher', 'name' => '', 'coursealias' => ''],
8789
];
8890
} else {
91+
require_once($CFG->dirroot .'/user/lib.php');
92+
8993
$context = \context_course::instance($this->cms->get('course'));
9094

9195
$rolesincontext = get_all_roles($context);

classes/local/lib.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,21 @@ public static function get_course_content_items(content_item $defaultmoduleconte
9090
*/
9191
public static function add_instance(\stdClass $instancedata, $mform = null): int {
9292
$cms = new cms();
93-
$cms->set('name', $instancedata->name);
9493
$cms->set('typeid', $instancedata->typeid);
9594
$cms->set('intro', '');
9695
$cms->set('course', $instancedata->course);
96+
$cms->set('name', '');
9797
$cms->save();
9898

9999
$instancedata->id = $cms->get('id');
100100
foreach (dsbase::get_datasources($cms) as $ds) {
101101
$ds->update_instance($instancedata, true);
102102
}
103103

104+
$renderer = new renderer($cms);
105+
$cms->set('name', $renderer->get_name());
106+
$cms->save();
107+
104108
return $cms->get('id');
105109
}
106110

@@ -114,20 +118,37 @@ public static function add_instance(\stdClass $instancedata, $mform = null): int
114118
public static function update_instance(\stdClass $instancedata, $mform): bool {
115119
$cm = get_coursemodule_from_id('cms', $instancedata->coursemodule, 0, false, MUST_EXIST);
116120
$cms = new cms($cm->instance);
117-
$cms->set('name', $instancedata->name);
118121
$cms->set('typeid', $instancedata->typeid);
119122
$cms->set('intro', '');
120123
$cms->set('course', $instancedata->course);
121-
$cms->save();
122124

123125
$instancedata->id = $cm->instance;
124126
foreach (dsbase::get_datasources($cms) as $ds) {
125127
$ds->update_instance($instancedata, false);
126128
}
127129

130+
$renderer = new renderer($cms);
131+
$cms->set('name', $renderer->get_name());
132+
$cms->save();
133+
128134
return true;
129135
}
130136

137+
/**
138+
* Rest the name field of cms instances.
139+
*
140+
* @param int $typeid
141+
* @throws \coding_exception
142+
*/
143+
public static function reset_cms_names(int $typeid) {
144+
$records = cms::get_records(['typeid' => $typeid]);
145+
foreach ($records as $cms) {
146+
$renderer = new renderer($cms);
147+
$cms->set('name', $renderer->get_name());
148+
$cms->save();
149+
}
150+
}
151+
131152
/**
132153
* Removes an instance of an activity.
133154
*

classes/local/model/cms_types.php

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ protected static function define_properties(): array {
6767
'type' => PARAM_INT,
6868
'default' => 1
6969
],
70+
'title_mustache' => [
71+
'type' => PARAM_TEXT,
72+
'default' => ''
73+
],
7074
'mustache' => [
7175
'type' => PARAM_RAW,
7276
'default' => '{{{debug}}}'

classes/local/renderer.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public function __construct($cms) {
5050
* @return \stdClass
5151
*/
5252
public function get_data(): \stdClass {
53-
global $CFG, $SITE;
54-
5553
$data = new \stdClass();
5654
$data->name = $this->cms->get('name');
5755

@@ -127,6 +125,28 @@ public function get_html(): string {
127125
return $content;
128126
}
129127

128+
/**
129+
* Renders the template label with the data and returns the result.
130+
*
131+
* @return string
132+
*/
133+
public function get_name(): string {
134+
$labelcache = \cache::make('mod_cms', 'cms_name');
135+
136+
$labelhash = $this->cms->get_content_hash();
137+
$label = $labelcache->get($labelhash);
138+
139+
if ($label === false) {
140+
$data = $this->get_data();
141+
$mustache = self::get_mustache();
142+
$template = $this->cms->get_type()->get('title_mustache');
143+
$label = $mustache->render($template, $data);
144+
$labelcache->set($labelhash, $label);
145+
}
146+
147+
return $label;
148+
}
149+
130150
/**
131151
* Get a Mustache engine suitable for use with this renderer.
132152
*
@@ -139,4 +159,20 @@ public static function get_mustache(): \Mustache_Engine {
139159
]);
140160
return $mustache;
141161
}
162+
163+
/**
164+
* Test the mustaceh template to see ifi it is valid.
165+
*
166+
* @param string $template
167+
* @return bool|string Returns true if no error occurred or an error message.
168+
*/
169+
public static function validate_template(string $template) {
170+
$engine = self::get_mustache();
171+
try {
172+
$engine->render($template);
173+
return true;
174+
} catch (\Throwable $e) {
175+
return $e->getMessage();
176+
}
177+
}
142178
}

classes/manage_content_types.php

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use mod_cms\form\cms_types_form;
2121
use mod_cms\form\cms_types_import_form;
2222
use mod_cms\local\datasource\base as dsbase;
23+
use mod_cms\local\lib;
2324
use mod_cms\local\model\cms_types;
2425
use mod_cms\local\table\content_types_list;
2526

@@ -353,6 +354,7 @@ public function update(int $id, \stdClass $data) {
353354
$cleandata = cms_types::clean_record($data);
354355
$instance->from_record($cleandata);
355356
$instance->update();
357+
lib::reset_cms_names($id);
356358

357359
// Do post update actions for data sources.
358360
foreach (dsbase::get_datasources($instance) as $ds) {

db/caches.php

+7
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@
3232
'staticacceleration' => false,
3333
'canuselocalstore' => true,
3434
],
35+
'cms_name' => [
36+
'mode' => cache_store::MODE_APPLICATION,
37+
'simplekeys' => true,
38+
'simpledata' => true,
39+
'staticacceleration' => false,
40+
'canuselocalstore' => true,
41+
],
3542
];

db/install.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<XMLDB PATH="mod/cms/db" VERSION="20230524" COMMENT="XMLDB file for mod_cms."
2+
<XMLDB PATH="mod/cms/db" VERSION="20230809" COMMENT="XMLDB file for mod_cms."
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
55
>
@@ -29,6 +29,7 @@
2929
<FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Description of the custom content type."/>
3030
<FIELD NAME="descriptionformat" TYPE="int" LENGTH="4" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="The format of the description field of the custom content type."/>
3131
<FIELD NAME="datasources" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Datasources used by this cms type, comma separated."/>
32+
<FIELD NAME="title_mustache" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The name of this custom content type instance."/>
3233
<FIELD NAME="mustache" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Mustache template for the content."/>
3334
<FIELD NAME="customdata" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Custom data, in JSON format."/>
3435
<FIELD NAME="isvisible" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="Is this visible in the activity list."/>

db/upgrade.php

+19
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,24 @@ function xmldb_cms_upgrade($oldversion) {
148148
upgrade_mod_savepoint(true, 2023080400, 'cms');
149149
}
150150

151+
if ($oldversion < 2023080900) {
152+
$table = new xmldb_table('cms_types');
153+
$field = new xmldb_field('title_mustache', XMLDB_TYPE_TEXT, null, null, false, null, null, 'datasources');
154+
155+
// Conditionally launch add field.
156+
if (!$dbman->field_exists($table, $field)) {
157+
$dbman->add_field($table, $field);
158+
}
159+
160+
upgrade_mod_savepoint(true, 2023080900, 'cms');
161+
}
162+
163+
if ($oldversion < 2023081401) {
164+
// Make sure title_mustache is set to something by default.
165+
$DB->execute("UPDATE {cms_types} SET title_mustache = name WHERE title_mustache='' OR title_mustache IS NULL");
166+
167+
upgrade_mod_savepoint(true, 2023081401, 'cms');
168+
}
169+
151170
return true;
152171
}

lang/en/cms.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
$string['settings'] = 'Custom content type settings';
4242
$string['table:name'] = 'Custom content type';
4343
$string['pluginadministration'] = 'Plugin administration';
44-
$string['mustache'] = 'Mustache template';
45-
$string['mustache_help'] = 'The above content will need to be a valid {$a}. Sample variables that are available for use in this template are given below.';
46-
$string['mustache_template'] = 'mustache template';
4744
$string['preview'] = 'Preview (click "{$a}" to update)';
4845
$string['customfield_manage_heading'] = 'Manage custom fields for content type "{$a}"';
4946
$string['manage_types_return'] = 'Return to manage types';
@@ -58,6 +55,13 @@
5855
$string['cachedef_cms_content'] = 'CMS content';
5956
$string['visibility_updated'] = 'Visibility updated';
6057

58+
// Template form section.
59+
$string['instance:header'] = 'Activity fields';
60+
$string['instance:name'] = 'Name';
61+
$string['mustache'] = 'Content';
62+
$string['mustache_help'] = 'The two fields above will form the content displayed in the activity. They both will need to be valid {$a}. Sample variables that are available for use in these templates are given below.';
63+
$string['mustache_template'] = 'mustache templates';
64+
6165
// Event strings.
6266
$string['event:cms_type_created'] = 'Custom content type created';
6367
$string['event_cms_type_created_desc'] = 'The user with ID: {$a->userid} has created a custom content type with ID: {$a->typeid}';

mod_form.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,10 @@ public function __construct($current, $section, $cm, $course) {
6464
protected function definition() {
6565
$mform = $this->_form;
6666

67-
// Adding the standard "name" field.
68-
$mform->addElement('text', 'name', get_string('name'), array('size' => '64'));
69-
$mform->setType('name', PARAM_TEXT);
70-
$mform->addRule('name', null, 'required', null, 'client');
71-
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
72-
7367
$mform->addElement('hidden', 'typeid', $this->typeid);
7468
$mform->setType('typeid', PARAM_INT);
7569

76-
// Add form elements for datas ources.
70+
// Add form elements for data sources.
7771
foreach (dsbase::get_datasources($this->cms) as $ds) {
7872
$ds->instance_form_definition($this, $mform);
7973
}

tests/cms_instance_test.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function test_add_instance() {
6565
$this->assertEquals(1, $count);
6666

6767
$cms = new cms($cmsid);
68-
$this->assertEquals('Some module', $cms->get('name'));
68+
$this->assertEquals('Field A', $cms->get('name'));
6969
$this->assertEquals($cmstype->get('id'), $cms->get('typeid'));
7070

7171
$ds = new dsfields($cms);
@@ -93,12 +93,13 @@ public function test_update_instance() {
9393
$cmsid = $moduleinfo->instance;
9494

9595
// Modify some fields and update.
96-
$moduleinfo->name = 'New name';
96+
$cmstype->set('title_mustache', '{{fields.bfield}}');
97+
$cmstype->save();
9798
$moduleinfo->customfield_bfield = 'Field B';
9899
update_module($moduleinfo);
99100

100101
$cms = new cms($cmsid);
101-
$this->assertEquals('New name', $cms->get('name'));
102+
$this->assertEquals('Field B', $cms->get('name'));
102103
$this->assertEquals($cmstype->get('id'), $cms->get('typeid'));
103104

104105
$ds = new dsfields($cms);

0 commit comments

Comments
 (0)