Skip to content

Commit

Permalink
MDL-81744 mod_feedback: Improve 'Templates' page UI
Browse files Browse the repository at this point in the history
- Add new actions to the table in a single action menu.
- Refactor 'usetemplate' module to allow to be called multiple
  times in the same page.
- Remove 'mode' parameter and logic from template management.
- Add a heading to template preview page.
- Add 'Actions' menu to the template preview page.
- Fix related behat scenarios.
  • Loading branch information
roland04 committed Oct 4, 2024
1 parent 0eacab8 commit 5aa749f
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 210 deletions.
11 changes: 11 additions & 0 deletions .upgradenotes/MDL-81744-2024100308251816.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
issueNumber: MDL-81744
notes:
mod_feedback:
- message: >-
The 'mode' parameter has been deprecated from 'edit_template_action_bar'
and 'templates_table' contructors.
type: deprecated
- message: >-
The 'use_template' template has been removed as it is not needed
anymore.
type: removed
2 changes: 1 addition & 1 deletion mod/feedback/amd/build/usetemplate.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/feedback/amd/build/usetemplate.min.js.map

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

64 changes: 38 additions & 26 deletions mod/feedback/amd/src/usetemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,51 @@ const selectors = {
modaltrigger: '[data-action="usetemplate"]',
};

let initialized = false;

/**
* Initialize module
*/
export const init = () => {
const trigger = document.querySelector(selectors.modaltrigger);

trigger.addEventListener('click', event => {
event.preventDefault();
if (initialized) {
// We already added the event listeners (can be called multiple times).
return;
}

document.addEventListener('click', event => {
// Use the template.
const trigger = event.target.closest(selectors.modaltrigger);
if (trigger) {
event.preventDefault();

const modalForm = new ModalForm({
modalConfig: {
title: getString('use_this_template', 'mod_feedback'),
},
formClass: 'mod_feedback\\form\\use_template_form',
args: {
id: trigger.getAttribute('data-dataid'),
templateid: trigger.getAttribute('data-templateid')
},
saveButtonText: getString('save', 'core')
});
const modalForm = new ModalForm({
modalConfig: {
title: getString('use_this_template', 'mod_feedback'),
},
formClass: 'mod_feedback\\form\\use_template_form',
args: {
id: trigger.getAttribute('data-dataid'),
templateid: trigger.getAttribute('data-templateid')
},
saveButtonText: getString('save', 'core')
});

// Show a toast notification when the form is submitted.
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
if (event.detail.result) {
window.location.assign(event.detail.url);
} else {
Notification.addNotification({
type: 'error',
message: getString('saving_failed', 'mod_feedback')
});
}
});
// Show a toast notification when the form is submitted.
modalForm.addEventListener(modalForm.events.FORM_SUBMITTED, event => {
if (event.detail.result) {
window.location.assign(event.detail.url);
} else {
Notification.addNotification({
type: 'error',
message: getString('saving_failed', 'mod_feedback')
});
}
});

modalForm.show();
modalForm.show();
}
});

initialized = true;
};
3 changes: 2 additions & 1 deletion mod/feedback/classes/form/create_template_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ public function definition() {
get_string('name', 'feedback'),
['maxlength' => '200', 'size' => '50']);
$mform->setType('templatename', PARAM_TEXT);
$mform->addRule('templatename', null, 'required', null, 'client');

if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
$mform->addElement('checkbox',
'ispublic', '',
get_string('public', 'feedback'));
get_string('availableforallcourses', 'feedback'));
}
}

