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