Skip to content

Commit

Permalink
MDL-75289 mod_data: Implement prototyped tertiary nav
Browse files Browse the repository at this point in the history
On the Database page:
- The List/single view is displayed in the right.
- The buttons Import entries, export entries and Export to portfolio
have been moved to the Actions menu.
- The List/Single view headings have been removed.
- The "Save settings" secondary button is hidden when advanced search
is enabled.
- The result (Found X out of Y entries, No records found) are not
displayed as notifications anymore.

On the Presets page:
- The buttons (Import, Export and Save as preset) have been moved to an
actions menu to the tertiary navigation. Some of these options have been
renamed.
- Remove the Action column heading from the table.

On the Presets preview page:
- Move the preset name to the heading in the tertiary navigation (Preview
of xxxxx), and remove the current preset name from the page.
- Align the List/single template to the right in the tertiary navigation.
- Make primary the "Use this preset" button.

On the Fields page:
- Remove the "Manage fields" menu.
- Remove the Export and Save as preset from the tertiary navigation.
- Align Create a field to the right in the tertiary navigation.
- Add a description at the top of the page.
- Remove the Action column heading from the table.
- Move field actions (Edit and Delete) to ellipsis.

On the Templates page:
- Move Export and Save as preset to the Actions menu.
- Move the templates list to a tertiary navigation selector and remove
the template heading.
- Reorder the templates list (Add entry template should be displayed
at the begining, instead of List template).
- Rename "Enable editor" to "Enable code editor".
  • Loading branch information
sarjona committed Oct 31, 2022
1 parent 53c4fc9 commit 61e8b80
Show file tree
Hide file tree
Showing 40 changed files with 804 additions and 500 deletions.
19 changes: 19 additions & 0 deletions mod/data/classes/manager.php
Expand Up @@ -314,6 +314,25 @@ public function can_manage_templates(?int $userid = null): bool {
return has_capability('mod/data:managetemplates', $this->context, $userid);
}

/** Check if the user can export entries on the current context.
*
* @param int $userid the user id to check ($USER->id if null).
* @return bool if the user can export entries on current context.
*/
public function can_export_entries(?int $userid = null): bool {
global $USER, $DB;

if (!$userid) {
$userid = $USER->id;
}

// Exportallentries and exportentry are basically the same capability.
return has_capability('mod/data:exportallentries', $this->context) ||
has_capability('mod/data:exportentry', $this->context) ||
(has_capability('mod/data:exportownentry', $this->context) &&
$DB->record_exists('data_records', ['userid' => $userid, 'dataid' => $this->instance->id]));
}

/**
* Update the database templates.
*
Expand Down
151 changes: 80 additions & 71 deletions mod/data/classes/output/action_bar.php
Expand Up @@ -17,6 +17,7 @@
namespace mod_data\output;

use mod_data\manager;
use mod_data\preset;
use moodle_url;
use url_select;

Expand Down Expand Up @@ -55,57 +56,28 @@ public function __construct(int $id, moodle_url $pageurl) {
* Generate the output for the action bar in the field page.
*
* @param bool $hasfieldselect Whether the field selector element should be rendered.
* @param bool $hassaveaspreset Whether the save as preset button element should be rendered.
* @param bool $hasexportpreset Whether the export as preset button element should be rendered.
* @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore.
* @return string The HTML code for the action bar.
*/
public function get_fields_action_bar(
bool $hasfieldselect = false,
bool $hassaveaspreset = false,
bool $hasexportpreset = false
?bool $unused1 = null,
?bool $unused2 = null
): string {
global $PAGE, $DB;

$createfieldlink = new moodle_url('/mod/data/field.php', ['id' => $this->cmid]);
$presetslink = new moodle_url('/mod/data/preset.php', ['id' => $this->cmid]);

$menu = [
$createfieldlink->out(false) => get_string('managefields', 'mod_data'),
$presetslink->out(false) => get_string('usestandard', 'mod_data'),
];

$selected = $createfieldlink->out(false);
global $PAGE;

$urlselect = new url_select($menu, $selected, null, 'fieldactionselect');
$urlselect->set_label(get_string('fieldsnavigation', 'mod_data'), ['class' => 'sr-only']);
if ($unused1 !== null || $unused2 !== null) {
debugging('Deprecated argument passed to get_fields_action_bar method', DEBUG_DEVELOPER);
}

$fieldselect = null;
if ($hasfieldselect) {
$fieldselect = $this->get_create_fields();
}

$saveaspresetbutton = null;
$exportpresetbutton = null;
$hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);

