Skip to content

Commit bea4e50

Browse files
committed
MOBILE-3413 h5pactivity: Support links to reports
1 parent dc85f96 commit bea4e50

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

src/addon/mod/h5pactivity/h5pactivity.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { AddonModH5PActivityModuleHandler } from './providers/module-handler';
2323
import { AddonModH5PActivityProvider } from './providers/h5pactivity';
2424
import { AddonModH5PActivityPrefetchHandler } from './providers/prefetch-handler';
2525
import { AddonModH5PActivityIndexLinkHandler } from './providers/index-link-handler';
26+
import { AddonModH5PActivityReportLinkHandler } from './providers/report-link-handler';
2627

2728
// List of providers (without handlers).
2829
export const ADDON_MOD_H5P_ACTIVITY_PROVIDERS: any[] = [
@@ -40,6 +41,7 @@ export const ADDON_MOD_H5P_ACTIVITY_PROVIDERS: any[] = [
4041
AddonModH5PActivityModuleHandler,
4142
AddonModH5PActivityPrefetchHandler,
4243
AddonModH5PActivityIndexLinkHandler,
44+
AddonModH5PActivityReportLinkHandler,
4345
]
4446
})
4547
export class AddonModH5PActivityModule {
@@ -48,10 +50,12 @@ export class AddonModH5PActivityModule {
4850
prefetchDelegate: CoreCourseModulePrefetchDelegate,
4951
prefetchHandler: AddonModH5PActivityPrefetchHandler,
5052
linksDelegate: CoreContentLinksDelegate,
51-
indexHandler: AddonModH5PActivityIndexLinkHandler) {
53+
indexHandler: AddonModH5PActivityIndexLinkHandler,
54+
reportLinkHandler: AddonModH5PActivityReportLinkHandler) {
5255

5356
moduleDelegate.registerHandler(moduleHandler);
5457
prefetchDelegate.registerHandler(prefetchHandler);
5558
linksDelegate.registerHandler(indexHandler);
59+
linksDelegate.registerHandler(reportLinkHandler);
5660
}
5761
}

src/addon/mod/h5pactivity/providers/h5pactivity.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ export class AddonModH5PActivityProvider {
223223
try {
224224
const response: AddonModH5PActivityGetResultsResult = await site.read('mod_h5pactivity_get_results', params, preSets);
225225

226+
if (response.warnings[0]) {
227+
throw response.warnings[0]; // Cannot view attempt.
228+
}
229+
226230
return this.formatAttemptResults(response.attempts[0]);
227231
} catch (error) {
228232
if (CoreUtils.instance.isWebServiceError(error)) {
@@ -445,6 +449,10 @@ export class AddonModH5PActivityProvider {
445449

446450
const response: AddonModH5PActivityGetAttemptsResult = await site.read('mod_h5pactivity_get_attempts', params, preSets);
447451

452+
if (response.warnings[0]) {
453+
throw response.warnings[0]; // Cannot view user attempts.
454+
}
455+
448456
return this.formatUserAttempts(response.usersattempts[0]);
449457
}
450458

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

src/core/contentlinks/providers/helper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins
3232
import { CoreSite } from '@classes/site';
3333
import { CoreMainMenuProvider } from '@core/mainmenu/providers/mainmenu';
3434

35+
import { makeSingleton } from '@singletons/core.singletons';
36+
3537
/**
3638
* Service that provides some features regarding content links.
3739
*/
@@ -358,3 +360,5 @@ export class CoreContentLinksHelperProvider {
358360
}
359361
}
360362
}
363+
364+
export class CoreContentLinksHelper extends makeSingleton(CoreContentLinksHelperProvider) {}

0 commit comments

Comments
 (0)