Skip to content

Commit

Permalink
MDL-75146 mod_data: migrate templates editor to mustache
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranrecio authored and sarjona committed Jul 22, 2022
1 parent c6db008 commit e2da14b
Show file tree
Hide file tree
Showing 13 changed files with 953 additions and 290 deletions.
2 changes: 1 addition & 1 deletion mod/data/amd/build/templateseditor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mod/data/amd/build/templateseditor.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions mod/data/amd/src/templateseditor.js
Expand Up @@ -41,6 +41,10 @@ const selectors = {
const registerEventListeners = (d, mode) => {
const toggleTemplateEditor = document.querySelector(selectors.toggleTemplateEditor);

if (!toggleTemplateEditor) {
return;
}

toggleTemplateEditor.addEventListener('click', async(event) => {
event.preventDefault();
// Whether the event action attempts to enable or disable the template editor.
Expand Down
138 changes: 137 additions & 1 deletion mod/data/classes/manager.php
Expand Up @@ -16,11 +16,13 @@

namespace mod_data;

use context_module;
use cm_info;
use context_module;
use completion_info;
use data_field_base;
use mod_data\event\course_module_viewed;
use mod_data\event\template_viewed;
use mod_data\event\template_updated;
use stdClass;

/**
Expand All @@ -38,6 +40,23 @@ class manager {
/** Pluginname name. */
const PLUGIN = 'mod_data';

/** Template list with their files required to save the information of a preset. */
const TEMPLATES_LIST = [
'listtemplate' => 'listtemplate.html',
'singletemplate' => 'singletemplate.html',
'asearchtemplate' => 'asearchtemplate.html',
'addtemplate' => 'addtemplate.html',
'rsstemplate' => 'rsstemplate.html',
'csstemplate' => 'csstemplate.css',
'jstemplate' => 'jstemplate.js',
'listtemplateheader' => 'listtemplateheader.html',
'listtemplatefooter' => 'listtemplatefooter.html',
'rsstitletemplate' => 'rsstitletemplate.html',
];

/** @var string plugin path. */
private $path;

/** @var stdClass course_module record. */
private $instance;

Expand All @@ -47,17 +66,24 @@ class manager {
/** @var cm_info course_modules record. */
private $cm;

/** @var array the current data_fields records.
* Do not acces this attribute directly, use $this->get_field_records instead
*/
private $_fieldrecords = null;

/**
* Class contructor.
*
* @param cm_info $cm course module info object
* @param stdClass $instance activity instance object.
*/
public function __construct(cm_info $cm, stdClass $instance) {
global $CFG;
$this->cm = $cm;
$this->instance = $instance;
$this->context = context_module::instance($cm->id);
$this->instance->cmidnumber = $cm->idnumber;
$this->path = $CFG->dirroot . '/mod/' . self::MODULE;
}

/**
Expand Down Expand Up @@ -167,4 +193,114 @@ public function set_template_viewed() {
$event->add_record_snapshot(self::MODULE, $this->instance);
$event->trigger();
}

/**
* Return the database fields.
*
* @return data_field_base[] the field instances.
*/
public function get_fields(): array {
$result = [];
$fieldrecords = $this->get_field_records();
foreach ($fieldrecords as $fieldrecord) {
$result[$fieldrecord->id] = $this->get_field($fieldrecord);
}
return $result;
}

/**
* Return the field records (the current data_fields records).
*
* @return stdClass[] an array of records
*/
public function get_field_records() {
global $DB;
if ($this->_fieldrecords === null) {
$this->_fieldrecords = $DB->get_records('data_fields', ['dataid' => $this->instance->id]);
}
return $this->_fieldrecords;
}

/**
* Return a specific field instance from a field record.
*
* @param stdClass $fieldrecord the fieldrecord to convert
* @return data_field_base the data field class instance
*/
public function get_field(stdClass $fieldrecord): data_field_base {
$filepath = "{$this->path}/field/{$fieldrecord->type}/field.class.php";
$classname = "data_field_{$fieldrecord->type}";
if (!file_exists($filepath) || !class_exists($classname)) {
return new data_field_base($fieldrecord, $this->instance, $this->cm);
}
require_once($filepath);
$newfield = new $classname($fieldrecord, $this->instance, $this->cm);
return $newfield;
}

/**
* Return a specific template.
*
* NOTE: this method returns a default template if the module template is empty.
* However, it won't update the template database field.
*
* @param string $templatename
* @param array $options extra display options array
* @return template the template instance
*/
public function get_template(string $templatename, array $options = []): template {
if ($templatename == 'single') {
$templatename == 'singletemplate';
}
$instance = $this->instance;
$templatestr = $instance->{$templatename} ?? '';
if (empty($templatestr)) {
$templatestr = data_generate_default_template($instance, $templatename, 0, false, false);
}
// Some templates have extra options.
if ($templatename == 'singletemplate') {
$options['comments'] = true;
$options['ratings'] = true;
}
return new template($this, $templatestr, $options);
}

/**
* Update the database templates.
*
* @param stdClass $newtemplates an object with all the new templates
* @return bool if updated successfully.
*/
public function update_templates(stdClass $newtemplates) {
global $DB;
$record = (object)[
'id' => $this->instance->id,
];
foreach (self::TEMPLATES_LIST as $templatename => $templatefile) {
if (!isset($newtemplates->{$templatename})) {
continue;
}
$record->{$templatename} = $newtemplates->{$templatename};
}

// The add entry form cannot repeat tags.
if (isset($record->addtemplate) && !data_tags_check($this->instance->id, $record->addtemplate)) {
return false;
}

$DB->update_record(self::MODULE, $record);
$this->instance = $DB->get_record(self::MODULE, ['id' => $this->cm->instance], '*', MUST_EXIST);

// Trigger an event for saving the templates.
$event = template_updated::create(array(
'context' => $this->context,
'courseid' => $this->cm->course,
'other' => array(
'dataid' => $this->instance->id,
)
));
$event->trigger();

return true;
}
}

0 comments on commit e2da14b

Please sign in to comment.