if ($hasfields) {
if ($hassaveaspreset) {
$saveaspresetlink = new moodle_url('/mod/data/preset.php',
['d' => $this->id, 'action' => 'export']);
$saveaspresetbutton = new \single_button($saveaspresetlink,
get_string('saveaspreset', 'mod_data'), 'post', false);
}

if ($hasexportpreset) {
$exportpresetlink = new moodle_url('/mod/data/preset.php',
['d' => $this->id, 'action' => 'export']);
$exportpresetbutton = new \single_button($exportpresetlink,
get_string('exportpreset', 'mod_data'), 'get', false);
}
}
$renderer = $PAGE->get_renderer('mod_data');
$fieldsactionbar = new fields_action_bar($this->id, $urlselect, null, $saveaspresetbutton,
$exportpresetbutton, $fieldselect);
$fieldsactionbar = new fields_action_bar($this->id, null, null, null, null, $fieldselect);

return $renderer->render_fields_action_bar($fieldsactionbar);
}
Expand All @@ -130,7 +102,7 @@ public function get_create_fields(): \action_menu {
foreach ($menufield as $fieldtype => $fieldname) {
$fieldselectparams['newtype'] = $fieldtype;
$fieldselect->add(new \action_menu_link(
new \moodle_url('/mod/data/field.php', $fieldselectparams),
new moodle_url('/mod/data/field.php', $fieldselectparams),
new \pix_icon('field/' . $fieldtype, $fieldname, 'data'),
$fieldname,
false
Expand All @@ -145,9 +117,10 @@ public function get_create_fields(): \action_menu {
* Generate the output for the action selector in the view page.
*
* @param bool $hasentries Whether entries exist.
* @param string $mode The current view mode (list, view...).
* @return string The HTML code for the action selector.
*/
public function get_view_action_bar(bool $hasentries): string {
public function get_view_action_bar(bool $hasentries, string $mode): string {
global $PAGE;

$viewlistlink = new moodle_url('/mod/data/view.php', ['d' => $this->id]);
Expand All @@ -167,7 +140,7 @@ public function get_view_action_bar(bool $hasentries): string {
$urlselect = new url_select($menu, $activeurl->out(false), null, 'viewactionselect');
$urlselect->set_label(get_string('viewnavigation', 'mod_data'), ['class' => 'sr-only']);
$renderer = $PAGE->get_renderer('mod_data');
$viewactionbar = new view_action_bar($this->id, $urlselect, $hasentries);
$viewactionbar = new view_action_bar($this->id, $urlselect, $hasentries, $mode);

return $renderer->render_view_action_bar($viewactionbar);
}
Expand All @@ -178,7 +151,7 @@ public function get_view_action_bar(bool $hasentries): string {
* @return string The HTML code for the action selector.
*/
public function get_templates_action_bar(): string {
global $PAGE, $DB;
global $PAGE;

$listtemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id,
'mode' => 'listtemplate']);
Expand All @@ -192,38 +165,21 @@ public function get_templates_action_bar(): string {
$jstemplatelink = new moodle_url('/mod/data/templates.php', ['d' => $this->id, 'mode' => 'jstemplate']);

$menu = [
$listtemplatelink->out(false) => get_string('listtemplate', 'mod_data'),
$addtemplatelink->out(false) => get_string('addtemplate', 'mod_data'),
$singletemplatelink->out(false) => get_string('singletemplate', 'mod_data'),
$listtemplatelink->out(false) => get_string('listtemplate', 'mod_data'),
$advancedsearchtemplatelink->out(false) => get_string('asearchtemplate', 'mod_data'),
$addtemplatelink->out(false) => get_string('addtemplate', 'mod_data'),
$rsstemplatelink->out(false) => get_string('rsstemplate', 'mod_data'),
$csstemplatelink->out(false) => get_string('csstemplate', 'mod_data'),
$jstemplatelink->out(false) => get_string('jstemplate', 'mod_data'),
$rsstemplatelink->out(false) => get_string('rsstemplate', 'mod_data'),
];

$urlselect = new url_select($menu, $this->currenturl->out(false), null, 'templatesactionselect');
$urlselect->set_label(get_string('templatesnavigation', 'mod_data'), ['class' => 'sr-only']);

$hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);

$saveaspresetbutton = null;
$exportpresetbutton = null;

if ($hasfields) {
$saveaspresetlink = new moodle_url('/mod/data/preset.php',
['d' => $this->id, 'action' => 'export']);
$saveaspresetbutton = new \single_button($saveaspresetlink,
get_string('saveaspreset', 'mod_data'), 'post', false);

$exportpresetlink = new moodle_url('/mod/data/preset.php',
['d' => $this->id, 'action' => 'export', 'sesskey' => sesskey()]);
$exportpresetbutton = new \single_button($exportpresetlink,
get_string('exportpreset', 'mod_data'), 'get', false);
}
$selectmenu = new \core\output\select_menu('presetsactions', $menu, $this->currenturl->out(false));
$selectmenu->set_label(get_string('templatesnavigation', 'mod_data'), ['class' => 'sr-only']);

$renderer = $PAGE->get_renderer('mod_data');
$templatesactionbar = new templates_action_bar($this->id, $urlselect, $saveaspresetbutton,
$exportpresetbutton);
$presetsactions = $this->get_presets_actions_select(false);
$templatesactionbar = new templates_action_bar($this->id, $selectmenu, null, null, $presetsactions);

return $renderer->render_templates_action_bar($templatesactionbar);
}
Expand All @@ -237,10 +193,8 @@ public function get_presets_action_bar(): string {
global $PAGE;

$renderer = $PAGE->get_renderer('mod_data');
if (!has_capability('mod/data:managetemplates', \context_module::instance($this->cmid))) {
return '';
}
$presetsactionbar = new presets_action_bar($this->cmid);
$presetsactionbar = new presets_action_bar($this->cmid, $this->get_presets_actions_select(true));

return $renderer->render_presets_action_bar($presetsactionbar);
}

Expand Down Expand Up @@ -277,12 +231,67 @@ public function get_presets_preview_action_bar(manager $manager, string $fullnam
$urlselect->set_label(get_string('templatesnavigation', manager::PLUGINNAME), ['class' => 'sr-only']);

$data = [
'title' => get_string('preview', manager::PLUGINNAME),
'title' => get_string('preview', manager::PLUGINNAME, preset::get_name_from_plugin($fullname)),
'hasback' => true,
'backtitle' => get_string('back'),
'backurl' => new moodle_url('/mod/data/preset.php', ['id' => $cm->id]),
'extraurlselect' => $urlselect->export_for_template($renderer),
];
return $renderer->render_from_template('mod_data/action_bar', $data);
}

/**
* Helper method to get the selector for the presets action.
*
* @param bool $hasimport Whether the Import buttons must be included or not.
* @return \action_menu|null The selector object used to display the presets actions. Null when the import button is not
* displayed and the database hasn't any fields.
*/
protected function get_presets_actions_select(bool $hasimport = false): ?\action_menu {
global $DB;

$hasfields = $DB->record_exists('data_fields', ['dataid' => $this->id]);

// Early return if the database has no fields and the import action won't be displayed.
if (!$hasfields && !$hasimport) {
return null;
}

$actionsselect = new \action_menu();
$actionsselect->set_menu_trigger(get_string('actions'), 'btn btn-secondary');

if ($hasimport) {
// Import.
$actionsselectparams = ['id' => $this->cmid, 'action' => 'import'];
$actionsselect->add(new \action_menu_link(
new moodle_url('/mod/data/preset.php', $actionsselectparams),
null,
get_string('importpreset', 'mod_data'),
false,
['data-action' => 'importpresets', 'data-dataid' => $this->cmid]
));
}

// If the database has no fields, export and save as preset options shouldn't be displayed.
if ($hasfields) {
// Export.
$actionsselectparams = ['id' => $this->cmid, 'action' => 'export'];
$actionsselect->add(new \action_menu_link(
new moodle_url('/mod/data/preset.php', $actionsselectparams),
null,
get_string('exportpreset', 'mod_data'),
false
));
// Save as preset.
$actionsselect->add(new \action_menu_link(
new moodle_url('/mod/data/preset.php', $actionsselectparams),
null,
get_string('saveaspreset', 'mod_data'),
false,
['data-action' => 'saveaspreset', 'data-dataid' => $this->id]
));
}

return $actionsselect;
}
}
35 changes: 8 additions & 27 deletions mod/data/classes/output/fields_action_bar.php
Expand Up @@ -31,41 +31,29 @@ class fields_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $id;

/** @var \url_select $urlselect The URL selector object. */
private $urlselect;

/** @var \single_select|null $fieldselect The field selector object or null. */
/** @var \action_menu|null $fieldselect The field selector object or null. */
private $fieldselect;

/** @var \single_button|null $saveaspresetbutton The save as preset single button object or null. */
private $saveaspresetbutton;

/** @var \single_button|null $exportpresetbutton The export preset single button object or null. */
private $exportpresetbutton;

/**
* The class constructor.
*
* @param int $id The database module id
* @param \url_select $urlselect The URL selector object
* @param null $unused This parameter has been deprecated since 4.0 and should not be used anymore.
* @param \single_button|null $saveaspresetbutton The save as preset single button object or null
* @param \single_button|null $exportpresetbutton The export preset single button object or null
* @param null $unused1 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused2 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused3 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param null $unused4 This parameter has been deprecated since 4.1 and should not be used anymore.
* @param \action_menu|null $fieldselect The field selector object or null
*/
public function __construct(int $id, \url_select $urlselect, $unused = null,
?\single_button $saveaspresetbutton = null, ?\single_button $exportpresetbutton = null,
public function __construct(int $id, $unused1 = null, $unused2 = null,
$unused3 = null, $unused4 = null,
?\action_menu $fieldselect = null) {

if ($unused !== null) {
if ($unused1 !== null || $unused2 !== null || $unused3 !== null || $unused4 !== null) {
debugging('Deprecated argument passed to fields_action_bar constructor', DEBUG_DEVELOPER);
}

$this->id = $id;
$this->urlselect = $urlselect;
$this->fieldselect = $fieldselect;
$this->saveaspresetbutton = $saveaspresetbutton;
$this->exportpresetbutton = $exportpresetbutton;
}

/**
Expand All @@ -78,19 +66,12 @@ public function export_for_template(\renderer_base $output): array {

$data = [
'd' => $this->id,
'urlselect' => $this->urlselect->export_for_template($output),
];

if ($this->fieldselect) {
$data['fieldselect'] = $this->fieldselect->export_for_template($output);
}

$data['saveaspreset'] = $this->saveaspresetbutton;

if ($this->exportpresetbutton) {
$data['exportpreset'] = $this->exportpresetbutton->export_for_template($output);
}

return $data;
}
}
2 changes: 1 addition & 1 deletion mod/data/classes/output/presets.php
Expand Up @@ -100,7 +100,7 @@ private function get_presets(renderer_base $output): array {
}
$actions = $this->get_preset_action_menu($output, $preset, $userid);

$fullname = "{$userid}/{$preset->shortname}";
$fullname = $preset->get_fullname();
$id = $this->manager->get_instance()->id;
$previewurl = new moodle_url(
'/mod/data/preset.php',
Expand Down
20 changes: 13 additions & 7 deletions mod/data/classes/output/presets_action_bar.php
Expand Up @@ -16,7 +16,6 @@

namespace mod_data\output;

use moodle_url;
use templatable;
use renderable;

Expand All @@ -32,13 +31,18 @@ class presets_action_bar implements templatable, renderable {
/** @var int $id The database module id. */
private $cmid;

/** @var \action_menu $actionsselect The presets actions selector object. */
private $actionsselect;

/**
* The class constructor.
*
* @param int $cmid The database module id
* @param \action_menu|null $actionsselect The presets actions selector object.
*/
public function __construct(int $cmid) {
public function __construct(int $cmid, ?\action_menu $actionsselect) {
$this->cmid = $cmid;
$this->actionsselect = $actionsselect;
}

/**
Expand All @@ -48,12 +52,14 @@ public function __construct(int $cmid) {
* @return array
*/
public function export_for_template(\renderer_base $output): array {
$importpresetlink = new moodle_url('/mod/data/preset.php', [
'id' => $this->cmid, 'action' => 'import'
]);
return [
$data = [
'id' => $this->cmid,
'importpreseturl' => $importpresetlink->out(false),
];

if ($this->actionsselect) {
$data['actionsselect'] = $this->actionsselect->export_for_template($output);
}

return $data;
}
}

0 comments on commit 61e8b80

Please sign in to comment.