2014-07-22 23:20:45 -04:00
|
|
|
export default Ember.ArrayController.extend({
|
2014-03-19 10:27:21 -04:00
|
|
|
needs: ["adminUser"],
|
|
|
|
user: Em.computed.alias('controllers.adminUser'),
|
|
|
|
sortProperties: ['granted_at'],
|
|
|
|
sortAscending: false,
|
|
|
|
|
2015-02-24 20:52:43 -05:00
|
|
|
groupedBadges: function(){
|
2015-08-13 15:14:08 -04:00
|
|
|
const allBadges = this.get('model');
|
2015-02-24 20:52:43 -05:00
|
|
|
|
2015-08-13 15:14:08 -04:00
|
|
|
var grouped = _.groupBy(allBadges, badge => badge.badge_id);
|
2015-02-24 20:52:43 -05:00
|
|
|
|
|
|
|
var expanded = [];
|
2015-08-13 15:14:08 -04:00
|
|
|
const expandedBadges = allBadges.get('expandedBadges');
|
2015-02-24 20:52:43 -05:00
|
|
|
|
|
|
|
_(grouped).each(function(badges){
|
|
|
|
var lastGranted = badges[0].granted_at;
|
|
|
|
|
|
|
|
_.each(badges, function(badge) {
|
|
|
|
lastGranted = lastGranted < badge.granted_at ? badge.granted_at : lastGranted;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(badges.length===1 || _.include(expandedBadges, badges[0].badge.id)){
|
|
|
|
_.each(badges, badge => expanded.push(badge));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = {
|
|
|
|
badge: badges[0].badge,
|
|
|
|
granted_at: lastGranted,
|
|
|
|
badges: badges,
|
|
|
|
count: badges.length,
|
|
|
|
grouped: true
|
|
|
|
};
|
|
|
|
|
|
|
|
expanded.push(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
return _(expanded).sortBy(group => group.granted_at).reverse().value();
|
|
|
|
|
|
|
|
|
|
|
|
}.property('model', 'model.@each', 'model.expandedBadges.@each'),
|
|
|
|
|
2014-03-25 06:15:34 -04:00
|
|
|
/**
|
|
|
|
Array of badges that have not been granted to this user.
|
|
|
|
|
|
|
|
@property grantableBadges
|
|
|
|
@type {Boolean}
|
|
|
|
**/
|
|
|
|
grantableBadges: function() {
|
|
|
|
var granted = {};
|
|
|
|
this.get('model').forEach(function(userBadge) {
|
|
|
|
granted[userBadge.get('badge_id')] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
var badges = [];
|
|
|
|
this.get('badges').forEach(function(badge) {
|
2014-05-21 03:22:42 -04:00
|
|
|
if (badge.get('multiple_grant') || !granted[badge.get('id')]) {
|
2014-03-25 06:15:34 -04:00
|
|
|
badges.push(badge);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-15 20:37:11 -05:00
|
|
|
return _.sortBy(badges, "name");
|
2014-03-25 06:15:34 -04:00
|
|
|
}.property('badges.@each', 'model.@each'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
Whether there are any badges that can be granted.
|
|
|
|
|
|
|
|
@property noBadges
|
|
|
|
@type {Boolean}
|
|
|
|
**/
|
|
|
|
noBadges: Em.computed.empty('grantableBadges'),
|
|
|
|
|
2014-03-19 10:27:21 -04:00
|
|
|
actions: {
|
|
|
|
|
2015-02-24 20:52:43 -05:00
|
|
|
expandGroup: function(userBadge){
|
|
|
|
const model = this.get('model');
|
|
|
|
model.set('expandedBadges', model.get('expandedBadges') || []);
|
|
|
|
model.get('expandedBadges').pushObject(userBadge.badge.id);
|
|
|
|
},
|
|
|
|
|
2014-03-19 10:27:21 -04:00
|
|
|
/**
|
|
|
|
Grant the selected badge to the user.
|
|
|
|
|
|
|
|
@method grantBadge
|
|
|
|
@param {Integer} badgeId id of the badge we want to grant.
|
|
|
|
**/
|
|
|
|
grantBadge: function(badgeId) {
|
|
|
|
var self = this;
|
2015-02-24 20:52:43 -05:00
|
|
|
Discourse.UserBadge.grant(badgeId, this.get('user.username'), this.get('badgeReason')).then(function(userBadge) {
|
|
|
|
self.set('badgeReason', '');
|
2014-03-19 10:27:21 -04:00
|
|
|
self.pushObject(userBadge);
|
2014-03-25 06:15:34 -04:00
|
|
|
Ember.run.next(function() {
|
|
|
|
// Update the selected badge ID after the combobox has re-rendered.
|
2014-03-28 04:49:30 -04:00
|
|
|
var newSelectedBadge = self.get('grantableBadges')[0];
|
|
|
|
if (newSelectedBadge) {
|
|
|
|
self.set('selectedBadgeId', newSelectedBadge.get('id'));
|
|
|
|
}
|
2014-03-25 06:15:34 -04:00
|
|
|
});
|
2014-03-19 10:27:21 -04:00
|
|
|
}, function() {
|
|
|
|
// Failure
|
|
|
|
bootbox.alert(I18n.t('generic_error'));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Revoke the selected userBadge.
|
|
|
|
|
|
|
|
@method revokeBadge
|
|
|
|
@param {Discourse.UserBadge} userBadge the `Discourse.UserBadge` instance that needs to be revoked.
|
|
|
|
**/
|
|
|
|
revokeBadge: function(userBadge) {
|
|
|
|
var self = this;
|
|
|
|
return bootbox.confirm(I18n.t("admin.badges.revoke_confirm"), I18n.t("no_value"), I18n.t("yes_value"), function(result) {
|
|
|
|
if (result) {
|
|
|
|
userBadge.revoke().then(function() {
|
|
|
|
self.get('model').removeObject(userBadge);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|