forked from nihavi/CRElection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
219 lines (215 loc) · 7.01 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
//CR Election portal
require_once("config.php");
session_start();
function my_shuffle($candidates) {
$n = [];
foreach ($candidates as $id => $candidate) {
$n[] = [ $id, $candidate ];
}
shuffle($n);
$s = [];
foreach ($n as $value) {
$s[$value[0]] = $value[1];
}
return $s;
}
$htmlOutput = '';
if ( allowed() ) {
if (isset($is_stv) && $is_stv) {
$htmlOutput .= '';
$candidates = get_candidates();
$candidates = my_shuffle($candidates);
ob_start();
?>
<script src="<?php echo $base_url; ?>bower_components/jquery/dist/jquery.min.js"></script>
<script src="<?php echo $base_url; ?>bower_components/Sortable/Sortable.min.js"></script>
<div class="clearfix text-center">
<div class="stv-half">
<h3>Select your <span class="vote-count-ordinal">1st</span> preference</h3>
<ul class="stv-list">
<?php foreach ($candidates as $candidate_id => $candidate_name): ?>
<li class="clearfix listed-candidate candidate<?php echo $candidate_id; ?>">
<?php echo $candidate_name; ?>
<button
class="select-candidate-btn"
data-id="<?php echo $candidate_id; ?>"
data-name="<?php echo $candidate_name; ?>">➡</button>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="stv-half">
<h3>Your preference</h3>
<div class="stv-pref-container">
<ul class="stv-list stv-index">
</ul>
<ul class="stv-list preference-order">
</ul>
</div>
<form action='vote.php' method='post' class='vote' onsubmit='return checkVotes(this)'>
<input type="hidden" name="candidates_string" class="final-preference-order">
<input type="submit" class="btn" value="Submit">
</form>
</div>
</div>
<script>
function ordinalize(num) {
if ( [11,12,13].indexOf(num % 100) === -1 ){
switch (num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return num + 'st';
case 2: return num + 'nd';
case 3: return num + 'rd';
}
}
return num + 'th';
}
var setIndexLI = function (count) {
var $stvIndex = $('.stv-index').empty();
for (var i = 0; i < count; i++) {
$stvIndex.append( $('<li>').text(i+1) );
}
};
var voteUp = function () {
var $voteCounter = $('.vote-count-ordinal');
var count = parseInt($voteCounter.text());
setIndexLI(count);
$voteCounter.text(ordinalize(count + 1));
};
var voteDown = function () {
var $voteCounter = $('.vote-count-ordinal');
var count = parseInt($voteCounter.text());
setIndexLI(count - 2);
$voteCounter.text(ordinalize(count - 1));
};
var checkVotes = function (form) {
var minVotes = <?php echo $delegation_size; ?>;
var maxVotes = <?php echo count($candidates); ?>;
var votes = Array.from(
$('.preference-order')
.children()
.map(function (a, b) {
return $(this).data('id');
})
);
var numOfVotes = votes.length;
if (numOfVotes < minVotes || numOfVotes > maxVotes) {
alert("You have to vote for atleast "+minVotes+" candidates");
return false;
}
form.candidates_string.value = votes.join(' ');
};
$(function () {
var remove = function () {
var $this = $(this);
var id = $this.data('id');
$('.listed-candidate.candidate'+id).show();
$this.closest('.selected-candidate').remove();
voteDown();
};
var getSelectedCandidateLI = function (name, id) {
return $('<li class="selected-candidate clearfix"></li>')
.data('id', id)
.html('⋮ ' + name)
.append( $('<button>').text('✖').click(remove).data('id', id ) );
};
var $preferenceOrderList = $('.preference-order');
Sortable.create($preferenceOrderList[0], {
animation: 200
});
$('.select-candidate-btn').on('click', function () {
var $this = $(this);
$this.closest('.listed-candidate').hide();
$preferenceOrderList.append(
getSelectedCandidateLI($this.data('name'), $this.data('id'))
);
voteUp();
});
});
</script>
<?php
$htmlOutput .= ob_get_contents();
ob_end_clean();
}
else {
if ( (empty($multiple_votes) || $max_votes <= 1) ) {
// Check and account for multiple votes
$input_type = 'radio';
$input_req = 'required';
}
else {
$htmlOutput .= "
<script>
function checkVotes(form) {
var allowed = ".$max_votes.";
var n_allowed = ".$max_n_votes.";
var inputs = form.getElementsByTagName('input');
var voted = 0, n_voted = 0;
for ( i=0; i<inputs.length; i++ ) {
if ( inputs[i].name == 'candidate_id[]' && inputs[i].checked )
++voted;
else if ( inputs[i].name == 'n_candidate_id[]' && inputs[i].checked )
++n_voted;
}
if ( voted != allowed || n_voted > n_allowed) {
if ( n_allowed == 0 ) {
alert('Vote for exactly '+allowed+' candidates.');
}
else {
alert('Please cast exactly '+allowed+' positive and maximum '+n_allowed+' negative votes.');
}
return false;
}
return true;
}
</script>
";
$input_type = 'checkbox';
$input_req = '';
}
if ( $negative_votes ) {
// Check and account for negative votes
if ( $max_n_votes <= 1) {
$n_input_type = 'radio';
}
else {
$n_input_type = 'checkbox';
}
}
$htmlOutput .= "<form action='vote.php' method='post' class='vote' onsubmit='return checkVotes(this)'>";
$candidates = get_candidates();
if ( count($candidates) ) {
if ( $negative_votes ) {
$htmlOutput .= '<table><!--tr><th>positive</th><th>negative</th><th>candidate</th></tr-->';
}
foreach ( $candidates as $id => $name ){
if ( $negative_votes ) {
$htmlOutput .= "<tr><td class='pos'><input type='$input_type' name='candidate_id[]' value='$id' $input_req></td>
<td class='neg'><input type='$n_input_type' name='n_candidate_id[]' value='$id'></td>
<td class='name'>$name</td></tr>";
}
else {
$htmlOutput .= "<label><input type='$input_type' name='candidate_id[]' value='$id' $input_req>$name</label><br>";
}
}
$htmlOutput .= '</table>';
$htmlOutput .= "<input class='btn' type='submit' value='Vote'></form>";
}
else {
$htmlOutput .= "No Candidates in the list.";
}
}
}
else {
if ( isset( $_SESSION["done_voting"] ) && $_SESSION["done_voting"] ) {
$htmlOutput .= "Your response has been recorded.<br><a class='btn' href=''>Refresh</a>";
$htmlOutput .= "<script>document.body.onload=function(){setTimeout(function(){window.location=''}, 5000)}</script>";
unset($_SESSION["done_voting"]);
}
else {
$htmlOutput .= "<strong>Access denied.</strong> Please ask the administrator to allow you to vote. <br><a class='btn' href=''>Reload</a>";
$htmlOutput .= "<script>document.body.onload=function(){setTimeout(function(){window.location=''}, 1000)}</script>";
}
}
include('template.php');