Skip to content

Commit

Permalink
Merge pull request #3767 from dpalou/MOBILE-4401
Browse files Browse the repository at this point in the history
MOBILE-4401 wiki: Fix selected subwiki when wiki is empty
  • Loading branch information
alfonso-salces authored Aug 2, 2023
2 parents 5ba622a + 58ea59d commit b366ee3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
73 changes: 38 additions & 35 deletions src/addons/mod/wiki/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,42 +343,19 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
* @param subwiki Subwiki.
*/
protected async fetchSubwikiPages(subwiki: AddonModWikiSubwiki): Promise<void> {
const subwikiPages = await AddonModWiki.getSubwikiPages(subwiki.wikiid, {
groupId: subwiki.groupid,
userId: subwiki.userid,
cmId: this.module.id,
});
const subwikiPages = subwiki.id <= 0 ?
[] :
await AddonModWiki.getSubwikiPages(subwiki.wikiid, {
groupId: subwiki.groupid,
userId: subwiki.userid,
cmId: this.module.id,
});

if (!this.currentPage) {
if (!this.pageTitle) {
// No page specified, search first page.
const firstPage = subwikiPages.find((page) => page.firstpage );
if (firstPage) {
this.currentPage = firstPage.id;
this.pageTitle = firstPage.title;
}
} else {
// Got the page title but not its ID. Search the page.
const page = subwikiPages.find((page) => page.title === this.pageTitle );
if (page) {
this.currentPage = page.id;
}
}
}
this.setCurrentPage(subwikiPages);

// Now get the offline pages.
const dbPages = await AddonModWikiOffline.getSubwikiNewPages(subwiki.id, subwiki.wikiid, subwiki.userid, subwiki.groupid);

// If no page specified, search page title in the offline pages.
if (!this.currentPage) {
const searchTitle = this.pageTitle ? this.pageTitle : this.wiki?.firstpagetitle ?? '';
const pageExists = dbPages.some((page) => page.title == searchTitle);

if (pageExists) {
this.pageTitle = searchTitle;
}
}

this.subwikiPages = AddonModWiki.sortPagesByTitle(
(<(AddonModWikiSubwikiPage | AddonModWikiPageDBRecord)[]> subwikiPages).concat(dbPages),
);
Expand All @@ -389,6 +366,32 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
}
}

/**
* Set current page if needed.
*
* @param subwikiPages List of subwiki pages.
*/
setCurrentPage(subwikiPages: AddonModWikiSubwikiPage[]): void {
if (this.currentPage) {
return; // Already set, nothing to do.
}

if (this.pageTitle) {
// Got the page title but not its ID. Search the page.
const page = subwikiPages.find((page) => page.title === this.pageTitle);
if (page) {
this.currentPage = page.id;
}

return;
}

// No page specified, search first page.
const firstPage = subwikiPages.find((page) => page.firstpage);
this.currentPage = firstPage?.id;
this.pageTitle = firstPage?.title ?? this.wiki?.firstpagetitle;
}

/**
* Get the subwikis.
*
Expand Down Expand Up @@ -710,7 +713,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
* @returns Whether there is any subwiki selected.
*/
protected isAnySubwikiSelected(): boolean {
return this.subwikiData.subwikiSelected > 0 || this.subwikiData.userSelected > 0 || this.subwikiData.groupSelected > 0;
return this.subwikiData.subwikiSelected !== 0 || this.subwikiData.userSelected > 0 || this.subwikiData.groupSelected > 0;
}

/**
Expand Down Expand Up @@ -752,7 +755,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
* @param groupId Group ID of the subwiki to select.
*/
protected setSelectedWiki(subwikiId: number | undefined, userId: number | undefined, groupId: number | undefined): void {
this.subwikiData.subwikiSelected = AddonModWikiOffline.convertToPositiveNumber(subwikiId);
this.subwikiData.subwikiSelected = subwikiId ?? 0;
this.subwikiData.userSelected = AddonModWikiOffline.convertToPositiveNumber(userId);
this.subwikiData.groupSelected = AddonModWikiOffline.convertToPositiveNumber(groupId);
}
Expand Down Expand Up @@ -800,7 +803,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
}

const sameSubwiki = this.currentSubwiki &&
((this.currentSubwiki.id && this.currentSubwiki.id === editedPageData.subwikiId) ||
((this.currentSubwiki.id > 0 && this.currentSubwiki.id === editedPageData.subwikiId) ||
(this.currentSubwiki.userid === editedPageData.userId && this.currentSubwiki.groupid === editedPageData.groupId));

if (sameSubwiki && editedPageData.pageTitle === this.pageTitle) {
Expand Down Expand Up @@ -1024,7 +1027,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
candidateSubwikiId = subwiki.id;
}
} else if (subwiki.groupid > 0) {
// Check if it's a current user' group.
// Check if it's a current user's group.
if (showMyGroupsLabel) {
candidateSubwikiId = subwiki.id;
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/features/course/tests/behat/basic_usage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ Feature: Test basic usage of one course in app

When I press the back button in the app
And I press "Test wiki name" in the app
And I press "OK" in the app
Then the header should be "Test wiki name" in the app

When I press the back button in the app
Expand Down Expand Up @@ -199,7 +198,6 @@ Feature: Test basic usage of one course in app

When I press the back button in the app
And I press "Test wiki name" in the app
And I press "OK" in the app
Then the header should be "Test wiki name" in the app

When I press the back button in the app
Expand Down

0 comments on commit b366ee3

Please sign in to comment.