2016-01-28 23:48:14 +01:00
|
|
|
import Ember from 'ember';
|
2015-11-02 23:02:59 +01:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
dateGroups: Ember.computed.reads('pollController.dateGroups'),
|
2015-11-03 12:57:41 +01:00
|
|
|
dates: Ember.computed.reads('pollController.dates'),
|
|
|
|
pollController: Ember.inject.controller('poll'),
|
|
|
|
sortedUsers: Ember.computed.sort('pollController.model.users', 'usersSorting'),
|
|
|
|
usersSorting: ['creationDate'],
|
2015-11-02 23:02:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* evaluates poll data
|
|
|
|
* if free text answers are allowed evaluation is disabled
|
|
|
|
*/
|
|
|
|
evaluation: function() {
|
|
|
|
// disable evaluation if answer type is free text
|
|
|
|
if (this.get('model.answerType') === 'FreeText') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-01-28 23:48:14 +01:00
|
|
|
let evaluation = [];
|
|
|
|
let options = [];
|
|
|
|
let lookup = [];
|
2015-11-02 23:02:59 +01:00
|
|
|
|
|
|
|
// init options array
|
2016-01-28 23:48:14 +01:00
|
|
|
this.get('model.options').forEach(function(option, index) {
|
2015-11-02 23:02:59 +01:00
|
|
|
options[index] = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
// init array of evalutation objects
|
|
|
|
// create object for every possible answer
|
2016-01-28 23:48:14 +01:00
|
|
|
this.get('model.answers').forEach(function(answer) {
|
2015-11-02 23:02:59 +01:00
|
|
|
evaluation.push({
|
|
|
|
id: answer.label,
|
|
|
|
label: answer.label,
|
|
|
|
options: Ember.$.extend([], options)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// create object for no answer if answers are not forced
|
2016-01-28 23:48:14 +01:00
|
|
|
if (!this.get('model.forceAnswer')) {
|
2015-11-02 23:02:59 +01:00
|
|
|
evaluation.push({
|
|
|
|
id: null,
|
|
|
|
label: 'no answer',
|
|
|
|
options: Ember.$.extend([], options)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// create lookup array
|
2016-01-28 23:48:14 +01:00
|
|
|
evaluation.forEach(function(value, index) {
|
2015-11-02 23:02:59 +01:00
|
|
|
lookup[value.id] = index;
|
|
|
|
});
|
|
|
|
|
|
|
|
// loop over all users
|
2016-01-28 23:48:14 +01:00
|
|
|
this.get('model.users').forEach(function(user) {
|
2015-11-02 23:02:59 +01:00
|
|
|
// loop over all selections of the user
|
2016-01-28 23:48:14 +01:00
|
|
|
user.get('selections').forEach(function(selection, optionindex) {
|
|
|
|
let answerindex;
|
2015-11-02 23:02:59 +01:00
|
|
|
|
|
|
|
// get answer index by lookup array
|
|
|
|
if (typeof lookup[selection.value.label] === 'undefined') {
|
|
|
|
answerindex = lookup[null];
|
2016-01-28 23:48:14 +01:00
|
|
|
} else {
|
2015-11-02 23:02:59 +01:00
|
|
|
answerindex = lookup[selection.value.label];
|
|
|
|
}
|
|
|
|
|
|
|
|
// increment counter
|
|
|
|
try {
|
2016-01-28 23:48:14 +01:00
|
|
|
evaluation[answerindex].options[optionindex] = evaluation[answerindex].options[optionindex] + 1;
|
2015-11-02 23:02:59 +01:00
|
|
|
} catch (e) {
|
|
|
|
// ToDo: Throw an error
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return evaluation;
|
|
|
|
}.property('model.users.@each'),
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate colspan for a row which should use all columns in table
|
|
|
|
* used by evaluation row
|
|
|
|
*/
|
2016-01-28 23:48:14 +01:00
|
|
|
fullRowColspan: Ember.computed('model.options.@each', function() {
|
2015-11-02 23:02:59 +01:00
|
|
|
return this.get('model.options.length') + 2;
|
2016-01-28 23:48:14 +01:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-01-28 23:48:14 +01:00
|
|
|
isEvaluable: Ember.computed('model.users.@each', 'model.isFreeText', function() {
|
|
|
|
if (
|
2015-11-02 23:02:59 +01:00
|
|
|
!this.get('model.isFreeText') &&
|
|
|
|
this.get('model.users.length') > 0
|
|
|
|
) {
|
|
|
|
return true;
|
2016-01-28 23:48:14 +01:00
|
|
|
} else {
|
2015-11-02 23:02:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-28 23:48:14 +01:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
|
|
|
optionCount: function() {
|
|
|
|
return this.get('model.options.length');
|
|
|
|
}.property('model.options')
|
|
|
|
});
|