2018-11-01 03:13:19 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2021-10-05 04:01:20 +08:00
|
|
|
import { CurrentPoll } from '/imports/api/polls';
|
2022-05-24 01:09:50 +08:00
|
|
|
import { escapeHtml } from '/imports/utils/string-utils';
|
2021-05-29 08:28:47 +08:00
|
|
|
import { defineMessages } from 'react-intl';
|
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;
|
2024-02-21 20:16:53 +08:00
|
|
|
const MAX_POLL_RESULT_KEY_LENGTH = 10;
|
|
|
|
const POLL_BAR_CHAR = '\u220E';
|
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-05-27 01:52:55 +08:00
|
|
|
const pollTypes = {
|
|
|
|
YesNo: 'YN',
|
|
|
|
YesNoAbstention: 'YNA',
|
|
|
|
TrueFalse: 'TF',
|
|
|
|
Letter: 'A-',
|
|
|
|
A2: 'A-2',
|
|
|
|
A3: 'A-3',
|
|
|
|
A4: 'A-4',
|
|
|
|
A5: 'A-5',
|
2021-06-12 00:55:53 +08:00
|
|
|
Custom: 'CUSTOM',
|
2021-05-27 01:52:55 +08:00
|
|
|
Response: 'R-',
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
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-29 08:28:47 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
legendTitle: {
|
|
|
|
id: 'app.polling.pollingTitle',
|
|
|
|
description: 'heading for chat poll legend',
|
|
|
|
},
|
|
|
|
pollQuestionTitle: {
|
|
|
|
id: 'app.polling.pollQuestionTitle',
|
|
|
|
description: 'title displayed before poll question',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-31 22:24:25 +08:00
|
|
|
const getUsedLabels = (listOfAnswers, possibleLabels) => listOfAnswers.map(
|
|
|
|
(answer) => {
|
|
|
|
if (answer.key.length >= 2) {
|
|
|
|
const formattedLabel = answer.key.slice(0, 2).toUpperCase();
|
|
|
|
if (possibleLabels.includes(formattedLabel)) {
|
|
|
|
return formattedLabel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const getFormattedAnswerValue = (answerText) => {
|
|
|
|
// In generatePossibleLabels there is a check to see if the
|
|
|
|
// answer's length is greater than 2
|
|
|
|
const newText = answerText.slice(2).trim();
|
|
|
|
return newText;
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateAlphabetList = () => Array.from(Array(26))
|
|
|
|
.map((e, i) => i + 65).map((x) => String.fromCharCode(x));
|
|
|
|
|
|
|
|
const generatePossibleLabels = (alphabetCharacters) => {
|
|
|
|
// Remove the Letter from the beginning and the following sign, if any, like so:
|
|
|
|
// "A- the answer is" -> Remove "A-" -> "the answer is"
|
|
|
|
const listOfForbiddenSignsToStart = ['.', ':', '-'];
|
|
|
|
|
|
|
|
const possibleLabels = [];
|
|
|
|
for (let i = 0; i < alphabetCharacters.length; i += 1) {
|
|
|
|
for (let j = 0; j < listOfForbiddenSignsToStart.length; j += 1) {
|
|
|
|
possibleLabels.push(alphabetCharacters[i] + listOfForbiddenSignsToStart[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return possibleLabels;
|
|
|
|
};
|
|
|
|
|
2024-02-21 20:16:53 +08:00
|
|
|
const truncate = (text, length) => {
|
|
|
|
let resultText = text;
|
|
|
|
if (resultText.length < length) {
|
|
|
|
const diff = length - resultText.length;
|
|
|
|
const padding = ' '.repeat(diff);
|
|
|
|
resultText += padding;
|
|
|
|
} else if (resultText.length > length) {
|
|
|
|
resultText = `${resultText.substring(0, MAX_POLL_RESULT_KEY_LENGTH - 3)}...`;
|
|
|
|
}
|
|
|
|
return resultText;
|
|
|
|
};
|
|
|
|
|
2021-05-29 08:28:47 +08:00
|
|
|
const getPollResultsText = (isDefaultPoll, answers, numRespondents, intl) => {
|
2021-05-20 22:53:52 +08:00
|
|
|
let responded = 0;
|
|
|
|
let resultString = '';
|
|
|
|
let optionsString = '';
|
|
|
|
|
2023-07-31 22:24:25 +08:00
|
|
|
const alphabetCharacters = generateAlphabetList();
|
|
|
|
const possibleLabels = generatePossibleLabels(alphabetCharacters);
|
|
|
|
|
|
|
|
// We need to guarantee that the labels are in the correct order, and that all options have label
|
|
|
|
const pollAnswerMatchLabeledFormat = getUsedLabels(answers, possibleLabels);
|
|
|
|
const isPollAnswerMatchFormat = !isDefaultPoll
|
|
|
|
? pollAnswerMatchLabeledFormat.reduce(
|
|
|
|
(acc, label, index) => acc && !!label && label[0] === alphabetCharacters[index][0], true,
|
|
|
|
)
|
|
|
|
: false;
|
|
|
|
|
2024-02-21 20:16:53 +08:00
|
|
|
let longestKeyLength = answers.reduce(
|
|
|
|
(acc, item) => (item.key.length > acc ? item.key.length : acc), 0,
|
|
|
|
);
|
|
|
|
longestKeyLength = Math.min(longestKeyLength, MAX_POLL_RESULT_KEY_LENGTH);
|
|
|
|
|
2021-05-20 22:53:52 +08:00
|
|
|
answers.map((item) => {
|
|
|
|
responded += item.numVotes;
|
|
|
|
return item;
|
2023-07-31 22:24:25 +08:00
|
|
|
}).forEach((item, index) => {
|
2021-05-20 22:53:52 +08:00
|
|
|
const numResponded = responded === numRespondents ? numRespondents : responded;
|
2024-02-21 20:16:53 +08:00
|
|
|
const pct = Math.round((item.numVotes / (numResponded || 1)) * 100);
|
|
|
|
const pctBars = POLL_BAR_CHAR.repeat((pct * MAX_POLL_RESULT_BARS) / 100);
|
2021-05-20 22:53:52 +08:00
|
|
|
const pctFotmatted = `${Number.isNaN(pct) ? 0 : pct}%`;
|
|
|
|
if (isDefaultPoll) {
|
2024-02-21 20:16:53 +08:00
|
|
|
let translatedKey = pollAnswerIds[item.key.toLowerCase()]
|
2021-06-08 03:21:21 +08:00
|
|
|
? intl.formatMessage(pollAnswerIds[item.key.toLowerCase()])
|
|
|
|
: item.key;
|
2024-02-21 20:16:53 +08:00
|
|
|
translatedKey = truncate(translatedKey, longestKeyLength);
|
|
|
|
resultString += `${translatedKey}: ${item.numVotes || 0} ${pctBars}${POLL_BAR_CHAR} ${pctFotmatted}\n`;
|
2021-05-20 22:53:52 +08:00
|
|
|
} else {
|
2023-07-31 22:24:25 +08:00
|
|
|
if (isPollAnswerMatchFormat) {
|
|
|
|
resultString += `${pollAnswerMatchLabeledFormat[index][0]}`;
|
|
|
|
const formattedAnswerValue = getFormattedAnswerValue(item.key);
|
|
|
|
optionsString += `${pollAnswerMatchLabeledFormat[index][0]}: ${formattedAnswerValue}\n`;
|
|
|
|
} else {
|
2024-02-21 20:16:53 +08:00
|
|
|
let { key } = item;
|
|
|
|
key = truncate(key, longestKeyLength);
|
|
|
|
resultString += key;
|
2023-07-31 22:24:25 +08:00
|
|
|
}
|
2024-02-21 20:16:53 +08:00
|
|
|
resultString += `: ${item.numVotes || 0} ${pctBars}${POLL_BAR_CHAR} ${pctFotmatted}\n`;
|
2021-05-20 22:53:52 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return { resultString, optionsString };
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
2021-05-20 22:53:52 +08:00
|
|
|
|
2021-07-03 03:58:33 +08:00
|
|
|
const isDefaultPoll = (pollType) => pollType !== pollTypes.Custom
|
|
|
|
&& pollType !== pollTypes.Response;
|
2021-05-29 08:28:47 +08:00
|
|
|
|
|
|
|
const getPollResultString = (pollResultData, intl) => {
|
2021-07-03 03:58:33 +08:00
|
|
|
const formatBoldBlack = (s) => s.bold().fontcolor('black');
|
2021-05-29 08:28:47 +08:00
|
|
|
|
2022-05-24 01:09:50 +08:00
|
|
|
const sanitize = (value) => escapeHtml(value);
|
2021-05-29 08:28:47 +08:00
|
|
|
|
2021-06-17 06:31:09 +08:00
|
|
|
const { answers, numRespondents, questionType } = pollResultData;
|
2024-02-21 20:16:53 +08:00
|
|
|
const isDefault = isDefaultPoll(questionType);
|
2021-07-03 03:58:33 +08:00
|
|
|
let {
|
|
|
|
resultString,
|
|
|
|
optionsString,
|
2024-02-21 20:16:53 +08:00
|
|
|
} = getPollResultsText(isDefault, answers, numRespondents, intl);
|
2021-05-29 08:28:47 +08:00
|
|
|
resultString = sanitize(resultString);
|
|
|
|
optionsString = sanitize(optionsString);
|
|
|
|
|
|
|
|
let pollText = formatBoldBlack(resultString);
|
2024-02-21 20:16:53 +08:00
|
|
|
if (optionsString !== '') {
|
2021-05-29 08:28:47 +08:00
|
|
|
pollText += formatBoldBlack(`<br/><br/>${intl.formatMessage(intlMessages.legendTitle)}<br/>`);
|
|
|
|
pollText += optionsString;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pollQuestion = pollResultData.questionText;
|
|
|
|
if (pollQuestion.trim() !== '') {
|
|
|
|
const sanitizedPollQuestion = sanitize(pollQuestion.split('<br#>').join(' '));
|
|
|
|
|
|
|
|
pollText = `${formatBoldBlack(intl.formatMessage(intlMessages.pollQuestionTitle))}<br/>${sanitizedPollQuestion}<br/><br/>${pollText}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pollText;
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
2021-05-29 08:28:47 +08:00
|
|
|
|
2021-05-27 01:52:55 +08:00
|
|
|
const matchYesNoPoll = (yesValue, noValue, contentString) => {
|
|
|
|
const ynPollString = `(${yesValue}\\s*\\/\\s*${noValue})|(${noValue}\\s*\\/\\s*${yesValue})`;
|
|
|
|
const ynOptionsRegex = new RegExp(ynPollString, 'gi');
|
2021-08-17 01:25:16 +08:00
|
|
|
const ynPoll = contentString.replace(/\n/g, '').match(ynOptionsRegex) || [];
|
2021-05-27 01:52:55 +08:00
|
|
|
return ynPoll;
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
2021-05-27 01:52:55 +08:00
|
|
|
|
|
|
|
const matchYesNoAbstentionPoll = (yesValue, noValue, abstentionValue, contentString) => {
|
|
|
|
const ynaPollString = `(${yesValue}\\s*\\/\\s*${noValue}\\s*\\/\\s*${abstentionValue})|(${yesValue}\\s*\\/\\s*${abstentionValue}\\s*\\/\\s*${noValue})|(${abstentionValue}\\s*\\/\\s*${yesValue}\\s*\\/\\s*${noValue})|(${abstentionValue}\\s*\\/\\s*${noValue}\\s*\\/\\s*${yesValue})|(${noValue}\\s*\\/\\s*${yesValue}\\s*\\/\\s*${abstentionValue})|(${noValue}\\s*\\/\\s*${abstentionValue}\\s*\\/\\s*${yesValue})`;
|
|
|
|
const ynaOptionsRegex = new RegExp(ynaPollString, 'gi');
|
2021-08-17 01:25:16 +08:00
|
|
|
const ynaPoll = contentString.replace(/\n/g, '').match(ynaOptionsRegex) || [];
|
2021-05-27 01:52:55 +08:00
|
|
|
return ynaPoll;
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
2021-05-27 01:52:55 +08:00
|
|
|
|
|
|
|
const matchTrueFalsePoll = (trueValue, falseValue, contentString) => {
|
|
|
|
const tfPollString = `(${trueValue}\\s*\\/\\s*${falseValue})|(${falseValue}\\s*\\/\\s*${trueValue})`;
|
|
|
|
const tgOptionsRegex = new RegExp(tfPollString, 'gi');
|
|
|
|
const tfPoll = contentString.match(tgOptionsRegex) || [];
|
|
|
|
return tfPoll;
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
2021-05-27 01:52:55 +08:00
|
|
|
|
2021-07-03 03:58:33 +08:00
|
|
|
const checkPollType = (
|
|
|
|
type,
|
|
|
|
optList,
|
|
|
|
yesValue,
|
|
|
|
noValue,
|
|
|
|
abstentionValue,
|
|
|
|
trueValue,
|
|
|
|
falseValue,
|
|
|
|
) => {
|
2021-05-27 01:52:55 +08:00
|
|
|
let _type = type;
|
|
|
|
let pollString = '';
|
|
|
|
let defaultMatch = null;
|
|
|
|
let isDefault = null;
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
case pollTypes.Letter:
|
2021-08-17 01:25:16 +08:00
|
|
|
pollString = optList.map((x) => x.val.toUpperCase()).sort().join('');
|
2023-02-02 04:34:25 +08:00
|
|
|
defaultMatch = pollString.match(/^(ABCDEF)|(ABCDE)|(ABCD)|(ABC)|(AB)$/gi);
|
2021-05-27 01:52:55 +08:00
|
|
|
isDefault = defaultMatch && pollString.length === defaultMatch[0].length;
|
|
|
|
_type = isDefault ? `${_type}${defaultMatch[0].length}` : pollTypes.Custom;
|
|
|
|
break;
|
|
|
|
case pollTypes.TrueFalse:
|
|
|
|
pollString = optList.map((x) => x.val).join('/');
|
|
|
|
defaultMatch = matchTrueFalsePoll(trueValue, falseValue, pollString);
|
|
|
|
isDefault = defaultMatch.length > 0 && pollString.length === defaultMatch[0].length;
|
|
|
|
if (!isDefault) _type = pollTypes.Custom;
|
|
|
|
break;
|
|
|
|
case pollTypes.YesNoAbstention:
|
|
|
|
pollString = optList.map((x) => x.val).join('/');
|
|
|
|
defaultMatch = matchYesNoAbstentionPoll(yesValue, noValue, abstentionValue, pollString);
|
|
|
|
isDefault = defaultMatch.length > 0 && pollString.length === defaultMatch[0].length;
|
|
|
|
if (!isDefault) {
|
|
|
|
// also try to match only yes/no
|
2021-08-17 01:25:16 +08:00
|
|
|
defaultMatch = matchYesNoPoll(yesValue, noValue, pollString);
|
2021-05-27 01:52:55 +08:00
|
|
|
isDefault = defaultMatch.length > 0 && pollString.length === defaultMatch[0].length;
|
|
|
|
_type = isDefault ? pollTypes.YesNo : _type = pollTypes.Custom;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _type;
|
2021-07-03 03:58:33 +08:00
|
|
|
};
|
2021-05-27 01:52:55 +08:00
|
|
|
|
2022-04-29 04:18:35 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} input
|
|
|
|
*/
|
|
|
|
const validateInput = (input) => {
|
|
|
|
let _input = input;
|
|
|
|
while (/^\s/.test(_input)) _input = _input.substring(1);
|
|
|
|
return _input;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} input
|
|
|
|
*/
|
|
|
|
const removeEmptyLineSpaces = (input) => {
|
|
|
|
const filteredInput = input.split('\n').filter((val) => val.trim() !== '');
|
|
|
|
return filteredInput;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String|Array} questionAndOptions
|
|
|
|
*/
|
|
|
|
const getSplittedQuestionAndOptions = (questionAndOptions) => {
|
|
|
|
const inputList = Array.isArray(questionAndOptions)
|
|
|
|
? questionAndOptions
|
|
|
|
: questionAndOptions.split('\n').filter((val) => val !== '');
|
|
|
|
const splittedQuestion = inputList.length > 0 ? inputList[0] : questionAndOptions;
|
|
|
|
const optionsList = inputList.slice(1);
|
|
|
|
|
|
|
|
optionsList.forEach((val, i) => { optionsList[i] = { val }; });
|
|
|
|
|
|
|
|
return {
|
|
|
|
splittedQuestion,
|
|
|
|
optionsList,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-11-01 03:13:19 +08:00
|
|
|
export default {
|
|
|
|
pollTypes,
|
2021-10-05 04:01:20 +08:00
|
|
|
currentPoll: () => CurrentPoll.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-07-03 03:58:33 +08:00
|
|
|
isDefaultPoll,
|
|
|
|
getPollResultString,
|
|
|
|
matchYesNoPoll,
|
|
|
|
matchYesNoAbstentionPoll,
|
|
|
|
matchTrueFalsePoll,
|
|
|
|
checkPollType,
|
2022-04-29 04:18:35 +08:00
|
|
|
validateInput,
|
|
|
|
removeEmptyLineSpaces,
|
|
|
|
getSplittedQuestionAndOptions,
|
2024-02-21 20:16:53 +08:00
|
|
|
POLL_BAR_CHAR,
|
2018-11-01 03:13:19 +08:00
|
|
|
};
|