From 02b1959f0f98b8c16177af2fbecd1a7ee67ca28f Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Sun, 5 Dec 2021 19:29:42 +0000
Subject: [PATCH] add per question random order

---
 helios/media/static_templates/question.html | 18 +++++++++++++++++-
 helios/templates/election_questions.html    |  3 ++-
 heliosbooth/vote.html                       |  4 ++--
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/helios/media/static_templates/question.html b/helios/media/static_templates/question.html
index 5e57691..15b3882 100644
--- a/helios/media/static_templates/question.html
+++ b/helios/media/static_templates/question.html
@@ -4,7 +4,7 @@
 <h4>
 {#if $T.admin_p}[
 {#if $T.question$index > 0}<a href="javascript:question_move_up({$T.question$index});">^</a>]&nbsp;[{#/if}
-<a onclick="return confirm('Are you sure you want to remove this question?');" href="javascript:question_remove({$T.question$index})">x</a>] [<a href="javascript:question_edit({$T.question$index})">edit</a>] {#/if}{$T.question$index + 1}. {$T.question.question} ({$T.question.choice_type}, select between {$T.question.min} and {#if $T.question.max != null}{$T.question.max}{#else}unlimited{#/if} answers, result type {$T.question.result_type}.)</h4>
+<a onclick="return confirm('Are you sure you want to remove this question?');" href="javascript:question_remove({$T.question$index})">x</a>] [<a href="javascript:question_edit({$T.question$index})">edit</a>] {#/if}{$T.question$index + 1}. {$T.question.question} ({$T.question.choice_type}, select between {$T.question.min} and {#if $T.question.max != null}{$T.question.max}{#else}unlimited{#/if} answers, result type {$T.question.result_type}{#if $T.question.randomize_answer_order}, random answer order{#/if}.)</h4>
 <ul>
 {#foreach $T.question.answers as answer}
 <li> {$T.answer}
@@ -55,6 +55,13 @@ Result Type:&nbsp;
     <option>relative</option>
 </select>
 
+&nbsp;&nbsp;
+&nbsp;&nbsp;
+Random Answer Order:&nbsp;
+<select name="randomize_answer_order">
+  <option selected value="0">no</option>
+  <option value="1">yes</option>
+</select>
 </p>
 
 <table id="answer_table_{$T.question$index}">
@@ -115,6 +122,15 @@ Result Type:&nbsp;
     <option>relative</option>
 </select>
 
+&nbsp;&nbsp;
+&nbsp;&nbsp;
+Random Answer Order:&nbsp;
+<select name="randomize_answer_order">
+  <option selected value="0">no</option>
+  <option value="1">yes</option>
+</select>
+</p>
+
 </p>
 
 <table id="answer_table" style="width:100%;">
diff --git a/helios/templates/election_questions.html b/helios/templates/election_questions.html
index 468c65e..c721c8a 100644
--- a/helios/templates/election_questions.html
+++ b/helios/templates/election_questions.html
@@ -39,7 +39,7 @@
       if (form.max.value != "")
         max = parseInt(form.max.value);
         
-      var new_q = {'question' : form.question.value, 'min': parseInt(form.min.value), 'max': max, 'short_name': form.question.value, 'answers': [], 'answer_urls': [], 'choice_type': form.choice_type.value, 'tally_type': 'homomorphic', 'result_type': form.result_type.value};
+      var new_q = {'question' : form.question.value, 'min': parseInt(form.min.value), 'max': max, 'short_name': form.question.value, 'answers': [], 'answer_urls': [], 'choice_type': form.choice_type.value, 'tally_type': 'homomorphic', 'result_type': form.result_type.value, 'randomize_answer_order': form.randomize_answer_order.value == "1"};
       var i = 0;
       while(true) {
           i += 1;
@@ -86,6 +86,7 @@
     form[0].max.value = q_data.max;
     form[0].question.value = q_data.question;
     form[0].result_type.value = q_data.result_type;
+    form[0].randomize_answer_order.value = q_data.randomize_answer_order ? "1" : "0";
 
     // add the answers
     var answer_table = $('#answer_table_' + q_num);
diff --git a/heliosbooth/vote.html b/heliosbooth/vote.html
index 3f42230..f413513 100644
--- a/heliosbooth/vote.html
+++ b/heliosbooth/vote.html
@@ -190,8 +190,8 @@ BOOTH.setup_election = function(raw_json, election_metadata) {
     // initialize array so it is the identity permutation
     $(ordering).each(function(j, answer) {ordering[j]=j;});
 
-    // if we want reordering, then we shuffle the array
-    if (election_metadata && election_metadata.randomize_answer_order) {
+    // if we want reordering, at election or question level, then we shuffle the array
+    if ((election_metadata && election_metadata.randomize_answer_order) || question.randomize_answer_order) {
       shuffleArray(ordering);
     }
 
-- 
GitLab