From bd29d156b36a22ddc0c149b345c57d013b095623 Mon Sep 17 00:00:00 2001
From: germanocaumo <germanocaumo@gmail.com>
Date: Wed, 30 Jun 2021 15:19:09 +0000
Subject: [PATCH] fix(poll): Respect maxCustom property in quick-polls

More than 5 answers are now of Custom type poll and answers passed directly.
Increase A-I slide options detection
---
 .../quick-poll-dropdown/component.jsx         | 21 +++++++++++++---
 .../imports/ui/components/poll/component.jsx  |  4 +--
 .../ui/components/presentation/service.js     | 25 +++++++++++++++----
 .../private/config/settings.yml               |  2 +-
 4 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/bigbluebutton-html5/imports/ui/components/actions-bar/quick-poll-dropdown/component.jsx b/bigbluebutton-html5/imports/ui/components/actions-bar/quick-poll-dropdown/component.jsx
index 3b16d18ae9..f344a92478 100644
--- a/bigbluebutton-html5/imports/ui/components/actions-bar/quick-poll-dropdown/component.jsx
+++ b/bigbluebutton-html5/imports/ui/components/actions-bar/quick-poll-dropdown/component.jsx
@@ -10,6 +10,9 @@ import DropdownList from '/imports/ui/components/dropdown/list/component';
 import DropdownListItem from '/imports/ui/components/dropdown/list/item/component';
 import { styles } from '../styles';
 
