Skip to content

Commit

Permalink
MOBILE-4616 forum: Check user has already posted to eachuser forum
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Oct 17, 2024
1 parent ac71989 commit 21f5f1e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
24 changes: 15 additions & 9 deletions src/addons/mod/forum/classes/forum-discussions-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
AddonModForumSortOrder,
} from '../services/forum';
import { AddonModForumOffline, AddonModForumOfflineDiscussion } from '../services/forum-offline';
import { ADDON_MOD_FORUM_DISCUSSIONS_PER_PAGE } from '../constants';
import { ADDON_MOD_FORUM_DISCUSSIONS_PER_PAGE, AddonModForumType } from '../constants';
import { CoreSites } from '@services/sites';

export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource<AddonModForumDiscussionItem> {

Expand Down Expand Up @@ -168,8 +169,6 @@ export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource

/**
* Load some specific data for current group.
*
* @returns Promise resolved when done.
*/
async loadSelectedGroupData(): Promise<void> {
if (!this.usesGroups) {
Expand Down Expand Up @@ -203,6 +202,17 @@ export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource

discussions.push(...onlineDiscussions);

// If the user has already posted in a Each user posts a single discussion forum, don't allow to post again.
// This check is only needed in offline mode.
if (this.canAddDiscussionToGroup && this.forum?.type === AddonModForumType.EACHUSER) {
const userId = CoreSites.getCurrentSiteUserId();

this.canAddDiscussionToGroup = !discussions.some((discussion) =>
this.isOfflineDiscussion(discussion) || !this.isNewDiscussionForm(discussion) && discussion.userid === userId);

console.error('check', this.canAddDiscussionToGroup);

Check failure on line 213 in src/addons/mod/forum/classes/forum-discussions-source.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement
}

return {
items: discussions,
hasMoreItems: canLoadMore,
Expand Down Expand Up @@ -250,7 +260,7 @@ export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource
}

// Hide author for first post and type single.
if (this.forum.type === 'single') {
if (this.forum.type === AddonModForumType.SINGLE) {
for (const discussion of discussions) {
if (discussion.userfullname && discussion.parent === 0) {
discussion.userfullname = false;
Expand Down Expand Up @@ -297,7 +307,7 @@ export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource
const promises = offlineDiscussions.map(async (offlineDiscussion) => {
const discussion = offlineDiscussion as unknown as AddonModForumDiscussion;

if (discussion.parent === 0 || forum.type === 'single') {
if (discussion.parent === 0 || forum.type === AddonModForumType.SINGLE) {
// Do not show author for first post and type single.
return;
}
Expand All @@ -322,8 +332,6 @@ export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource

/**
* Invalidate cache data.
*
* @returns Promise resolved when done.
*/
async invalidateCache(): Promise<void> {
const promises: Promise<void>[] = [];
Expand All @@ -341,8 +349,6 @@ export class AddonModForumDiscussionsSource extends CoreRoutedItemsManagerSource

/**
* Invalidate list cache data.
*
* @returns Promise resolved when done.
*/
async invalidateList(): Promise<void> {
if (this.forum) {
Expand Down
9 changes: 5 additions & 4 deletions src/addons/mod/forum/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
ADDON_MOD_FORUM_PREFERENCE_SORTORDER,
ADDON_MOD_FORUM_REPLY_DISCUSSION_EVENT,
ADDON_MOD_FORUM_SEARCH_PAGE_NAME,
AddonModForumType,
} from '@addons/mod/forum/constants';
import { CoreSearchGlobalSearch } from '@features/search/services/global-search';
import { CoreToasts } from '@services/toasts';
Expand Down Expand Up @@ -411,11 +412,11 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
this.dataRetrieved.emit(forum);

switch (forum.type) {
case 'news':
case 'blog':
case AddonModForumType.NEWS:
case AddonModForumType.BLOG:
this.addDiscussionText = Translate.instant('addon.mod_forum.addanewtopic');
break;
case 'qanda':
case AddonModForumType.QANDA:
this.addDiscussionText = Translate.instant('addon.mod_forum.addanewquestion');
break;
default:
Expand Down Expand Up @@ -451,7 +452,7 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom
const cutoffDateReached = AddonModForumHelper.isCutoffDateReached(forum)
&& !accessInfo.cancanoverridecutoff;
this.canAddDiscussion = !!forum.cancreatediscussions && !cutoffDateReached;
this.showQAMessage = forum.type === 'qanda' && !accessInfo.canviewqandawithoutposting;
this.showQAMessage = forum.type === AddonModForumType.QANDA && !accessInfo.canviewqandawithoutposting;

return;
}),
Expand Down
10 changes: 10 additions & 0 deletions src/addons/mod/forum/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ export const enum AddonModForumSortorder {
REPLIES_ASC = 6,
}

export const enum AddonModForumType {
NEWS = 'news',
SOCIAL = 'social',
GENERAL = 'general',
EACHUSER = 'eachuser',
SINGLE = 'single',
QANDA = 'qanda',
BLOG = 'blog',
}

export const ADDON_MOD_FORUM_ALL_PARTICIPANTS = -1;
export const ADDON_MOD_FORUM_ALL_GROUPS = -2;
5 changes: 3 additions & 2 deletions src/addons/mod/forum/pages/discussion/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
ADDON_MOD_FORUM_MANUAL_SYNCED,
ADDON_MOD_FORUM_MARK_READ_EVENT,
ADDON_MOD_FORUM_REPLY_DISCUSSION_EVENT,
AddonModForumType,
} from '../../constants';
import { CoreCourseContentsPage } from '@features/course/pages/contents/contents';
import { CoreToasts } from '@services/toasts';
Expand Down Expand Up @@ -512,7 +513,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes

// Show Q&A message if user hasn't posted.
const currentUserId = CoreSites.getCurrentSiteUserId();
this.showQAMessage = forum.type === 'qanda' && !accessInfo.canviewqandawithoutposting &&
this.showQAMessage = forum.type === AddonModForumType.QANDA && !accessInfo.canviewqandawithoutposting &&
!posts.some(post => post.author.id === currentUserId);

return;
Expand All @@ -534,7 +535,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes
throw new Error('Invalid forum discussion.');
}

if (this.startingPost && this.startingPost.author && this.forum.type == 'single') {
if (this.startingPost && this.startingPost.author && this.forum.type === AddonModForumType.SINGLE) {
// Hide author and groups for first post and type single.
delete this.startingPost.author.fullname;
delete this.startingPost.author.groups;
Expand Down
21 changes: 12 additions & 9 deletions src/addons/mod/forum/services/forum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
ADDON_MOD_FORUM_PREFERENCE_SORTORDER,
ADDON_MOD_FORUM_REPLY_DISCUSSION_EVENT,
AddonModForumSortorder,
AddonModForumType,
} from '../constants';

declare module '@singletons/events' {
Expand Down Expand Up @@ -417,7 +418,10 @@ export class AddonModForumProvider {
* @param options Other options.
* @returns Promise resolved when the forums are retrieved.
*/
async getCourseForums(courseId: number, options: CoreSitesCommonWSOptions = {}): Promise<AddonModForumData[]> {
async getCourseForums(
courseId: number,
options: CoreSitesCommonWSOptions = {},
): Promise<AddonModForumGetForumsByCoursesWSResponse> {
const site = await CoreSites.getSite(options.siteId);

const params: AddonModForumGetForumsByCoursesWSParams = {
Expand Down Expand Up @@ -483,8 +487,7 @@ export class AddonModForumProvider {
*/
async getForum(courseId: number, cmId: number, options: CoreSitesCommonWSOptions = {}): Promise<AddonModForumData> {
const forums = await this.getCourseForums(courseId, options);

const forum = forums.find(forum => forum.cmid == cmId);
const forum = forums.find(forum => forum.cmid === cmId);

if (!forum) {
throw new CoreError(Translate.instant('core.course.modulenotfound'));
Expand Down Expand Up @@ -1385,7 +1388,7 @@ type AddonModForumGetForumsByCoursesWSParams = {
export type AddonModForumData = {
id: number; // Forum id.
course: number; // Course id.
type: string; // The forum type.
type: AddonModForumType; // The forum type.
name: string; // Forum name.
intro: string; // The forum intro.
introformat: number; // Intro format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN).
Expand Down Expand Up @@ -1421,6 +1424,11 @@ export type AddonModForumData = {
unreadpostscount?: number; // The number of unread posts for tracked forums.
};

/**
* Data returned by mod_forum_get_forums_by_courses WS.
*/
type AddonModForumGetForumsByCoursesWSResponse = AddonModForumData[];

/**
* Forum discussion.
*/
Expand Down Expand Up @@ -1751,11 +1759,6 @@ export type AddonModForumGetForumDiscussionsPaginatedWSResponse = {
warnings?: CoreWSExternalWarning[];
};

/**
* Data returned by mod_forum_get_forums_by_courses WS.
*/
export type AddonModForumGetForumsByCoursesWSResponse = AddonModForumData[];

/**
* Array options of mod_forum_add_discussion WS.
*/
Expand Down

0 comments on commit 21f5f1e

Please sign in to comment.