|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { Injectable } from '@angular/core'; |
| 16 | +import { NavController } from 'ionic-angular'; |
| 17 | +import { CoreDomUtils } from '@providers/utils/dom'; |
| 18 | +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; |
| 19 | +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; |
| 20 | +import { CoreContentLinksHelper } from '@core/contentlinks/providers/helper'; |
| 21 | +import { CoreCourse } from '@core/course/providers/course'; |
| 22 | +import { AddonModH5PActivity } from './h5pactivity'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Handler to treat links to H5P activity report. |
| 26 | + */ |
| 27 | +@Injectable() |
| 28 | +export class AddonModH5PActivityReportLinkHandler extends CoreContentLinksHandlerBase { |
| 29 | + name = 'AddonModH5PActivityReportLinkHandler'; |
| 30 | + featureName = 'CoreCourseModuleDelegate_AddonModH5PActivity'; |
| 31 | + pattern = /\/mod\/h5pactivity\/report\.php.*([\&\?]a=\d+)/; |
| 32 | + |
| 33 | + constructor() { |
| 34 | + super(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the list of actions for a link (url). |
| 39 | + * |
| 40 | + * @param siteIds List of sites the URL belongs to. |
| 41 | + * @param url The URL to treat. |
| 42 | + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} |
| 43 | + * @param courseId Course ID related to the URL. Optional but recommended. |
| 44 | + * @return List of (or promise resolved with list of) actions. |
| 45 | + */ |
| 46 | + getActions(siteIds: string[], url: string, params: any, courseId?: number): |
| 47 | + CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> { |
| 48 | + courseId = courseId || params.courseid || params.cid; |
| 49 | + |
| 50 | + return [{ |
| 51 | + action: async (siteId, navCtrl?): Promise<void> => { |
| 52 | + try { |
| 53 | + const id = Number(params.a); |
| 54 | + |
| 55 | + if (!courseId) { |
| 56 | + courseId = await this.getCourseId(id, siteId); |
| 57 | + } |
| 58 | + |
| 59 | + if (typeof params.attemptid != 'undefined') { |
| 60 | + this.openAttemptResults(id, Number(params.attemptid), courseId, siteId, navCtrl); |
| 61 | + } else { |
| 62 | + const userId = params.userid ? Number(params.userid) : undefined; |
| 63 | + |
| 64 | + this.openUserAttempts(id, courseId, siteId, userId, navCtrl); |
| 65 | + } |
| 66 | + } catch (error) { |
| 67 | + CoreDomUtils.instance.showErrorModalDefault(error, 'Error processing link.'); |
| 68 | + } |
| 69 | + } |
| 70 | + }]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get course Id for an activity. |
| 75 | + * |
| 76 | + * @param id Activity ID. |
| 77 | + * @param siteId Site ID. |
| 78 | + * @return Promise resolved with course ID. |
| 79 | + */ |
| 80 | + protected async getCourseId(id: number, siteId: string): Promise<number> { |
| 81 | + const modal = CoreDomUtils.instance.showModalLoading(); |
| 82 | + |
| 83 | + try { |
| 84 | + const module = await CoreCourse.instance.getModuleBasicInfoByInstance(id, 'h5pactivity', siteId); |
| 85 | + |
| 86 | + return module.course; |
| 87 | + } finally { |
| 88 | + modal.dismiss(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Check if the handler is enabled for a certain site (site + user) and a URL. |
| 94 | + * If not defined, defaults to true. |
| 95 | + * |
| 96 | + * @param siteId The site ID. |
| 97 | + * @param url The URL to treat. |
| 98 | + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} |
| 99 | + * @param courseId Course ID related to the URL. Optional but recommended. |
| 100 | + * @return Whether the handler is enabled for the URL and site. |
| 101 | + */ |
| 102 | + async isEnabled(siteId: string, url: string, params: any, courseId?: number): Promise<boolean> { |
| 103 | + return AddonModH5PActivity.instance.isPluginEnabled(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Open attempt results. |
| 108 | + * |
| 109 | + * @param id Activity ID. |
| 110 | + * @param attemptId Attempt ID. |
| 111 | + * @param courseId Course ID. |
| 112 | + * @param siteId Site ID. |
| 113 | + * @param navCtrl The NavController to use to navigate. |
| 114 | + * @return Promise resolved when done. |
| 115 | + */ |
| 116 | + protected openAttemptResults(id: number, attemptId: number, courseId: number, siteId: string, navCtrl?: NavController): void { |
| 117 | + |
| 118 | + const pageParams = { |
| 119 | + courseId: courseId, |
| 120 | + h5pActivityId: id, |
| 121 | + attemptId: attemptId, |
| 122 | + }; |
| 123 | + |
| 124 | + CoreContentLinksHelper.instance.goInSite(navCtrl, 'AddonModH5PActivityAttemptResultsPage', pageParams, siteId); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Open user attempts. |
| 129 | + * |
| 130 | + * @param id Activity ID. |
| 131 | + * @param courseId Course ID. |
| 132 | + * @param siteId Site ID. |
| 133 | + * @param userId User ID. If not defined, current user in site. |
| 134 | + * @param navCtrl The NavController to use to navigate. |
| 135 | + * @return Promise resolved when done. |
| 136 | + */ |
| 137 | + protected openUserAttempts(id: number, courseId: number, siteId: string, userId?: number, navCtrl?: NavController): void { |
| 138 | + |
| 139 | + const pageParams = { |
| 140 | + courseId: courseId, |
| 141 | + h5pActivityId: id, |
| 142 | + userId: userId, |
| 143 | + }; |
| 144 | + |
| 145 | + CoreContentLinksHelper.instance.goInSite(navCtrl, 'AddonModH5PActivityUserAttemptsPage', pageParams, siteId); |
| 146 | + } |
| 147 | +} |
0 commit comments