Skip to content

Commit

Permalink
MDL-75234 mod_data: load default templates when empty
Browse files Browse the repository at this point in the history
The mod_data is forcing teachers to understand how to write templates
even if they want to use basic forms. With this patch the default
templates will be auto updated unless the user manually define the
templates.
  • Loading branch information
ferranrecio committed Oct 3, 2022
1 parent 24f97ed commit 2a09911
Show file tree
Hide file tree
Showing 19 changed files with 464 additions and 295 deletions.
4 changes: 2 additions & 2 deletions 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.

68 changes: 56 additions & 12 deletions mod/data/amd/src/templateseditor.js
Expand Up @@ -22,23 +22,66 @@
*/

import {get_string as getString} from 'core/str';
import {confirm as confirmDialogue} from 'core/notification';
import {prefetchStrings} from 'core/prefetch';
import {relativeUrl} from 'core/url';
import {saveCancel} from 'core/notification';

prefetchStrings('admin', ['confirmation']);
prefetchStrings('mod_data', [
'resettemplateconfirmtitle',
'resettemplateconfirm',
'resettemplate',
'enabletemplateeditorcheck',
'editorenable'
]);

/**
* Template editor constants.
*/
const selectors = {
toggleTemplateEditor: 'input[name="useeditor"]',
resetTemplate: 'input[name="defaultform"]',
resetButton: 'input[name="resetbutton"]',
editForm: '#edittemplateform',
};

/**
* Register event listeners for the module.
*
* @param {int} d The database ID
* @param {int} instanceId The database ID
* @param {string} mode The template mode
*/
const registerEventListeners = (d, mode) => {
const registerEventListeners = (instanceId, mode) => {
registerResetButton();
registerEditorToggler(instanceId, mode);
};

const registerResetButton = () => {
const editForm = document.querySelector(selectors.editForm);
const resetButton = document.querySelector(selectors.resetButton);
const resetTemplate = document.querySelector(selectors.resetTemplate);

if (!resetButton || !resetTemplate || !editForm) {
return;
}

resetButton.addEventListener('click', async(event) => {
event.preventDefault();
saveCancel(
getString('resettemplateconfirmtitle', 'mod_data'),
getString('resettemplateconfirm', 'mod_data'),
getString('resettemplate', 'mod_data'),
() => {
resetTemplate.value = "true";
editForm.submit();
},
null,
{triggerElement: event.target}
);
});
};

const registerEditorToggler = (instanceId, mode) => {
const toggleTemplateEditor = document.querySelector(selectors.toggleTemplateEditor);

if (!toggleTemplateEditor) {
Expand All @@ -52,27 +95,28 @@ const registerEventListeners = (d, mode) => {

if (enableTemplateEditor) {
// Display a confirmation dialog before enabling the template editor.
confirmDialogue(
saveCancel(
getString('confirmation', 'admin'),
getString('enabletemplateeditorcheck', 'mod_data'),
getString('yes', 'core'),
getString('no', 'core'),
getString('editorenable', 'mod_data'),
() => {
window.location = relativeUrl('/mod/data/templates.php', {d: d, mode: mode, useeditor: true});
}
window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: true});
},
null,
{triggerElement: event.target}
);
} else {
window.location = relativeUrl('/mod/data/templates.php', {d: d, mode: mode, useeditor: false});
window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: false});
}
});
};

/**
* Initialize the module.
*
* @param {int} d The database ID
* @param {int} instanceId The database ID
* @param {string} mode The template mode
*/
export const init = (d, mode) => {
registerEventListeners(d, mode);
export const init = (instanceId, mode) => {
registerEventListeners(instanceId, mode);
};
19 changes: 19 additions & 0 deletions mod/data/classes/manager.php
Expand Up @@ -20,9 +20,11 @@
use context_module;
use completion_info;
use data_field_base;
use mod_data_renderer;
use mod_data\event\course_module_viewed;
use mod_data\event\template_viewed;
use mod_data\event\template_updated;
use moodle_page;
use core_component;
use stdClass;

Expand Down Expand Up @@ -155,6 +157,18 @@ public function get_coursemodule(): cm_info {
return $this->cm;
}

/**
* Return the current module renderer.
*
* @param moodle_page|null $page the current page
* @return mod_data_renderer the module renderer
*/
public function get_renderer(?moodle_page $page = null): mod_data_renderer {
global $PAGE;
$page = $page ?? $PAGE;
return $page->get_renderer(self::PLUGINNAME);
}

/**
* Trigger module viewed event and set the module viewed for completion.
*
Expand Down Expand Up @@ -262,6 +276,11 @@ public function get_field(stdClass $fieldrecord): data_field_base {
* NOTE: this method returns a default template if the module template is empty.
* However, it won't update the template database field.
*
* Some possible options:
* - search: string with the current searching text.
* - page: integer repesenting the current pagination page numbre (if any)
* - baseurl: a moodle_url object to the current page.
*
* @param string $templatename
* @param array $options extra display options array
* @return template the template instance
Expand Down
16 changes: 10 additions & 6 deletions mod/data/classes/output/template_editor.php
Expand Up @@ -93,7 +93,8 @@ private function get_editors_data(bool $usehtmleditor): array {
global $PAGE;

$result = [];
$instance = $this->manager->get_instance();
$manager = $this->manager;
$instance = $manager->get_instance();

// Setup editor.
editors_head_setup();
Expand All @@ -112,40 +113,43 @@ private function get_editors_data(bool $usehtmleditor): array {

// Add editors.
if ($this->templatename === 'listtemplate') {
$template = $manager->get_template('listtemplateheader');
$result[] = $this->generate_editor_data(
$editor,
'header',
'listtemplateheader',
$instance->listtemplateheader
$template->get_template_content()
);
$maineditorname = 'multientry';
} else {
$maineditorname = $this->templatename;
}

$value = $instance->{$this->templatename} ?? '';
$template = $manager->get_template($this->templatename);
$result[] = $this->generate_editor_data(
$editor,
$maineditorname,
$this->templatename,
$value
$template->get_template_content()
);

if ($this->templatename === 'listtemplate') {
$template = $manager->get_template('listtemplatefooter');
$result[] = $this->generate_editor_data(
$editor,
'footer',
'listtemplatefooter',
$instance->listtemplatefooter
$template->get_template_content()
);
}

if ($this->templatename === 'rsstemplate') {
$template = $manager->get_template('rsstitletemplate');
$result[] = $this->generate_editor_data(
$editor,
'rsstitletemplate',
'rsstitletemplate',
$instance->rsstitletemplate
$template->get_template_content()
);
}

Expand Down

0 comments on commit 2a09911

Please sign in to comment.