Skip to content

Commit 817815f

Browse files
committed
MOBILE-1158 contentlinks: Refactor contentlinks to fix multisite checks
1 parent d9d43b7 commit 817815f

File tree

11 files changed

+304
-306
lines changed

11 files changed

+304
-306
lines changed

www/addons/grades/services/grades.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ angular.module('mm.addons.grades')
2121
* @ngdoc service
2222
* @name $mmaGrades
2323
*/
24-
.factory('$mmaGrades', function($q, $log, $mmSite, $mmText, $ionicPlatform, $translate, $mmCourse, $mmCourses) {
24+
.factory('$mmaGrades', function($q, $log, $mmSite, $mmText, $ionicPlatform, $translate, $mmCourse, $mmCourses, $mmSitesManager) {
2525

2626
$log = $log.getInstance('$mmaGrades');
2727

@@ -185,15 +185,20 @@ angular.module('mm.addons.grades')
185185
};
186186

187187
/**
188-
* Returns whether or not the plugin is enabled for the current site.
188+
* Returns whether or not the plugin is enabled for a certain site.
189189
*
190190
* @module mm.addons.grades
191191
* @ngdoc method
192192
* @name $mmaGrades#isPluginEnabled
193-
* @return {Boolean} True if plugin is enabled, false otherwise.
193+
* @param {String} [siteId] Site ID. If not defined, current site.
194+
* @return {Boolean} True if plugin is enabled, false otherwise.
194195
*/
195-
self.isPluginEnabled = function() {
196-
return $mmSite.wsAvailable('gradereport_user_get_grades_table');
196+
self.isPluginEnabled = function(siteId) {
197+
siteId = siteId || $mmSite.getId();
198+
199+
return $mmSitesManager.getSite(siteId).then(function(site) {
200+
return site.wsAvailable('gradereport_user_get_grades_table');
201+
});
197202
};
198203

199204
/**
@@ -202,15 +207,16 @@ angular.module('mm.addons.grades')
202207
* @module mm.addons.grades
203208
* @ngdoc method
204209
* @name $mmaGrades#isPluginEnabledForCourse
205-
* @param {Number} courseId Course ID.
206-
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
210+
* @param {Number} courseId Course ID.
211+
* @param {String} [siteId] Site ID. If not defined, current site.
212+
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
207213
*/
208-
self.isPluginEnabledForCourse = function(courseId) {
214+
self.isPluginEnabledForCourse = function(courseId, siteId) {
209215
if (!courseId) {
210216
return $q.reject();
211217
}
212218

213-
return $mmCourses.getUserCourse(courseId, true).then(function(course) {
219+
return $mmCourses.getUserCourse(courseId, true, siteId).then(function(course) {
214220
if (course && typeof course.showgrades != 'undefined' && !course.showgrades) {
215221
return false;
216222
}

www/addons/grades/services/handlers.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ angular.module('mm.addons.grades')
3939
/**
4040
* Check if handler is enabled.
4141
*
42-
* @return {Boolean} True if handler is enabled, false otherwise.
42+
* @return {Promise} Promise resolved with true if handler is enabled, false otherwise.
4343
*/
4444
self.isEnabled = function() {
4545
return $mmaGrades.isPluginEnabled();
@@ -104,7 +104,7 @@ angular.module('mm.addons.grades')
104104
/**
105105
* Check if handler is enabled.
106106
*
107-
* @return {Boolean} True if handler is enabled, false otherwise.
107+
* @return {Promise} Promise resolved with true if handler is enabled, false otherwise.
108108
*/
109109
self.isEnabled = function() {
110110
return $mmaGrades.isPluginEnabled();
@@ -167,37 +167,54 @@ angular.module('mm.addons.grades')
167167
var self = {};
168168

169169
/**
170-
* Whether or not the handler is enabled for the site.
170+
* Whether or not the handler is enabled for a certain site and course.
171171
*
172-
* @return {Boolean}
172+
* @param {String} siteId Site ID.
173+
* @param {Number} courseId Course ID.
174+
* @return {Promise} Promise resolved with true if enabled.
173175
*/
174-
self.isEnabled = function() {
175-
return $mmaGrades.isPluginEnabled();
176-
};
176+
function isEnabled(siteId, courseId) {
177+
return $mmaGrades.isPluginEnabled(siteId).then(function(enabled) {
178+
if (enabled) {
179+
return $mmaGrades.isPluginEnabledForCourse(courseId, siteId);
180+
}
181+
});
182+
}
177183

178184
/**
179185
* Get actions to perform with the link.
180186
*
181-
* @param {String} url URL to treat.
182-
* @return {Object[]} List of actions. See {@link $mmContentLinksDelegate#registerLinkHandler}.
187+
* @param {String[]} siteIds Site IDs the URL belongs to.
188+
* @param {String} url URL to treat.
189+
* @return {Object[]} Promise resolved with the list of actions.
190+
* See {@link $mmContentLinksDelegate#registerLinkHandler}.
183191
*/
184-
self.getActions = function(url) {
192+
self.getActions = function(siteIds, url) {
185193
// Check it's a grade URL.
186194
if (url.indexOf('/grade/report/user/index.php') > -1) {
187195
var params = $mmUtil.extractUrlParams(url);
188196
if (typeof params.id != 'undefined') {
189-
// Return actions.
190-
return [{
191-
message: 'mm.core.view',
192-
icon: 'ion-eye',
193-
action: function(siteId) {
194-
var stateParams = {
195-
course: {id: parseInt(params.id, 10)},
196-
userid: parseInt(params.userid, 10)
197-
};
198-
$mmContentLinksHelper.goInSite('site.grades', stateParams, siteId);
197+
var courseId = parseInt(params.id, 10);
198+
// Pass false because all sites should have the same siteurl.
199+
return $mmContentLinksHelper.filterSupportedSites(siteIds, isEnabled, false, courseId).then(function(ids) {
200+
if (!ids.length) {
201+
return [];
202+
} else {
203+
// Return actions.
204+
return [{
205+
message: 'mm.core.view',
206+
icon: 'ion-eye',
207+
sites: ids,
208+
action: function(siteId) {
209+
var stateParams = {
210+
course: {id: courseId},
211+
userid: parseInt(params.userid, 10)
212+
};
213+
$mmContentLinksHelper.goInSite('site.grades', stateParams, siteId);
214+
}
215+
}];
199216
}
200-
}];
217+
});
201218
}
202219
}
203220
return [];

www/addons/messages/services/handlers.js

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -293,62 +293,73 @@ angular.module('mm.addons.messages')
293293
var self = {};
294294

295295
/**
296-
* Whether or not the handler is enabled for the site.
296+
* Whether or not the handler is enabled for a certain site.
297297
*
298-
* @return {Boolean}
298+
* @param {String} siteId Site ID.
299+
* @return {Promise} Promise resolved with true if enabled.
299300
*/
300-
self.isEnabled = function() {
301-
return $mmaMessages.isPluginEnabled();
302-
};
301+
function isEnabledForSite(siteId) {
302+
return $mmaMessages.isPluginEnabled(siteId);
303+
}
303304

304305
/**
305306
* Get actions to perform with the link.
306307
*
307-
* @param {String} url URL to treat.
308-
* @return {Object[]} List of actions. See {@link $mmContentLinksDelegate#registerLinkHandler}.
308+
* @param {String[]} siteIds Site IDs the URL belongs to.
309+
* @param {String} url URL to treat.
310+
* @return {Object[]} Promise resolved with the list of actions.
311+
* See {@link $mmContentLinksDelegate#registerLinkHandler}.
309312
*/
310-
self.getActions = function(url) {
313+
self.getActions = function(siteIds, url) {
311314
// Check it's a messages URL.
312315
if (url.indexOf('/message/index.php') > -1) {
313-
var params = $mmUtil.extractUrlParams(url);
314-
// Return actions.
315-
return [{
316-
message: 'mm.core.view',
317-
icon: 'ion-eye',
318-
action: function(siteId) {
319-
var stateName,
320-
stateParams;
321-
322-
if (typeof params.user1 != 'undefined' && typeof params.user2 != 'undefined') {
323-
// Check if the current user is in the conversation.
324-
if ($mmSite.getUserId() == params.user1) {
325-
stateName = 'site.messages-discussion';
326-
stateParams = {userId: parseInt(params.user2, 10)};
327-
} else if ($mmSite.getUserId() == params.user2) {
328-
stateName = 'site.messages-discussion';
329-
stateParams = {userId: parseInt(params.user1, 10)};
330-
} else {
331-
// He isn't, open in browser.
332-
$mmUtil.openInBrowser(url);
333-
return;
316+
// Pass false because all sites should have the same siteurl.
317+
return $mmContentLinksHelper.filterSupportedSites(siteIds, isEnabledForSite, false).then(function(ids) {
318+
if (!ids.length) {
319+
return [];
320+
} else {
321+
// Return actions.
322+
var params = $mmUtil.extractUrlParams(url);
323+
return [{
324+
message: 'mm.core.view',
325+
icon: 'ion-eye',
326+
sites: ids,
327+
action: function(siteId) {
328+
var stateName,
329+
stateParams;
330+
331+
if (typeof params.user1 != 'undefined' && typeof params.user2 != 'undefined') {
332+
// Check if the current user is in the conversation.
333+
if ($mmSite.getUserId() == params.user1) {
334+
stateName = 'site.messages-discussion';
335+
stateParams = {userId: parseInt(params.user2, 10)};
336+
} else if ($mmSite.getUserId() == params.user2) {
337+
stateName = 'site.messages-discussion';
338+
stateParams = {userId: parseInt(params.user1, 10)};
339+
} else {
340+
// He isn't, open in browser.
341+
$mmUtil.openInBrowser(url);
342+
return;
343+
}
344+
} else if (typeof params.id != 'undefined') {
345+
stateName = 'site.messages-discussion';
346+
stateParams = {userId: parseInt(params.id, 10)};
347+
}
348+
349+
if (!stateName) {
350+
// Go to messaging index page. We use redirect state to view the side menu.
351+
$state.go('redirect', {
352+
siteid: siteId,
353+
state: 'site.messages',
354+
params: {}
355+
});
356+
} else {
357+
$mmContentLinksHelper.goInSite(stateName, stateParams, siteId);
358+
}
334359
}
335-
} else if (typeof params.id != 'undefined') {
336-
stateName = 'site.messages-discussion';
337-
stateParams = {userId: parseInt(params.id, 10)};
338-
}
339-
340-
if (!stateName) {
341-
// Go to messaging index page. We use redirect state to view the side menu.
342-
$state.go('redirect', {
343-
siteid: siteId,
344-
state: 'site.messages',
345-
params: {}
346-
});
347-
} else {
348-
$mmContentLinksHelper.goInSite(stateName, stateParams, siteId);
349-
}
360+
}];
350361
}
351-
}];
362+
});
352363
}
353364
return [];
354365
};

www/addons/messages/services/messages.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -595,26 +595,30 @@ angular.module('mm.addons.messages')
595595
* @return {Promise} Resolved when enabled, otherwise rejected.
596596
* @protected
597597
*/
598-
self._isMessagingEnabled = function() {
599-
var enabled = $mmSite.canUseAdvancedFeature('messaging', 'unknown');
600-
601-
if (enabled === 'unknown') {
602-
// On older version we cannot check other than calling a WS. If the request
603-
// fails there is a very high chance that messaging is disabled.
604-
$log.debug('Using WS call to check if messaging is enabled.');
605-
return $mmSite.read('core_message_search_contacts', {
606-
searchtext: 'CheckingIfMessagingIsEnabled',
607-
onlymycourses: 0
608-
}, {
609-
emergencyCache: false,
610-
cacheKey: self._getCacheKeyForEnabled()
611-
});
612-
}
598+
self._isMessagingEnabled = function(siteId) {
599+
siteId = siteId || $mmSite.getId();
600+
601+
return $mmSitesManager.getSite(siteId).then(function(site) {
602+
var enabled = site.canUseAdvancedFeature('messaging', 'unknown');
603+
604+
if (enabled === 'unknown') {
605+
// On older version we cannot check other than calling a WS. If the request
606+
// fails there is a very high chance that messaging is disabled.
607+
$log.debug('Using WS call to check if messaging is enabled.');
608+
return site.read('core_message_search_contacts', {
609+
searchtext: 'CheckingIfMessagingIsEnabled',
610+
onlymycourses: 0
611+
}, {
612+
emergencyCache: false,
613+
cacheKey: self._getCacheKeyForEnabled()
614+
});
615+
}
613616

614-
if (enabled) {
615-
return $q.when(true);
616-
}
617-
return $q.reject();
617+
if (enabled) {
618+
return true;
619+
}
620+
return $q.reject();
621+
});
618622
};
619623

620624
/**
@@ -648,30 +652,30 @@ angular.module('mm.addons.messages')
648652
};
649653

650654
/**
651-
* Returns whether or not the plugin is enabled for the current site.
655+
* Returns whether or not the plugin is enabled in a certain site.
652656
*
653657
* Do not abuse this method.
654658
*
655659
* @module mm.addons.messages
656660
* @ngdoc method
657661
* @name $mmaMessages#isPluginEnabled
658-
* @return {Promise} Rejected when not enabled.
659-
*/
660-
self.isPluginEnabled = function() {
661-
var infos,
662-
enabled = $q.when(true);
663-
664-
if (!$mmSite.isLoggedIn()) {
665-
enabled = $q.reject();
666-
} else if (!$mmSite.canUseAdvancedFeature('messaging')) {
667-
enabled = $q.reject();
668-
} else if (!$mmSite.wsAvailable('core_message_get_messages')) {
669-
enabled = $q.reject();
670-
} else {
671-
enabled = self._isMessagingEnabled();
672-
}
662+
* @param {String} [siteId] Site ID. If not defined, current site.
663+
* @return {Promise} Promise resolved with true if enabled, rejected or resolved with false otherwise.
664+
*/
665+
self.isPluginEnabled = function(siteId) {
666+
siteId = siteId || $mmSite.getId();
673667

674-
return enabled;
668+
return $mmSitesManager.getSite(siteId).then(function(site) {
669+
if (!site.canUseAdvancedFeature('messaging')) {
670+
return false;
671+
} else if (!site.wsAvailable('core_message_get_messages')) {
672+
return false;
673+
} else {
674+
return self._isMessagingEnabled(siteId).then(function() {
675+
return true;
676+
});
677+
}
678+
});
675679
};
676680

677681
/**

www/addons/notifications/directives/actions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ angular.module('mm.addons.notifications')
2727
// Directive link function.
2828
function link(scope) {
2929
if (scope.contexturl) {
30-
scope.actions = $mmContentLinksDelegate.getActionsFor(scope.contexturl, scope.courseid);
30+
$mmContentLinksDelegate.getActionsFor(scope.contexturl, scope.courseid).then(function(actions) {
31+
scope.actions = actions;
32+
});
3133
}
3234
}
3335

0 commit comments

Comments
 (0)