Skip to content

Commit

Permalink
MDL-69092 mod_lti: Add pagination to toolconfigure.php.
Browse files Browse the repository at this point in the history
* Add local function to access subsets of proxies and types direct from DB.
* Add local function to access count of proxies and types direct from DB.
* Add new external function to get both proxies and types with pagination.
* Add new external function to get count of proxies and types.
* Implement pagination using page factory in JS.
* Added unit tests to cover new external functions.
* Add mod_lti behat generators and tests.
* Show first and last button in paging bar.
* Created helper class to assist with new functions.
  • Loading branch information
andrewmadden committed Nov 17, 2021
1 parent e11332c commit f435f31
Show file tree
Hide file tree
Showing 22 changed files with 1,007 additions and 70 deletions.
2 changes: 1 addition & 1 deletion mod/lti/amd/build/tool_configure_controller.min.js

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

2 changes: 1 addition & 1 deletion mod/lti/amd/build/tool_configure_controller.min.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions mod/lti/amd/build/tool_types_and_proxies.min.js

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

1 change: 1 addition & 0 deletions mod/lti/amd/build/tool_types_and_proxies.min.js.map

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

132 changes: 108 additions & 24 deletions mod/lti/amd/src/tool_configure_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.1
*/
define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/events', 'mod_lti/keys', 'mod_lti/tool_type',
'mod_lti/tool_proxy', 'core/str', 'core/config'],
function($, ajax, notification, templates, ltiEvents, KEYS, toolType, toolProxy, str, config) {
define(['jquery', 'core/ajax', 'core/paged_content_factory', 'core/notification', 'core/templates', 'mod_lti/events',
'mod_lti/keys', 'mod_lti/tool_types_and_proxies', 'mod_lti/tool_type', 'mod_lti/tool_proxy', 'core/str', 'core/config'],
function($, ajax,
pagedContentFactory, notification, templates, ltiEvents, KEYS,
toolTypesAndProxies, toolType, toolProxy, str, config) {

var SELECTORS = {
EXTERNAL_REGISTRATION_CONTAINER: '#external-registration-container',
Expand All @@ -34,6 +36,7 @@ define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/e
CARTRIDGE_REGISTRATION_CONTAINER: '#cartridge-registration-container',
CARTRIDGE_REGISTRATION_FORM: '#cartridge-registration-form',
ADD_TOOL_FORM: '#add-tool-form',
TOOL_CARD_CONTAINER: '#tool-card-container',
TOOL_LIST_CONTAINER: '#tool-list-container',
TOOL_CREATE_BUTTON: '#tool-create-button',
TOOL_CREATE_LTILEGACY_BUTTON: '#tool-createltilegacy-button',
Expand All @@ -52,6 +55,17 @@ define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/e
return $(SELECTORS.TOOL_LIST_CONTAINER);
};

/**
* Get the tool card container element.
*
* @method getToolCardContainer
* @private
* @return {Object} jQuery object
*/
const getToolCardContainer = function() {
return $(SELECTORS.TOOL_CARD_CONTAINER);
};

/**
* Get the external registration container element.
*
Expand Down Expand Up @@ -285,29 +299,99 @@ define(['jquery', 'core/ajax', 'core/notification', 'core/templates', 'mod_lti/e
* @private
*/
var reloadToolList = function() {
var promise = $.Deferred();
var container = getToolListContainer();
startLoading(container);

$.when(
toolType.query(),
toolProxy.query({'orphanedonly': true})
)
.done(function(types, proxies) {
templates.render('mod_lti/tool_list', {tools: types, proxies: proxies})
.done(function(html, js) {
container.empty();
container.append(html);
templates.runTemplateJS(js);
promise.resolve();
}).fail(promise.reject);
const cardContainer = getToolCardContainer();
const listContainer = getToolListContainer();
const limit = 60;
// Get initial data with zero limit and offset.
fetchToolCount().done(function(data) {
pagedContentFactory.createWithTotalAndLimit(
data.count,
limit,
function(pagesData) {
return pagesData.map(function(pageData) {
return fetchToolData(pageData.limit, pageData.offset)
.then(function(data) {
return renderToolData(data);
});
});
},
{
'showFirstLast': true
})
.done(function(html, js) {
// Add the paged content into the page.
templates.replaceNodeContents(cardContainer, html, js);
})
.fail(promise.reject);
.always(stopLoading(listContainer));
});
startLoading(listContainer);
};

promise.fail(notification.exception)
.always(function() {
stopLoading(container);
});
/**
* Fetch the count of tool type and proxy datasets.
*
* @return {*|void}
*/
const fetchToolCount = function() {
return toolTypesAndProxies.count({'orphanedonly': true})
.done(function(data) {
return data;
}).catch(function(error) {
// Add debug message, then return empty data.
notification.exception(error);
return {
'count': 0
};
});
};

/**
* Fetch the data for tool type and proxy cards.
*
* @param {number} limit Maximum number of datasets to get.
* @param {number} offset Offset count for fetching the data.
* @return {*|void}
*/
const fetchToolData = function(limit, offset) {
const args = {'orphanedonly': true};
// Only add limit and offset to args if they are integers and not null, otherwise defaults will be used.
if (limit !== null && !Number.isNaN(limit)) {
args.limit = limit;
}
if (offset !== null && !Number.isNaN(offset)) {
args.offset = offset;
}
return toolTypesAndProxies.query(args)
.done(function(data) {
return data;
}).catch(function(error) {
// Add debug message, then return empty data.
notification.exception(error);
return {
'types': [],
'proxies': [],
'limit': limit,
'offset': offset
};
});
};

/**
* Render Tool and Proxy cards from data.
*
* @param {Object} data Contains arrays of data objects to populate cards.
* @return {*}
*/
const renderToolData = function(data) {
const context = {
tools: data.types,
proxies: data.proxies,
};
return templates.render('mod_lti/tool_list', context)
.done(function(html, js) {
return {html, js};
}
);
};

/**
Expand Down
67 changes: 67 additions & 0 deletions mod/lti/amd/src/tool_types_and_proxies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Provides an interface for external tools in the Moodle server.
*
* @module mod_lti/tool_types_and_proxies
* @class tool_types_and_proxies
* @copyright 2020 Andrew Madden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 4.0
*/
import ajax from 'core/ajax';

/**
* Get a list of LTI tool types and tool proxies from Moodle for the given
* search args.
*
* See also:
* mod/lti/classes/external.php get_tool_types_and_proxies()
*
* @method query
* @public
* @param {Object} args Search parameters
* @return {Promise} Promise that will be resolved when the ajax call returns.
*/
export const query = (args) => {
const request = {
methodname: 'mod_lti_get_tool_types_and_proxies',
args: args || {}
};

return ajax.call([request])[0];
};

/**
* Get a count of LTI tool types and tool proxies from Moodle for the given
* search args.
*
* See also:
* mod/lti/classes/external.php get_tool_types_and_proxies_count()
*
* @method count
* @public
* @param {Object} args Search parameters
* @return {Promise} Promise that will be resolved when the ajax call returns.
*/
export const count = (args) => {
const request = {
methodname: 'mod_lti_get_tool_types_and_proxies_count',
args: args || {}
};

return ajax.call([request])[0];
};
Loading

0 comments on commit f435f31

Please sign in to comment.