Sindbad~EG File Manager
<?php
class PollController extends BaseController {
//Get Polls
public function GetPolls($user_id){
try {
$current_dt = date('Y-m-d H:i:s');
$user = User::find($user_id);
if($user!=null)
{
$active_polls = DB::select("select * from polls p
where '$current_dt' between p.start_at and p.end_at
and p.poll_id not in (select poll_id from poll_response where user_id = ".$user_id .")
order by start_at
");
$upcoming_polls = DB::select("select * from polls p
where p.start_at > '$current_dt'
order by start_at
");
$voted_polls = DB::select("select * from polls p
where p.poll_id in (select poll_id from poll_response where user_id = ".$user_id .")
order by start_at
");
return View::make('poll.polls')->with('user_id',$user_id)->with('active_polls',$active_polls)->with('upcoming_polls',$upcoming_polls)->with('voted_polls',$voted_polls);
}
else
{
return View::make('poll.msg')->with('msg',AppMessage::$GENERAL_ERROR);
}
} catch (Exception $e) {
Log::error('PollController - GetPolls : '.$e->getMessage());
return View::make('poll.msg')->with('msg','Error : ' .$e->getMessage());
}
}
//get poll by id
public function GetPoll($id,$user_id){
try {
$user = User::find($user_id);
if($user!=null)
{
$poll = Poll::find($id);
$options = PollOption::where('poll_id',$id)->get();
return View::make('poll.view')->with('user_id',$user_id)->with('poll',$poll)->with('options',$options);
}
else
{
return View::make('poll.msg')->with('msg',AppMessage::$GENERAL_ERROR);
}
} catch (Exception $e) {
Log::error('PollController - GetPoll : '.$e->getMessage());
return View::make('poll.msg')->with('msg','Error : ' .$e->getMessage());
}
}
//Store vote
public function StorePoll($id,$user_id){
try {
$option_text = Input::get("option_text");
$poll = Poll::find($id);
$pr = PollResponse::where('poll_id',$id)->where('user_id',$user_id)->first();
if($pr != null)
{
return Redirect::back()->with('msgError','Sorry Already You given Vote');
}
//$options = PollOption::where('poll_id',$id)->get();
$option_id = intval(Input::get('option'));
if(intval($poll->is_text) == 1 )
{
if(empty($option_text))
{
return Redirect::back()->with('msgError','Please Enter your comments');
}
}
else
{
if(!($option_id>0))
{
return Redirect::back()->with('msgError','Please Choose Any One Option');
}
}
$pr = new PollResponse;
$pr->poll_id = $id;
$pr->user_id = $user_id;
if(intval($poll->is_text)== 1 )
{
$rules = array(
'option_text'=>'required|max:500',
);
$validator =Validator::make(Input::all(),$rules);
if ($validator -> fails())
{
return Redirect::back()->withInput()->with('msgError',' Please Enter below 500 characters');
}
else
{
$pr->option_text = $option_text;
}
}
else
{
$pr->poll_option_id = $option_id;
}
$pr->save();
return Redirect::to('polls'.'/'. $user_id)->with('msgSuccess','Thank you for participating in the poll.');
} catch (Exception $e) {
Log::error('PollController - GetUser : '.$e->getMessage());
return View::make('poll.msg')->with('msg','Error : ' .$e->getMessage());
}
}
//Get Result
public function GetResult($user_id){
try {
$polls = Poll::get();
$optionsdb = PollOption::get();
$options = [];
foreach ($optionsdb as $key => $value) {
$options[$value->poll_id][] = $value;
}
$respons = [];
$responsdb = PollResponse::selectRaw('poll_option_id, count(*) as count')->groupBy('poll_option_id')->get();
//var_dump($respons);
$respons = [];
foreach ($responsdb as $key => $value) {
$respons[$value->poll_option_id] = $value->count;
}
//var_dump($respons);
return View::make('poll.result')->with('user_id',$user_id)->with('polls',$polls)->with('respons',$respons)->with('options',$options);
} catch (Exception $e) {
Log::error('PollController - GetPolls : '.$e->getMessage());
return View::make('poll.msg')->with('msg','Error : ' .$e->getMessage());
}
}
/******************************* saravanan ***************************************/
public function create()
{
return View::make('poll.pollcreate');
}
public function pollPost()
{
$hdnarr= Input::get("hdnarr");
$title= Input::get("title");
$description=Input::get("description");
$hall_name=Input::get("hall_name");
$session_name=Input::get("session_name");
$start_at= Input::get("start_at");
$end_at= Input::get("end_at");
$is_text= Input::get("is_text");
// var_dump($is_text);
// exit();
$rules = array(
'title' => 'required|unique:polls', // just a normal required validation
'description' => 'required', // required and must be unique in the ducks table
'start_at' => 'required|date_format:d/m/Y H:i',
'end_at' => 'required|date_format:d/m/Y H:i',
// 'hdnarr' => 'required'
);
if(empty($is_text))
{
$rules = array(
'title' => 'required|unique:polls', // just a normal required validation
'description' => 'required', // required and must be unique in the ducks table
'start_at' => 'required|date_format:d/m/Y H:i',
'end_at' => 'required|date_format:d/m/Y H:i',
'hdnarr' => 'required'
);
}
$messages = array('title.unique'=> $title . ' this title already taken','hdnarr.required'=> 'Please add atleast one option' );
$validator = Validator::make(Input::all(), $rules,$messages);
if($validator->fails()) {
return Redirect::to('pollcreate')->withInput()
->withErrors($validator);
}
else
{
$arrayOfmyJsonString=json_decode($hdnarr); //conver to json to array
$polls=new Poll();
$polls->title=$title;
$polls->description=$description;
$polls->hall_name=$hall_name;
$polls->session_name=$session_name;
$polls->start_at = Helper::toDBDatetime($start_at);
$polls->end_at = Helper::toDBDatetime($end_at);
$polls->is_text=$is_text;
$polls->save();
if(empty($is_text)) {
foreach ($arrayOfmyJsonString as $key => $value) {
$option=new PollOption();
$option->poll_id = $polls->poll_id;
$option->option_text=$value->option_name;
$option->option_text=$value->option_name;
$option->save();
}
}
return Redirect::to('pollindex')->with('success','Add successfully!');
}
}
public function pollIndex()
{
$result=PollResponse::get();
$respons=[];
foreach ($result as $key => $value) {
$respons[$value->poll_id][]=$value->poll_id;
}
$polls = Poll::get(); //DB::select('call sp_polls()');
return View::make('poll.pollindex')->with('polls',$polls)
->with('respons',$respons);
}
public function pollDelete($poll_id)
{
$option = PollOption::where('poll_id', '=', $poll_id)->delete();
$polls = Poll::find($poll_id);
$polls->delete();
return Redirect::to('pollindex')->with('success','Delete successfully!');
}
public function pollView($poll_id)
{
$result=PollResponse::where('poll_id','=', $poll_id)->get();
$respons=[];
foreach ($result as $key => $value) {
$respons[$value->poll_option_id][]=$value;
}
$poll = Poll::find($poll_id);
$options = PollOption::where('poll_id','=', $poll_id)->get();
$barData = $this->getPollOptionsChart1($poll_id);
$barData = json_encode($barData);
//exit();
if($poll->is_text == 1)
{
return View::make('poll.bigscreen1')->with('poll',$poll)->with('options',$options)->with('barData',$barData);
}
else
{
return View::make('poll.bigscreen')->with('poll',$poll)->with('options',$options)->with('barData',$barData)->with('respons',$respons);
}
}
// public function pollScreen($poll_id)
// {
// $result=PollResponse::where('poll_id','=', $poll_id)->get();
// $respons=[];
// foreach ($result as $key => $value) {
// $respons[$value->poll_option_id][]=$value;
// }
// $poll = Poll::find($poll_id);
// $options = PollOption::where('poll_id','=', $poll_id)->get();
// $barData = $this->getPollOptionsChart($poll_id);
// $barData = json_encode($barData);
// //exit();
// //return View::make('poll.pollview')->with('poll',$poll)->with('options',$options)->with('barData',$barData)->with('respons',$respons);
// return View::make('poll.bigscreen')->with('poll',$poll)->with('options',$options)->with('barData',$barData)->with('respons',$respons);
// }
public function pollEdit($poll_id)
{
$poll = Poll::find($poll_id);
$options_db = PollOption::where('poll_id','=', $poll_id)->get();
$result = PollResponse::where('poll_id','=', $poll_id)->get();
$respons=[];
foreach ($result as $key => $value) {
$respons[$value->poll_option_id][]=$value;
}
$options = [];
foreach ($options_db as $key => $value) {
$value->status = 1;
if(isset($respons[$value->poll_option_id]))
{
$value->is_delete=0;
}
else{
$value->is_delete=1;
}
array_push($options, $value);
}
$options = json_encode($options);
return View::make('poll.polledit')->with('poll',$poll)->with('options',$options)
->with('respons',$respons);
}
public function pollStart()
{
$poll_id= Input::get("poll_id");
$start_at= date("Y-m-d H:i:s");
//$end_at= date("Y-m-d H:i:s", strtotime("+10 minutes"));
$end_at= date("Y-m-d H:i:s", strtotime("+1 hours"));
$poll = Poll::find($poll_id);
$poll->start_at =$start_at;
$poll->end_at = $end_at;
$poll->save();
$array = array(
'start_at' =>date('d/m/Y H:i:s',strtotime($start_at)),
'end_at' => date('d/m/Y H:i:s',strtotime($end_at))
);
return Response::json($array);
//return json_encode($array, JSON_PRETTY_PRINT);
}
public function pollStop()
{
$poll_id= Input::get("poll_id");
// $start_at= date("Y-m-d H:i:s");
$end_at= date("Y-m-d H:i:s");
$poll = Poll::find($poll_id);
// $poll->start_at =$start_at;
$poll->end_at = $end_at;
$poll->save();
$array = array(
// 'start_at' =>$start_at ,
'end_at' =>date('d/m/Y H:i:s',strtotime($end_at))
);
return Response::json($array);
// $end_at=date('d/m/Y H:i',strtotime($end_at)) [0];
return json_encode($array, JSON_PRETTY_PRINT);
}
public function pollUpdate($poll_id)
{
$hdnarr= Input::get("hdnarr");
$title= Input::get("title");
$description=Input::get("description");
$hall_name=Input::get("hall_name");
$session_name=Input::get("session_name");
$start_at= Input::get("start_at");
$end_at= Input::get("end_at");
$is_text= Input::get("is_text");
$rules= [];
$hdnarr = $hdnarr=="[]"? "":$hdnarr;
if(intval($is_text) == 1)
{
$rules = array(
'title' => 'required', // just a normal required validation
'description' => 'required', // required and must be unique in the ducks table
'start_at' => 'required|date_format:d/m/Y H:i',
'end_at' => 'required|date_format:d/m/Y H:i',
);
}
else
{
$rules = array(
'title' => 'required', // just a normal required validation
'description' => 'required', // required and must be unique in the ducks table
'start_at' => 'required|date_format:d/m/Y H:i',
'end_at' => 'required|date_format:d/m/Y H:i',
'hdnarr' => 'required'
);
}
$data = array(
'title' => $title, // just a normal required validation
'description' => $description, // required and must be unique in the ducks table
'start_at' => $start_at,
'end_at' => $end_at,
'hdnarr' => $hdnarr
);
$messages = array('title.unique'=> $title . ' this title already taken','hdnarr.required'=> 'Please add atleast one option' );
$validator = Validator::make($data, $rules,$messages);
if ($validator->fails()) {
return Redirect::to('polledit/'.$poll_id)->withInput()
->withErrors($validator);
}
else
{
$arrayOfmyJsonString=json_decode($hdnarr); //conver to json to array
$poll = Poll::find($poll_id);
$poll->title=$title;
$poll->description=$description;
$poll->hall_name=$hall_name;
$poll->session_name=$session_name;
$poll->start_at = Helper::toDBDatetime($start_at);
$poll->end_at = Helper::toDBDatetime($end_at);
$poll->is_text=$is_text;
$poll->save();
if(empty($is_text)) {
foreach ($arrayOfmyJsonString as $key => $options)
{
if($options->poll_option_id > 0 && $options->status == 0)
{
$option = PollOption::find($options->poll_option_id);
$option->delete();
}
if($options->poll_option_id == 0 && $options->status == 1)
{
$option=new PollOption();
$option->poll_id = $poll->poll_id;
$option->option_text=$options->option_text;
$option->save();
}
if($options->poll_option_id >0 && $options->status == 1)
{
$option = PollOption::find($options->poll_option_id);
$option->poll_id = $poll->poll_id;
$option->option_text=$options->option_text;
$option->save();
}
}
}
return Redirect::to('pollindex')->with('success','update successfully!');
}
}
/******************************* saravanan ***************************************/
public function getPollOptionsChart1($poll_id)
{
$chartData = array();
try {
$options = PollOption::where('poll_id',$poll_id)->get();
$responsdb = PollResponse::where('poll_id',$poll_id)->get();
$respons = [];
$total_count = 0;
foreach ($responsdb as $key => $value) {
$respons[$value->poll_option_id][] = $value;
}
foreach ($options as $key => $value) {
if(isset($respons[$value->poll_option_id]))
{
$option_count = count($respons[$value->poll_option_id]);
//var_dump($option_count);
$total_count += $option_count;
}
}
//var_dump($total_count);
foreach ($options as $key => $value) {
$option_count = 0;
if(isset($respons[$value->poll_option_id]))
{
$option_count = count($respons[$value->poll_option_id]);
}
$percentage = 0;
if($option_count>0)
{
$percentage = round(($option_count / $total_count) * 100);
}
$chartData[] = ['x' => $value->option_text,'y'=> $percentage."%"];
}
} catch (Exception $e)
{
echo $e->getMessage();
Log::error("PollController -> getCampaignsComparePercentageChart() " . $e->getMessage());
}
return $chartData;
}
public function getPollOptionsChart($poll_id)
{
$chartData = array();
try {
$options = PollOption::where('poll_id',$poll_id)->get();
$responsdb = PollResponse::where('poll_id',$poll_id)->get();
$respons = [];
$total_count = 0;
foreach ($responsdb as $key => $value) {
$respons[$value->poll_option_id][] = $value;
}
foreach ($options as $key => $value) {
if(isset($respons[$value->poll_option_id]))
{
$option_count = count($respons[$value->poll_option_id]);
//var_dump($option_count);
$total_count += $option_count;
}
}
//var_dump($total_count);
foreach ($options as $key => $value) {
$option_count = 0;
if(isset($respons[$value->poll_option_id]))
{
$option_count = count($respons[$value->poll_option_id]);
}
$percentage = 0;
if($option_count>0)
{
$percentage = round(($option_count / $total_count) * 100);
}
$chartData[] = ['x' => $value->option_text,'y'=> $percentage."%"];
}
} catch (Exception $e)
{
echo $e->getMessage();
Log::error("PollController -> getPollOptionsChart() " . $e->getMessage());
}
return Response::json($chartData);
}
public function getEndTime($poll_id)
{
try {
$poll = Poll::find($poll_id);
$currnet_time = date('d/m/Y H:i');
$start_time = date('d/m/Y H:i',strtotime($poll->start_at));
$end_time = date('d/m/Y H:i',strtotime($poll->end_at));
$show_time = "";
if($start_time > $currnet_time)
{
$show_time = $end_time;
}
elseif($currnet_time < $end_time)
{
$show_time = $currnet_time;
}
else
{
$show_time = $end_time;
}
} catch (Exception $e)
{
echo $e->getMessage();
Log::error("PollController -> getEndTime() " . $e->getMessage());
}
return Response::json($show_time);
}
public function getCommand($poll_id)
{
$result = PollResponse::join('users as u','u.user_id','=','poll_response.user_id')->where('poll_id','=', $poll_id)->
select('u.prefix','u.first_name','poll_response.*')
->orderBy('poll_response.created_at', 'DESC')->get();
return View::make('poll.command')->with('respons',$result);
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists