diff --git a/src/addon/mod/h5pactivity/h5pactivity.module.ts b/src/addon/mod/h5pactivity/h5pactivity.module.ts index 771ac2ad6..3053c1168 100644 --- a/src/addon/mod/h5pactivity/h5pactivity.module.ts +++ b/src/addon/mod/h5pactivity/h5pactivity.module.ts @@ -23,6 +23,7 @@ import { AddonModH5PActivityModuleHandler } from './providers/module-handler'; import { AddonModH5PActivityProvider } from './providers/h5pactivity'; import { AddonModH5PActivityPrefetchHandler } from './providers/prefetch-handler'; import { AddonModH5PActivityIndexLinkHandler } from './providers/index-link-handler'; +import { AddonModH5PActivityReportLinkHandler } from './providers/report-link-handler'; // List of providers (without handlers). export const ADDON_MOD_H5P_ACTIVITY_PROVIDERS: any[] = [ @@ -40,6 +41,7 @@ export const ADDON_MOD_H5P_ACTIVITY_PROVIDERS: any[] = [ AddonModH5PActivityModuleHandler, AddonModH5PActivityPrefetchHandler, AddonModH5PActivityIndexLinkHandler, + AddonModH5PActivityReportLinkHandler, ] }) export class AddonModH5PActivityModule { @@ -48,10 +50,12 @@ export class AddonModH5PActivityModule { prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModH5PActivityPrefetchHandler, linksDelegate: CoreContentLinksDelegate, - indexHandler: AddonModH5PActivityIndexLinkHandler) { + indexHandler: AddonModH5PActivityIndexLinkHandler, + reportLinkHandler: AddonModH5PActivityReportLinkHandler) { moduleDelegate.registerHandler(moduleHandler); prefetchDelegate.registerHandler(prefetchHandler); linksDelegate.registerHandler(indexHandler); + linksDelegate.registerHandler(reportLinkHandler); } } diff --git a/src/addon/mod/h5pactivity/providers/h5pactivity.ts b/src/addon/mod/h5pactivity/providers/h5pactivity.ts index 240ff537c..dd9ff494d 100644 --- a/src/addon/mod/h5pactivity/providers/h5pactivity.ts +++ b/src/addon/mod/h5pactivity/providers/h5pactivity.ts @@ -223,6 +223,10 @@ export class AddonModH5PActivityProvider { try { const response: AddonModH5PActivityGetResultsResult = await site.read('mod_h5pactivity_get_results', params, preSets); + if (response.warnings[0]) { + throw response.warnings[0]; // Cannot view attempt. + } + return this.formatAttemptResults(response.attempts[0]); } catch (error) { if (CoreUtils.instance.isWebServiceError(error)) { @@ -445,6 +449,10 @@ export class AddonModH5PActivityProvider { const response: AddonModH5PActivityGetAttemptsResult = await site.read('mod_h5pactivity_get_attempts', params, preSets); + if (response.warnings[0]) { + throw response.warnings[0]; // Cannot view user attempts. + } + return this.formatUserAttempts(response.usersattempts[0]); } diff --git a/src/addon/mod/h5pactivity/providers/report-link-handler.ts b/src/addon/mod/h5pactivity/providers/report-link-handler.ts new file mode 100644 index 000000000..615b151a7 --- /dev/null +++ b/src/addon/mod/h5pactivity/providers/report-link-handler.ts @@ -0,0 +1,147 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// 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 } from 'ionic-angular'; +import { CoreDomUtils } from '@providers/utils/dom'; +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelper } from '@core/contentlinks/providers/helper'; +import { CoreCourse } from '@core/course/providers/course'; +import { AddonModH5PActivity } from './h5pactivity'; + +/** + * Handler to treat links to H5P activity report. + */ +@Injectable() +export class AddonModH5PActivityReportLinkHandler extends CoreContentLinksHandlerBase { + name = 'AddonModH5PActivityReportLinkHandler'; + featureName = 'CoreCourseModuleDelegate_AddonModH5PActivity'; + pattern = /\/mod\/h5pactivity\/report\.php.*([\&\?]a=\d+)/; + + constructor() { + super(); + } + + /** + * Get the list of actions for a link (url). + * + * @param siteIds List of sites the URL belongs to. + * @param url The URL to treat. + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param courseId Course ID related to the URL. Optional but recommended. + * @return List of (or promise resolved with list of) actions. + */ + getActions(siteIds: string[], url: string, params: any, courseId?: number): + CoreContentLinksAction[] | Promise { + courseId = courseId || params.courseid || params.cid; + + return [{ + action: async (siteId, navCtrl?): Promise => { + try { + const id = Number(params.a); + + if (!courseId) { + courseId = await this.getCourseId(id, siteId); + } + + if (typeof params.attemptid != 'undefined') { + this.openAttemptResults(id, Number(params.attemptid), courseId, siteId, navCtrl); + } else { + const userId = params.userid ? Number(params.userid) : undefined; + + this.openUserAttempts(id, courseId, siteId, userId, navCtrl); + } + } catch (error) { + CoreDomUtils.instance.showErrorModalDefault(error, 'Error processing link.'); + } + } + }]; + } + + /** + * Get course Id for an activity. + * + * @param id Activity ID. + * @param siteId Site ID. + * @return Promise resolved with course ID. + */ + protected async getCourseId(id: number, siteId: string): Promise { + const modal = CoreDomUtils.instance.showModalLoading(); + + try { + const module = await CoreCourse.instance.getModuleBasicInfoByInstance(id, 'h5pactivity', siteId); + + return module.course; + } finally { + modal.dismiss(); + } + } + + /** + * Check if the handler is enabled for a certain site (site + user) and a URL. + * If not defined, defaults to true. + * + * @param siteId The site ID. + * @param url The URL to treat. + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param courseId Course ID related to the URL. Optional but recommended. + * @return Whether the handler is enabled for the URL and site. + */ + async isEnabled(siteId: string, url: string, params: any, courseId?: number): Promise { + return AddonModH5PActivity.instance.isPluginEnabled(); + } + + /** + * Open attempt results. + * + * @param id Activity ID. + * @param attemptId Attempt ID. + * @param courseId Course ID. + * @param siteId Site ID. + * @param navCtrl The NavController to use to navigate. + * @return Promise resolved when done. + */ + protected openAttemptResults(id: number, attemptId: number, courseId: number, siteId: string, navCtrl?: NavController): void { + + const pageParams = { + courseId: courseId, + h5pActivityId: id, + attemptId: attemptId, + }; + + CoreContentLinksHelper.instance.goInSite(navCtrl, 'AddonModH5PActivityAttemptResultsPage', pageParams, siteId); + } + + /** + * Open user attempts. + * + * @param id Activity ID. + * @param courseId Course ID. + * @param siteId Site ID. + * @param userId User ID. If not defined, current user in site. + * @param navCtrl The NavController to use to navigate. + * @return Promise resolved when done. + */ + protected openUserAttempts(id: number, courseId: number, siteId: string, userId?: number, navCtrl?: NavController): void { + + const pageParams = { + courseId: courseId, + h5pActivityId: id, + userId: userId, + }; + + CoreContentLinksHelper.instance.goInSite(navCtrl, 'AddonModH5PActivityUserAttemptsPage', pageParams, siteId); + } +} diff --git a/src/core/contentlinks/providers/helper.ts b/src/core/contentlinks/providers/helper.ts index 54bb261c1..fc3d45b5f 100644 --- a/src/core/contentlinks/providers/helper.ts +++ b/src/core/contentlinks/providers/helper.ts @@ -32,6 +32,8 @@ import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins import { CoreSite } from '@classes/site'; import { CoreMainMenuProvider } from '@core/mainmenu/providers/mainmenu'; +import { makeSingleton } from '@singletons/core.singletons'; + /** * Service that provides some features regarding content links. */ @@ -358,3 +360,5 @@ export class CoreContentLinksHelperProvider { } } } + +export class CoreContentLinksHelper extends makeSingleton(CoreContentLinksHelperProvider) {}