diff --git a/src/addon/mod/quiz/providers/grade-link-handler.ts b/src/addon/mod/quiz/providers/grade-link-handler.ts new file mode 100644 index 000000000..1cb006b8c --- /dev/null +++ b/src/addon/mod/quiz/providers/grade-link-handler.ts @@ -0,0 +1,48 @@ +// (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 { CoreSitesProvider } from '@providers/sites'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreContentLinksModuleGradeHandler } from '@core/contentlinks/classes/module-grade-handler'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { AddonModQuizProvider } from './quiz'; + +/** + * Handler to treat links to quiz grade. + */ +@Injectable() +export class AddonModQuizGradeLinkHandler extends CoreContentLinksModuleGradeHandler { + name = 'AddonModQuizGradeLinkHandler'; + canReview = false; + + constructor(courseHelper: CoreCourseHelperProvider, domUtils: CoreDomUtilsProvider, sitesProvider: CoreSitesProvider, + protected quizProvider: AddonModQuizProvider) { + super(courseHelper, domUtils, sitesProvider, 'AddonModQuiz', 'quiz'); + } + + /** + * 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 this.quizProvider.isPluginEnabled(); + } +} diff --git a/src/addon/mod/quiz/providers/index-link-handler.ts b/src/addon/mod/quiz/providers/index-link-handler.ts new file mode 100644 index 000000000..e3181f2b8 --- /dev/null +++ b/src/addon/mod/quiz/providers/index-link-handler.ts @@ -0,0 +1,44 @@ +// (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 { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { AddonModQuizProvider } from './quiz'; + +/** + * Handler to treat links to quiz index. + */ +@Injectable() +export class AddonModQuizIndexLinkHandler extends CoreContentLinksModuleIndexHandler { + name = 'AddonModQuizIndexLinkHandler'; + + constructor(courseHelper: CoreCourseHelperProvider, protected quizProvider: AddonModQuizProvider) { + super(courseHelper, 'AddonModQuiz', 'quiz'); + } + + /** + * 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 this.quizProvider.isPluginEnabled(); + } +} diff --git a/src/addon/mod/quiz/providers/review-link-handler.ts b/src/addon/mod/quiz/providers/review-link-handler.ts new file mode 100644 index 000000000..2aa6dece7 --- /dev/null +++ b/src/addon/mod/quiz/providers/review-link-handler.ts @@ -0,0 +1,117 @@ +// (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 { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; +import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper'; +import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { AddonModQuizProvider } from './quiz'; + +/** + * Handler to treat links to quiz review. + */ +@Injectable() +export class AddonModQuizReviewLinkHandler extends CoreContentLinksHandlerBase { + name = 'AddonModQuizReviewLinkHandler'; + featureName = 'CoreCourseModuleDelegate_AddonModQuiz'; + pattern = /\/mod\/quiz\/review\.php.*([\&\?]attempt=\d+)/; + + constructor(protected domUtils: CoreDomUtilsProvider, protected quizProvider: AddonModQuizProvider, + protected courseHelper: CoreCourseHelperProvider, protected linkHelper: CoreContentLinksHelperProvider) { + 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 { + + courseId = courseId || params.courseid || params.cid; + + return [{ + action: (siteId, navCtrl?): void => { + // Retrieve the quiz ID using the attempt ID. + const modal = this.domUtils.showModalLoading(), + attemptId = parseInt(params.attempt, 10), + page = parseInt(params.page, 10); + let quizId; + + this.getQuizIdByAttemptId(attemptId).then((id) => { + quizId = id; + + // Get the courseId if we don't have it. + if (courseId) { + return courseId; + } else { + return this.courseHelper.getModuleCourseIdByInstance(quizId, 'quiz', siteId); + } + }).then((courseId) => { + // Go to the review page. + const pageParams = { + quizId: quizId, + attemptId: attemptId, + courseId: courseId, + page: params.showall ? -1 : (isNaN(page) ? -1 : page) + }; + + this.linkHelper.goInSite(navCtrl, 'AddonModQuizReviewPage', pageParams, siteId); + }).catch((error) => { + + this.domUtils.showErrorModalDefault(error, 'An error occurred while loading the required data.'); + }).finally(() => { + modal.dismiss(); + }); + } + }]; + } + /** + * Get a quiz ID by attempt ID. + * + * @param {number} attemptId Attempt ID. + * @return {Promise} Promise resolved with the quiz ID. + */ + protected getQuizIdByAttemptId(attemptId: number): Promise { + // Use getAttemptReview to retrieve the quiz ID. + return this.quizProvider.getAttemptReview(attemptId).then((reviewData) => { + if (reviewData.attempt && reviewData.attempt.quiz) { + return reviewData.attempt.quiz; + } + + return Promise.reject(null); + }); + } + + /** + * 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 this.quizProvider.isPluginEnabled(); + } +} diff --git a/src/addon/mod/quiz/quiz.module.ts b/src/addon/mod/quiz/quiz.module.ts index 0745f81b4..4c0126ed6 100644 --- a/src/addon/mod/quiz/quiz.module.ts +++ b/src/addon/mod/quiz/quiz.module.ts @@ -16,6 +16,7 @@ import { NgModule } from '@angular/core'; import { CoreCronDelegate } from '@providers/cron'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; +import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { AddonModQuizAccessRuleDelegate } from './providers/access-rules-delegate'; import { AddonModQuizProvider } from './providers/quiz'; import { AddonModQuizOfflineProvider } from './providers/quiz-offline'; @@ -24,6 +25,9 @@ import { AddonModQuizSyncProvider } from './providers/quiz-sync'; import { AddonModQuizModuleHandler } from './providers/module-handler'; import { AddonModQuizPrefetchHandler } from './providers/prefetch-handler'; import { AddonModQuizSyncCronHandler } from './providers/sync-cron-handler'; +import { AddonModQuizIndexLinkHandler } from './providers/index-link-handler'; +import { AddonModQuizGradeLinkHandler } from './providers/grade-link-handler'; +import { AddonModQuizReviewLinkHandler } from './providers/review-link-handler'; import { AddonModQuizComponentsModule } from './components/components.module'; // Access rules. @@ -60,16 +64,24 @@ import { AddonModQuizAccessTimeLimitModule } from './accessrules/timelimit/timel AddonModQuizSyncProvider, AddonModQuizModuleHandler, AddonModQuizPrefetchHandler, - AddonModQuizSyncCronHandler + AddonModQuizSyncCronHandler, + AddonModQuizIndexLinkHandler, + AddonModQuizGradeLinkHandler, + AddonModQuizReviewLinkHandler ] }) export class AddonModQuizModule { constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModQuizModuleHandler, prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModQuizPrefetchHandler, - cronDelegate: CoreCronDelegate, syncHandler: AddonModQuizSyncCronHandler) { + cronDelegate: CoreCronDelegate, syncHandler: AddonModQuizSyncCronHandler, linksDelegate: CoreContentLinksDelegate, + indexHandler: AddonModQuizIndexLinkHandler, gradeHandler: AddonModQuizGradeLinkHandler, + reviewHandler: AddonModQuizReviewLinkHandler) { moduleDelegate.registerHandler(moduleHandler); prefetchDelegate.registerHandler(prefetchHandler); cronDelegate.register(syncHandler); + linksDelegate.registerHandler(indexHandler); + linksDelegate.registerHandler(gradeHandler); + linksDelegate.registerHandler(reviewHandler); } } diff --git a/src/core/contentlinks/classes/base-handler.ts b/src/core/contentlinks/classes/base-handler.ts index 1b29c509d..9f79ddbda 100644 --- a/src/core/contentlinks/classes/base-handler.ts +++ b/src/core/contentlinks/classes/base-handler.ts @@ -68,7 +68,7 @@ export class CoreContentLinksHandlerBase implements CoreContentLinksHandler { * @return {CoreContentLinksAction[]|Promise} List of (or promise resolved with list of) actions. */ getActions(siteIds: string[], url: string, params: any, courseId?: number): - CoreContentLinksAction[] | Promise { + CoreContentLinksAction[] | Promise { return []; }