2019-09-26 12:43:29 +02:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
2018-05-14 10:18:21 +02:00
|
|
|
//
|
|
|
|
// 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 } from '@angular/core';
|
|
|
|
import { NavController, NavOptions } from 'ionic-angular';
|
2018-08-15 16:13:04 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-08-16 12:20:23 +02:00
|
|
|
import { CoreEventsProvider } from '@providers/events';
|
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
2018-05-14 10:18:21 +02:00
|
|
|
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate';
|
|
|
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
2018-08-15 16:13:04 +02:00
|
|
|
import { AddonModForumProvider } from './forum';
|
2018-08-16 12:20:23 +02:00
|
|
|
import { AddonModForumIndexComponent } from '../components/index/index';
|
2018-10-18 11:04:04 +02:00
|
|
|
import { CoreConstants } from '@core/constants';
|
2018-05-14 10:18:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to support forum modules.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class AddonModForumModuleHandler implements CoreCourseModuleHandler {
|
|
|
|
name = 'AddonModForum';
|
|
|
|
modName = 'forum';
|
|
|
|
|
2018-10-18 11:04:04 +02: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
|
|
|
|
};
|
|
|
|
|
2018-08-15 16:13:04 +02:00
|
|
|
constructor(private courseProvider: CoreCourseProvider, private forumProvider: AddonModForumProvider,
|
2018-08-16 12:20:23 +02:00
|
|
|
private translate: TranslateService, private eventsProvider: CoreEventsProvider,
|
|
|
|
private sitesProvider: CoreSitesProvider) {}
|
2018-05-14 10:18:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the handler is enabled on a site level.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Whether or not the handler is enabled on a site level.
|
2018-05-14 10:18:21 +02:00
|
|
|
*/
|
|
|
|
isEnabled(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the data required to display the module in the course contents view.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param module The module object.
|
|
|
|
* @param courseId The course ID.
|
|
|
|
* @param sectionId The section ID.
|
|
|
|
* @return Data to render the module.
|
2018-05-14 10:18:21 +02:00
|
|
|
*/
|
|
|
|
getData(module: any, courseId: number, sectionId: number): CoreCourseModuleHandlerData {
|
2018-08-16 12:20:23 +02:00
|
|
|
const data: CoreCourseModuleHandlerData = {
|
2018-12-04 12:05:01 +01:00
|
|
|
icon: this.courseProvider.getModuleIconSrc(this.modName, module.modicon),
|
2018-05-14 10:18:21 +02:00
|
|
|
title: module.name,
|
|
|
|
class: 'addon-mod_forum-handler',
|
|
|
|
showDownloadButton: true,
|
2019-01-22 12:20:04 +01:00
|
|
|
action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions, params?: any): void {
|
|
|
|
const pageParams = {module: module, courseId: courseId};
|
|
|
|
if (params) {
|
|
|
|
Object.assign(pageParams, params);
|
|
|
|
}
|
|
|
|
navCtrl.push('AddonModForumIndexPage', pageParams, options);
|
2018-05-14 10:18:21 +02:00
|
|
|
}
|
|
|
|
};
|
2018-08-15 16:13:04 +02:00
|
|
|
|
2018-10-31 11:40:01 +01:00
|
|
|
if (typeof module.afterlink != 'undefined') {
|
|
|
|
data.extraBadgeColor = '';
|
|
|
|
const match = />(\d+)[^<]+/.exec(module.afterlink);
|
|
|
|
data.extraBadge = match ? this.translate.instant('addon.mod_forum.unreadpostsnumber', {$a : match[1] }) : '';
|
|
|
|
} else {
|
|
|
|
this.updateExtraBadge(data, courseId, module.id);
|
|
|
|
}
|
2018-08-16 12:20:23 +02:00
|
|
|
|
|
|
|
const event = this.eventsProvider.on(AddonModForumProvider.MARK_READ_EVENT, (eventData) => {
|
|
|
|
if (eventData.courseId == courseId && eventData.moduleId == module.id) {
|
|
|
|
this.updateExtraBadge(data, eventData.courseId, eventData.moduleId, eventData.siteId);
|
|
|
|
}
|
|
|
|
}, this.sitesProvider.getCurrentSiteId());
|
|
|
|
|
|
|
|
data.onDestroy = (): void => {
|
|
|
|
event && event.off();
|
|
|
|
};
|
2018-08-15 16:13:04 +02:00
|
|
|
|
|
|
|
return data;
|
2018-05-14 10:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the component to render the module. This is needed to support singleactivity course format.
|
|
|
|
* The component returned must implement CoreCourseModuleMainComponent.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param course The course object.
|
|
|
|
* @param module The module object.
|
|
|
|
* @return The component to use, undefined if not found.
|
2018-05-14 10:18:21 +02:00
|
|
|
*/
|
|
|
|
getMainComponent(course: any, module: any): any {
|
|
|
|
return AddonModForumIndexComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to display the course refresher in single activity course format. If it returns false, a refresher must be
|
|
|
|
* included in the template that calls the doRefresh method of the component. Defaults to true.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @return Whether the refresher should be displayed.
|
2018-05-14 10:18:21 +02:00
|
|
|
*/
|
|
|
|
displayRefresherInSingleActivity(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-16 12:20:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers an update for the extra badge text.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param data Course Module Handler data.
|
|
|
|
* @param courseId Course ID.
|
|
|
|
* @param moduleId Course module ID.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
2018-08-16 12:20:23 +02:00
|
|
|
*/
|
|
|
|
updateExtraBadge(data: CoreCourseModuleHandlerData, courseId: number, moduleId: number, siteId?: string): void {
|
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
if (!siteId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.extraBadge = this.translate.instant('core.loading');
|
|
|
|
data.extraBadgeColor = 'light';
|
|
|
|
|
|
|
|
this.forumProvider.invalidateForumData(courseId).finally(() => {
|
|
|
|
// Handle unread posts.
|
2020-09-02 16:00:18 +02:00
|
|
|
this.forumProvider.getForum(courseId, moduleId, {siteId}).then((forumData) => {
|
2018-08-16 12:20:23 +02:00
|
|
|
data.extraBadgeColor = '';
|
|
|
|
data.extraBadge = forumData.unreadpostscount ? this.translate.instant('addon.mod_forum.unreadpostsnumber',
|
|
|
|
{$a : forumData.unreadpostscount }) : '';
|
|
|
|
}).catch(() => {
|
|
|
|
data.extraBadgeColor = '';
|
|
|
|
data.extraBadge = '';
|
|
|
|
// Ignore errors.
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-05-14 10:18:21 +02:00
|
|
|
}
|