From 57f1bd3a01cdaa370bf5e87fe41ab5d12cd9bbe2 Mon Sep 17 00:00:00 2001 From: Peter Dias Date: Wed, 9 Feb 2022 09:31:56 +0800 Subject: [PATCH] MDL-73370 contentbank: Move settings cog to tertiary navigation * Add headings to subpages * Update context header based on current context * Tweak layout. --- contentbank/classes/helper.php | 25 +++++ contentbank/classes/output/viewcontent.php | 99 +++++++++++++++++ .../contenttype/h5p/classes/form/editor.php | 4 +- contentbank/edit.php | 5 +- contentbank/index.php | 2 +- .../templates/contentbankmenu.mustache | 57 ++++++++++ contentbank/templates/viewcontent.mustache | 1 - .../viewcontent/toolbarview.mustache | 50 +++++++-- contentbank/view.php | 102 ------------------ lang/en/contentbank.php | 6 +- lang/en/deprecated.txt | 1 + 11 files changed, 234 insertions(+), 118 deletions(-) create mode 100644 contentbank/templates/contentbankmenu.mustache diff --git a/contentbank/classes/helper.php b/contentbank/classes/helper.php index 463f7ede337..f4cb921d279 100644 --- a/contentbank/classes/helper.php +++ b/contentbank/classes/helper.php @@ -44,6 +44,8 @@ class helper { global $PAGE, $DB; $PAGE->set_context($context); + $PAGE->set_heading(self::get_page_heading($context)); + $PAGE->set_secondary_active_tab('contentbank'); $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]); switch ($context->contextlevel) { @@ -68,4 +70,27 @@ class helper { $PAGE->set_pagelayout('standard'); } } + + /** + * Get the page heading based on the current context + * + * @param \context $context The current context of the page + * @return string + */ + public static function get_page_heading(\context $context): string { + global $SITE; + + $title = get_string('contentbank'); + if ($context->id == \context_system::instance()->id) { + $title = $SITE->fullname; + } else if ($context->contextlevel == CONTEXT_COURSE) { + $course = get_course($context->instanceid); + $title = $course->fullname; + } else if ($context->contextlevel == CONTEXT_COURSECAT) { + $category = \core_course_category::get($context->instanceid); + $title = $category->get_formatted_name(); + } + + return $title; + } } diff --git a/contentbank/classes/output/viewcontent.php b/contentbank/classes/output/viewcontent.php index 48871a6fb57..b514c4da142 100644 --- a/contentbank/classes/output/viewcontent.php +++ b/contentbank/classes/output/viewcontent.php @@ -60,6 +60,100 @@ class viewcontent implements renderable, templatable { $this->content = $content; } + /** + * Get the content of the "More" dropdown in the tertiary navigation + * + * @return array|null The options to be displayed in a dropdown in the tertiary navigation + * @throws \moodle_exception + */ + protected function get_edit_actions_dropdown(): ?array { + global $PAGE; + $options = []; + if ($this->contenttype->can_manage($this->content)) { + // Add the visibility item to the menu. + switch($this->content->get_visibility()) { + case content::VISIBILITY_UNLISTED: + $visibilitylabel = get_string('visibilitysetpublic', 'core_contentbank'); + $newvisibility = content::VISIBILITY_PUBLIC; + break; + case content::VISIBILITY_PUBLIC: + $visibilitylabel = get_string('visibilitysetunlisted', 'core_contentbank'); + $newvisibility = content::VISIBILITY_UNLISTED; + break; + default: + $url = new \moodle_url('/contentbank/index.php', ['contextid' => $this->content->get_contextid()]); + throw new moodle_exception('contentvisibilitynotfound', 'error', $url, $this->content->get_visibility()); + } + + if ($visibilitylabel) { + $options[$visibilitylabel] = [ + 'data-action' => 'setcontentvisibility', + 'data-visibility' => $newvisibility, + 'data-contentid' => $this->content->get_id(), + ]; + } + + // Add the rename content item to the menu. + $options[get_string('rename')] = [ + 'data-action' => 'renamecontent', + 'data-contentname' => $this->content->get_name(), + 'data-contentid' => $this->content->get_id(), + ]; + + if ($this->contenttype->can_upload()) { + $options[get_string('replacecontent', 'contentbank')] = ['data-action' => 'upload']; + + $PAGE->requires->js_call_amd( + 'core_contentbank/upload', + 'initModal', + [ + '[data-action=upload]', + \core_contentbank\form\upload_files::class, + $this->content->get_contextid(), + $this->content->get_id() + ] + ); + } + } + + if ($this->contenttype->can_download($this->content)) { + $url = new moodle_url($this->contenttype->get_download_url($this->content)); + $options[get_string('download')] = [ + 'url' => $url->out() + ]; + } + + // Add the delete content item to the menu. + if ($this->contenttype->can_delete($this->content)) { + $options[get_string('delete')] = [ + 'data-action' => 'deletecontent', + 'data-contentname' => $this->content->get_name(), + 'data-uses' => count($this->content->get_uses()), + 'data-contentid' => $this->content->get_id(), + 'data-contextid' => $this->content->get_contextid(), + ]; + } + + $dropdown = []; + if ($options) { + foreach ($options as $key => $attribs) { + $url = $attribs['url'] ?? '#'; + $dropdown['options'][] = [ + 'label' => $key, + 'url' => $url, + 'attributes' => array_map(function ($key, $value) { + return [ + 'name' => $key, + 'value' => $value + ]; + }, array_keys($attribs), $attribs) + ]; + } + } + + return $dropdown; + } + /** * Export this data so it can be used as the context for a mustache template. * @@ -88,6 +182,11 @@ class viewcontent implements renderable, templatable { $closeurl = new moodle_url('/contentbank/index.php', ['contextid' => $this->content->get_contextid()]); $data->closeurl = $closeurl->out(false); + $data->actionmenu = $this->get_edit_actions_dropdown(); + $data->heading = $this->content->get_name(); + if ($this->content->get_visibility() == content::VISIBILITY_UNLISTED) { + $data->heading = get_string('visibilitytitleunlisted', 'contentbank', $data->heading); + } return $data; } diff --git a/contentbank/contenttype/h5p/classes/form/editor.php b/contentbank/contenttype/h5p/classes/form/editor.php index 7ac641be0ab..26dc2d360d6 100644 --- a/contentbank/contenttype/h5p/classes/form/editor.php +++ b/contentbank/contenttype/h5p/classes/form/editor.php @@ -51,7 +51,7 @@ class editor extends edit_content { * Defines the form fields. */ protected function definition() { - global $DB; + global $DB, $OUTPUT; $mform = $this->_form; $errors = []; @@ -70,6 +70,7 @@ class editor extends edit_content { $this->h5peditor = new h5peditor(); $this->set_display_vertical(); + $mform->addElement('html', $OUTPUT->heading($this->_customdata['heading'], 2)); if ($id) { // The H5P editor needs the H5P content id (h5p table). @@ -114,7 +115,6 @@ class editor extends edit_content { } $mform->addElement('cancel', 'cancel', get_string('back')); } else { - $this->add_action_buttons(); $this->h5peditor->add_editor_to_form($mform); $this->add_action_buttons(); } diff --git a/contentbank/edit.php b/contentbank/edit.php index 353483fa745..f272d4b4599 100644 --- a/contentbank/edit.php +++ b/contentbank/edit.php @@ -76,7 +76,8 @@ if (!$contenttype->can_edit($content)) { $values = [ 'contextid' => $contextid, 'plugin' => $pluginname, - 'id' => $id + 'id' => $id, + 'heading' => $heading ]; $title = get_string('contentbank'); @@ -91,8 +92,6 @@ $PAGE->navbar->add(get_string('edit')); $PAGE->set_title($title); $PAGE->set_pagelayout('incourse'); -$PAGE->set_heading($heading); - // Instantiate the content type form. $editorclass = "$contenttypename\\form\\editor"; if (!class_exists($editorclass)) { diff --git a/contentbank/index.php b/contentbank/index.php index 1c44bf5e47a..2ba512e6c66 100644 --- a/contentbank/index.php +++ b/contentbank/index.php @@ -52,7 +52,6 @@ if ($contextid == \context_system::instance()->id) { $PAGE->set_context($context); } $PAGE->set_title($title); -$PAGE->set_heading($title); $PAGE->add_body_class('limitedwidth'); $PAGE->set_pagetype('contentbank'); $PAGE->set_secondary_active_tab('contentbank'); @@ -110,6 +109,7 @@ if (has_capability('moodle/contentbank:upload', $context)) { } echo $OUTPUT->header(); +echo $OUTPUT->heading($title, 2); echo $OUTPUT->box_start('generalbox'); // If needed, display notifications. diff --git a/contentbank/templates/contentbankmenu.mustache b/contentbank/templates/contentbankmenu.mustache new file mode 100644 index 00000000000..dae6f006b1d --- /dev/null +++ b/contentbank/templates/contentbankmenu.mustache @@ -0,0 +1,57 @@ +{{! +This file is part of Moodle - http://moodle.org/ + +Moodle is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Moodle is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more comments. + +You should have received a copy of the GNU General Public License +along with Moodle. If not, see . +}} +{{! + @template core_contentbank/contentbankmenu + + Contentbank view toolbar. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * options - JSON object - the options available in the dropdown + + Example context (json): + { + "options": [ + { + "url": "www.google.com", + "label": "Google", + "attributes": [ + { + "name": "data-attrib", + "value": "1" + } + ] + } + ] + } +}} + diff --git a/contentbank/templates/viewcontent.mustache b/contentbank/templates/viewcontent.mustache index 46ebe49536e..2a8a8b0c873 100644 --- a/contentbank/templates/viewcontent.mustache +++ b/contentbank/templates/viewcontent.mustache @@ -44,5 +44,4 @@
{{{ contenthtml }}}
- {{>core_contentbank/viewcontent/toolbarview}} diff --git a/contentbank/templates/viewcontent/toolbarview.mustache b/contentbank/templates/viewcontent/toolbarview.mustache index a879a225afe..7dde8d58a82 100644 --- a/contentbank/templates/viewcontent/toolbarview.mustache +++ b/contentbank/templates/viewcontent/toolbarview.mustache @@ -34,17 +34,51 @@ along with Moodle. If not, see . Example context (json): { "usercanedit" : true, + "heading" : "This is a heading", "editcontenturl" : "http://something/contentbank/edit.php?contextid=1&plugin=h5p&id=1", - "closeurl" : "http://moodle.test/h5pcb/moodle/contentbank/index.php" + "closeurl" : "http://moodle.test/h5pcb/moodle/contentbank/index.php", + "actionmenu": { + "options": [ + { + "url": "www.google.com", + "label": "Google", + "attributes": [ + { + "name": "data-attrib", + "value": "1" + } + ] + } + ] + } } }} +{{#heading}}

{{heading}}

{{/heading}} {{#usercanedit}} -
- - {{#str}}edit{{/str}} - - - {{#str}}close, core_contentbank{{/str}} - +
+
+ + + {{#actionmenu}} +
+ {{> core_contentbank/contentbankmenu }} +
+ {{/actionmenu}} + + +
{{/usercanedit}} +{{^usercanedit}} + {{#actionmenu}} + {{> core_contentbank/contentbankmenu }} + {{/actionmenu}} +{{/usercanedit}} diff --git a/contentbank/view.php b/contentbank/view.php index 534f8a6eaaa..36db1ce0950 100644 --- a/contentbank/view.php +++ b/contentbank/view.php @@ -55,122 +55,20 @@ if ($PAGE->course) { $cb = new \core_contentbank\contentbank(); $content = $cb->get_content_from_id($record->id); $contenttype = $content->get_content_type_instance(); -$pageheading = $record->name; if (!$content->is_view_allowed()) { $cburl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id, 'errormsg' => 'notavailable']); redirect($cburl); } -if ($content->get_visibility() == content::VISIBILITY_UNLISTED) { - $pageheading = get_string('visibilitytitleunlisted', 'contentbank', $record->name); -} - $PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id])); $PAGE->set_context($context); $PAGE->navbar->add($record->name); -$PAGE->set_heading($pageheading); $title .= ": ".$record->name; $PAGE->set_title($title); $PAGE->set_pagetype('contentbank'); $PAGE->set_pagelayout('incourse'); -// Create the cog menu with all the secondary actions, such as delete, rename... -$actionmenu = new action_menu(); -if ($contenttype->can_manage($content)) { - // Add the visibility item to the menu. - switch($content->get_visibility()) { - case content::VISIBILITY_UNLISTED: - $visibilitylabel = get_string('visibilitysetpublic', 'core_contentbank'); - $newvisibility = content::VISIBILITY_PUBLIC; - $visibilityicon = 't/hide'; - break; - case content::VISIBILITY_PUBLIC: - $visibilitylabel = get_string('visibilitysetunlisted', 'core_contentbank'); - $newvisibility = content::VISIBILITY_UNLISTED; - $visibilityicon = 't/show'; - break; - default: - print_error('contentvisibilitynotfound', 'error', $returnurl, $content->get_visibility()); - break; - } - - $attributes = [ - 'data-action' => 'setcontentvisibility', - 'data-visibility' => $newvisibility, - 'data-contentid' => $content->get_id(), - ]; - $actionmenu->add_secondary_action(new action_menu_link( - new moodle_url('#'), - new pix_icon($visibilityicon, $visibilitylabel), - $visibilitylabel, - false, - $attributes - )); - - // Add the rename content item to the menu. - $attributes = [ - 'data-action' => 'renamecontent', - 'data-contentname' => $content->get_name(), - 'data-contentid' => $content->get_id(), - ]; - $actionmenu->add_secondary_action(new action_menu_link( - new moodle_url('#'), - new pix_icon('e/styleparagraph', get_string('rename')), - get_string('rename'), - false, - $attributes - )); - - if ($contenttype->can_upload()) { - $actionmenu->add_secondary_action(new action_menu_link( - new moodle_url('/contentbank/view.php', ['contextid' => $context->id, 'id' => $content->get_id()]), - new pix_icon('i/upload', get_string('upload')), - get_string('replacecontent', 'contentbank'), - false, - ['data-action' => 'upload'] - )); - $PAGE->requires->js_call_amd( - 'core_contentbank/upload', - 'initModal', - ['[data-action=upload]', \core_contentbank\form\upload_files::class, $context->id, $content->get_id()] - ); - } -} -if ($contenttype->can_download($content)) { - // Add the download content item to the menu. - $actionmenu->add_secondary_action(new action_menu_link( - new moodle_url($contenttype->get_download_url($content)), - new pix_icon('t/download', get_string('download')), - get_string('download'), - false - )); -} -if ($contenttype->can_delete($content)) { - // Add the delete content item to the menu. - $attributes = [ - 'data-action' => 'deletecontent', - 'data-contentname' => $content->get_name(), - 'data-uses' => count($content->get_uses()), - 'data-contentid' => $content->get_id(), - 'data-contextid' => $context->id, - ]; - $actionmenu->add_secondary_action(new action_menu_link( - new moodle_url('#'), - new pix_icon('t/delete', get_string('delete')), - get_string('delete'), - false, - $attributes - )); -} - -// Add the cog menu to the header. -$PAGE->add_header_action(html_writer::div( - $OUTPUT->render($actionmenu), - 'd-print-none', - ['id' => 'region-main-settings-menu'] -)); - echo $OUTPUT->header(); // If needed, display notifications. diff --git a/lang/en/contentbank.php b/lang/en/contentbank.php index c127217dc7e..55573a35fa9 100644 --- a/lang/en/contentbank.php +++ b/lang/en/contentbank.php @@ -24,7 +24,6 @@ $string['author'] = 'Author'; $string['contentbank'] = 'Content bank'; -$string['close'] = 'Close'; $string['choosecontext'] = 'Choose course or category...'; $string['contentbankpreferences'] = 'Content bank preferences'; $string['contentdeleted'] = 'The content has been deleted.'; @@ -47,6 +46,7 @@ $string['eventcontentuploaded'] = 'Content uploaded'; $string['eventcontentviewed'] = 'Content viewed'; $string['errordeletingcontentfromcategory'] = 'Error deleting content from category {$a}.'; $string['errornofile'] = 'A compatible file is needed to create content.'; +$string['exit'] = 'Exit'; $string['deletecontent'] = 'Delete content'; $string['deletecontentconfirm'] = 'Are you sure you want to delete the content \'{$a->name}\' and all associated files? This action cannot be undone.'; $string['deletecontentconfirmlinked'] = 'The content will only be deleted from the content bank. Any places which currently link to it will be automatically updated to use a copy of the content instead.'; @@ -56,6 +56,7 @@ $string['file'] = 'Upload content'; $string['file_help'] = 'Files may be stored in the content bank for use in courses. Only files used by content types enabled on the site may be uploaded.'; $string['itemsfound'] = '{$a} items found'; $string['lastmodified'] = 'Last modified'; +$string['more'] = 'More'; $string['name'] = 'Content'; $string['nocontentavailable'] = 'No content available'; $string['nocontenttypes'] = 'No content types available'; @@ -91,3 +92,6 @@ $string['visibilitypref_help'] = 'Content you create in the content bank will us $string['visibilitysetpublic'] = 'Make public'; $string['visibilitysetunlisted'] = 'Make unlisted'; $string['visibilitytitleunlisted'] = '{$a} (Unlisted)'; + +// Deprecated since 4.0. +$string['close'] = 'Close'; diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index cf3eb335f9a..3a74976206f 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -78,3 +78,4 @@ loggedoff_help,core_message loggedoffdescription,core_message sendingvia,core_message sendingviawhen,core_message +close,core_contentbank \ No newline at end of file