2015-11-03 12:57:41 +01:00
|
|
|
import Ember from 'ember';
|
2016-02-22 23:42:01 +01:00
|
|
|
import moment from 'moment';
|
2015-11-03 12:57:41 +01:00
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
classNames: ['evaluation-summary'],
|
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
evaluationBestOptions: Ember.computed('poll.users.[]', function() {
|
2016-01-19 05:46:52 +01:00
|
|
|
let options = [];
|
|
|
|
let bestOptions = [];
|
2015-11-03 12:57:41 +01:00
|
|
|
// can not evaluate answer type free text
|
2016-01-19 05:46:52 +01:00
|
|
|
if (this.get('poll.isFreeText')) {
|
2015-11-03 12:57:41 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// can not evaluate a poll without users
|
2016-01-19 05:46:52 +01:00
|
|
|
if (Ember.isEmpty(this.get('poll.users'))) {
|
2015-11-03 12:57:41 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-01-19 05:46:52 +01:00
|
|
|
this.get('poll.users').forEach(function(user) {
|
|
|
|
user.get('selections').forEach(function(selection, i) {
|
|
|
|
if (options.length - 1 < i) {
|
2015-11-03 12:57:41 +01:00
|
|
|
options.push({
|
|
|
|
answers: [],
|
|
|
|
key: i,
|
|
|
|
score: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-19 05:46:52 +01:00
|
|
|
if (typeof options[i].answers[selection.get('type')] === 'undefined') {
|
2015-11-03 12:57:41 +01:00
|
|
|
options[i].answers[selection.get('type')] = 0;
|
|
|
|
}
|
|
|
|
options[i].answers[selection.get('type')]++;
|
|
|
|
|
|
|
|
switch (selection.get('type')) {
|
|
|
|
case 'yes':
|
|
|
|
options[i].score += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'maybe':
|
|
|
|
options[i].score += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'no':
|
|
|
|
options[i].score -= 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
options.sort(function(a, b) {
|
2016-03-20 16:05:20 +01:00
|
|
|
return b.score - a.score;
|
2015-11-03 12:57:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
bestOptions.push(
|
|
|
|
options[0]
|
|
|
|
);
|
2016-01-19 05:46:52 +01:00
|
|
|
let i = 1;
|
2016-01-20 03:19:10 +01:00
|
|
|
while (true) {
|
2015-11-03 12:57:41 +01:00
|
|
|
if (
|
|
|
|
typeof options[i] !== 'undefined' &&
|
|
|
|
bestOptions[0].score === options[i].score
|
|
|
|
) {
|
|
|
|
bestOptions.push(
|
|
|
|
options[i]
|
|
|
|
);
|
2016-01-20 03:19:10 +01:00
|
|
|
} else {
|
2015-11-03 12:57:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2016-01-19 05:46:52 +01:00
|
|
|
bestOptions.forEach((bestOption, i) => {
|
|
|
|
if (this.get('poll.isFindADate')) {
|
2016-03-20 15:30:54 +01:00
|
|
|
const date = this.get('dates').objectAt(bestOption.key);
|
2016-02-22 23:42:01 +01:00
|
|
|
const format = date.hasTime ? 'LLLL' : moment.localeData()
|
|
|
|
.longDateFormat('LLLL')
|
|
|
|
.replace(
|
|
|
|
moment.localeData().longDateFormat('LT'), '')
|
|
|
|
.trim();
|
|
|
|
bestOptions[i].title = date.title.format(format);
|
2016-01-20 03:19:10 +01:00
|
|
|
} else {
|
2016-03-20 15:30:54 +01:00
|
|
|
const option = this.get('poll.options').objectAt(bestOption.key);
|
2016-02-22 23:42:01 +01:00
|
|
|
bestOptions[i].title = option.get('title');
|
2015-11-03 12:57:41 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return bestOptions;
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-03 12:57:41 +01:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
evaluationBestOptionsMultiple: Ember.computed('evaluationBestOptions', function() {
|
2015-11-03 12:57:41 +01:00
|
|
|
if (this.get('evaluationBestOptions.length') > 1) {
|
|
|
|
return true;
|
2016-01-20 03:19:10 +01:00
|
|
|
} else {
|
2015-11-03 12:57:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-03 12:57:41 +01:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
evaluationLastParticipation: Ember.computed('sortedUsers.[]', function() {
|
2015-11-03 12:57:41 +01:00
|
|
|
return this.get('sortedUsers.lastObject.creationDate');
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-03 12:57:41 +01:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
evaluationParticipants: Ember.computed('poll.users.[]', function() {
|
2015-11-03 12:57:41 +01:00
|
|
|
return this.get('poll.users.length');
|
2016-05-20 23:43:05 +02:00
|
|
|
})
|
2015-11-03 12:57:41 +01:00
|
|
|
});
|