141 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-02-16 11:18:12 +01:00
// (C) Copyright 2015 Moodle Pty Ltd.
//
// 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.
import { Injectable, Type } from '@angular/core';
2021-03-01 15:38:08 +01:00
import { AddonModForum, AddonModForumProvider } from '../forum';
2021-02-16 11:18:12 +01:00
import { makeSingleton, Translate } from '@singletons';
import { CoreEvents } from '@singletons/events';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@features/course/services/module-delegate';
import { CoreConstants, ModPurpose } from '@/core/constants';
2021-02-16 11:18:12 +01:00
import { AddonModForumIndexComponent } from '../../components/index';
import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler';
import { CoreCourseModuleData } from '@features/course/services/course-helper';
import { CoreIonicColorNames } from '@singletons/colors';
2021-02-16 11:18:12 +01:00
/**
* Handler to support forum modules.
*/
@Injectable({ providedIn: 'root' })
export class AddonModForumModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler {
2021-02-16 11:18:12 +01:00
static readonly PAGE_NAME = 'mod_forum';
name = 'AddonModForum';
modName = 'forum';
protected pageName = AddonModForumModuleHandlerService.PAGE_NAME;
2021-02-16 11:18:12 +01:00
supportedFeatures = {
[CoreConstants.FEATURE_GROUPS]: true,
[CoreConstants.FEATURE_GROUPINGS]: true,
[CoreConstants.FEATURE_MOD_INTRO]: true,
[CoreConstants.FEATURE_COMPLETION_TRACKS_VIEWS]: true,
[CoreConstants.FEATURE_COMPLETION_HAS_RULES]: true,
[CoreConstants.FEATURE_GRADE_HAS_GRADE]: true,
[CoreConstants.FEATURE_GRADE_OUTCOMES]: true,
[CoreConstants.FEATURE_BACKUP_MOODLE2]: true,
[CoreConstants.FEATURE_SHOW_DESCRIPTION]: true,
[CoreConstants.FEATURE_RATE]: true,
[CoreConstants.FEATURE_PLAGIARISM]: true,
[CoreConstants.FEATURE_MOD_PURPOSE]: ModPurpose.MOD_PURPOSE_COLLABORATION,
2021-02-16 11:18:12 +01:00
};
/**
* @inheritdoc
2021-02-16 11:18:12 +01:00
*/
async getData(module: CoreCourseModuleData, courseId: number): Promise<CoreCourseModuleHandlerData> {
const data = await super.getData(module, courseId);
2021-02-16 11:18:12 +01:00
if ('afterlink' in module && !!module.afterlink) {
data.extraBadgeColor = undefined;
2021-02-16 11:18:12 +01:00
const match = />(\d+)[^<]+/.exec(module.afterlink);
data.extraBadge = match ? Translate.instant('addon.mod_forum.unreadpostsnumber', { $a : match[1] }) : '';
2021-02-16 11:18:12 +01:00
} else {
this.updateExtraBadge(data, courseId, module.id);
}
const event = CoreEvents.on(
AddonModForumProvider.MARK_READ_EVENT,
eventData => {
2021-02-16 11:18:12 +01:00
if (eventData.courseId !== courseId || eventData.moduleId !== module.id) {
return;
}
this.updateExtraBadge(data, eventData.courseId, eventData.moduleId, eventData.siteId);
},
CoreSites.getCurrentSiteId(),
2021-02-16 11:18:12 +01:00
);
data.onDestroy = () => event.off();
return data;
}
/**
* @inheritdoc
2021-02-16 11:18:12 +01:00
*/
async getMainComponent(): Promise<Type<unknown> | undefined> {
return AddonModForumIndexComponent;
}
/**
* @inheritdoc
2021-02-16 11:18:12 +01:00
*/
displayRefresherInSingleActivity(): boolean {
return false;
}
/**
* Triggers an update for the extra badge text.
*
* @param data Course Module Handler data.
* @param courseId Course ID.
* @param moduleId Course module ID.
* @param siteId Site ID. If not defined, current site.
*/
async updateExtraBadge(data: CoreCourseModuleHandlerData, courseId: number, moduleId: number, siteId?: string): Promise<void> {
siteId = siteId || CoreSites.getCurrentSiteId();
2021-02-16 11:18:12 +01:00
if (!siteId) {
return;
}
data.extraBadge = Translate.instant('core.loading');
data.extraBadgeColor = CoreIonicColorNames.DARK;
2021-02-16 11:18:12 +01:00
await CoreUtils.ignoreErrors(AddonModForum.invalidateForumData(courseId));
2021-02-16 11:18:12 +01:00
try {
// Handle unread posts.
const forum = await AddonModForum.getForum(courseId, moduleId, { siteId });
2021-02-16 11:18:12 +01:00
data.extraBadgeColor = undefined;
2021-02-16 11:18:12 +01:00
data.extraBadge = forum.unreadpostscount
? Translate.instant(
2021-02-16 11:18:12 +01:00
'addon.mod_forum.unreadpostsnumber',
{ $a : forum.unreadpostscount },
)
: '';
} catch {
2021-02-16 11:18:12 +01:00
// Ignore errors.
data.extraBadgeColor = undefined;
2021-02-16 11:18:12 +01:00
data.extraBadge = '';
}
}
}
export const AddonModForumModuleHandler = makeSingleton(AddonModForumModuleHandlerService);