Skip to content

Commit

Permalink
Limit the voting choices to a single option #41
Browse files Browse the repository at this point in the history
* add a new boolean property 'type' to the XPollClass for single and multple options
* refactored duplicated code into a macro to display the options input
* updated all the translations pages with the new added translations
  • Loading branch information
acotiuga committed Oct 15, 2019
1 parent 2667be8 commit a0328a3
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 62 deletions.
Expand Up @@ -70,6 +70,9 @@ XPoll.XPollClass_status_active=Actif
XPoll.XPollClass_status_finished=Terminé
XPoll.XPollClass_status_hint=Affiche la phase de ce sondage
XPoll.XPollClass_status_inpreparation=En cours de préparation
XPoll.XPollClass_type_hint=Un sondage peut être configuré pour ne nécessiter qu'une ou plusieurs valeurs
XPoll.XPollClass_type_0=Autoriser plusieurs options
XPoll.XPollClass_type_1=Peut sélectionner une seule option

## Livetable
xpoll.livetable._actions=Actions
Expand Down
Expand Up @@ -65,6 +65,9 @@ XPoll.XPollClass_status=Állapot
XPoll.XPollClass_status_active=Aktív
XPoll.XPollClass_status_finished=Befejeződött
XPoll.XPollClass_status_inpreparation=Előkészítés alatt
XPoll.XPollClass_type_hint=A lekérdezés konfigurálható úgy, hogy csak egy vagy több értékre legyen szükség
XPoll.XPollClass_type_0=Több lehetőség engedélyezése
XPoll.XPollClass_type_1=Kiválaszthat egyetlen lehetőséget

## Livetable
xpoll.livetable._actions=Műveletek
Expand Down
Expand Up @@ -65,6 +65,9 @@ XPoll.XPollClass_status=Stato
XPoll.XPollClass_status_active=Attivo
XPoll.XPollClass_status_finished=Finito
XPoll.XPollClass_status_inpreparation=In preparazione
XPoll.XPollClass_type_hint=Un sondaggio può essere configurato per richiedere solo uno o più valori
XPoll.XPollClass_type_0=Consenti più opzioni
XPoll.XPollClass_type_1=È possibile selezionare una singola opzione

## Livetable
xpoll.livetable._actions=Azioni
Expand Down
Expand Up @@ -63,6 +63,9 @@ XPoll.XPollClass_status=Stav
XPoll.XPollClass_status_active=Aktívny
XPoll.XPollClass_status_finished=Ukončený
XPoll.XPollClass_status_inpreparation=V príprave
XPoll.XPollClass_type_hint=Prieskum môže byť nakonfigurovaný tak, aby vyžadoval iba jednu alebo viac hodnôt
XPoll.XPollClass_type_0=Povoliť viac možností
XPoll.XPollClass_type_1=Môže vybrať jednu možnosť

## Livetable
xpoll.livetable._actions=Akcie
Expand Down
3 changes: 3 additions & 0 deletions application-xpoll-ui/src/main/resources/XPoll/Translation.xml
Expand Up @@ -71,6 +71,9 @@ XPoll.XPollClass_status_active=Active
XPoll.XPollClass_status_finished=Finished
XPoll.XPollClass_status_hint=Shows the stage of this poll
XPoll.XPollClass_status_inpreparation=In preparation
XPoll.XPollClass_type_hint=A poll can be configured to require only one or multiple values
XPoll.XPollClass_type_0=Allow multiple options
XPoll.XPollClass_type_1=Can select a single option