Expand Down
5 changes: 3 additions & 2 deletions mod/feedback/classes/output/edit_action_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ private function get_edit_actions_menu(): action_menu {
$actionsselect = new action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-outline-primary');

$hasitems = $DB->record_exists('feedback_item', ['feedback' => $this->feedback->id]);
// Export.
if ($DB->record_exists('feedback_item', ['feedback' => $this->feedback->id])) {
if ($hasitems) {
$exporturl = new moodle_url('/mod/feedback/export.php', $this->urlparams + ['action' => 'exportfile']);
$actionsselect->add(new action_menu_link(
$exporturl,
Expand All @@ -119,7 +120,7 @@ private function get_edit_actions_menu(): action_menu {
$cancreatetemplates = has_any_capability([
'mod/feedback:createprivatetemplate',
'mod/feedback:createpublictemplate'], \context_module::instance($this->cmid));
if ($cancreatetemplates) {
if ($cancreatetemplates && $hasitems) {
$PAGE->requires->js_call_amd('mod_feedback/createtemplate', 'init');
$actionsselect->add(new action_menu_link(
new moodle_url('#'),
Expand Down
66 changes: 47 additions & 19 deletions mod/feedback/classes/output/edit_template_action_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use context_system;
use moodle_url;
use action_link;
use action_menu;
use action_menu_link;
use pix_icon;

/**
* Class actionbar - Display the action bar
Expand All @@ -31,19 +34,22 @@
class edit_template_action_bar extends base_action_bar {
/** @var int $templateid The template that is being edited/used */
private $templateid;
/** @var string $mode The type of view we are dealing with */
private $mode;

/**
* edit_template_action_bar constructor.
* @param int $cmid
* @param int $templateid
* @param string $mode
* @param string|null $mode This parameter has been deprecated since 4.5 and should not be used anymore.
*/
public function __construct(int $cmid, int $templateid, string $mode) {
public function __construct(int $cmid, int $templateid, ?string $mode = null) {
if ($mode !== null) {
debugging(
'The age argument has been deprecated. Please remove it from your method calls.',
DEBUG_DEVELOPER,
);
}
parent::__construct($cmid);
$this->templateid = $templateid;
$this->mode = $mode;
}

/**
Expand All @@ -52,35 +58,57 @@ public function __construct(int $cmid, int $templateid, string $mode) {
* @return array
*/
public function get_items(): array {
global $DB;
$additionalparams = ($this->mode ? ['mode' => $this->mode] : []);
$templateurl = new moodle_url('/mod/feedback/manage_templates.php', $this->urlparams + $additionalparams);
global $DB, $OUTPUT, $PAGE;

$templateurl = new moodle_url('/mod/feedback/manage_templates.php', $this->urlparams);
$template = $DB->get_record('feedback_template', array('id' => $this->templateid), '*', MUST_EXIST);

// Back button.
$items['left'][]['actionlink'] = new action_link($templateurl, get_string('back'), null, ['class' => 'btn btn-secondary']);

// Actions.
if (has_capability('mod/feedback:edititems', $this->context)) {
$items['usetemplate'] = $this->urlparams + [
'templateid' => $this->templateid
];
$actionsselect = new action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-outline-primary');
$PAGE->requires->js_call_amd('mod_feedback/usetemplate', 'init');

// Use template.
$actionsselect->add(new action_menu_link(
new moodle_url('#'),
new pix_icon('i/files', get_string('preview')),
get_string('use_this_template', 'mod_feedback'),
false,
['data-action' => 'usetemplate', 'data-dataid' => $this->cmid, 'data-templateid' => $this->templateid],
));
}

$template = $DB->get_record('feedback_template', array('id' => $this->templateid), '*', MUST_EXIST);
$systemcontext = context_system::instance();
// Delete.
$showdelete = has_capability('mod/feedback:deletetemplate', $this->context);
if ($template->ispublic) {
$showdelete = has_capability('mod/feedback:createpublictemplate', $systemcontext) &&
has_capability('mod/feedback:deletetemplate', $systemcontext);
$showdelete = has_all_capabilities(
['mod/feedback:createpublictemplate', 'mod/feedback:deletetemplate'],
context_system::instance()
);
}

if ($showdelete) {
$params = $this->urlparams + $additionalparams + [
$params = $this->urlparams + [
'deletetemplate' => $this->templateid,
'sesskey' => sesskey()
];
$deleteurl = new moodle_url('/mod/feedback/manage_templates.php', $params);
$deleteaction = new confirm_action(get_string('confirmdeletetemplate', 'feedback'));
$items['export'] = new action_link($deleteurl, get_string('delete'), $deleteaction, ['class' => 'btn btn-secondary']);
$deleteaction = new action_link(
$deleteurl,
get_string('delete'),
new confirm_action(get_string('confirmdeletetemplate', 'feedback')),
['class' => 'text-danger'],
new pix_icon('t/delete', get_string('delete_template', 'feedback')),
);
$deleteaction->primary = false;
$actionsselect->add($deleteaction);
}

$items['actionsselect'] = count($actionsselect->get_primary_actions()) > 0 ? $actionsselect : null;

return $items;
}
}
Loading

0 comments on commit 5aa749f

Please sign in to comment.