Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions www/addons/grades/services/grades.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ angular.module('mm.addons.grades')
* @ngdoc service
* @name $mmaGrades
*/
.factory('$mmaGrades', function($q, $log, $mmSite, $mmText, $ionicPlatform, $translate, $mmCourse, $mmCourses) {
.factory('$mmaGrades', function($q, $log, $mmSite, $mmText, $ionicPlatform, $translate, $mmCourse, $mmCourses, $mmSitesManager) {

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

Expand Down Expand Up @@ -185,15 +185,20 @@ angular.module('mm.addons.grades')
};

/**
* Returns whether or not the plugin is enabled for the current site.
* Returns whether or not the plugin is enabled for a certain site.
*
* @module mm.addons.grades
* @ngdoc method
* @name $mmaGrades#isPluginEnabled
* @return {Boolean} True if plugin is enabled, false otherwise.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Boolean} True if plugin is enabled, false otherwise.
*/
self.isPluginEnabled = function() {
return $mmSite.wsAvailable('gradereport_user_get_grades_table');
self.isPluginEnabled = function(siteId) {
siteId = siteId || $mmSite.getId();

return $mmSitesManager.getSite(siteId).then(function(site) {
return site.wsAvailable('gradereport_user_get_grades_table');
});
};

/**
Expand All @@ -202,15 +207,16 @@ angular.module('mm.addons.grades')
* @module mm.addons.grades
* @ngdoc method
* @name $mmaGrades#isPluginEnabledForCourse
* @param {Number} courseId Course ID.
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
* @param {Number} courseId Course ID.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
*/
self.isPluginEnabledForCourse = function(courseId) {
self.isPluginEnabledForCourse = function(courseId, siteId) {
if (!courseId) {
return $q.reject();
}

return $mmCourses.getUserCourse(courseId, true).then(function(course) {
return $mmCourses.getUserCourse(courseId, true, siteId).then(function(course) {
if (course && typeof course.showgrades != 'undefined' && !course.showgrades) {
return false;
}
Expand Down
59 changes: 38 additions & 21 deletions www/addons/grades/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ angular.module('mm.addons.grades')
/**
* Check if handler is enabled.
*
* @return {Boolean} True if handler is enabled, false otherwise.
* @return {Promise} Promise resolved with true if handler is enabled, false otherwise.
*/
self.isEnabled = function() {
return $mmaGrades.isPluginEnabled();
Expand Down Expand Up @@ -104,7 +104,7 @@ angular.module('mm.addons.grades')
/**
* Check if handler is enabled.
*
* @return {Boolean} True if handler is enabled, false otherwise.
* @return {Promise} Promise resolved with true if handler is enabled, false otherwise.
*/
self.isEnabled = function() {
return $mmaGrades.isPluginEnabled();
Expand Down Expand Up @@ -167,37 +167,54 @@ angular.module('mm.addons.grades')
var self = {};

/**
* Whether or not the handler is enabled for the site.
* Whether or not the handler is enabled for a certain site and course.
*
* @return {Boolean}
* @param {String} siteId Site ID.
* @param {Number} courseId Course ID.
* @return {Promise} Promise resolved with true if enabled.
*/
self.isEnabled = function() {
return $mmaGrades.isPluginEnabled();
};
function isEnabled(siteId, courseId) {
return $mmaGrades.isPluginEnabled(siteId).then(function(enabled) {
if (enabled) {
return $mmaGrades.isPluginEnabledForCourse(courseId, siteId);
}
});
}

/**
* Get actions to perform with the link.
*
* @param {String} url URL to treat.
* @return {Object[]} List of actions. See {@link $mmContentLinksDelegate#registerLinkHandler}.
* @param {String[]} siteIds Site IDs the URL belongs to.
* @param {String} url URL to treat.
* @return {Object[]} Promise resolved with the list of actions.
* See {@link $mmContentLinksDelegate#registerLinkHandler}.
*/
self.getActions = function(url) {
self.getActions = function(siteIds, url) {
// Check it's a grade URL.
if (url.indexOf('/grade/report/user/index.php') > -1) {
var params = $mmUtil.extractUrlParams(url);
if (typeof params.id != 'undefined') {
// Return actions.
return [{
message: 'mm.core.view',
icon: 'ion-eye',
action: function(siteId) {
var stateParams = {
course: {id: parseInt(params.id, 10)},
userid: parseInt(params.userid, 10)
};
$mmContentLinksHelper.goInSite('site.grades', stateParams, siteId);
var courseId = parseInt(params.id, 10);
// Pass false because all sites should have the same siteurl.
return $mmContentLinksHelper.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) {
var stateParams = {
course: {id: courseId},
userid: parseInt(params.userid, 10)
};
$mmContentLinksHelper.goInSite('site.grades', stateParams, siteId);
}
}];
}
}];
});
}
}
return [];
Expand Down
101 changes: 56 additions & 45 deletions www/addons/messages/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,62 +293,73 @@ angular.module('mm.addons.messages')
var self = {};

/**
* Whether or not the handler is enabled for the site.
* Whether or not the handler is enabled for a certain site.
*
* @return {Boolean}
* @param {String} siteId Site ID.
* @return {Promise} Promise resolved with true if enabled.
*/
self.isEnabled = function() {
return $mmaMessages.isPluginEnabled();
};
function isEnabledForSite(siteId) {
return $mmaMessages.isPluginEnabled(siteId);
}

/**
* Get actions to perform with the link.
*
* @param {String} url URL to treat.
* @return {Object[]} List of actions. See {@link $mmContentLinksDelegate#registerLinkHandler}.
* @param {String[]} siteIds Site IDs the URL belongs to.
* @param {String} url URL to treat.
* @return {Object[]} Promise resolved with the list of actions.
* See {@link $mmContentLinksDelegate#registerLinkHandler}.
*/
self.getActions = function(url) {
self.getActions = function(siteIds, url) {
// Check it's a messages URL.
if (url.indexOf('/message/index.php') > -1) {
var params = $mmUtil.extractUrlParams(url);
// Return actions.
return [{
message: 'mm.core.view',
icon: 'ion-eye',
action: function(siteId) {
var stateName,
stateParams;

if (typeof params.user1 != 'undefined' && typeof params.user2 != 'undefined') {
// Check if the current user is in the conversation.
if ($mmSite.getUserId() == params.user1) {
stateName = 'site.messages-discussion';
stateParams = {userId: parseInt(params.user2, 10)};
} else if ($mmSite.getUserId() == params.user2) {
stateName = 'site.messages-discussion';
stateParams = {userId: parseInt(params.user1, 10)};
} else {
// He isn't, open in browser.
$mmUtil.openInBrowser(url);
return;
// Pass false because all sites should have the same siteurl.
return $mmContentLinksHelper.filterSupportedSites(siteIds, isEnabledForSite, false).then(function(ids) {
if (!ids.length) {
return [];
} else {
// Return actions.
var params = $mmUtil.extractUrlParams(url);
return [{
message: 'mm.core.view',
icon: 'ion-eye',
sites: ids,
action: function(siteId) {
var stateName,
stateParams;

if (typeof params.user1 != 'undefined' && typeof params.user2 != 'undefined') {
// Check if the current user is in the conversation.
if ($mmSite.getUserId() == params.user1) {
stateName = 'site.messages-discussion';
stateParams = {userId: parseInt(params.user2, 10)};
} else if ($mmSite.getUserId() == params.user2) {
stateName = 'site.messages-discussion';
stateParams = {userId: parseInt(params.user1, 10)};
} else {
// He isn't, open in browser.
$mmUtil.openInBrowser(url);
return;
}
} else if (typeof params.id != 'undefined') {
stateName = 'site.messages-discussion';
stateParams = {userId: parseInt(params.id, 10)};
}

if (!stateName) {
// Go to messaging index page. We use redirect state to view the side menu.
$state.go('redirect', {
siteid: siteId,
state: 'site.messages',
params: {}
});
} else {
$mmContentLinksHelper.goInSite(stateName, stateParams, siteId);
}
}
} else if (typeof params.id != 'undefined') {
stateName = 'site.messages-discussion';
stateParams = {userId: parseInt(params.id, 10)};
}

if (!stateName) {
// Go to messaging index page. We use redirect state to view the side menu.
$state.go('redirect', {
siteid: siteId,
state: 'site.messages',
params: {}
});
} else {
$mmContentLinksHelper.goInSite(stateName, stateParams, siteId);
}
}];
}
}];
});
}
return [];
};
Expand Down
76 changes: 40 additions & 36 deletions www/addons/messages/services/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,26 +595,30 @@ angular.module('mm.addons.messages')
* @return {Promise} Resolved when enabled, otherwise rejected.
* @protected
*/
self._isMessagingEnabled = function() {
var enabled = $mmSite.canUseAdvancedFeature('messaging', 'unknown');

if (enabled === 'unknown') {
// On older version we cannot check other than calling a WS. If the request
// fails there is a very high chance that messaging is disabled.
$log.debug('Using WS call to check if messaging is enabled.');
return $mmSite.read('core_message_search_contacts', {
searchtext: 'CheckingIfMessagingIsEnabled',
onlymycourses: 0
}, {
emergencyCache: false,
cacheKey: self._getCacheKeyForEnabled()
});
}
self._isMessagingEnabled = function(siteId) {
siteId = siteId || $mmSite.getId();

return $mmSitesManager.getSite(siteId).then(function(site) {
var enabled = site.canUseAdvancedFeature('messaging', 'unknown');

if (enabled === 'unknown') {
// On older version we cannot check other than calling a WS. If the request
// fails there is a very high chance that messaging is disabled.
$log.debug('Using WS call to check if messaging is enabled.');
return site.read('core_message_search_contacts', {
searchtext: 'CheckingIfMessagingIsEnabled',
onlymycourses: 0
}, {
emergencyCache: false,
cacheKey: self._getCacheKeyForEnabled()
});
}

if (enabled) {
return $q.when(true);
}
return $q.reject();
if (enabled) {
return true;
}
return $q.reject();
});
};

/**
Expand Down Expand Up @@ -648,30 +652,30 @@ angular.module('mm.addons.messages')
};

/**
* Returns whether or not the plugin is enabled for the current site.
* Returns whether or not the plugin is enabled in a certain site.
*
* Do not abuse this method.
*
* @module mm.addons.messages
* @ngdoc method
* @name $mmaMessages#isPluginEnabled
* @return {Promise} Rejected when not enabled.
*/
self.isPluginEnabled = function() {
var infos,
enabled = $q.when(true);

if (!$mmSite.isLoggedIn()) {
enabled = $q.reject();
} else if (!$mmSite.canUseAdvancedFeature('messaging')) {
enabled = $q.reject();
} else if (!$mmSite.wsAvailable('core_message_get_messages')) {
enabled = $q.reject();
} else {
enabled = self._isMessagingEnabled();
}
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with true if enabled, rejected or resolved with false otherwise.
*/
self.isPluginEnabled = function(siteId) {
siteId = siteId || $mmSite.getId();

return enabled;
return $mmSitesManager.getSite(siteId).then(function(site) {
if (!site.canUseAdvancedFeature('messaging')) {
return false;
} else if (!site.wsAvailable('core_message_get_messages')) {
return false;
} else {
return self._isMessagingEnabled(siteId).then(function() {
return true;
});
}
});
};

/**
Expand Down
15 changes: 10 additions & 5 deletions www/addons/mod_assign/services/assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ angular.module('mm.addons.mod_assign')
* @ngdoc controller
* @name $mmaModAssign
*/
.factory('$mmaModAssign', function($mmSite, $q, $mmUser) {
.factory('$mmaModAssign', function($mmSite, $q, $mmUser, $mmSitesManager) {
var self = {};

/**
Expand Down Expand Up @@ -179,15 +179,20 @@ angular.module('mm.addons.mod_assign')
};

/**
* Check if assignments plugin is enabled.
* Check if assignments plugin is enabled in a certain site.
*
* @module mm.addons.mod_assign
* @ngdoc method
* @name $mmaModAssign#isPluginEnabled
* @return {Boolean} True if plugin is enabled, false otherwise.
* @param {String} [siteId] Site ID. If not defined, current site.
* @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
*/
self.isPluginEnabled = function() {
return $mmSite.wsAvailable('mod_assign_get_assignments') && $mmSite.wsAvailable('mod_assign_get_submissions');
self.isPluginEnabled = function(siteId) {
siteId = siteId || $mmSite.getId();

return $mmSitesManager.getSite(siteId).then(function(site) {
return site.wsAvailable('mod_assign_get_assignments') && site.wsAvailable('mod_assign_get_submissions');
});
};

return self;
Expand Down
Loading