Skip to content

Commit

Permalink
MDL-63793 block_myoverview: Persist the user's paging limit preference
Browse files Browse the repository at this point in the history
* providers for paging preferences
* Moved the user pref persistence to the factory
* Added client defined namespace in config
* Define custom client events in the client instead of passing to the
  factory
  • Loading branch information
Peter committed Nov 19, 2018
1 parent 208950c commit 11988d7
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 27 deletions.
2 changes: 1 addition & 1 deletion blocks/myoverview/amd/build/view.min.js

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

59 changes: 53 additions & 6 deletions blocks/myoverview/amd/src/view.js
Expand Up @@ -31,7 +31,8 @@ define(
'core/notification',
'core/templates',
'core_course/events',
'block_myoverview/selectors'
'block_myoverview/selectors',
'core/paged_content_events',
],
function(
$,
Expand All @@ -42,7 +43,8 @@ function(
Notification,
Templates,
CourseEvents,
Selectors
Selectors,
PagedContentEvents
) {

var SELECTORS = {
Expand Down Expand Up @@ -75,6 +77,8 @@ function(

var lastLimit = 0;

var namespace = null;

/**
* Get filter values from DOM.
*
Expand All @@ -95,6 +99,7 @@ function(
var DEFAULT_PAGED_CONTENT_CONFIG = {
ignoreControlWhileLoading: true,
controlPlacementBottom: true,
persistentLimitKey: 'block_myoverview_user_paging_preference'
};

/**
Expand Down Expand Up @@ -377,17 +382,58 @@ function(
}
};

/**
* Return the callback to be passed to the subscribe event
*
* @param {Number} limit The paged limit that is passed through the event
*/
var setLimit = function(limit) {
this.find(Selectors.courseView.region).attr('data-paging', limit);
};

/**
* Intialise the paged list and cards views on page load.
* Returns an array of paged contents that we would like to handle here
*
* @param {object} root The root element for the courses view
* @param {string} namespace The namespace for all the events attached
*/
var registerPagedEventHandlers = function(root, namespace) {
var event = namespace + PagedContentEvents.SET_ITEMS_PER_PAGE_LIMIT;
PubSub.subscribe(event, setLimit.bind(root));
};

/**
* Intialise the courses list and cards views on page load.
*
* @param {object} root The root element for the courses view.
* @param {object} content The content element for the courses view.
*/
var initializePagedContent = function(root) {
namespace = "block_myoverview_" + root.attr('id') + "_" + Math.random();

var itemsPerPage = NUMCOURSES_PERPAGE;
var pagingLimit = parseInt(root.find(Selectors.courseView.region).attr('data-paging'), 10);
if (pagingLimit) {
itemsPerPage = NUMCOURSES_PERPAGE.map(function(value) {
var active = false;
if (value == pagingLimit) {
active = true;
}

return {
value: value,
active: active
};
});
}

var filters = getFilterValues(root);
var config = $.extend({}, DEFAULT_PAGED_CONTENT_CONFIG);
config.eventNamespace = namespace;

var pagedContentPromise = PagedContentFactory.createWithLimit(
NUMCOURSES_PERPAGE,
itemsPerPage,
function(pagesData, actions) {
var promises = [];

Expand Down Expand Up @@ -471,10 +517,11 @@ function(

return promises;
},
DEFAULT_PAGED_CONTENT_CONFIG
config
);

pagedContentPromise.then(function(html, js) {
registerPagedEventHandlers(root, namespace);
return Templates.replaceNodeContents(root.find(Selectors.courseView.region), html, js);
}).catch(Notification.exception);
};
Expand Down Expand Up @@ -556,12 +603,12 @@ function(
lastPage = 0;
courseOffset = 0;

initializePagedContent(root);

if (!root.attr('data-init')) {
registerEventListeners(root);
root.attr('data-init', true);
}

initializePagedContent(root);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion blocks/myoverview/block_myoverview.php
Expand Up @@ -52,8 +52,9 @@ public function get_content() {
$group = get_user_preferences('block_myoverview_user_grouping_preference');
$sort = get_user_preferences('block_myoverview_user_sort_preference');
$view = get_user_preferences('block_myoverview_user_view_preference');
$paging = get_user_preferences('block_myoverview_user_paging_preference');

$renderable = new \block_myoverview\output\main($group, $sort, $view);
$renderable = new \block_myoverview\output\main($group, $sort, $view, $paging);
$renderer = $this->page->get_renderer('block_myoverview');

$this->content = new stdClass();
Expand Down
13 changes: 11 additions & 2 deletions blocks/myoverview/classes/output/main.php
Expand Up @@ -59,6 +59,13 @@ class main implements renderable, templatable {
*/
private $view;

/**
* Store the paging preference
*
* @var string String matching the paging constants defined in myoverview/lib.php
*/
private $paging;

/**
* main constructor.
* Initialize the user preferences
Expand All @@ -67,10 +74,11 @@ class main implements renderable, templatable {
* @param string $sort Sort user preference
* @param string $view Display user preference
*/
public function __construct($grouping, $sort, $view) {
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;
}

/**
Expand Down Expand Up @@ -101,7 +109,8 @@ public function export_for_template(renderer_base $output) {
'nocoursesimg' => $nocoursesurl,
'grouping' => $this->grouping,
'sort' => $this->sort == BLOCK_MYOVERVIEW_SORTING_TITLE ? 'fullname' : 'ul.timeaccess desc',
'view' => $this->view
'view' => $this->view,
'paging' => $this->paging
];

$preferences = $this->get_preferences_as_booleans();
Expand Down
10 changes: 10 additions & 0 deletions blocks/myoverview/classes/privacy/provider.php
Expand Up @@ -49,6 +49,8 @@ public static function get_metadata(collection $collection) : collection {
$collection->add_user_preference('block_myoverview_user_view_preference', 'privacy:metadata:overviewviewpreference');
$collection->add_user_preference('block_myoverview_user_grouping_preference',
'privacy:metadata:overviewgroupingpreference');
$collection->add_user_preference('block_myoverview_user_paging_preference',
'privacy:metadata:overviewpagingpreference');
return $collection;
}
/**
Expand Down Expand Up @@ -94,5 +96,13 @@ public static function export_user_preferences(int $userid) {
);
}
}

$preference = get_user_preferences('block_myoverview_user_paging_preference', null, $userid);
if (isset($preference)) {
\core_privacy\local\request\writer::export_user_preference('block_myoverview',
'block_myoverview_user_paging_preference',
$preference,
get_string('privacy:metadata:overviewpagingpreference', 'block_myoverview'));
}
}
}
1 change: 1 addition & 0 deletions blocks/myoverview/lang/en/block_myoverview.php
Expand Up @@ -60,6 +60,7 @@
$string['privacy:metadata:overviewsortpreference'] = 'The Course overview block sort preference.';
$string['privacy:metadata:overviewviewpreference'] = 'The Course overview block view preference.';
$string['privacy:metadata:overviewgroupingpreference'] = 'The Course overview block grouping preference.';
$string['privacy:metadata:overviewpagingpreference'] = 'The Course overview block paging preference.';
$string['removefromfavourites'] = 'Unstar this course';
$string['summary'] = 'Summary';
$string['title'] = 'Title';
Expand Down
18 changes: 18 additions & 0 deletions blocks/myoverview/lib.php
Expand Up @@ -48,6 +48,13 @@
define('BLOCK_MYOVERVIEW_VIEW_LIST', 'list');
define('BLOCK_MYOVERVIEW_VIEW_SUMMARY', 'summary');

/**
* Constants for the user paging preferences
*/
define('BLOCK_MYOVERVIEW_PAGING_12', 12);
define('BLOCK_MYOVERVIEW_PAGING_24', 24);
define('BLOCK_MYOVERVIEW_PAGING_48', 48);

/**
* Get the current user preferences that are available
*
Expand Down Expand Up @@ -95,5 +102,16 @@ function block_myoverview_user_preferences() {
'default' => 'none'
);

$preferences['block_myoverview_user_paging_preference'] = array(
'null' => NULL_NOT_ALLOWED,
'default' => BLOCK_MYOVERVIEW_PAGING_12,
'type' => PARAM_INT,
'choices' => array(
BLOCK_MYOVERVIEW_PAGING_12,
BLOCK_MYOVERVIEW_PAGING_24,
BLOCK_MYOVERVIEW_PAGING_48
)
);

return $preferences;
}

0 comments on commit 11988d7

Please sign in to comment.