+const POLL_SETTINGS = Meteor.settings.public.poll;
+const MAX_CUSTOM_FIELDS = POLL_SETTINGS.maxCustom;
+
 const intlMessages = defineMessages({
   quickPollLabel: {
     id: 'app.poll.quickPollTitle',
@@ -47,7 +50,7 @@ const getAvailableQuickPolls = (slideId, parsedSlides, startPoll, pollTypes) =>
   const pollItemElements = parsedSlides.map((poll) => {
     let { poll: label, type } = poll;
     let itemLabel = label;
-    let answers = null;
+    let letterAnswers = [];
 
     if (type !== pollTypes.YesNo && 
         type !== pollTypes.YesNoAbstention && 
@@ -55,6 +58,16 @@ const getAvailableQuickPolls = (slideId, parsedSlides, startPoll, pollTypes) =>
     {
       const { options } = itemLabel;
       itemLabel = options.join('/').replace(/[\n.)]/g, '');
+      if (type === pollTypes.Custom) {
+        for (const option of options) {
+          const letterOption = option.replace(/[\r.)]/g, '');
+          if (letterAnswers.length < MAX_CUSTOM_FIELDS) {
+            letterAnswers.push(letterOption);
+          } else {
+            break;
+          }
+        }
+      }
     }
 
     // removes any whitespace from the label
@@ -72,7 +85,8 @@ const getAvailableQuickPolls = (slideId, parsedSlides, startPoll, pollTypes) =>
       <DropdownListItem
         label={itemLabel}
         key={_.uniqueId('quick-poll-item')}
-        onClick={() => startPoll(type, slideId, answers)}
+        onClick={() => startPoll(type, slideId, letterAnswers)}
+        answers={letterAnswers}
       />
     );
   });
@@ -112,14 +126,15 @@ class QuickPollDropdown extends Component {
 
     if (quickPollOptions.length === 0) return null;
 
+    let answers = null;
     let quickPollLabel = '';
     if (quickPolls.length > 0) {
       const { props: pollProps } = quickPolls[0];
       quickPollLabel = pollProps.label;
+      answers = pollProps.answers;
     }
 
     let singlePollType = null;
-    let answers = null;
     if (quickPolls.length === 1 && quickPollOptions.length) {
       const { type } = quickPollOptions[0];
       singlePollType = type;
diff --git a/bigbluebutton-html5/imports/ui/components/poll/component.jsx b/bigbluebutton-html5/imports/ui/components/poll/component.jsx
index bc2352767e..ed4de2a805 100644
--- a/bigbluebutton-html5/imports/ui/components/poll/component.jsx
+++ b/bigbluebutton-html5/imports/ui/components/poll/component.jsx
@@ -151,7 +151,7 @@ const intlMessages = defineMessages({
 
 const POLL_SETTINGS = Meteor.settings.public.poll;
 
-const MAX_CUSTOM_FIELDS = POLL_SETTINGS.max_custom;
+const MAX_CUSTOM_FIELDS = POLL_SETTINGS.maxCustom;
 const MAX_INPUT_CHARS = POLL_SETTINGS.maxTypedAnswerLength;
 const QUESTION_MAX_INPUT_CHARS = 400;
 const FILE_DRAG_AND_DROP_ENABLED = POLL_SETTINGS.allowDragAndDropFile;
@@ -494,7 +494,7 @@ class Poll extends Component {
                           label={intl.formatMessage(intlMessages.addOptionLabel)}
                           color="default"
                           icon="add"
-                          disabled={optList.length === MAX_CUSTOM_FIELDS}
+                          disabled={optList.length >= MAX_CUSTOM_FIELDS}
                           onClick={() => this.handleAddOption()}
                         />
                       )}
diff --git a/bigbluebutton-html5/imports/ui/components/presentation/service.js b/bigbluebutton-html5/imports/ui/components/presentation/service.js
index 5c82cda4af..df6a9180d7 100755
--- a/bigbluebutton-html5/imports/ui/components/presentation/service.js
+++ b/bigbluebutton-html5/imports/ui/components/presentation/service.js
@@ -4,6 +4,9 @@ import { Slides, SlidePositions } from '/imports/api/slides';
 import Auth from '/imports/ui/services/auth';
 import PollService from '/imports/ui/components/poll/service';
 
+const POLL_SETTINGS = Meteor.settings.public.poll;
+const MAX_CUSTOM_FIELDS = POLL_SETTINGS.maxCustom;
+
 const getCurrentPresentation = podId => Presentations.findOne({
   podId,
   current: true,
@@ -81,7 +84,7 @@ const parseCurrentSlideContent = (yesValue, noValue, abstentionValue, trueValue,
     content,
   } = currentSlide;
 
-  const pollRegex = /[1-6A-Fa-f][.)].*/g;
+  const pollRegex = /[1-9A-Ia-i][.)].*/g;
   let optionsPoll = content.match(pollRegex) || [];
   if (optionsPoll) optionsPoll = optionsPoll.map(opt => `\r${opt[0]}.`);
 
@@ -120,10 +123,22 @@ const parseCurrentSlideContent = (yesValue, noValue, abstentionValue, trueValue,
     return acc;
   }, []).filter(({
     options,
-  }) => options.length > 1 && options.length < 7).forEach(poll => quickPollOptions.push({
-    type: `${pollTypes.Letter}${poll.options.length}`,
-    poll,
-  }));
+  }) => options.length > 1 && options.length < 10).forEach(poll => {
+    if (poll.options.length <= 5 || MAX_CUSTOM_FIELDS <= 5) {
+      const maxAnswer = poll.options.length > MAX_CUSTOM_FIELDS 
+        ? MAX_CUSTOM_FIELDS
+        : poll.options.length
+      quickPollOptions.push({
+        type: `${pollTypes.Letter}${maxAnswer}`,
+        poll,
+      })
+    } else {
+      quickPollOptions.push({
+        type: pollTypes.Custom,
+        poll,
+      })
+    }
+  });
 
   if (quickPollOptions.length > 0) {
     content = content.replace(new RegExp(pollRegex), '');
diff --git a/bigbluebutton-html5/private/config/settings.yml b/bigbluebutton-html5/private/config/settings.yml
index 189402865f..8e6918b8da 100755
--- a/bigbluebutton-html5/private/config/settings.yml
+++ b/bigbluebutton-html5/private/config/settings.yml
@@ -379,7 +379,7 @@ public:
     toggleSelfVoice: false
   poll:
     enabled: true
-    max_custom: 5
+    maxCustom: 5
     allowDragAndDropFile: false
     maxTypedAnswerLength: 45
   captions:
-- 
GitLab