From 00993853956b5b8f17759bda36806f8696d06c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 10 May 2019 15:15:07 +0200 Subject: [PATCH] MOBILE-3002 block: Add Glossary random block feature --- scripts/langindex.json | 1 + .../glossaryrandom/glossaryrandom.module.ts | 38 +++++++++ src/addon/block/glossaryrandom/lang/en.json | 3 + .../glossaryrandom/providers/block-handler.ts | 50 +++++++++++ src/addon/mod/glossary/glossary.module.ts | 8 +- .../glossary/providers/edit-link-handler.ts | 85 +++++++++++++++++++ src/app/app.module.ts | 2 + src/assets/lang/en.json | 1 + 8 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 src/addon/block/glossaryrandom/glossaryrandom.module.ts create mode 100644 src/addon/block/glossaryrandom/lang/en.json create mode 100644 src/addon/block/glossaryrandom/providers/block-handler.ts create mode 100644 src/addon/mod/glossary/providers/edit-link-handler.ts diff --git a/scripts/langindex.json b/scripts/langindex.json index 20feed1fe..1c36c87ea 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -31,6 +31,7 @@ "addon.block_calendarupcoming.pluginname": "block_calendar_upcoming", "addon.block_comments.pluginname": "block_comments", "addon.block_completionstatus.pluginname": "block_completionstatus", + "addon.block_glossaryrandom.pluginname": "block_glossary_random", "addon.block_learningplans.pluginname": "block_lp", "addon.block_myoverview.all": "block_myoverview", "addon.block_myoverview.favourites": "block_myoverview", diff --git a/src/addon/block/glossaryrandom/glossaryrandom.module.ts b/src/addon/block/glossaryrandom/glossaryrandom.module.ts new file mode 100644 index 000000000..e6d238957 --- /dev/null +++ b/src/addon/block/glossaryrandom/glossaryrandom.module.ts @@ -0,0 +1,38 @@ +// (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 { NgModule } from '@angular/core'; +import { IonicModule } from 'ionic-angular'; +import { TranslateModule } from '@ngx-translate/core'; +import { CoreBlockDelegate } from '@core/block/providers/delegate'; +import { AddonBlockGlossaryRandomHandler } from './providers/block-handler'; + +@NgModule({ + declarations: [ + ], + imports: [ + IonicModule, + TranslateModule.forChild() + ], + exports: [ + ], + providers: [ + AddonBlockGlossaryRandomHandler + ] +}) +export class AddonBlockGlossaryRandomModule { + constructor(blockDelegate: CoreBlockDelegate, blockHandler: AddonBlockGlossaryRandomHandler) { + blockDelegate.registerHandler(blockHandler); + } +} diff --git a/src/addon/block/glossaryrandom/lang/en.json b/src/addon/block/glossaryrandom/lang/en.json new file mode 100644 index 000000000..1ae4de38c --- /dev/null +++ b/src/addon/block/glossaryrandom/lang/en.json @@ -0,0 +1,3 @@ +{ + "pluginname": "Random glossary entry" +} \ No newline at end of file diff --git a/src/addon/block/glossaryrandom/providers/block-handler.ts b/src/addon/block/glossaryrandom/providers/block-handler.ts new file mode 100644 index 000000000..48b97b5a1 --- /dev/null +++ b/src/addon/block/glossaryrandom/providers/block-handler.ts @@ -0,0 +1,50 @@ +// (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 { Injectable, Injector } from '@angular/core'; +import { TranslateService } from '@ngx-translate/core'; +import { CoreBlockHandlerData } from '@core/block/providers/delegate'; +import { CoreBlockPreRenderedComponent } from '@core/block/components/pre-rendered-block/pre-rendered-block'; +import { CoreBlockBaseHandler } from '@core/block/classes/base-block-handler'; + +/** + * Block handler. + */ +@Injectable() +export class AddonBlockGlossaryRandomHandler extends CoreBlockBaseHandler { + name = 'AddonBlockGlossaryRandom'; + blockName = 'glossary_random'; + + constructor(private translate: TranslateService) { + super(); + } + + /** + * Returns the data needed to render the block. + * + * @param {Injector} injector Injector. + * @param {any} block The block to render. + * @param {string} contextLevel The context where the block will be used. + * @param {number} instanceId The instance ID associated with the context level. + * @return {CoreBlockHandlerData|Promise} Data or promise resolved with the data. + */ + getDisplayData(injector: Injector, block: any, contextLevel: string, instanceId: number) + : CoreBlockHandlerData | Promise { + return { + title: block.contents.title || this.translate.instant('addon.block_glossaryrandom.pluginname'), + class: 'addon-block-glossary-random', + component: CoreBlockPreRenderedComponent + }; + } +} diff --git a/src/addon/mod/glossary/glossary.module.ts b/src/addon/mod/glossary/glossary.module.ts index ab114c54b..ac311c144 100644 --- a/src/addon/mod/glossary/glossary.module.ts +++ b/src/addon/mod/glossary/glossary.module.ts @@ -27,6 +27,7 @@ import { AddonModGlossarySyncCronHandler } from './providers/sync-cron-handler'; import { AddonModGlossaryIndexLinkHandler } from './providers/index-link-handler'; import { AddonModGlossaryEntryLinkHandler } from './providers/entry-link-handler'; import { AddonModGlossaryListLinkHandler } from './providers/list-link-handler'; +import { AddonModGlossaryEditLinkHandler } from './providers/edit-link-handler'; import { AddonModGlossaryComponentsModule } from './components/components.module'; import { CoreUpdateManagerProvider } from '@providers/update-manager'; @@ -54,7 +55,8 @@ export const ADDON_MOD_GLOSSARY_PROVIDERS: any[] = [ AddonModGlossarySyncCronHandler, AddonModGlossaryIndexLinkHandler, AddonModGlossaryEntryLinkHandler, - AddonModGlossaryListLinkHandler + AddonModGlossaryListLinkHandler, + AddonModGlossaryEditLinkHandler ] }) export class AddonModGlossaryModule { @@ -62,7 +64,8 @@ export class AddonModGlossaryModule { prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModGlossaryPrefetchHandler, cronDelegate: CoreCronDelegate, syncHandler: AddonModGlossarySyncCronHandler, linksDelegate: CoreContentLinksDelegate, indexHandler: AddonModGlossaryIndexLinkHandler, discussionHandler: AddonModGlossaryEntryLinkHandler, - updateManager: CoreUpdateManagerProvider, listLinkHandler: AddonModGlossaryListLinkHandler) { + updateManager: CoreUpdateManagerProvider, listLinkHandler: AddonModGlossaryListLinkHandler, + editLinkHandler: AddonModGlossaryEditLinkHandler) { moduleDelegate.registerHandler(moduleHandler); prefetchDelegate.registerHandler(prefetchHandler); @@ -70,6 +73,7 @@ export class AddonModGlossaryModule { linksDelegate.registerHandler(indexHandler); linksDelegate.registerHandler(discussionHandler); linksDelegate.registerHandler(listLinkHandler); + linksDelegate.registerHandler(editLinkHandler); // Allow migrating the tables from the old app to the new schema. updateManager.registerSiteTableMigration({ diff --git a/src/addon/mod/glossary/providers/edit-link-handler.ts b/src/addon/mod/glossary/providers/edit-link-handler.ts new file mode 100644 index 000000000..c2f34305c --- /dev/null +++ b/src/addon/mod/glossary/providers/edit-link-handler.ts @@ -0,0 +1,85 @@ +// (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 { Injectable } from '@angular/core'; +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; + +/** + * Content links handler for glossary new entry. + * Match mod/glossary/edit.php?cmid=6 with a valid data. + * Currently it only supports new entry. + */ +@Injectable() +export class AddonModGlossaryEditLinkHandler extends CoreContentLinksHandlerBase { + name = 'AddonModGlossaryEditLinkHandler'; + featureName = 'CoreCourseModuleDelegate_AddonModGlossary'; + pattern = /\/mod\/glossary\/edit\.php.*([\?\&](cmid)=\d+)/; + + constructor(private linkHelper: CoreContentLinksHelperProvider, private courseProvider: CoreCourseProvider, + private domUtils: CoreDomUtilsProvider) { + super(); + } + + /** + * Get the list of actions for a link (url). + * + * @param {string[]} siteIds List of sites the URL belongs to. + * @param {string} url The URL to treat. + * @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param {number} [courseId] Course ID related to the URL. Optional but recommended. + * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number): + CoreContentLinksAction[] | Promise { + + return [{ + action: (siteId, navCtrl?): void => { + const modal = this.domUtils.showModalLoading(), + cmId = parseInt(params.cmid, 10); + + this.courseProvider.getModuleBasicInfo(cmId, siteId).then((module) => { + const pageParams = { + courseId: module.course, + module: module, + glossary: module.module, + entry: null // It does not support entry editing. + }; + + return this.linkHelper.goInSite(navCtrl, 'AddonModGlossaryEditPage', pageParams, siteId); + }).finally(() => { + // Just in case. In fact we need to dismiss the modal before showing a toast or error message. + modal.dismiss(); + }); + } + }]; + } + + /** + * Check if the handler is enabled for a certain site (site + user) and a URL. + * If not defined, defaults to true. + * + * @param {string} siteId The site ID. + * @param {string} url The URL to treat. + * @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param {number} [courseId] Course ID related to the URL. Optional but recommended. + * @return {boolean|Promise} Whether the handler is enabled for the URL and site. + */ + isEnabled(siteId: string, url: string, params: any, courseId?: number): boolean | Promise { + return typeof params.cmid != 'undefined'; + } +} diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 4d4743294..6069ee983 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -95,6 +95,7 @@ import { AddonBlockCalendarMonthModule } from '@addon/block/calendarmonth/calend import { AddonBlockCalendarUpcomingModule } from '@addon/block/calendarupcoming/calendarupcoming.module'; import { AddonBlockCommentsModule } from '@addon/block/comments/comments.module'; import { AddonBlockCompletionStatusModule } from '@addon/block/completionstatus/completionstatus.module'; +import { AddonBlockGlossaryRandomModule } from '@addon/block/glossaryrandom/glossaryrandom.module'; import { AddonBlockHtmlModule } from '@addon/block/html/html.module'; import { AddonBlockMyOverviewModule } from '@addon/block/myoverview/myoverview.module'; import { AddonBlockNewsItemsModule } from '@addon/block/newsitems/newsitems.module'; @@ -227,6 +228,7 @@ export const CORE_PROVIDERS: any[] = [ AddonBlockCalendarUpcomingModule, AddonBlockCommentsModule, AddonBlockCompletionStatusModule, + AddonBlockGlossaryRandomModule, AddonBlockHtmlModule, AddonBlockLearningPlansModule, AddonBlockMyOverviewModule, diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 88734646f..cde20c4dc 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -31,6 +31,7 @@ "addon.block_calendarupcoming.pluginname": " Upcoming events", "addon.block_comments.pluginname": "Comments", "addon.block_completionstatus.pluginname": "Course completion status", + "addon.block_glossaryrandom.pluginname": "Random glossary entry", "addon.block_learningplans.pluginname": "Learning plans", "addon.block_myoverview.all": "All", "addon.block_myoverview.favourites": "Starred",