MOBILE-2414 sitehome: Support site news forum

main
Dani Palou 2018-05-17 16:33:28 +02:00
parent 981e1a6aac
commit d2faf24229
3 changed files with 64 additions and 5 deletions

View File

@ -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<any>} Promise resolved when done.
*/
protected loadContent(): Promise<any> {
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);
});

View File

@ -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.
});
}
}

View File

@ -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<any>} Promise resolved with the forum if found, rejected otherwise.
*/
getNewsForum(siteHomeId: number): Promise<any> {
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.
*