Skip to content

Commit 5f59a4c

Browse files
author
Tom Dickman
committed
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.
1 parent 29c3951 commit 5f59a4c

File tree

10 files changed

+109
-34
lines changed

10 files changed

+109
-34
lines changed

blocks/myoverview/amd/build/view.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/myoverview/amd/src/view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ function(
363363
var filters = getFilterValues(root);
364364

365365
var currentTemplate = '';
366-
if (filters.display == 'cards') {
366+
if (filters.display == 'card') {
367367
currentTemplate = TEMPLATES.COURSES_CARDS;
368368
} else if (filters.display == 'list') {
369369
currentTemplate = TEMPLATES.COURSES_LIST;

blocks/myoverview/block_myoverview.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function applicable_formats() {
7474
}
7575

7676
/**
77-
* Allow the block to have a configuration page
77+
* Allow the block to have a configuration page.
7878
*
7979
* @return boolean
8080
*/

blocks/myoverview/classes/output/main.php

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use renderable;
2828
use renderer_base;
2929
use templatable;
30+
use stdClass;
3031

3132
require_once($CFG->dirroot . '/blocks/myoverview/lib.php');
3233

@@ -39,75 +40,139 @@
3940
class main implements renderable, templatable {
4041

4142
/**
42-
* Store the grouping preference
43+
* Store the grouping preference.
4344
*
4445
* @var string String matching the grouping constants defined in myoverview/lib.php
4546
*/
4647
private $grouping;
4748

4849
/**
49-
* Store the sort preference
50+
* Store the sort preference.
5051
*
5152
* @var string String matching the sort constants defined in myoverview/lib.php
5253
*/
5354
private $sort;
5455

5556
/**
56-
* Store the view preference
57+
* Store the view preference.
5758
*
5859
* @var string String matching the view/display constants defined in myoverview/lib.php
5960
*/
6061
private $view;
6162

6263
/**
63-
* Store the paging preference
64+
* Store the paging preference.
6465
*
6566
* @var string String matching the paging constants defined in myoverview/lib.php
6667
*/
6768
private $paging;
6869

6970
/**
70-
* Store the display categories config setting
71+
* Store the display categories config setting.
7172
*
7273
* @var boolean
7374
*/
7475
private $displaycategories;
7576

77+
/**
78+
* Store the configuration values for the myoverview block.
79+
*
80+
* @var array Array of available layouts matching view/display constants defined in myoverview/lib.php
81+
*/
82+
private $layouts;
83+
7684
/**
7785
* main constructor.
7886
* Initialize the user preferences
7987
*
8088
* @param string $grouping Grouping user preference
8189
* @param string $sort Sort user preference
8290
* @param string $view Display user preference
91+
*
92+
* @throws \dml_exception
8393
*/
8494
public function __construct($grouping, $sort, $view, $paging) {
8595
$this->grouping = $grouping ? $grouping : BLOCK_MYOVERVIEW_GROUPING_ALL;
8696
$this->sort = $sort ? $sort : BLOCK_MYOVERVIEW_SORTING_TITLE;
87-
$this->view = $view ? $view : BLOCK_MYOVERVIEW_VIEW_CARD;
8897
$this->paging = $paging ? $paging : BLOCK_MYOVERVIEW_PAGING_12;
98+
8999
$config = get_config('block_myoverview');
90100
if (!$config->displaycategories) {
91101
$this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_OFF;
92102
} else {
93103
$this->displaycategories = BLOCK_MYOVERVIEW_DISPLAY_CATEGORIES_ON;
94104
}
105+
106+
$this->set_available_layouts();
107+
$this->view = $view ? $view : reset($this->layouts);
95108
}
96109

110+
97111
/**
98-
* Get the user preferences as an array to figure out what has been selected
112+
* Set the available layouts based on the config table settings,
113+
* if none are available, defaults to the cards view.
114+
*
115+
* @throws \dml_exception
116+
*
117+
*/
118+
public function set_available_layouts() {
119+
120+
if ($config = get_config('block_myoverview', 'layouts')) {
121+
$this->layouts = explode(',', $config);
122+
} else {
123+
$this->layouts = array(BLOCK_MYOVERVIEW_VIEW_CARD);
124+
}
125+
}
126+
127+
/**
128+
* Get the user preferences as an array to figure out what has been selected.
99129
*
100130
* @return array $preferences Array with the pref as key and value set to true
101131
*/
102132
public function get_preferences_as_booleans() {
103133
$preferences = [];
104-
$preferences[$this->view] = true;
105134
$preferences[$this->sort] = true;
106135
$preferences[$this->grouping] = true;
136+
// Only use the user view/display preference if it is in available layouts.
137+
if (in_array($this->view, $this->layouts)) {
138+
$preferences[$this->view] = true;
139+
} else {
140+
$preferences[reset($this->layouts)] = true;
141+
}
107142

108143
return $preferences;
109144
}
110145

146+
/**
147+
* Format a layout into an object for export as a Context variable to template.
148+
*
149+
* @param string $layoutname
150+
*
151+
* @return \stdClass $layout an object representation of a layout
152+
* @throws \coding_exception
153+
*/
154+
public function format_layout_for_export($layoutname) {
155+
$layout = new stdClass();
156+
157+
$layout->id = $layoutname;
158+
$layout->name = get_string($layoutname, 'block_myoverview');
159+
$layout->active = $this->view == $layoutname ? true : false;
160+
$layout->arialabel = get_string('aria:' . $layoutname, 'block_myoverview');
161+
162+
return $layout;
163+
}
164+
165+
/**
166+
* Get the available layouts formatted for export.
167+
*
168+
* @return array an array of objects representing available layouts
169+
*/
170+
public function get_formatted_available_layouts_for_export() {
171+
172+
return array_map(array($this, 'format_layout_for_export'), $this->layouts);
173+
174+
}
175+
111176
/**
112177
* Export this data so it can be used as the context for a mustache template.
113178
*
@@ -118,16 +183,20 @@ public function export_for_template(renderer_base $output) {
118183

119184
$nocoursesurl = $output->image_url('courses', 'block_myoverview')->out();
120185

186+
$preferences = $this->get_preferences_as_booleans();
187+
$availablelayouts = $this->get_formatted_available_layouts_for_export();
188+
121189
$defaultvariables = [
122190
'nocoursesimg' => $nocoursesurl,
123191
'grouping' => $this->grouping,
124192
'sort' => $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc',
125-
'view' => $this->view,
193+
// If the user preference display option is not available, default to first available layout.
194+
'view' => in_array($this->view, $this->layouts) ? $this->view : reset($this->layouts),
126195
'paging' => $this->paging,
196+
'layouts' => $availablelayouts,
127197
'displaycategories' => $this->displaycategories,
198+
'displaydropdown' => (count($availablelayouts) > 1) ? true : false,
128199
];
129-
130-
$preferences = $this->get_preferences_as_booleans();
131200
return array_merge($defaultvariables, $preferences);
132201

133202
}

blocks/myoverview/lang/en/block_myoverview.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
$string['future'] = 'Future';
5656
$string['inprogress'] = 'In progress';
5757
$string['lastaccessed'] = 'Last accessed';
58+
$string['layouts'] = 'Available Layouts';
59+
$string['layouts_help'] = 'The layouts which are available for selection by users';
5860
$string['list'] = 'List';
5961
$string['myoverview:myaddinstance'] = 'Add a new course overview block to Dashboard';
6062
$string['past'] = 'Past';

blocks/myoverview/lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
/**
4545
* Constants for the user preferences view options
4646
*/
47-
define('BLOCK_MYOVERVIEW_VIEW_CARD', 'cards');
47+
define('BLOCK_MYOVERVIEW_VIEW_CARD', 'card');
4848
define('BLOCK_MYOVERVIEW_VIEW_LIST', 'list');
4949
define('BLOCK_MYOVERVIEW_VIEW_SUMMARY', 'summary');
5050

0 commit comments

Comments
 (0)