2018-11-01 03:13:19 +08:00
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import Polls from '/imports/api/polls';
|
2021-05-20 22:53:52 +08:00
|
|
|
import caseInsensitiveReducer from '/imports/utils/caseInsensitiveReducer';
|
2018-11-01 03:13:19 +08:00
|
|
|
|
2021-02-06 00:29:58 +08:00
|
|
|
const POLL_AVATAR_COLOR = '#3B48A9';
|
2021-05-20 22:53:52 +08:00
|
|
|
const MAX_POLL_RESULT_BARS = 20;
|
2021-02-06 00:29:58 +08:00
|
|
|
|
2018-11-01 03:13:19 +08:00
|
|
|
// 'YN' = Yes,No
|
2021-01-24 09:22:40 +08:00
|
|
|
// 'YNA' = Yes,No,Abstention
|
2018-11-01 03:13:19 +08:00
|
|
|
// 'TF' = True,False
|
|
|
|
// 'A-2' = A,B
|
|
|
|
// 'A-3' = A,B,C
|
|
|
|
// 'A-4' = A,B,C,D
|
|
|
|
// 'A-5' = A,B,C,D,E
|
2021-01-24 09:22:40 +08:00
|
|
|
const pollTypes = ['YN', 'YNA', 'TF', 'A-2', 'A-3', 'A-4', 'A-5', 'custom'];
|
2018-11-01 03:13:19 +08:00
|
|
|
|
2019-05-23 02:00:44 +08:00
|
|
|
const pollAnswerIds = {
|
|
|
|
true: {
|
|
|
|
id: 'app.poll.answer.true',
|
|
|
|
description: 'label for poll answer True',
|
|
|
|
},
|
|
|
|
false: {
|
|
|
|
id: 'app.poll.answer.false',
|
|
|
|
description: 'label for poll answer False',
|
|
|
|
},
|
|
|
|
yes: {
|
|
|
|
id: 'app.poll.answer.yes',
|
|
|
|
description: 'label for poll answer Yes',
|
|
|
|
},
|
|
|
|
no: {
|
|
|
|
id: 'app.poll.answer.no',
|
|
|
|
description: 'label for poll answer No',
|
|
|
|
},
|
2021-01-24 09:22:40 +08:00
|
|
|
abstention: {
|
|
|
|
id: 'app.poll.answer.abstention',
|
|
|
|
description: 'label for poll answer Abstention',
|
|
|
|
},
|
2019-05-23 02:00:44 +08:00
|
|
|
a: {
|
|
|
|
id: 'app.poll.answer.a',
|
|
|
|
description: 'label for poll answer A',
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
id: 'app.poll.answer.b',
|
|
|
|
description: 'label for poll answer B',
|
|
|
|
},
|
|
|
|
c: {
|
|
|
|
id: 'app.poll.answer.c',
|
|
|
|
description: 'label for poll answer C',
|
|
|
|
},
|
|
|
|
d: {
|
|
|
|
id: 'app.poll.answer.d',
|
|
|
|
description: 'label for poll answer D',
|
|
|
|
},
|
|
|
|
e: {
|
|
|
|
id: 'app.poll.answer.e',
|
|
|
|
description: 'label for poll answer E',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-05-20 22:53:52 +08:00
|
|
|
const getPollResultString = (isDefaultPoll, answers, numRespondents) => {
|
|
|
|
let responded = 0;
|
|
|
|
let resultString = '';
|
|
|
|
let optionsString = '';
|
|
|
|
|
|
|
|
answers.map((item) => {
|
|
|
|
responded += item.numVotes;
|
|
|
|
return item;
|
|
|
|
}).reduce(caseInsensitiveReducer, []).map((item) => {
|
|
|
|
const numResponded = responded === numRespondents ? numRespondents : responded;
|
|
|
|
const pct = Math.round(item.numVotes / numResponded * 100);
|
|
|
|
const pctBars = "|".repeat(pct * MAX_POLL_RESULT_BARS / 100);
|
|
|
|
const pctFotmatted = `${Number.isNaN(pct) ? 0 : pct}%`;
|
|
|
|
if (isDefaultPoll) {
|
|
|
|
resultString += `${item.key}: ${item.numVotes || 0} |${pctBars} ${pctFotmatted}\n`;
|
|
|
|
} else {
|
|
|
|
resultString += `${item.id+1}: ${item.numVotes || 0} |${pctBars} ${pctFotmatted}\n`;
|
|
|
|
optionsString += `${item.id+1}: ${item.key}\n`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return { resultString, optionsString };
|
|
|
|
}
|
|
|
|
|
2018-11-01 03:13:19 +08:00
|
|
|
export default {
|
2020-07-20 22:34:46 +08:00
|
|
|
amIPresenter: () => Users.findOne(
|
|
|
|
{ userId: Auth.userID },
|
|
|
|
{ fields: { presenter: 1 } },
|
|
|
|
).presenter,
|
2018-11-01 03:13:19 +08:00
|
|
|
pollTypes,
|
2020-08-04 05:35:12 +08:00
|
|
|
currentPoll: () => Polls.findOne({ meetingId: Auth.meetingID }),
|
2019-05-23 02:00:44 +08:00
|
|
|
pollAnswerIds,
|
2021-02-06 00:29:58 +08:00
|
|
|
POLL_AVATAR_COLOR,
|
2021-05-20 22:53:52 +08:00
|
|
|
isDefaultPoll: (pollType) => { return pollType !== 'custom' && pollType !== 'R-'},
|
|
|
|
getPollResultString: getPollResultString,
|
2018-11-01 03:13:19 +08:00
|
|
|
};
|