## Livetable
xpoll.livetable._actions=Actions
Expand Down
14 changes: 14 additions & 0 deletions application-xpoll-ui/src/main/resources/XPoll/XPollClass.xml
Expand Up @@ -120,6 +120,20 @@
<values>inpreparation=In Preparation|active=Active|finished=Finished</values>
<classType>com.xpn.xwiki.objects.classes.StaticListClass</classType>
</status>
<type>
<customDisplay/>
<defaultValue>0</defaultValue>
<disabled>0</disabled>
<displayFormType>select</displayFormType>
<displayType>1/0</displayType>
<name>type</name>
<number>6</number>
<prettyName>Type</prettyName>
<unmodifiable>0</unmodifiable>
<validationMessage/>
<validationRegExp/>
<classType>com.xpn.xwiki.objects.classes.BooleanClass</classType>
</type>
<winner>
<customDisplay/>
<disabled>0</disabled>
Expand Down
Expand Up @@ -51,7 +51,9 @@ try {
def proposals = xpolldoc.getValue('proposals');
def votes = [];
for (proposal in proposals) {
if (request.get("${proposal.hashCode()}")) {
// The first rule applies checkbox inputs and the second one applies to radio inputs.
if (request.get(proposal.hashCode().toString()) ||
request.get(xcontext.user).equals(proposal.hashCode().toString())) {
votes.add(proposal);
}
}
Expand Down
135 changes: 74 additions & 61 deletions application-xpoll-ui/src/main/resources/XPoll/XPollSheet.xml
Expand Up @@ -39,7 +39,7 @@
<minorEdit>false</minorEdit>
<syntaxId>xwiki/2.1</syntaxId>
<hidden>true</hidden>
<content>{{velocity}}
<content>{{velocity output="false"}}
#*
Displays the value of the name property as the title of the document.
If value is empty (e.g. creation step), displays the document title.
Expand All @@ -51,7 +51,8 @@
#end
$doc.display('name')
#end
#macro (displayUser $user)
#macro (displayUser $userName)
#set ($user = $xwiki.getDocument($userName))
{{html wiki='true' clean='false'}}
&lt;div class='clearfix user'&gt;
&lt;span id="avatar_$user.fullName" class='avatar user-avatar-wrapper'&gt;
Expand All @@ -78,16 +79,48 @@
&lt;/div&gt;
{{/html}}
#end
#macro(displayInput $votes $voteCount $isCurrentUser $user)
#set ($disabled = '')
#if (!$isCurrentUser)
#set ($disabled = 'disabled')
#end
#foreach ($proposal in $proposals)
#set ($isChecked = $votes.contains($proposal))
#set ($checked = '')
#if ($isChecked)
#set ($checked = 'checked')
#set ($nbvotes = $voteCount.get($proposal))
#set ($nbvotes = $nbvotes + 1)
#set ($discard = $voteCount.put($proposal, $nbvotes))
#end
&lt;td&gt;
#if ($xcontext.action == 'export')
#if($isChecked)X#end
#else
#if ($isRadio)
&lt;input type="radio" name="$user" value="$proposal.hashCode()" $checked $disabled /&gt;
#else
&lt;input type="checkbox" name="$proposal.hashCode()" $checked $disabled /&gt;
#end
#end
&lt;/td&gt;
#end
#end
{{/velocity}}

{{velocity}}
#set ($discard = $xwiki.jsx.use('XPoll.XPollJSExtension'))
#set ($discard = $xwiki.ssx.use('XPoll.XPollSheet'))
#set ($pollObj = $doc.getObject('XPoll.XPollClass'))
#if ($hasEdit &amp;&amp; $xcontext.action == 'view' &amp;&amp; $doc.getValue('status') == 'inpreparation')
$services.localization.render('contrib.xpoll.preparation.hasRight')
#elseif (!$hasEdit)
$services.localization.render('contrib.xpoll.preparation.noRight')
#end

## Flag to know wheter the options must be displayed as checkbox or radio inputs.
#set ($isRadio = $pollObj.getProperty('type').value == 1)
#if ($xcontext.action == 'edit' || $doc.getValue('status') == 'inpreparation')
{{html wiki='true'}}
{{html wiki='true' clean='false'}}
#set ($discard = $xwiki.ssx.use('AppWithinMinutes.ClassSheetGenerator'))
#set ($discard = $doc.use('XPoll.XPollClass'))
## We don't have access to the form element to set the CSS class for the vertical form layout standard.
Expand All @@ -99,15 +132,15 @@
#end
(% class="row" %)
(((
(% class="col-xs-12 col-sm-6" %)
(% class="col-xs-12 col-sm-4" %)
(((
; &lt;label #if($xcontext.action == 'edit')for="XPoll.XPollClass_0_proposals"#end&gt;
$doc.displayPrettyName('proposals', false, false)
&lt;/label&gt;
(% class="xHint" %)$services.localization.render('XPoll.XPollClass_proposals_hint')
: $doc.display('proposals')
)))
(% class="col-xs-12 col-sm-6" %)
(% class="col-xs-12 col-sm-4" %)
(((
; &lt;label #if($xcontext.action == 'edit')for="XPoll.XPollClass_0_status"#end&gt;
$doc.displayPrettyName('status', false, false)
Expand All @@ -119,6 +152,18 @@
: $doc.display('status')
#end
)))
(% class="col-xs-12 col-sm-4" %)
(((
; &lt;label #if($xcontext.action == 'edit')for="XPoll.XPollClass_0_type"#end&gt;
$doc.displayPrettyName('type', false, false)
&lt;/label&gt;
(% class="xHint" %)$services.localization.render('XPoll.XPollClass_type_hint')
#if($xcontext.action == 'view')
: $doc.display('type')
#else
: $doc.display('type')
#end
)))
)))
#if ($xcontext.action == 'edit' || "$!doc.getValue('description')" != '')
; &lt;label #if($xcontext.action == 'edit')for="XPoll.XPollClass_0_description"#end&gt;
Expand All @@ -131,10 +176,9 @@
)))
{{/html}}
#else
#set ($status = $pollObj.getValue('status'))
#set ($proposals = $pollObj.getValue('proposals'))
$doc.display('description')

