Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions www/addons/mod/data/controllers/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

angular.module('mm.addons.mod_data')

/**
* Data entry controller.
*
* @module mm.addons.mod_data
* @ngdoc controller
* @name mmaModDataEntryCtrl
*/
.controller('mmaModDataEntryCtrl', function($scope, $stateParams, $mmaModData, mmaModDataComponent, $mmCourse, $q,
$mmText, $translate, $mmUtil, $mmSite, $mmaModDataHelper, $mmGroups, $ionicScrollDelegate) {

var module = $stateParams.module || {},
courseId = $stateParams.courseid,
entryId = $stateParams.entryid || false,
page = $stateParams.page || false,
data,
scrollView;

$scope.title = module.name;
$scope.description = module.description;
$scope.moduleUrl = module.url;
$scope.moduleName = $mmCourse.translateModuleName('data');
$scope.component = mmaModDataComponent;
$scope.databaseLoaded = false;
$scope.selectedGroup = $stateParams.group || 0;

function fetchEntryData(refresh) {
return $mmaModData.getDatabase(courseId, module.id).then(function(databaseData) {
data = databaseData;

$scope.title = data.name || $scope.title;
$scope.description = data.intro || $scope.description;
$scope.data = databaseData;

$scope.database = data;

return setEntryIdFromPage(data.id, page, $scope.selectedGroup).then(function() {
return $mmaModData.getDatabaseAccessInformation(data.id);
});
}).then(function(accessData) {
return $mmGroups.getActivityGroupInfo(data.coursemodule, accessData.canmanageentries).then(function(groupInfo) {
$scope.groupInfo = groupInfo;

// Check selected group is accessible.
if (groupInfo && groupInfo.groups && groupInfo.groups.length > 0) {
var found = false;
for (var x in groupInfo.groups) {
if (groupInfo.groups[x].id == $scope.selectedGroup) {
found = true;
break;
}
}
if (!found) {
$scope.selectedGroup = groupInfo.groups[0].id;
}
}

return $mmaModData.getEntry(data.id, entryId);
});
}).then(function(entry) {
$scope.cssTemplate = $mmaModDataHelper.prefixCSS(data.csstemplate, '.mma-data-entries-' + data.id);
$scope.entryContents = entry.entryviewcontents;

return $mmaModDataHelper.getPageInfoByEntry(data.id, entryId, $scope.selectedGroup).then(function(result) {
$scope.previousId = result.previousId;
$scope.nextId = result.nextId;
});
}).catch(function(message) {
if (!refresh) {
// Some call failed, retry without using cache since it might be a new activity.
return refreshAllData();
}

$mmUtil.showErrorModalDefault(message, 'mm.course.errorgetmodule', true);
return $q.reject();
}).finally(function() {
scrollTop();
$scope.databaseLoaded = true;
});
}

// Set group to see the database.
$scope.setGroup = function(groupId) {
$scope.selectedGroup = groupId;
$scope.databaseLoaded = false;

return setEntryIdFromPage(data.id, 0, $scope.selectedGroup).then(function() {
return fetchEntryData();
});
};

// Go to selected entry without changing state.
$scope.gotoEntry = function(entry) {
entryId = entry;
page = false;
$scope.databaseLoaded = false;
return fetchEntryData();

};

// Convenience function to refresh all the data.
function refreshAllData() {
var promises = [];

promises.push($mmaModData.invalidateDatabaseData(courseId));
if (data) {
promises.push($mmaModData.invalidateEntryData(data.id, entryId));
promises.push($mmGroups.invalidateActivityGroupInfo(data.coursemodule));
promises.push($mmaModData.invalidateEntriesData(data.id));
}

return $q.all(promises).finally(function() {
return fetchEntryData(true);
});
}

// Convenience function to translate page number to entry identifier.
function setEntryIdFromPage(dataId, pageNumber, group) {
if (pageNumber !== false) {
return $mmaModDataHelper.getPageInfoByPage(dataId, pageNumber, group).then(function(result) {
entryId = result.entryId;
page = false;
});
}

return $q.when();
}

fetchEntryData();

// Scroll to top.
function scrollTop() {
if (!scrollView) {
scrollView = $ionicScrollDelegate.$getByHandle('mmaModDataEntryScroll');
}
scrollView && scrollView.scrollTop && scrollView.scrollTop();
}

// Context Menu Description action.
$scope.expandDescription = function() {
$mmText.expandText($translate.instant('mm.core.description'), $scope.description, false, mmaModDataComponent, module.id);
};

// Pull to refresh.
$scope.refreshDatabase = function() {
if ($scope.databaseLoaded) {
return refreshAllData().finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
}
};

// Opens search.
$scope.gotoSearch = function() {
$mmUtil.openInApp($mmSite.getURL() + '/mod/data/view.php?mode=asearch&d=' + data.id);
};
});
3 changes: 0 additions & 3 deletions www/addons/mod/data/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ angular.module('mm.addons.mod_data')
$scope.refreshIcon = 'spinner';
$scope.syncIcon = 'spinner';
$scope.component = mmaModDataComponent;
$scope.componentId = module.id;
$scope.databaseLoaded = false;
$scope.selectedGroup = $stateParams.group || 0;

Expand Down Expand Up @@ -135,8 +134,6 @@ angular.module('mm.addons.mod_data')
});
};



