From 6c86998f7f16223bfa8fae0060b2ddcc4b6bfa44 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 26 Jul 2019 09:14:19 +0200 Subject: [PATCH] MOBILE-2816 group: Use canaccessallgroups --- .../mod/assign/components/index/index.ts | 3 +- .../components/submission/submission.ts | 4 +- .../pages/submission-list/submission-list.ts | 2 +- src/addon/mod/assign/providers/helper.ts | 4 +- .../mod/assign/providers/prefetch-handler.ts | 13 ++-- src/addon/mod/chat/pages/sessions/sessions.ts | 9 +-- src/addon/mod/data/components/index/index.ts | 11 +--- src/addon/mod/data/pages/edit/edit.ts | 11 +--- src/addon/mod/data/pages/entry/entry.ts | 11 +--- .../mod/feedback/components/index/index.ts | 2 +- .../pages/nonrespondents/nonrespondents.ts | 1 + .../feedback/pages/respondents/respondents.ts | 1 + .../pages/new-discussion/new-discussion.ts | 6 +- src/addon/mod/forum/providers/forum.ts | 4 +- .../mod/forum/providers/prefetch-handler.ts | 4 +- src/addon/mod/forum/providers/sync.ts | 4 +- .../mod/lesson/components/index/index.ts | 2 +- .../mod/lesson/providers/prefetch-handler.ts | 5 +- src/addon/mod/wiki/components/index/index.ts | 60 ++++++++----------- .../mod/wiki/providers/prefetch-handler.ts | 7 +-- .../index/addon-mod-workshop-index.html | 2 +- .../mod/workshop/components/index/index.ts | 14 +---- src/providers/groups.ts | 53 +++++++++++++--- upgrade.txt | 4 ++ 24 files changed, 110 insertions(+), 127 deletions(-) diff --git a/src/addon/mod/assign/components/index/index.ts b/src/addon/mod/assign/components/index/index.ts index 013c6b378..26d57888e 100644 --- a/src/addon/mod/assign/components/index/index.ts +++ b/src/addon/mod/assign/components/index/index.ts @@ -204,8 +204,7 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo this.showNumbers = groupInfo.groups.length == 0 || this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.5'); - return this.setGroup(this.group || (groupInfo.groups && groupInfo.groups[0] && groupInfo.groups[0].id) || - 0); + return this.setGroup(this.groupsProvider.validateGroupId(this.group, groupInfo)); }); } diff --git a/src/addon/mod/assign/components/submission/submission.ts b/src/addon/mod/assign/components/submission/submission.ts index 9d5fd32d2..49489a9a5 100644 --- a/src/addon/mod/assign/components/submission/submission.ts +++ b/src/addon/mod/assign/components/submission/submission.ts @@ -898,8 +898,8 @@ export class AddonModAssignSubmissionComponent implements OnInit, OnDestroy { if (this.assign.teamsubmission) { if (response.lastattempt.submissiongroup) { // Get the name of the group. - promises.push(this.groupsProvider.getActivityAllowedGroups(this.assign.cmid).then((groups) => { - groups.forEach((group) => { + promises.push(this.groupsProvider.getActivityAllowedGroups(this.assign.cmid).then((result) => { + result.groups.forEach((group) => { if (group.id == response.lastattempt.submissiongroup) { this.lastAttempt.submissiongroupname = group.name; } diff --git a/src/addon/mod/assign/pages/submission-list/submission-list.ts b/src/addon/mod/assign/pages/submission-list/submission-list.ts index abf2cd778..a8bcf8c6d 100644 --- a/src/addon/mod/assign/pages/submission-list/submission-list.ts +++ b/src/addon/mod/assign/pages/submission-list/submission-list.ts @@ -128,7 +128,7 @@ export class AddonModAssignSubmissionListPage implements OnInit, OnDestroy { return this.groupsProvider.getActivityGroupInfo(this.assign.cmid, false).then((groupInfo) => { this.groupInfo = groupInfo; - return this.setGroup(this.groupId || (groupInfo.groups && groupInfo.groups[0] && groupInfo.groups[0].id) || 0); + return this.setGroup(this.groupsProvider.validateGroupId(this.groupId, groupInfo)); }); }).catch((error) => { this.domUtils.showErrorModalDefault(error, 'Error getting assigment data.'); diff --git a/src/addon/mod/assign/providers/helper.ts b/src/addon/mod/assign/providers/helper.ts index f0c939766..5f8f5df21 100644 --- a/src/addon/mod/assign/providers/helper.ts +++ b/src/addon/mod/assign/providers/helper.ts @@ -165,11 +165,11 @@ export class AddonModAssignHelperProvider { } // If no participants returned and all groups specified, get participants by groups. - return this.groupsProvider.getActivityAllowedGroupsIfEnabled(assign.cmid, undefined, siteId).then((userGroups) => { + return this.groupsProvider.getActivityGroupInfo(assign.cmid, false, undefined, siteId).then((info) => { const promises = [], participants = {}; - userGroups.forEach((userGroup) => { + info.groups.forEach((userGroup) => { promises.push(this.assignProvider.listParticipants(assign.id, userGroup.id, ignoreCache, siteId) .then((parts) => { // Do not get repeated users. diff --git a/src/addon/mod/assign/providers/prefetch-handler.ts b/src/addon/mod/assign/providers/prefetch-handler.ts index a5f1e6fc9..9c76f2ec8 100644 --- a/src/addon/mod/assign/providers/prefetch-handler.ts +++ b/src/addon/mod/assign/providers/prefetch-handler.ts @@ -293,9 +293,9 @@ export class AddonModAssignPrefetchHandler extends CoreCourseActivityPrefetchHan return this.assignProvider.getSubmissions(assign.id, true, siteId).then((data) => { const promises = []; - if (data.canviewsubmissions) { - // Teacher, prefetch all submissions. - promises.push(this.groupsProvider.getActivityGroupInfo(assign.cmid, false, undefined, siteId).then((groupInfo) => { + promises.push(this.groupsProvider.getActivityGroupInfo(assign.cmid, false, undefined, siteId).then((groupInfo) => { + if (data.canviewsubmissions) { + // Teacher, prefetch all submissions. const groupProms = []; if (!groupInfo.groups || groupInfo.groups.length == 0) { groupInfo.groups = [{id: 0}]; @@ -354,8 +354,8 @@ export class AddonModAssignPrefetchHandler extends CoreCourseActivityPrefetchHan }); return Promise.all(groupProms); - })); - } + } + })); // Prefetch own submission, we need to do this for teachers too so the response with error is cached. promises.push( @@ -370,9 +370,6 @@ export class AddonModAssignPrefetchHandler extends CoreCourseActivityPrefetchHan }) ); - promises.push(this.groupsProvider.activityHasGroups(assign.cmid, siteId, true)); - promises.push(this.groupsProvider.getActivityAllowedGroups(assign.cmid, undefined, siteId, true)); - return Promise.all(promises); }); } diff --git a/src/addon/mod/chat/pages/sessions/sessions.ts b/src/addon/mod/chat/pages/sessions/sessions.ts index 35f26cb33..373a38dce 100644 --- a/src/addon/mod/chat/pages/sessions/sessions.ts +++ b/src/addon/mod/chat/pages/sessions/sessions.ts @@ -70,14 +70,7 @@ export class AddonModChatSessionsPage { return this.groupsProvider.getActivityGroupInfo(this.cmId, false).then((groupInfo) => { this.groupInfo = groupInfo; - - if (groupInfo.groups && groupInfo.groups.length > 0) { - if (!groupInfo.groups.find((group) => group.id === this.groupId)) { - this.groupId = groupInfo.groups[0].id; - } - } else { - this.groupId = 0; - } + this.groupId = this.groupsProvider.validateGroupId(this.groupId, groupInfo); return this.chatProvider.getSessions(this.chatId, this.groupId, this.showAll); }).then((sessions) => { diff --git a/src/addon/mod/data/components/index/index.ts b/src/addon/mod/data/components/index/index.ts index 37a806f43..f716141f5 100644 --- a/src/addon/mod/data/components/index/index.ts +++ b/src/addon/mod/data/components/index/index.ts @@ -226,16 +226,9 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp canSearch = true; canAdd = accessData.canaddentry; - return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule, accessData.canmanageentries) - .then((groupInfo) => { + return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule).then((groupInfo) => { this.groupInfo = groupInfo; - - // Check selected group is accessible. - if (groupInfo && groupInfo.groups && groupInfo.groups.length > 0) { - if (!groupInfo.groups.some((group) => this.selectedGroup == group.id)) { - this.selectedGroup = groupInfo.groups[0].id; - } - } + this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo); }); }).then(() => { return this.dataProvider.getFields(this.data.id).then((fields) => { diff --git a/src/addon/mod/data/pages/edit/edit.ts b/src/addon/mod/data/pages/edit/edit.ts index 35e74cd06..e008dda58 100644 --- a/src/addon/mod/data/pages/edit/edit.ts +++ b/src/addon/mod/data/pages/edit/edit.ts @@ -131,16 +131,9 @@ export class AddonModDataEditPage { return this.dataProvider.getDatabaseAccessInformation(data.id); }).then((accessData) => { if (this.entryId) { - return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule, accessData.canmanageentries) - .then((groupInfo) => { + return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule).then((groupInfo) => { this.groupInfo = groupInfo; - - // Check selected group is accessible. - if (groupInfo && groupInfo.groups && groupInfo.groups.length > 0) { - if (!groupInfo.groups.some((group) => this.selectedGroup == group.id)) { - this.selectedGroup = groupInfo.groups[0].id; - } - } + this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo); }); } }).then(() => { diff --git a/src/addon/mod/data/pages/entry/entry.ts b/src/addon/mod/data/pages/entry/entry.ts index 51e95ce9c..33fe5e72d 100644 --- a/src/addon/mod/data/pages/entry/entry.ts +++ b/src/addon/mod/data/pages/entry/entry.ts @@ -149,16 +149,9 @@ export class AddonModDataEntryPage implements OnDestroy { }).then((accessData) => { this.access = accessData; - return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule, accessData.canmanageentries) - .then((groupInfo) => { + return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule).then((groupInfo) => { this.groupInfo = groupInfo; - - // Check selected group is accessible. - if (groupInfo && groupInfo.groups && groupInfo.groups.length > 0) { - if (!groupInfo.groups.some((group) => this.selectedGroup == group.id)) { - this.selectedGroup = groupInfo.groups[0].id; - } - } + this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo); }); }).then(() => { const actions = this.dataHelper.getActions(this.data, this.access, this.entry); diff --git a/src/addon/mod/feedback/components/index/index.ts b/src/addon/mod/feedback/components/index/index.ts index 953a8c8bd..18aa04c65 100644 --- a/src/addon/mod/feedback/components/index/index.ts +++ b/src/addon/mod/feedback/components/index/index.ts @@ -269,7 +269,7 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity return this.groupsProvider.getActivityGroupInfo(cmId).then((groupInfo) => { this.groupInfo = groupInfo; - return this.setGroup(this.group); + return this.setGroup(this.groupsProvider.validateGroupId(this.group, groupInfo)); }); } diff --git a/src/addon/mod/feedback/pages/nonrespondents/nonrespondents.ts b/src/addon/mod/feedback/pages/nonrespondents/nonrespondents.ts index 1ca3de974..4f8c210b7 100644 --- a/src/addon/mod/feedback/pages/nonrespondents/nonrespondents.ts +++ b/src/addon/mod/feedback/pages/nonrespondents/nonrespondents.ts @@ -78,6 +78,7 @@ export class AddonModFeedbackNonRespondentsPage { return this.groupsProvider.getActivityGroupInfo(this.moduleId).then((groupInfo) => { this.groupInfo = groupInfo; + this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo); return this.loadGroupUsers(this.selectedGroup); }).catch((message) => { diff --git a/src/addon/mod/feedback/pages/respondents/respondents.ts b/src/addon/mod/feedback/pages/respondents/respondents.ts index e5d555231..4a4a669ea 100644 --- a/src/addon/mod/feedback/pages/respondents/respondents.ts +++ b/src/addon/mod/feedback/pages/respondents/respondents.ts @@ -99,6 +99,7 @@ export class AddonModFeedbackRespondentsPage { return this.groupsProvider.getActivityGroupInfo(this.moduleId).then((groupInfo) => { this.groupInfo = groupInfo; + this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo); return this.loadGroupAttempts(this.selectedGroup); }).catch((message) => { diff --git a/src/addon/mod/forum/pages/new-discussion/new-discussion.ts b/src/addon/mod/forum/pages/new-discussion/new-discussion.ts index be2fb7383..337966e1d 100644 --- a/src/addon/mod/forum/pages/new-discussion/new-discussion.ts +++ b/src/addon/mod/forum/pages/new-discussion/new-discussion.ts @@ -136,14 +136,14 @@ export class AddonModForumNewDiscussionPage implements OnDestroy { const promises = []; if (mode === CoreGroupsProvider.SEPARATEGROUPS || mode === CoreGroupsProvider.VISIBLEGROUPS) { - promises.push(this.groupsProvider.getActivityAllowedGroups(this.cmId).then((forumGroups) => { + promises.push(this.groupsProvider.getActivityAllowedGroups(this.cmId).then((result) => { let promise; if (mode === CoreGroupsProvider.VISIBLEGROUPS) { // We need to check which of the returned groups the user can post to. - promise = this.validateVisibleGroups(forumGroups); + promise = this.validateVisibleGroups(result.groups); } else { // WS already filters groups, no need to do it ourselves. Add "All participants" if needed. - promise = this.addAllParticipantsOption(forumGroups, true); + promise = this.addAllParticipantsOption(result.groups, true); } return promise.then((forumGroups) => { diff --git a/src/addon/mod/forum/providers/forum.ts b/src/addon/mod/forum/providers/forum.ts index 3659e11ef..6a8d7aa0f 100644 --- a/src/addon/mod/forum/providers/forum.ts +++ b/src/addon/mod/forum/providers/forum.ts @@ -254,13 +254,13 @@ export class AddonModForumProvider { formatDiscussionsGroups(cmId: number, discussions: any[]): Promise { discussions = this.utils.clone(discussions); - return this.groupsProvider.getActivityAllowedGroups(cmId).then((forumGroups) => { + return this.groupsProvider.getActivityAllowedGroups(cmId).then((result) => { const strAllParts = this.translate.instant('core.allparticipants'); const strAllGroups = this.translate.instant('core.allgroups'); // Turn groups into an object where each group is identified by id. const groups = {}; - forumGroups.forEach((fg) => { + result.groups.forEach((fg) => { groups[fg.id] = fg; }); diff --git a/src/addon/mod/forum/providers/prefetch-handler.ts b/src/addon/mod/forum/providers/prefetch-handler.ts index 6241dce19..e698a4977 100644 --- a/src/addon/mod/forum/providers/prefetch-handler.ts +++ b/src/addon/mod/forum/providers/prefetch-handler.ts @@ -255,7 +255,7 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand } // Activity uses groups, prefetch allowed groups. - return this.groupsProvider.getActivityAllowedGroups(forum.cmid).then((groups) => { + return this.groupsProvider.getActivityAllowedGroups(forum.cmid).then((result) => { if (mode === CoreGroupsProvider.SEPARATEGROUPS) { // Groups are already filtered by WS. Prefetch canAddDiscussionToAll to determine if user can pin/attach. return this.forumProvider.canAddDiscussionToAll(forum.id).catch(() => { @@ -278,7 +278,7 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand // The user can't post to all groups, let's check which groups he can post to. const groupPromises = []; - groups.forEach((group) => { + result.groups.forEach((group) => { groupPromises.push(this.forumProvider.canAddDiscussion(forum.id, group.id).catch(() => { // Ignore errors. })); diff --git a/src/addon/mod/forum/providers/sync.ts b/src/addon/mod/forum/providers/sync.ts index 5ad34cbb6..fcfbc2743 100644 --- a/src/addon/mod/forum/providers/sync.ts +++ b/src/addon/mod/forum/providers/sync.ts @@ -228,8 +228,8 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { if (data.groupid == AddonModForumProvider.ALL_GROUPS) { // Fetch all group ids. groupsPromise = this.forumProvider.getForumById(data.courseid, data.forumid, siteId).then((forum) => { - return this.groupsProvider.getActivityAllowedGroups(forum.cmid).then((groups) => { - return groups.map((group) => group.id); + return this.groupsProvider.getActivityAllowedGroups(forum.cmid).then((result) => { + return result.groups.map((group) => group.id); }); }); } else { diff --git a/src/addon/mod/lesson/components/index/index.ts b/src/addon/mod/lesson/components/index/index.ts index 8a0acdba1..4bb35cb61 100644 --- a/src/addon/mod/lesson/components/index/index.ts +++ b/src/addon/mod/lesson/components/index/index.ts @@ -223,7 +223,7 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo return this.groupsProvider.getActivityGroupInfo(this.module.id).then((groupInfo) => { this.groupInfo = groupInfo; - return this.setGroup(this.group || 0); + return this.setGroup(this.groupsProvider.validateGroupId(this.group, groupInfo)); }).finally(() => { this.reportLoaded = true; }); diff --git a/src/addon/mod/lesson/providers/prefetch-handler.ts b/src/addon/mod/lesson/providers/prefetch-handler.ts index a594a7493..bbd7666c6 100644 --- a/src/addon/mod/lesson/providers/prefetch-handler.ts +++ b/src/addon/mod/lesson/providers/prefetch-handler.ts @@ -366,11 +366,10 @@ export class AddonModLessonPrefetchHandler extends CoreCourseActivityPrefetchHan if (accessInfo.canviewreports) { // Prefetch reports data. - promises.push(this.groupsProvider.getActivityAllowedGroupsIfEnabled(module.id, undefined, siteId, true) - .then((groups) => { + promises.push(this.groupsProvider.getActivityGroupInfo(module.id, false, undefined, siteId, true).then((info) => { const subPromises = []; - groups.forEach((group) => { + info.groups.forEach((group) => { subPromises.push(this.lessonProvider.getRetakesOverview(lesson.id, group.id, false, true, siteId)); }); diff --git a/src/addon/mod/wiki/components/index/index.ts b/src/addon/mod/wiki/components/index/index.ts index 941417a68..7fb50cf84 100644 --- a/src/addon/mod/wiki/components/index/index.ts +++ b/src/addon/mod/wiki/components/index/index.ts @@ -265,44 +265,34 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp this.componentId = this.module.id; // Get real groupmode, in case it's forced by the course. - return this.groupsProvider.getActivityGroupMode(this.wiki.coursemodule).then((groupMode) => { + return this.groupsProvider.getActivityGroupInfo(this.wiki.coursemodule).then((groupInfo) => { + return this.fetchSubwikis(this.wiki.id).then(() => { + // Get the subwiki list data from the cache. + const subwikiList = this.wikiProvider.getSubwikiList(this.wiki.id); - if (groupMode === CoreGroupsProvider.SEPARATEGROUPS || groupMode === CoreGroupsProvider.VISIBLEGROUPS) { - // Get the groups available for the user. - promise = this.groupsProvider.getActivityAllowedGroups(this.wiki.coursemodule); - } else { - promise = Promise.resolve([]); - } - - return promise.then((userGroups) => { - return this.fetchSubwikis(this.wiki.id).then(() => { - // Get the subwiki list data from the cache. - const subwikiList = this.wikiProvider.getSubwikiList(this.wiki.id); - - if (!subwikiList) { - // Not found in cache, create a new one. - return this.createSubwikiList(userGroups); - } - - this.subwikiData.count = subwikiList.count; - this.setSelectedWiki(this.subwikiId, this.userId, this.groupId); - - // If nothing was selected using nav params, use the selected from cache. - if (!this.isAnySubwikiSelected()) { - this.setSelectedWiki(subwikiList.subwikiSelected, subwikiList.userSelected, - subwikiList.groupSelected); - } - - this.subwikiData.subwikis = subwikiList.subwikis; - }); - }).then(() => { - - if (!this.isAnySubwikiSelected() || this.subwikiData.count <= 0) { - return Promise.reject(this.translate.instant('addon.mod_wiki.errornowikiavailable')); + if (!subwikiList) { + // Not found in cache, create a new one. + return this.createSubwikiList(groupInfo.groups); } - }).then(() => { - return this.fetchWikiPage(); + + this.subwikiData.count = subwikiList.count; + this.setSelectedWiki(this.subwikiId, this.userId, this.groupId); + + // If nothing was selected using nav params, use the selected from cache. + if (!this.isAnySubwikiSelected()) { + this.setSelectedWiki(subwikiList.subwikiSelected, subwikiList.userSelected, + subwikiList.groupSelected); + } + + this.subwikiData.subwikis = subwikiList.subwikis; }); + }).then(() => { + + if (!this.isAnySubwikiSelected() || this.subwikiData.count <= 0) { + return Promise.reject(this.translate.instant('addon.mod_wiki.errornowikiavailable')); + } + }).then(() => { + return this.fetchWikiPage(); }); }); }).then(() => { diff --git a/src/addon/mod/wiki/providers/prefetch-handler.ts b/src/addon/mod/wiki/providers/prefetch-handler.ts index 5aaa5d158..5acebea4c 100644 --- a/src/addon/mod/wiki/providers/prefetch-handler.ts +++ b/src/addon/mod/wiki/providers/prefetch-handler.ts @@ -192,12 +192,7 @@ export class AddonModWikiPrefetchHandler extends CoreCourseActivityPrefetchHandl }); // Fetch group data. - promises.push(this.groupsProvider.getActivityGroupMode(module.id, siteId).then((groupMode) => { - if (groupMode === CoreGroupsProvider.SEPARATEGROUPS || groupMode === CoreGroupsProvider.VISIBLEGROUPS) { - // Get the groups available for the user. - return this.groupsProvider.getActivityAllowedGroups(module.id, userId, siteId); - } - })); + promises.push(this.groupsProvider.getActivityGroupInfo(module.id, false, userId, siteId)); // Fetch info to provide wiki links. promises.push(this.wikiProvider.getWiki(courseId, module.id, false, siteId).then((wiki) => { diff --git a/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html b/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html index 2c9f9be19..c90bb99ae 100644 --- a/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html +++ b/src/addon/mod/workshop/components/index/addon-mod-workshop-index.html @@ -144,7 +144,7 @@ {{ 'core.groupsseparate' | translate }} {{ 'core.groupsvisible' | translate }} - + {{groupOpt.name}} diff --git a/src/addon/mod/workshop/components/index/index.ts b/src/addon/mod/workshop/components/index/index.ts index e158670bf..6e96123a2 100644 --- a/src/addon/mod/workshop/components/index/index.ts +++ b/src/addon/mod/workshop/components/index/index.ts @@ -201,19 +201,9 @@ export class AddonModWorkshopIndexComponent extends CoreCourseModuleMainActivity this.access = accessData; if (accessData.canviewallsubmissions) { - return this.groupsProvider.getActivityGroupInfo(this.workshop.coursemodule, - accessData.canviewallsubmissions).then((groupInfo) => { + return this.groupsProvider.getActivityGroupInfo(this.workshop.coursemodule).then((groupInfo) => { this.groupInfo = groupInfo; - - // Check selected group is accessible. - if (groupInfo && groupInfo.groups && groupInfo.groups.length > 0) { - const found = groupInfo.groups.some((group) => { - return group.id == this.group; - }); - if (!found) { - this.group = groupInfo.groups[0].id; - } - } + this.group = this.groupsProvider.validateGroupId(this.group, groupInfo); }); } }).then(() => { diff --git a/src/providers/groups.ts b/src/providers/groups.ts index a6507bc1f..35d0d2e11 100644 --- a/src/providers/groups.ts +++ b/src/providers/groups.ts @@ -39,6 +39,12 @@ export interface CoreGroupInfo { * @type {boolean} */ visibleGroups?: boolean; + + /** + * The group ID to use by default. If all participants is visible, 0 will be used. First group ID otherwise. + * @type {number} + */ + defaultGroupId?: number; } /* @@ -103,7 +109,7 @@ export class CoreGroupsProvider { return Promise.reject(null); } - return response.groups; + return response; }); }); } @@ -138,7 +144,9 @@ export class CoreGroupsProvider { return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache); } - return []; + return { + groups: [] + }; }); } @@ -146,13 +154,13 @@ export class CoreGroupsProvider { * Helper function to get activity group info (group mode and list of groups). * * @param {number} cmId Course module ID. - * @param {boolean} [addAllParts=true] Whether to add the all participants option. Always true for visible groups. + * @param {boolean} [addAllParts] Deprecated. * @param {number} [userId] User ID. If not defined, use current user. * @param {string} [siteId] Site ID. If not defined, current site. * @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down). * @return {Promise} Promise resolved with the group info. */ - getActivityGroupInfo(cmId: number, addAllParts: boolean = true, userId?: number, siteId?: string, ignoreCache?: boolean) + getActivityGroupInfo(cmId: number, addAllParts?: boolean, userId?: number, siteId?: string, ignoreCache?: boolean) : Promise { const groupInfo: CoreGroupInfo = { @@ -167,16 +175,25 @@ export class CoreGroupsProvider { return this.getActivityAllowedGroups(cmId, userId, siteId, ignoreCache); } - return []; - }).then((groups) => { - if (groups.length <= 0) { + return { + groups: [], + canaccessallgroups: false + }; + }).then((result) => { + if (result.groups.length <= 0) { groupInfo.separateGroups = false; groupInfo.visibleGroups = false; + groupInfo.defaultGroupId = 0; } else { - if (addAllParts || groupInfo.visibleGroups) { + // The "canaccessallgroups" field was added in 3.4. Add all participants for visible groups in previous versions. + if (result.canaccessallgroups || (typeof result.canaccessallgroups == 'undefined' && groupInfo.visibleGroups)) { groupInfo.groups.push({ id: 0, name: this.translate.instant('core.allparticipants') }); + groupInfo.defaultGroupId = 0; + } else { + groupInfo.defaultGroupId = result.groups[0].id; } - groupInfo.groups = groupInfo.groups.concat(groups); + + groupInfo.groups = groupInfo.groups.concat(result.groups); } return groupInfo; @@ -417,4 +434,22 @@ export class CoreGroupsProvider { return site.invalidateWsCacheForKey(this.getUserGroupsInCourseCacheKey(courseId, userId)); }); } + + /** + * Validate a group ID. If the group is not visible by the user, it will return the first group ID. + * + * @param {number} groupId Group ID to validate. + * @param {CoreGroupInfo} groupInfo Group info. + * @return {number} Group ID to use. + */ + validateGroupId(groupId: number, groupInfo: CoreGroupInfo): number { + if (groupId > 0 && groupInfo && groupInfo.groups && groupInfo.groups.length > 0) { + // Check if the group is in the list of groups. + if (groupInfo.groups.some((group) => groupId == group.id)) { + return groupId; + } + } + + return groupInfo.defaultGroupId; + } } diff --git a/upgrade.txt b/upgrade.txt index 56a1634dd..6599608e6 100644 --- a/upgrade.txt +++ b/upgrade.txt @@ -1,6 +1,10 @@ This files describes API changes in the Moodle Mobile app, information provided here is intended especially for developers. +=== 3.7.1 === + +- CoreGroupsProvider.getActivityAllowedGroups and CoreGroupsProvider.getActivityAllowedGroupsIfEnabled now return the full response of core_group_get_activity_allowed_groups instead of just the groups. + === 3.7.0 === - The pushnotifications addon has been moved to core. All imports of that addon need to be fixed to use the right path and name.