/** * show vote * @param int $id [description] * @return Response */ public function showvote($id) { $results = array(); $poll = Poll::find($id); $results += array('poll' => $poll); $user_id = Auth::user()->id; $employee_id = Auth::user()->employee()->first()->id; // Check amount poll between start date and end date $dateCurrent = Carbon::now(); $onePlusDate = Carbon::now()->addDay(); $startDate = Carbon::createFromFormat('Y-m-d H:i:s', $dateCurrent->format('Y-m-d') . ' ' . '00:00:00'); $endDate = Carbon::createFromFormat('Y-m-d H:i:s', $onePlusDate->format('Y-m-d') . ' ' . '00:00:00'); $countAnswerInDay = DB::table('poll_answers')->join('poll_user_answers', 'poll_user_answers.answer_id', '=', 'poll_answers.id')->where('poll_answers.poll_id', $id)->whereBetween('poll_user_answers.created_at', [$startDate, $endDate])->count(); $results += array('countAnswerInDay' => $countAnswerInDay); // show result after vote $isShowResultAfterVote = $poll->show_results_req_vote; $results += array('isShowResultAfterVote' => $isShowResultAfterVote); // Check excess time to poll $timeDeadline = Carbon::createFromFormat('Y-m-d H:i:s', $poll->end_date); $checkExcessDeadline = false; $results += array('isShowResultAfterDealine' => $poll->show_results_finish); $votechart = array(); $checkExcessDeadline = $dateCurrent >= $timeDeadline; $results += array('checkExcessDeadline' => $checkExcessDeadline); // check to dealine and is show result if ($checkExcessDeadline || $isShowResultAfterVote) { // if excess time, we can caculate answer percentage poll and show graph. $votechart = DB::table('poll_user_answers')->select(DB::raw('count(*) as value, answer as label'))->join('poll_answers', 'poll_answers.id', '=', 'poll_user_answers.answer_id')->where('poll_answers.poll_id', $id)->groupBy('poll_user_answers.answer_id')->get(); $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'); foreach ($votechart as $key => $value) { $color = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)]; $highlight = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)]; $votechart[$key]->color = $color; $votechart[$key]->highlight = $highlight; } } $results += array('checkExcessDeadline' => $checkExcessDeadline, 'votechart' => $votechart); // caculate count vote and check is show vote number ? $countVote = 0; $showVoteNumber = $poll->show_vote_number; if ($showVoteNumber) { $countVoteDb = DB::table('poll_answers')->select(DB::raw('count(*) as value'))->where('poll_id', $id)->first(); $countVote = $countVoteDb->value; } // check have user already voted yet ? $checkUserVoted = DB::table('poll_answers')->join('poll_user_answers', 'poll_user_answers.answer_id', '=', 'poll_answers.id')->where('poll_answers.poll_id', $id)->where('poll_user_answers.user_id', $employee_id)->count(); $results += array('countVote' => $countVote, 'showVoteNumber' => $showVoteNumber, 'checkUserVoted' => $checkUserVoted); if ($poll) { return view("polls.vote", $results); } abort(404); }
public function update(Request $request, $id) { // Ensure that they filled out the form properly if ($request->has('title')) { // Auth check, to make sure they are still valid if (Auth::check()) { // Look up the poll being edited $poll = Poll::find($id); // Ensure that this poll exists, and belongs to this user if ($poll && $poll->owner_id == Auth::id()) { // Pull the formdata $formdata = $request->all(); // Save the formdata to the new poll object $poll->title = $formdata['title']; $poll->description = $formdata['description']; $poll->owner_id = Auth::id(); // Save the poll object $poll->save(); // Redirect the user back to their homepage with a success message return Redirect::to('/home')->with('success', 'Poll updated successfully'); } } } // Redirect the user back to their homepage with a failure message return Redirect::to('/home')->with('error', 'Please fill out all required fields and try again'); }
/** * Delete a poll * @param int $id * @return Response */ public function delete($id) { $poll = Poll::find($id); if ($poll) { $poll->delete(); Flash::success('Poll deleted'); } return redirect(route('polls.index')); }