#set ($status = $doc.getValue('status'))
#set ($proposals = $doc.getValue('proposals'))
#set ($usersCount = $doc.getObjects('XPoll.XPollVoteClass').size())
## Count only the existing users, without the deleted users.
## This is needed to update the number of user when the status is active and the deleted users are hidden.
Expand Down Expand Up @@ -174,49 +218,31 @@
## Users and their votes.
&lt;tbody&gt;
#set ($foundUser = false)
#foreach ($obj in $doc.getObjects('XPoll.XPollVoteClass'))
#set ($discard = $doc.use($obj))
#set ($user = $doc.getValue('user'))
#set ($votes = $doc.getValue('votes'))
&lt;tr #if ($user == $xcontext.user) class="active" #end&gt;
#set ($discard = $doc.use($obj))
#set ($user = $doc.getValue('user'))
#if ($user == $xcontext.user)
#foreach ($voteObj in $doc.getObjects('XPoll.XPollVoteClass'))
#set ($user = $voteObj.user)
#set ($isCurrentUser = $user == $xcontext.user)
#set ($votes = $voteObj.votes)
&lt;tr #if($isCurrentUser)class="active"#end&gt;
#if ($isCurrentUser)
#set ($foundUser = true)
#end
#set ($votes = $doc.getValue('votes'))
#if ($xwiki.exists($user))
&lt;td&gt;
#displayUser($xwiki.getDocument($user))
#displayUser($user)
&lt;/td&gt;
#foreach ($proposal in $proposals)
#set ($checked = $votes.contains($proposal))
#if ($checked)
#set ($nbvotes = $voteCount.get($proposal))
#set ($nbvotes = $nbvotes + 1)
#set ($discard = $voteCount.put($proposal, $nbvotes))
#end ##
&lt;td&gt;
&lt;input type="checkbox" name="${proposal.hashCode()}"
#if ($checked)CHECKED#end #if ($xcontext.user != $user) disabled #end /&gt;
&lt;/td&gt;
#end
#end ##
#displayInput($votes $voteCount $isCurrentUser $user)
#end
&lt;/tr&gt;
#end

## Display the current user if he hasn't voted yet.
#if (!$foundUser &amp;&amp; $xcontext.user != 'XWiki.XWikiGuest')
&lt;tr class="active"&gt;
&lt;td&gt;
#displayUser($xwiki.getDocument($xcontext.user))
#displayUser($xcontext.user)
&lt;/td&gt;
#foreach ($proposal in $proposals)
#set ($checked = false)
&lt;td&gt;
&lt;input type="checkbox" name="${proposal.hashCode()}" #if($checked)CHECKED#end /&gt;
&lt;/td&gt;
#end
## The 3rd parameter is true in order to not disable the input for current user.
#displayInput([] {} true $xcontext.user)
&lt;/tr&gt;
#end
&lt;/tbody&gt;
Expand Down Expand Up @@ -245,7 +271,7 @@
&lt;/form&gt;
{{/html}}
#elseif ($status=="finished")
{{html wiki='true'}}
{{html wiki='true' clean='false'}}
$services.localization.render('contrib.xpoll.finish.message')
#set ($voteCount = {})
#foreach ($proposal in $proposals)
Expand All @@ -262,29 +288,16 @@
&lt;/tr&gt;

## Users and their votes.
#foreach ($obj in $doc.getObjects('XPoll.XPollVoteClass'))
#set ($discard = $doc.use($obj))
#set ($user = $doc.getValue('user'))
#set ($votes = $doc.getValue('votes'))
&lt;tr #if ($user == $xcontext.user) class="active" #end&gt;
&lt;td&gt;
#displayUser($xwiki.getDocument($user))
&lt;/td&gt;
#foreach ($proposal in $proposals)
#set ($checked = $votes.contains($proposal))
#if ($checked)
#set ($nbvotes = $voteCount.get($proposal))
#set ($nbvotes = $nbvotes + 1)
#set ($discard = $voteCount.put($proposal, $nbvotes))
#end ##
#foreach ($voteObj in $doc.getObjects('XPoll.XPollVoteClass'))
#set ($user = $voteObj.user)
#set ($votes = $voteObj.votes)
#set ($isCurrentUser = $user == $xcontext.user)
&lt;tr #if($isCurrentUser)class="active"#end&gt;
&lt;td&gt;
#if ($xcontext.action == 'export')
#if ($checked) X #end##
#else
&lt;input type="checkbox" name="$proposal" #if ($checked)CHECKED#end disabled /&gt;
#end ##
#displayUser($user)
&lt;/td&gt;
#end
## The 3rd parameter is false in order to disable the input when the poll is finished.
#displayInput($votes $voteCount false $user)
&lt;/tr&gt;
#end

Expand Down
14 changes: 14 additions & 0 deletions application-xpoll-ui/src/main/resources/XPoll/XPollTemplate.xml
Expand Up @@ -123,6 +123,20 @@
<values>inpreparation=In Preparation|active=Active|finished=Finished</values>
<classType>com.xpn.xwiki.objects.classes.StaticListClass</classType>
</status>
<type>
<customDisplay/>
<defaultValue>0</defaultValue>
<disabled>0</disabled>
<displayFormType>select</displayFormType>
<displayType>1/0</displayType>
<name>type</name>
<number>6</number>
<prettyName>Type</prettyName>
<unmodifiable>0</unmodifiable>
<validationMessage/>
<validationRegExp/>
<classType>com.xpn.xwiki.objects.classes.BooleanClass</classType>
</type>
<winner>
<customDisplay/>
<disabled>0</disabled>
Expand Down

0 comments on commit a0328a3

Please sign in to comment.