From d2faf242294822af98437de671584a474ceacd12 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 17 May 2018 16:33:28 +0200 Subject: [PATCH] MOBILE-2414 sitehome: Support site news forum --- src/core/sitehome/components/index/index.ts | 25 ++++++++++++++++++++- src/core/sitehome/components/news/news.ts | 22 +++++++++++++++--- src/core/sitehome/providers/sitehome.ts | 22 +++++++++++++++++- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/core/sitehome/components/index/index.ts b/src/core/sitehome/components/index/index.ts index 27b414e28..0e2659129 100644 --- a/src/core/sitehome/components/index/index.ts +++ b/src/core/sitehome/components/index/index.ts @@ -18,6 +18,7 @@ import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; +import { CoreSiteHomeProvider } from '../../providers/sitehome'; /** * Component that displays site home index. @@ -38,7 +39,7 @@ export class CoreSiteHomeIndexComponent implements OnInit { constructor(private domUtils: CoreDomUtilsProvider, private sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider, private courseHelper: CoreCourseHelperProvider, - private prefetchDelegate: CoreCourseModulePrefetchDelegate) { + private prefetchDelegate: CoreCourseModulePrefetchDelegate, private siteHomeProvider: CoreSiteHomeProvider) { this.siteHomeId = sitesProvider.getCurrentSite().getSiteHomeId(); } @@ -87,6 +88,7 @@ export class CoreSiteHomeIndexComponent implements OnInit { * @return {Promise} Promise resolved when done. */ protected loadContent(): Promise { + let hasNewsItem = false; this.hasContent = false; const config = this.sitesProvider.getCurrentSite().getStoredConfig() || { numsections: 1 }; @@ -114,6 +116,10 @@ export class CoreSiteHomeIndexComponent implements OnInit { return; } + if (item == 'news') { + hasNewsItem = true; + } + this.hasContent = true; this.items.push(item); }); @@ -137,6 +143,23 @@ export class CoreSiteHomeIndexComponent implements OnInit { // Add log in Moodle. this.courseProvider.logView(this.siteHomeId); + + if (hasNewsItem && this.block && this.block.modules) { + // Remove forum activity (news one only) to prevent duplicates. + this.siteHomeProvider.getNewsForum(this.siteHomeId).then((forum) => { + // Search the module that belongs to site news. + for (let i = 0; i < this.block.modules.length; i++) { + const module = this.block.modules[i]; + + if (module.modname == 'forum' && module.instance == forum.id) { + this.block.modules.splice(i, 1); + break; + } + } + }).catch(() => { + // Ignore errors. + }); + } }).catch((error) => { this.domUtils.showErrorModalDefault(error, 'core.course.couldnotloadsectioncontent', true); }); diff --git a/src/core/sitehome/components/news/news.ts b/src/core/sitehome/components/news/news.ts index 5f928e845..35e3aff95 100644 --- a/src/core/sitehome/components/news/news.ts +++ b/src/core/sitehome/components/news/news.ts @@ -14,6 +14,9 @@ import { Component, OnInit } from '@angular/core'; import { CoreSitesProvider } from '@providers/sites'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; +import { CoreSiteHomeProvider } from '../../providers/sitehome'; /** * Component that displays site home news. @@ -27,7 +30,8 @@ export class CoreSiteHomeNewsComponent implements OnInit { show: boolean; siteHomeId: number; - constructor(private sitesProvider: CoreSitesProvider) { + constructor(private sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider, + private moduleDelegate: CoreCourseModuleDelegate, private siteHomeProvider: CoreSiteHomeProvider) { this.siteHomeId = sitesProvider.getCurrentSite().getSiteHomeId(); } @@ -36,11 +40,23 @@ export class CoreSiteHomeNewsComponent implements OnInit { */ ngOnInit(): void { // Get number of news items to show. - const newsItems = this.sitesProvider.getCurrentSite().getStoredConfig('newsitems') || 0; + const currentSite = this.sitesProvider.getCurrentSite(), + newsItems = currentSite.getStoredConfig('newsitems') || 0; if (!newsItems) { return; } - // @todo: Implement it once forum is supported. + const siteHomeId = currentSite.getSiteHomeId(); + + // Get the news forum. + this.siteHomeProvider.getNewsForum(siteHomeId).then((forum) => { + return this.courseProvider.getModuleBasicInfo(forum.cmid).then((module) => { + this.show = true; + this.module = module; + module.handlerData = this.moduleDelegate.getModuleDataFor(module.modname, module, siteHomeId, module.section); + }); + }).catch(() => { + // Ignore errors. + }); } } diff --git a/src/core/sitehome/providers/sitehome.ts b/src/core/sitehome/providers/sitehome.ts index 41ee3893d..651df4a89 100644 --- a/src/core/sitehome/providers/sitehome.ts +++ b/src/core/sitehome/providers/sitehome.ts @@ -17,6 +17,7 @@ import { CoreLoggerProvider } from '@providers/logger'; import { CoreSitesProvider } from '@providers/sites'; import { CoreSite } from '@classes/site'; import { CoreCourseProvider } from '@core/course/providers/course'; +import { AddonModForumProvider } from '@addon/mod/forum/providers/forum'; /** * Service that provides some features regarding site home. @@ -25,10 +26,29 @@ import { CoreCourseProvider } from '@core/course/providers/course'; export class CoreSiteHomeProvider { protected logger; - constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider) { + constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider, + private forumProvider: AddonModForumProvider) { this.logger = logger.getInstance('CoreSiteHomeProvider'); } + /** + * Get the news forum for the Site Home. + * + * @param {number} siteHomeId Site Home ID. + * @return {Promise} Promise resolved with the forum if found, rejected otherwise. + */ + getNewsForum(siteHomeId: number): Promise { + return this.forumProvider.getCourseForums(siteHomeId).then((forums) => { + for (let i = 0; i < forums.length; i++) { + if (forums[i].type == 'news') { + return forums[i]; + } + } + + return Promise.reject(null); + }); + } + /** * Returns whether or not the frontpage is available for the current site. *