From 5f59a4c0630dcef474ba22f33967624b3ea9939f Mon Sep 17 00:00:00 2001 From: Tom Dickman Date: Tue, 12 Mar 2019 14:26:51 +1100 Subject: [PATCH] MDL-64901 block_myoverview: admin settings to control available layouts Added an admin setting which allows the administrator to set the available layouts for users and which defaults to all layouts being available, or the card layout only if no layouts are enabled. If only one layout is enabled, the dropdown `nav-display-selector` template will not be displayed as it can no longer be utilised. If the user preference is set to a layout that is no longer available, this is ignored and the first available layout defaulted to. --- blocks/myoverview/amd/build/view.min.js | Bin 6423 -> 6422 bytes blocks/myoverview/amd/src/view.js | 2 +- blocks/myoverview/block_myoverview.php | 2 +- blocks/myoverview/classes/output/main.php | 91 +++++++++++++++--- .../myoverview/lang/en/block_myoverview.php | 2 + blocks/myoverview/lib.php | 2 +- blocks/myoverview/settings.php | 11 +++ blocks/myoverview/templates/main.mustache | 5 +- .../templates/nav-display-selector.mustache | 24 ++--- blocks/myoverview/version.php | 2 +- 10 files changed, 108 insertions(+), 33 deletions(-) diff --git a/blocks/myoverview/amd/build/view.min.js b/blocks/myoverview/amd/build/view.min.js index 90bf1953ad86a6e5aa13dec3885b2e665ca94bf3..e113a14a3557ce0b702fd1bda970d22339ffaaf5 100644 GIT binary patch delta 16 XcmbPkG|gy(7T;uFKJm>?d{?;uFscP% delta 14 VcmbPcG~H-}79V5rW-Y!qTmU7!1kV5f diff --git a/blocks/myoverview/amd/src/view.js b/blocks/myoverview/amd/src/view.js index 6a836464609..0dc37ba13e7 100644 --- a/blocks/myoverview/amd/src/view.js +++ b/blocks/myoverview/amd/src/view.js @@ -363,7 +363,7 @@ function( var filters = getFilterValues(root); var currentTemplate = ''; - if (filters.display == 'cards') { + if (filters.display == 'card') { currentTemplate = TEMPLATES.COURSES_CARDS; } else if (filters.display == 'list') { currentTemplate = TEMPLATES.COURSES_LIST; diff --git a/blocks/myoverview/block_myoverview.php b/blocks/myoverview/block_myoverview.php index 617771902fa..01744b96149 100644 --- a/blocks/myoverview/block_myoverview.php +++ b/blocks/myoverview/block_myoverview.php @@ -74,7 +74,7 @@ class block_myoverview extends block_base { } /** - * Allow the block to have a configuration page + * Allow the block to have a configuration page. * * @return boolean */ diff --git a/blocks/myoverview/classes/output/main.php b/blocks/myoverview/classes/output/main.php index 7ef8c386e72..8df2b478a59 100644 --- a/blocks/myoverview/classes/output/main.php +++ b/blocks/myoverview/classes/output/main.php @@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die(); use renderable; use renderer_base; use templatable; +use stdClass; require_once($CFG->dirroot . '/blocks/myoverview/lib.php'); @@ -39,40 +40,47 @@ require_once($CFG->dirroot . '/blocks/myoverview/lib.php'); class main implements renderable, templatable { /** - * Store the grouping preference + * Store the grouping preference. * * @var string String matching the grouping constants defined in myoverview/lib.php */ private $grouping; /** - * Store the sort preference + * Store the sort preference. * * @var string String matching the sort constants defined in myoverview/lib.php */ private $sort; /** - * Store the view preference + * Store the view preference. * * @var string String matching the view/display constants defined in myoverview/lib.php */ private $view; /** - * Store the paging preference + * Store the paging preference. * * @var string String matching the paging constants defined in myoverview/lib.php */ private $paging; /** - * Store the display categories config setting + * Store the display categories config setting. * * @var boolean */ private $displaycategories; + /** + * Store the configuration values for the myoverview block. + * + * @var array Array of available layouts matching view/display constants defined in myoverview/lib.php + */ + private $layouts; + /** * main constructor. * Initialize the user preferences @@ -80,34 +88,91 @@ class main implements renderable, templatable { * @param string $grouping Grouping user preference * @param string $sort Sort user preference * @param string $view Display user preference + * + * @throws \dml_exception */ public function __construct($grouping, $sort, $view, $paging) { $this->grouping = $grouping ? $grouping : BLOCK_MYOVERVIEW_GROUPING_ALL; $this->sort = $sort ? $sort : BLOCK_MYOVERVIEW_SORTING_TITLE; - $this->view = $view ? $view : BLOCK_MYOVERVIEW_VIEW_CARD; $this->paging = $paging ? $paging : BLOCK_MYOVERVIEW_PAGING_12; + $config = get_config('block_myoverview'); if (!$config->displaycategories) { $this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_OFF; } else { $this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_ON; } + + $this->set_available_layouts(); + $this->view = $view ? $view : reset($this->layouts); } + /** - * Get the user preferences as an array to figure out what has been selected + * Set the available layouts based on the config table settings, + * if none are available, defaults to the cards view. + * + * @throws \dml_exception + * + */ + public function set_available_layouts() { + + if ($config = get_config('block_myoverview', 'layouts')) { + $this->layouts = explode(',', $config); + } else { + $this->layouts = array(BLOCK_MYOVERVIEW_VIEW_CARD); + } + } + + /** + * Get the user preferences as an array to figure out what has been selected. * * @return array $preferences Array with the pref as key and value set to true */ public function get_preferences_as_booleans() { $preferences = []; - $preferences[$this->view] = true; $preferences[$this->sort] = true; $preferences[$this->grouping] = true; + // Only use the user view/display preference if it is in available layouts. + if (in_array($this->view, $this->layouts)) { + $preferences[$this->view] = true; + } else { + $preferences[reset($this->layouts)] = true; + } return $preferences; } + /** + * Format a layout into an object for export as a Context variable to template. + * + * @param string $layoutname + * + * @return \stdClass $layout an object representation of a layout + * @throws \coding_exception + */ + public function format_layout_for_export($layoutname) { + $layout = new stdClass(); + + $layout->id = $layoutname; + $layout->name = get_string($layoutname, 'block_myoverview'); + $layout->active = $this->view == $layoutname ? true : false; + $layout->arialabel = get_string('aria:' . $layoutname, 'block_myoverview'); + + return $layout; + } + + /** + * Get the available layouts formatted for export. + * + * @return array an array of objects representing available layouts + */ + public function get_formatted_available_layouts_for_export() { + + return array_map(array($this, 'format_layout_for_export'), $this->layouts); + + } + /** * Export this data so it can be used as the context for a mustache template. * @@ -118,16 +183,20 @@ class main implements renderable, templatable { $nocoursesurl = $output->image_url('courses', 'block_myoverview')->out(); + $preferences = $this->get_preferences_as_booleans(); + $availablelayouts = $this->get_formatted_available_layouts_for_export(); + $defaultvariables = [ 'nocoursesimg' => $nocoursesurl, 'grouping' => $this->grouping, 'sort' => $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc', - 'view' => $this->view, + // If the user preference display option is not available, default to first available layout. + 'view' => in_array($this->view, $this->layouts) ? $this->view : reset($this->layouts), 'paging' => $this->paging, + 'layouts' => $availablelayouts, 'displaycategories' => $this->displaycategories, + 'displaydropdown' => (count($availablelayouts) > 1) ? true : false, ]; - - $preferences = $this->get_preferences_as_booleans(); return array_merge($defaultvariables, $preferences); } diff --git a/blocks/myoverview/lang/en/block_myoverview.php b/blocks/myoverview/lang/en/block_myoverview.php index 79a0b786dd6..637368ba2dc 100644 --- a/blocks/myoverview/lang/en/block_myoverview.php +++ b/blocks/myoverview/lang/en/block_myoverview.php @@ -55,6 +55,8 @@ $string['favourites'] = 'Starred'; $string['future'] = 'Future'; $string['inprogress'] = 'In progress'; $string['lastaccessed'] = 'Last accessed'; +$string['layouts'] = 'Available Layouts'; +$string['layouts_help'] = 'The layouts which are available for selection by users'; $string['list'] = 'List'; $string['myoverview:myaddinstance'] = 'Add a new course overview block to Dashboard'; $string['past'] = 'Past'; diff --git a/blocks/myoverview/lib.php b/blocks/myoverview/lib.php index d5bc9e23f19..652177f0fa3 100644 --- a/blocks/myoverview/lib.php +++ b/blocks/myoverview/lib.php @@ -44,7 +44,7 @@ define('BLOCK_MYOVERVIEW_SORTING_LASTACCESSED', 'lastaccessed'); /** * Constants for the user preferences view options */ -define('BLOCK_MYOVERVIEW_VIEW_CARD', 'cards'); +define('BLOCK_MYOVERVIEW_VIEW_CARD', 'card'); define('BLOCK_MYOVERVIEW_VIEW_LIST', 'list'); define('BLOCK_MYOVERVIEW_VIEW_SUMMARY', 'summary'); diff --git a/blocks/myoverview/settings.php b/blocks/myoverview/settings.php index f3b2edf8a56..b17ea4e0f6e 100644 --- a/blocks/myoverview/settings.php +++ b/blocks/myoverview/settings.php @@ -34,4 +34,15 @@ if ($ADMIN->fulltree) { get_string('displaycategories_help', 'block_myoverview'), 1)); + $choices = array(BLOCK_MYOVERVIEW_VIEW_CARD => get_string('card', 'block_myoverview'), + BLOCK_MYOVERVIEW_VIEW_LIST => get_string('list', 'block_myoverview'), + BLOCK_MYOVERVIEW_VIEW_SUMMARY => get_string('summary', 'block_myoverview')); + + $settings->add(new admin_setting_configmulticheckbox( + 'block_myoverview/layouts', + get_string('layouts', 'block_myoverview'), + get_string('layouts_help', 'block_myoverview'), + $choices, + $choices)); + } diff --git a/blocks/myoverview/templates/main.mustache b/blocks/myoverview/templates/main.mustache index 059263cf7ba..d575234de21 100644 --- a/blocks/myoverview/templates/main.mustache +++ b/blocks/myoverview/templates/main.mustache @@ -29,8 +29,9 @@ {{> block_myoverview/nav-grouping-selector }} {{> block_myoverview/nav-sort-selector }} - - {{> block_myoverview/nav-display-selector }} + {{#displaydropdown}} + {{> block_myoverview/nav-display-selector }} + {{/displaydropdown}}
diff --git a/blocks/myoverview/templates/nav-display-selector.mustache b/blocks/myoverview/templates/nav-display-selector.mustache index ce8555b1a04..8a1cb325a30 100644 --- a/blocks/myoverview/templates/nav-display-selector.mustache +++ b/blocks/myoverview/templates/nav-display-selector.mustache @@ -31,26 +31,18 @@ aria-label="{{#str}} aria:displaydropdown, block_myoverview {{/str}}"> {{#pix}} a/view_icon_active {{/pix}} - {{#cards}}{{#str}} card, block_myoverview {{/str}}{{/cards}} + {{#card}}{{#str}} card, block_myoverview {{/str}}{{/card}} {{#list}}{{#str}} list, block_myoverview {{/str}}{{/list}} {{#summary}}{{#str}} summary, block_myoverview {{/str}}{{/summary}}
diff --git a/blocks/myoverview/version.php b/blocks/myoverview/version.php index bc9f9836674..25b71946d99 100644 --- a/blocks/myoverview/version.php +++ b/blocks/myoverview/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2019052000; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2019060400; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2019051100; // Requires this Moodle version. $plugin->component = 'block_myoverview'; // Full name of the plugin (used for diagnostics). -- 2.43.0