fix vote checkbox
This commit is contained in:
@@ -32,55 +32,80 @@ class VoteController extends Controller
|
|||||||
|
|
||||||
private function insertVote($request)
|
private function insertVote($request)
|
||||||
{
|
{
|
||||||
foreach ($request->vote as $key => $value) {
|
// dd($request->vote[0]['nominee_id']);
|
||||||
Result::updateOrCreate([
|
$votes = array_map(function($nominee_id) use($request){
|
||||||
|
return[
|
||||||
'voter_id' => Auth::id(),
|
'voter_id' => Auth::id(),
|
||||||
'election_id' => Util::getCurrentElection(),
|
'election_id' => Util::getCurrentElection(),
|
||||||
'position_id' => $value['position_id'],
|
'position_id' => $request->vote[0]['position_id'],
|
||||||
], ['nominee_id' => $value['nominee_id']]);
|
'nominee_id' => $nominee_id
|
||||||
|
];
|
||||||
|
|
||||||
|
},$request->vote[0]['nominee_id']);
|
||||||
|
|
||||||
|
foreach($votes as $vote){
|
||||||
|
Result::updateOrCreate([
|
||||||
|
'voter_id' => $vote['voter_id'],
|
||||||
|
'election_id' => $vote['election_id'],
|
||||||
|
'position_id' => $vote['position_id'],
|
||||||
|
'nominee_id' => $vote['nominee_id'],
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validNominee($id, $position_id)
|
private function validNominee($id, $position_id)
|
||||||
{
|
{
|
||||||
return Nominee::where([
|
try{
|
||||||
['id', '=', $id],
|
$nominee = Nominee::where('position_id', '=', $position_id)->whereIn('id',$id)->count();
|
||||||
['position_id', '=', $position_id]
|
}catch(Exception $e){
|
||||||
])->count();
|
dd($id);
|
||||||
|
}
|
||||||
|
return $nominee;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isPositionExist($id)
|
private function isPositionExist($id)
|
||||||
{
|
{
|
||||||
return Position::where('id', $id)->count();
|
try{
|
||||||
|
return Position::where('id', $id)->count();
|
||||||
|
}catch(Exception $e){
|
||||||
|
dd($id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function voteAllPosition($request)
|
private function voteAllPosition($request)
|
||||||
{
|
{
|
||||||
$total_position = Position::where('election_id', Util::getCurrentElection())->count();
|
$total_position = Position::where('election_id', Util::getCurrentElection())->count();
|
||||||
$total_vote = count($request->vote);
|
$total_vote = !empty($request->vote) ? count($request->vote) : 0;
|
||||||
|
|
||||||
return $total_vote >= $total_position;
|
return $total_vote >= $total_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function validateRequest($request)
|
private function validateRequest($request)
|
||||||
{
|
{
|
||||||
$result['status'] = 'failed';
|
$result['status'] = 'failed';
|
||||||
$vote = $request->vote;
|
$vote = $request->vote;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the user vote on all position
|
* Check if the user vote on all position
|
||||||
*/
|
*/
|
||||||
if (!$this->voteAllPosition($request)) {
|
if (!$this->voteAllPosition($request)) {
|
||||||
|
dd('test');
|
||||||
$result['message'] = 'You must vote on all position.';
|
$result['message'] = 'You must vote on all position.';
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($vote as $key => $value) {
|
foreach ($vote as $value) {
|
||||||
/**
|
/**
|
||||||
* Check if position_id and nominee_id has a value
|
* Check if position_id and nominee_id has a value
|
||||||
*/
|
*/
|
||||||
|
// dd($vote,$value);
|
||||||
if (empty($value['position_id']) || empty($value['nominee_id'])) {
|
if (empty($value['position_id']) || empty($value['nominee_id'])) {
|
||||||
|
|
||||||
|
dd($value,empty($value['position_id']),empty($value['nominee_id']));
|
||||||
$result['message'] = 'You must vote on all position.';
|
$result['message'] = 'You must vote on all position.';
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -90,6 +115,7 @@ class VoteController extends Controller
|
|||||||
* Check if the Position you vote exists
|
* Check if the Position you vote exists
|
||||||
*/
|
*/
|
||||||
if (!$this->isPositionExist($value['position_id'])) {
|
if (!$this->isPositionExist($value['position_id'])) {
|
||||||
|
dd('test');
|
||||||
$result['message'] = 'Invalid Position.';
|
$result['message'] = 'Invalid Position.';
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -97,8 +123,11 @@ class VoteController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Check if the Nominee you vote on certain position exists
|
* Check if the Nominee you vote on certain position exists
|
||||||
*/
|
*/
|
||||||
elseif (!$this->validNominee($value['nominee_id'], $value['position_id'])) {
|
// elseif (!$this->validNominee($value['nominee_id'], $value['position_id'])) {
|
||||||
|
elseif($this->validNominee($value['nominee_id'],$value['position_id']) <= 0){
|
||||||
|
|
||||||
$result['message'] = 'Invalid Nominee for '.Position::find($value['position_id'])->name.' position.';
|
$result['message'] = 'Invalid Nominee for '.Position::find($value['position_id'])->name.' position.';
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="row col-md-10 col-md-offset-1">
|
<div class="row col-md-10 col-md-offset-1">
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<h4>Vote Information</h4>
|
<h4>Maklumat Undian</h4>
|
||||||
<hr />
|
<hr />
|
||||||
<ul class="list-group">
|
<ul class="list-group">
|
||||||
<li class="list-group-item" v-for="position in data.positions">
|
<li class="list-group-item" v-for="position in data.positions">
|
||||||
<b>{{ position.name }} : </b>{{ getName(position.id) }}
|
<b>{{ position.name }} : </b>{{ getName(position.id) }}
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
<!-- <ul class="list-group">
|
||||||
|
<li class="list-group-item" v-for="result in data.result">
|
||||||
|
<b>{{ getPosition(result.position_id) }} : </b>{{ getNominee(result.nominee_id) }}
|
||||||
|
</li>
|
||||||
|
</ul> -->
|
||||||
|
|
||||||
<li class="list-group-item">
|
<li class="list-group-item">
|
||||||
<center>
|
<center>
|
||||||
<button class="btn btn-success" @click="util.showModal('#vote-modal')">Submit Vote</button>
|
<button class="btn btn-success" @click="util.showModal('#vote-modal')">Submit Vote</button>
|
||||||
@@ -16,7 +23,7 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-8">
|
<div class="col-md-8">
|
||||||
<h4>Select Candidates</h4>
|
<h4>Pilih Calon</h4>
|
||||||
<hr />
|
<hr />
|
||||||
<div class="panel panel-info" v-for="position in data.positions">
|
<div class="panel panel-info" v-for="position in data.positions">
|
||||||
<div class="panel-heading">{{ position.name }}</div>
|
<div class="panel-heading">{{ position.name }}</div>
|
||||||
@@ -26,16 +33,17 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th></th>
|
<th></th>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Partylist</th>
|
<th>No. Anggota</th>
|
||||||
<th>Unit</th>
|
<th>Unit</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr v-for="nominee in data.nominees" v-if="nominee.position_id==position.id"
|
<tr v-for="(nominee, index) in data.nominees" v-if="nominee.position_id == position.id"
|
||||||
@click="vote(position.id,nominee.id)">
|
:key="nominee.id">
|
||||||
<td><input type="radio" :name="position.id" :id="nominee.id"/></td>
|
<td><input type="checkbox" :id="nominee.id" @change="vote(position.id, nominee.id)" />
|
||||||
|
</td>
|
||||||
<td>{{ nominee.name }}</td>
|
<td>{{ nominee.name }}</td>
|
||||||
<td>{{ getPartylist(nominee.partylist_id) }}</td>
|
<td>{{ nominee.no_anggota }}</td>
|
||||||
<td>{{ nominee.unit }}</td>
|
<td>{{ nominee.unit }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
@@ -44,18 +52,18 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="modal fade" id="vote-modal" tabindex="-2" role="dialog" aria-labelledby="exampleModalLabel"
|
<div class="modal" id="vote-modal" tabindex="-2" role="dialog" aria-labelledby="exampleModalLabel"
|
||||||
aria-hidden="true">
|
aria-hidden="true">
|
||||||
<div class="modal-dialog" role="document">
|
<div class="modal-dialog" role="document">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h5 class="modal-title" id="exampleModalLabel">Vote</h5>
|
<h5 class="modal-title" id="exampleModalLabel">AGM</h5>
|
||||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||||
<span aria-hidden="true">×</span>
|
<span aria-hidden="true">×</span>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<h3>Note: You can only vote once, so vote wisely.</h3>
|
<h3>Nota: You can only vote once, so vote wisely.</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
||||||
@@ -64,7 +72,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -76,7 +83,7 @@ export default {
|
|||||||
for (var i in this.data.positions) {
|
for (var i in this.data.positions) {
|
||||||
this.selected.push({
|
this.selected.push({
|
||||||
position_id: this.data.positions[i]['id'],
|
position_id: this.data.positions[i]['id'],
|
||||||
nominee_id: null,
|
nominee_id: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -84,40 +91,53 @@ export default {
|
|||||||
data: function () {
|
data: function () {
|
||||||
return {
|
return {
|
||||||
selected: [],
|
selected: [],
|
||||||
name: {}
|
name: {},
|
||||||
|
selectedNominees: {
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
test: function(){
|
test: function () {
|
||||||
console.log('tab');
|
console.log('tab');
|
||||||
this.util.showModal('#vote-modal')
|
this.util.showModal('#vote-modal')
|
||||||
},
|
},
|
||||||
submit: function () {
|
submit: function () {
|
||||||
// this.util.hideModal('#vote-modal');
|
if (Object.keys(this.selected).length === 0) {
|
||||||
if (this.hasNullVote()) return;
|
// No selections made
|
||||||
|
this.util.notify('Please select at least one nominee.', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var vm = this;
|
var vm = this;
|
||||||
this.util.notify('Submitting your vote, please wait...', 'loading');
|
this.util.notify('Submitting your vote, please wait...', 'loading');
|
||||||
let data = {};
|
console.log(this.selected)
|
||||||
data['vote'] = this.selected;
|
// Prepare data to send to the server
|
||||||
axios.post(config.API+'election/vote', data)
|
let data = {
|
||||||
.then(response=>{
|
vote: this.selected
|
||||||
|
};
|
||||||
|
|
||||||
|
// Log the data to ensure it's correctly formatted
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
// Send a POST request to the server
|
||||||
|
axios.post(config.API + 'election/vote', data)
|
||||||
|
.then(response => {
|
||||||
$.notifyClose();
|
$.notifyClose();
|
||||||
if (vm.util.showResult(response, 'success')) {
|
if (vm.util.showResult(response, 'success')) {
|
||||||
vm.data.result = response.data.result;
|
vm.data.result = response.data.result;
|
||||||
vm.$router.push({ name: 'Voter Home' });
|
vm.$router.push({ name: 'Voter Home' });
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
.catch(error=>{
|
.catch(error => {
|
||||||
$.notifyClose();
|
$.notifyClose();
|
||||||
vm.util.showResult(error, 'error');
|
vm.util.showResult(error, 'error');
|
||||||
})
|
});
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
hasNullVote: function () {
|
hasNullVote: function () {
|
||||||
let selected = this.selected;
|
console.log('null' + this.selectedNominees);
|
||||||
|
let selected = this.selectedNominees;
|
||||||
for (var i in selected)
|
for (var i in selected)
|
||||||
if (selected[i]['nominee_id'] == null) {
|
if (selected[i]['nominee_id'] == null) {
|
||||||
this.util.notify('You must vote on all position', 'error');
|
this.util.notify('You must vote on all position', 'error');
|
||||||
@@ -127,19 +147,27 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
reset: function () {
|
reset: function () {
|
||||||
let selected = this.selected;
|
console.log(this.selectedNominees);
|
||||||
|
let selected = this.selectedNominees;
|
||||||
for (var i in selected) {
|
for (var i in selected) {
|
||||||
$('#'+selected[i]['nominee_id']).selected(false);
|
$('#' + selected[i]['nominee_id']).selected(false);
|
||||||
selected[i]['nominee_id'] = null;
|
selected[i]['nominee_id'] = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
vote: function (position_id, nominee_id) {
|
vote: function (position_id, nominee_id) {
|
||||||
$('#'+nominee_id).selected();
|
if (!this.selected[0]['position_id']) {
|
||||||
for (var i in this.selected)
|
this.$set(this.selected[0], position_id, []);
|
||||||
if (this.selected[i]['position_id'] == position_id) {
|
}
|
||||||
this.selected[i]['nominee_id'] = nominee_id;
|
const index = this.selected[0]['nominee_id'].indexOf(nominee_id);
|
||||||
}
|
if (index === -1) {
|
||||||
|
// Nominee not found, add it
|
||||||
|
this.selected[0]['nominee_id'].push(nominee_id);
|
||||||
|
} else {
|
||||||
|
// Nominee found, remove it
|
||||||
|
this.selected[0]['nominee_id'].splice(index, 1);
|
||||||
|
}
|
||||||
|
console.log(this.selected[0])
|
||||||
},
|
},
|
||||||
|
|
||||||
getName: function (id) {
|
getName: function (id) {
|
||||||
@@ -152,17 +180,29 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getNominee: function (id) {
|
getNominee: function (id) {
|
||||||
|
let positions = this.data.positions;
|
||||||
let nominees = this.data.nominees;
|
let nominees = this.data.nominees;
|
||||||
for (var i in nominees)
|
for (var i in nominees)
|
||||||
if (nominees[i]['id'] == id) return nominees[i]['name'];
|
if (nominees[i]['id']==id) return nominees[i].name;
|
||||||
|
return '';
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// getNominee: function (id) {
|
||||||
|
// let nominees = this.data.nominees;
|
||||||
|
// for (var i in nominees)
|
||||||
|
// if (nominees[i]['id'] == id) return nominees[i]['name'];
|
||||||
|
// },
|
||||||
|
|
||||||
getPartylist: function (id) {
|
getPartylist: function (id) {
|
||||||
let partylists = this.data.partylists;
|
let partylists = this.data.partylists;
|
||||||
for (var i in partylists)
|
for (var i in partylists)
|
||||||
if (partylists[i]['id'] == id) return partylists[i]['name'];
|
if (partylists[i]['id'] == id) return partylists[i]['name'];
|
||||||
return 'No Partylist';
|
return 'No Partylist';
|
||||||
}
|
},
|
||||||
|
|
||||||
|
// uncheck: function (checkedName) {
|
||||||
|
// this.checkedName = this.checkedNames.filter(name => name !== checkedName);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
Reference in New Issue
Block a user