// Convenience function to refresh all the data.
function refreshAllData(sync, showErrors) {
var promises = [];
Expand Down
21 changes: 20 additions & 1 deletion www/addons/mod/data/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ angular.module('mm.addons.mod_data', ['mm.core'])
templateUrl: 'addons/mod/data/templates/index.html'
}
}
})

.state('site.mod_data-entry', {
url: '/mod_data-entry',
params: {
module: null,
moduleid: null, // Redundant parameter to fix a problem passing object as parameters. To be fixed in MOBILE-1370.
courseid: null,
entryid: null,
page: null,
group: null
},
views: {
'site': {
controller: 'mmaModDataEntryCtrl',
templateUrl: 'addons/mod/data/templates/entry.html'
}
}
});

})
Expand All @@ -41,4 +59,5 @@ angular.module('mm.addons.mod_data', ['mm.core'])
$mmCourseDelegateProvider.registerContentHandler('mmaModData', 'data', '$mmaModDataHandlers.courseContent');
$mmCoursePrefetchDelegateProvider.registerPrefetchHandler('mmaModData', 'data', '$mmaModDataPrefetchHandler');
$mmContentLinksDelegateProvider.registerLinkHandler('mmaModData:index', '$mmaModDataHandlers.indexLinksHandler');
});
$mmContentLinksDelegateProvider.registerLinkHandler('mmaModData:entry', '$mmaModDataHandlers.showEntryLinksHandler');
});
83 changes: 61 additions & 22 deletions www/addons/mod/data/services/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ angular.module('mm.addons.mod_data')
return getEntriesPrefixCacheKey(dataId) + groupId;
}

/**
* Get cache key for database entry data WS calls.
*
* @param {Number} dataId Data ID for caching purposes.
* @param {Number} entryId Entry ID.
* @return {String} Cache key.
*/
function getEntryCacheKey(dataId, entryId) {
return getDatabaseDataPrefixCacheKey(dataId) + ':entry:' + entryId;
}

/**
* Get cache key for database fields data WS calls.
*
Expand Down Expand Up @@ -297,7 +308,10 @@ angular.module('mm.addons.mod_data')
return $mmSitesManager.getSite(siteId).then(function(site) {
var params = {
databaseid: dataId,
returncontents: 1
returncontents: 1,
page: page || 0,
perpage: perPage || 0,
groupid: groupId || 0
},
preSets = {
cacheKey: getEntriesCacheKey(dataId, groupId)
Expand All @@ -307,22 +321,10 @@ angular.module('mm.addons.mod_data')
params.sort = sort;
}

if (order) {
if (typeof order !== "undefined") {
params.order = order;
}

if (page) {
params.page = page;
}

if (perPage) {
params.perpage = perPage;
}

if (typeof groupId !== "undefined") {
params.groupid = groupId;
}

if (forceCache) {
preSets.omitExpires = true;
} else if (ignoreCache) {
Expand Down Expand Up @@ -350,6 +352,48 @@ angular.module('mm.addons.mod_data')
});
};

/**
* Get an entry of the database activity.
*
* @module mm.addons.mod_data
* @ngdoc method
* @name $mmaModData#getEntry
* @param {Number} dataId Data ID for caching purposes.
* @param {Number} entryId Entry ID.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved when the database entry is retrieved.
*/
self.getEntry = function(dataId, entryId, siteId) {
return $mmSitesManager.getSite(siteId).then(function(site) {
var params = {
entryid: entryId,
returncontents: 1
},
preSets = {
cacheKey: getEntryCacheKey(dataId, entryId)
};

return site.read('mod_data_get_entry', params, preSets);
});
};

/**
* Invalidates database entry data.
*
* @module mm.addons.mod_data
* @ngdoc method
* @name $mmaModData#invalidateEntryData
* @param {Number} dataId Data ID for caching purposes.
* @param {Number} entryId Entry ID.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved when the data is invalidated.
*/
self.invalidateEntryData = function(dataId, entryId, siteId) {
return $mmSitesManager.getSite(siteId).then(function(site) {
return site.invalidateWsCacheForKey(getEntryCacheKey(dataId, entryId));
});
};

/**
* Get the list of configured fields for the given database.
*
Expand Down Expand Up @@ -412,11 +456,6 @@ angular.module('mm.addons.mod_data')
*/
self.fetchAllEntries = function(dataId, groupId, sort, order, perPage, forceCache, ignoreCache, siteId) {
siteId = siteId || $mmSite.getId();

if (typeof perPage == 'undefined') {
perPage = 10;
}

return fetchEntriesRecursive(dataId, groupId, sort, order, perPage, forceCache, ignoreCache, [], 0, siteId);
};

Expand All @@ -440,9 +479,9 @@ angular.module('mm.addons.mod_data')
entries = entries.concat(result.entries);

var canLoadMore = ((page + 1) * perPage) < result.totalcount;
if (canLoadMore) {
return fetchEntriesRecursive(dataId, groupId, sort, order, perPage, forceCache, ignoreCache, entries,
page + 1, siteId);
if (perPage && canLoadMore) {
return fetchEntriesRecursive(dataId, groupId, sort, order, perPage, forceCache, ignoreCache, entries, page + 1,
siteId);
}
return entries;
});
Expand Down
Loading