From 52a73f4138847edd115b3d09b3f1d06b7b7d508b Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 28 Feb 2018 16:14:37 +0100 Subject: [PATCH] MOBILE-2333 siteaddons: Support basic course format site addons --- .../components/components.module.ts | 10 ++- .../course-format/course-format.html | 1 + .../components/course-format/course-format.ts | 68 +++++++++++++++++++ src/core/siteaddons/providers/helper.ts | 55 ++++++++++++++- 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/core/siteaddons/components/course-format/course-format.html create mode 100644 src/core/siteaddons/components/course-format/course-format.ts diff --git a/src/core/siteaddons/components/components.module.ts b/src/core/siteaddons/components/components.module.ts index 61021a6b9..15a6f3019 100644 --- a/src/core/siteaddons/components/components.module.ts +++ b/src/core/siteaddons/components/components.module.ts @@ -21,12 +21,14 @@ import { CoreCompileHtmlComponentModule } from '../../compile/components/compile import { CoreSiteAddonsAddonContentComponent } from './addon-content/addon-content'; import { CoreSiteAddonsModuleIndexComponent } from './module-index/module-index'; import { CoreSiteAddonsCourseOptionComponent } from './course-option/course-option'; +import { CoreSiteAddonsCourseFormatComponent } from './course-format/course-format'; @NgModule({ declarations: [ CoreSiteAddonsAddonContentComponent, CoreSiteAddonsModuleIndexComponent, - CoreSiteAddonsCourseOptionComponent + CoreSiteAddonsCourseOptionComponent, + CoreSiteAddonsCourseFormatComponent ], imports: [ CommonModule, @@ -40,11 +42,13 @@ import { CoreSiteAddonsCourseOptionComponent } from './course-option/course-opti exports: [ CoreSiteAddonsAddonContentComponent, CoreSiteAddonsModuleIndexComponent, - CoreSiteAddonsCourseOptionComponent + CoreSiteAddonsCourseOptionComponent, + CoreSiteAddonsCourseFormatComponent ], entryComponents: [ CoreSiteAddonsModuleIndexComponent, - CoreSiteAddonsCourseOptionComponent + CoreSiteAddonsCourseOptionComponent, + CoreSiteAddonsCourseFormatComponent ] }) export class CoreSiteAddonsComponentsModule {} diff --git a/src/core/siteaddons/components/course-format/course-format.html b/src/core/siteaddons/components/course-format/course-format.html new file mode 100644 index 000000000..69f8afc63 --- /dev/null +++ b/src/core/siteaddons/components/course-format/course-format.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/core/siteaddons/components/course-format/course-format.ts b/src/core/siteaddons/components/course-format/course-format.ts new file mode 100644 index 000000000..5dd0e1148 --- /dev/null +++ b/src/core/siteaddons/components/course-format/course-format.ts @@ -0,0 +1,68 @@ +// (C) Copyright 2015 Martin Dougiamas +// +// 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 { Component, OnInit, Input, ViewChild } from '@angular/core'; +import { CoreSiteAddonsProvider } from '../../providers/siteaddons'; +import { CoreSiteAddonsAddonContentComponent } from '../addon-content/addon-content'; + +/** + * Component that displays the index of a course format site addon. + */ +@Component({ + selector: 'core-site-addons-course-format', + templateUrl: 'course-format.html', +}) +export class CoreSiteAddonsCourseFormatComponent implements OnInit { + @Input() course: any; // The course to render. + @Input() sections: any[]; // List of course sections. + @Input() downloadEnabled?: boolean; // Whether the download of sections and modules is enabled. + + @ViewChild(CoreSiteAddonsAddonContentComponent) content: CoreSiteAddonsAddonContentComponent; + + component: string; + method: string; + args: any; + bootstrapResult: any; + + constructor(protected siteAddonsProvider: CoreSiteAddonsProvider) { } + + /** + * Component being initialized. + */ + ngOnInit(): void { + if (this.course && this.course.format) { + const handler = this.siteAddonsProvider.getSiteAddonHandler(this.course.format); + if (handler) { + this.component = handler.addon.component; + this.method = handler.handlerSchema.method; + this.args = { + courseid: this.course.id, + downloadenabled: this.downloadEnabled + }; + this.bootstrapResult = handler.bootstrapResult; + } + } + } + + /** + * Refresh the data. + * + * @param {any} [refresher] Refresher. + * @param {Function} [done] Function to call when done. + * @return {Promise} Promise resolved when done. + */ + doRefresh(refresher?: any, done?: () => void): Promise { + return Promise.resolve(this.content.refreshData()); + } +} diff --git a/src/core/siteaddons/providers/helper.ts b/src/core/siteaddons/providers/helper.ts index 66885636a..d51bfa002 100644 --- a/src/core/siteaddons/providers/helper.ts +++ b/src/core/siteaddons/providers/helper.ts @@ -27,10 +27,12 @@ import { CoreCourseModulePrefetchDelegate } from '../../course/providers/module- import { CoreCourseOptionsDelegate, CoreCourseOptionsHandler, CoreCourseOptionsHandlerData } from '../../course/providers/options-delegate'; +import { CoreCourseFormatDelegate, CoreCourseFormatHandler } from '../../course/providers/format-delegate'; import { CoreUserDelegate, CoreUserProfileHandler, CoreUserProfileHandlerData } from '../../user/providers/user-delegate'; import { CoreDelegateHandler } from '../../../classes/delegate'; import { CoreSiteAddonsModuleIndexComponent } from '../components/module-index/module-index'; import { CoreSiteAddonsCourseOptionComponent } from '../components/course-option/course-option'; +import { CoreSiteAddonsCourseFormatComponent } from '../components/course-format/course-format'; import { CoreSiteAddonsProvider } from './siteaddons'; import { CoreSiteAddonsModulePrefetchHandler } from '../classes/module-prefetch-handler'; import { CoreCompileProvider } from '../../compile/providers/compile'; @@ -51,7 +53,8 @@ export class CoreSiteAddonsHelperProvider { private userDelegate: CoreUserDelegate, private langProvider: CoreLangProvider, private siteAddonsProvider: CoreSiteAddonsProvider, private prefetchDelegate: CoreCourseModulePrefetchDelegate, private compileProvider: CoreCompileProvider, private utils: CoreUtilsProvider, - private coursesProvider: CoreCoursesProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate) { + private coursesProvider: CoreCoursesProvider, private courseOptionsDelegate: CoreCourseOptionsDelegate, + private courseFormatDelegate: CoreCourseFormatDelegate) { this.logger = logger.getInstance('CoreSiteAddonsHelperProvider'); } @@ -284,6 +287,11 @@ export class CoreSiteAddonsHelperProvider { result.restrict); break; + case 'CoreCourseFormatDelegate': + uniqueName = this.registerCourseFormatHandler(addon, handlerName, handlerSchema, result.jsResult, + result.restrict); + break; + default: // Nothing to do. } @@ -300,6 +308,51 @@ export class CoreSiteAddonsHelperProvider { }); } + /** + * Given a handler in an addon, register it in the course format delegate. + * + * @param {any} addon Data of the addon. + * @param {string} handlerName Name of the handler in the addon. + * @param {any} handlerSchema Data about the handler. + * @param {any} [bootstrapResult] Result of executing the bootstrap JS. + * @param {any} [restrict] List of users and courses the handler is restricted to. + * @return {string} A string to identify the handler. + */ + protected registerCourseFormatHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any, + restrict?: any): string { + if (!handlerSchema) { + // Required data not provided, stop. + return; + } + + // Create the base handler. + const formatName = addon.component.replace('format_', ''), + baseHandler = this.getBaseHandler(formatName); + let handler: CoreCourseFormatHandler; + + // Extend the base handler, adding the properties required by the delegate. + handler = Object.assign(baseHandler, { + canViewAllSections: (course: any): boolean => { + return typeof handlerSchema.canviewallsections != 'undefined' ? handlerSchema.canviewallsections : true; + }, + displayEnableDownload: (course: any): boolean => { + return typeof handlerSchema.displayenabledownload != 'undefined' ? handlerSchema.displayenabledownload : true; + }, + displaySectionSelector: (course: any): boolean => { + return typeof handlerSchema.displaysectionselector != 'undefined' ? handlerSchema.displaysectionselector : true; + }, + getCourseFormatComponent: (course: any): any => { + if (handlerSchema.method) { + return CoreSiteAddonsCourseFormatComponent; + } + } + }); + + this.courseFormatDelegate.registerHandler(handler); + + return formatName; + } + /** * Given a handler in an addon, register it in the course options delegate. *