2018-01-22 11:10:53 +08:00
|
|
|
import GrantBadgeController from "discourse/mixins/grant-badge-controller";
|
2015-08-19 09:24:09 +08:00
|
|
|
|
2018-01-22 11:10:53 +08:00
|
|
|
export default Ember.Controller.extend(GrantBadgeController, {
|
2016-10-21 01:26:41 +08:00
|
|
|
adminUser: Ember.inject.controller(),
|
|
|
|
user: Ember.computed.alias('adminUser.model'),
|
2018-01-22 11:10:53 +08:00
|
|
|
userBadges: Ember.computed.alias('model'),
|
|
|
|
allBadges: Ember.computed.alias('badges'),
|
2016-10-21 01:26:41 +08:00
|
|
|
|
|
|
|
sortedBadges: Ember.computed.sort('model', 'badgeSortOrder'),
|
|
|
|
badgeSortOrder: ['granted_at:desc'],
|
2014-03-19 22:27:21 +08:00
|
|
|
|
2015-02-25 09:52:43 +08:00
|
|
|
groupedBadges: function(){
|
2015-08-14 03:14:08 +08:00
|
|
|
const allBadges = this.get('model');
|
2015-02-25 09:52:43 +08:00
|
|
|
|
2015-08-14 03:14:08 +08:00
|
|
|
var grouped = _.groupBy(allBadges, badge => badge.badge_id);
|
2015-02-25 09:52:43 +08:00
|
|
|
|
|
|
|
var expanded = [];
|
2015-08-14 03:14:08 +08:00
|
|
|
const expandedBadges = allBadges.get('expandedBadges');
|
2015-02-25 09:52:43 +08: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();
|
2016-04-29 04:49:24 +08:00
|
|
|
}.property('model', 'model.[]', 'model.expandedBadges.[]'),
|
2015-02-25 09:52:43 +08:00
|
|
|
|
2014-03-19 22:27:21 +08:00
|
|
|
actions: {
|
|
|
|
|
2015-02-25 09:52:43 +08:00
|
|
|
expandGroup: function(userBadge){
|
|
|
|
const model = this.get('model');
|
|
|
|
model.set('expandedBadges', model.get('expandedBadges') || []);
|
|
|
|
model.get('expandedBadges').pushObject(userBadge.badge.id);
|
|
|
|
},
|
|
|
|
|
2018-01-22 11:10:53 +08:00
|
|
|
grantBadge() {
|
|
|
|
this.grantBadge(this.get('selectedBadgeId'), this.get('user.username'), this.get('badgeReason'))
|
|
|
|
.then(() => {
|
|
|
|
this.set('badgeReason', '');
|
|
|
|
Ember.run.next(() => {
|
|
|
|
// Update the selected badge ID after the combobox has re-rendered.
|
|
|
|
const newSelectedBadge = this.get('grantableBadges')[0];
|
|
|
|
if (newSelectedBadge) {
|
|
|
|
this.set('selectedBadgeId', newSelectedBadge.get('id'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function() {
|
|
|
|
// Failure
|
|
|
|
bootbox.alert(I18n.t('generic_error'));
|
2014-03-25 18:15:34 +08:00
|
|
|
});
|
2014-03-19 22:27:21 +08:00
|
|
|
},
|
|
|
|
|
2016-10-21 01:26:41 +08:00
|
|
|
revokeBadge(userBadge) {
|
|
|
|
return bootbox.confirm(I18n.t("admin.badges.revoke_confirm"), I18n.t("no_value"), I18n.t("yes_value"), result => {
|
2014-03-19 22:27:21 +08:00
|
|
|
if (result) {
|
2016-10-21 01:26:41 +08:00
|
|
|
userBadge.revoke().then(() => {
|
|
|
|
this.get('model').removeObject(userBadge);
|
2014-03-19 22:27:21 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|