From 45ac1535e7a61d4d8c64f6c76d501de2f289e312 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 26 Jul 2016 12:41:51 +0200 Subject: [PATCH] MOBILE-1710 links: Handle grade.php for quiz and scorm --- www/addons/mod/quiz/services/handlers.js | 8 ++- www/addons/mod/scorm/services/handlers.js | 19 +++++-- .../contentlinks/services/helper.js | 54 +++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/www/addons/mod/quiz/services/handlers.js b/www/addons/mod/quiz/services/handlers.js index 88feb0d6331..1887d626f0b 100644 --- a/www/addons/mod/quiz/services/handlers.js +++ b/www/addons/mod/quiz/services/handlers.js @@ -143,7 +143,7 @@ angular.module('mm.addons.mod_quiz') self.linksHandler = function() { var self = {}, - patterns = ['/mod/quiz/view.php', '/mod/quiz/review.php']; + patterns = ['/mod/quiz/view.php', '/mod/quiz/review.php', '/mod/quiz/grade.php']; /** * Whether or not the handler is enabled for a certain site. @@ -245,7 +245,13 @@ angular.module('mm.addons.mod_quiz') } }); } + } else if (url.indexOf(patterns[2]) > -1) { + // Quiz grade. + // @todo Go to review user best attempt if it isn't current user. + return $mmContentLinksHelper.treatModuleGradeUrl(siteIds, url, isEnabled, courseId); } + + return $q.when([]); }; diff --git a/www/addons/mod/scorm/services/handlers.js b/www/addons/mod/scorm/services/handlers.js index b71f95b7455..3d469bd0f46 100644 --- a/www/addons/mod/scorm/services/handlers.js +++ b/www/addons/mod/scorm/services/handlers.js @@ -172,7 +172,8 @@ angular.module('mm.addons.mod_scorm') */ self.linksHandler = function() { - var self = {}; + var self = {}, + patterns = ['/mod/scorm/view.php', '/mod/scorm/grade.php']; /** * Whether or not the handler is enabled for a certain site. @@ -201,9 +202,15 @@ angular.module('mm.addons.mod_scorm') */ self.getActions = function(siteIds, url, courseId) { // Check it's a SCORM URL. - if (typeof self.handles(url) != 'undefined') { + if (url.indexOf(patterns[0]) > -1) { + // SCORM index. return $mmContentLinksHelper.treatModuleIndexUrl(siteIds, url, isEnabled, courseId); + } else if (url.indexOf(patterns[1]) > -1) { + // SCORM grade. + // @todo Go to user attempts list if it isn't current user. + return $mmContentLinksHelper.treatModuleGradeUrl(siteIds, url, isEnabled, courseId); } + return $q.when([]); }; @@ -214,9 +221,11 @@ angular.module('mm.addons.mod_scorm') * @return {String} Site URL. Undefined if the URL doesn't belong to this handler. */ self.handles = function(url) { - var position = url.indexOf('/mod/scorm/view.php'); - if (position > -1) { - return url.substr(0, position); + for (var i = 0; i < patterns.length; i++) { + var position = url.indexOf(patterns[i]); + if (position > -1) { + return url.substr(0, position); + } } }; diff --git a/www/core/components/contentlinks/services/helper.js b/www/core/components/contentlinks/services/helper.js index dc669d8036d..d68b6a1af4b 100644 --- a/www/core/components/contentlinks/services/helper.js +++ b/www/core/components/contentlinks/services/helper.js @@ -273,6 +273,60 @@ angular.module('mm.core.contentlinks') }); }; + /** + * Treats a module grade URL (grade.php). + * + * @module mm.core.contentlinks + * @ngdoc method + * @name $mmContentLinksHelper#treatModuleGradeUrl + * @param {String[]} siteIds Site IDs the URL belongs to. + * @param {String} url URL to treat. + * @param {Function} isEnabled Function to check if the module is enabled. @see $mmContentLinksHelper#filterSupportedSites. + * @param {Number} [courseId] Course ID related to the URL. + * @param {Function} [gotoReview] Function to go to review page if user is not current user. + * @return {Promise} Promise resolved with the list of actions. + */ + self.treatModuleGradeUrl = function(siteIds, url, isEnabled, courseId, gotoReview) { + var params = $mmUtil.extractUrlParams(url); + if (typeof params.id != 'undefined') { + // If courseId is not set we check if it's set in the URL as a param. + courseId = courseId || params.courseid || params.cid; + + // Pass false because all sites should have the same siteurl. + return self.filterSupportedSites(siteIds, isEnabled, false, courseId).then(function(ids) { + if (!ids.length) { + return []; + } else { + // Return actions. + return [{ + message: 'mm.core.view', + icon: 'ion-eye', + sites: ids, + action: function(siteId) { + // Check if userid is the site's current user. + var modal = $mmUtil.showModalLoading(); + $mmSitesManager.getSite(siteId).then(function(site) { + if (!params.userid || params.userid == site.getUserId()) { + // No user specified or current user. Navigate to module. + $mmCourseHelper.navigateToModule(parseInt(params.id, 10), siteId, courseId); + } else if (angular.isFunction(gotoReview)) { + // gotoReview function is defined, use it. + gotoReview(url, params, courseId, siteId); + } else { + // Not current user and no gotoReview function specified, open it in browser. + $mmUtil.openInBrowser(url); + } + }).finally(function() { + modal.dismiss(); + }); + } + }]; + } + }); + } + return $q.when([]); + }; + /** * Treats a URL that belongs to a module's index page. *