public function fire()
 {
     //fetch all users
     $getusers = DB::table('users')->select(array('users.*'))->where('users.userstatus', '=', 'approved')->orderBy('created_at', 'DESC')->get();
     //process for the records found
     if (!empty($getusers)) {
         $queryOfferHelp = array();
         foreach ($getusers as $user_info) {
             $diffDate = KarmaHelper::dateDiff(date("Y-m-d H:i:s"), $user_info->created_at);
             if ($diffDate->days > -1) {
                 $user_id = $user_info->id;
                 $location = $user_info->location;
                 /*$myKarmaDataOfInCompleteState= DB::select(DB::raw("SELECT COUNT( * ) AS AGGREGATE FROM  `users_mykarma` WHERE  `status` =  'completed' AND  `user_id` =".$user_id." AND  created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)"));
                 		$myKarmaDataOfInCompleteState= DB::select(DB::raw("SELECT COUNT( * ) AS AGGREGATE FROM  `users_mykarma` WHERE  `status` !=  'completed' AND  `user_id` =".$user_id." AND  created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)"));*/
                 $myKarmaDataOfCompleteState = Mykarma::where('status', '=', 'completed')->where('user_id', '=', $user_id)->where('unread_flag', '=', 'true')->where('created_at', '>=', Carbon::now()->subDay(1))->count();
                 $myKarmaDataOfInCompleteState = Mykarma::where('status', '!=', 'completed')->where('user_id', '=', $user_id)->where('entry_type', '=', 'Meeting')->where('unread_flag', '=', 'true')->where('created_at', '>=', Carbon::now()->subDay(1))->count();
                 $myKarmaDataOfQuery = Mykarma::where('user_id', '=', $user_id)->where('entry_type', '=', 'Query')->get();
                 foreach ($myKarmaDataOfQuery as $key => $value) {
                     $allQuery[] = $value->entry_id;
                 }
                 if (!empty($allQuery)) {
                     $queryOfferHelp = Mykarma::whereIn('entry_id', $allQuery)->where('users_role', '=', 'OfferedHelp')->where('unread_flag', '=', 'true')->where('created_at', '>=', Carbon::now()->subDay(1))->count();
                 }
                 // fetch user connections on KC
                 $getUserConnection = KarmaHelper::getUserConnection($user_id, $location);
                 $user_connection_onkc = 0;
                 if (!empty($getUserConnection)) {
                     foreach ($getUserConnection as $key => $value) {
                         if (isset($value->con_user_id)) {
                             $user_connection_onkc++;
                         }
                     }
                 }
                 // fetch pending karmanote requests
                 $totalPendingRequest = 0;
                 $PendingRequest = array();
                 $PendingRequest = KarmaHelper::getPendingKarmaNotes($user_info->id);
                 if (!empty($PendingRequest)) {
                     $totalPendingRequest = count($PendingRequest);
                 }
                 //fetch pending KM requests only received no read no unread
                 $totalReceivedRequest = 0;
                 $GiverInMeeting = User::find($user_info->id)->Giver()->where('status', 'pending')->orderBy('updated_at', 'DESC')->get();
                 if (!empty($GiverInMeeting)) {
                     $totalReceivedRequest = count($GiverInMeeting);
                 }
                 //fetch pending karma intros
                 $totalintroductionInitiated = 0;
                 $IntroducerInitiated = User::find($user_info->id)->Introducer;
                 if (!empty($IntroducerInitiated)) {
                     foreach ($IntroducerInitiated as $key => $value) {
                         $value['user_id_receiver'] = User::find($value['user_id_receiver'])->toArray();
                         if (!empty($value['user_id_giver'])) {
                             $value['user_id_giver'] = User::find($value['user_id_giver'])->toArray();
                         } else {
                             $value['user_id_giver'] = Connection::find($value['connection_id_giver'])->toArray();
                         }
                         if ($value->status == 'pending') {
                             $totalintroductionInitiated++;
                         }
                     }
                 }
                 // fetch queries that have been posted in the last 7 days within common groups including both public & private.
                 $Usergroup = User::find($user_info->id)->Groups;
                 $All_groups = '';
                 $group_question = 0;
                 $totagroupquestion = 0;
                 $yesterday = Carbon::now()->subDays(1);
                 $one_week_ago = Carbon::now()->subWeeks(1);
                 if (!$Usergroup->isEmpty()) {
                     foreach ($Usergroup as $key => $value) {
                         $All_groups[] = $value->id;
                     }
                     if (!empty($All_groups)) {
                         $group_question = DB::table('group_questions')->join('questions', 'group_questions.question_id', '=', 'questions.id')->select(array('questions.id'))->whereIn('group_questions.group_id', $All_groups)->where('questions.user_id', '!=', $user_info->id)->where('questions.queryStatus', '=', 'open')->where('questions.created_at', '>=', $one_week_ago)->where('questions.created_at', '<=', $yesterday)->orderBy('questions.created_at', 'DESC')->groupBy('question_id')->get();
                         if (!empty($group_question)) {
                             $totagroupquestion = count($group_question);
                         }
                     }
                 }
                 // fetch weekly suggestion option value set from admin
                 $weekly_suggestion = "KarmaNote";
                 $weekly_suggestion = Adminoption::Where("option_name", "=", "Weekly Suggestion")->select("option_value")->first();
                 if (!empty($weekly_suggestion)) {
                     $weekly_suggestion = $weekly_suggestion->option_value;
                 }
                 $getkcUser = $getsuggestion = array();
                 if ($weekly_suggestion == "KarmaMeeting") {
                     // fetch a random users on KC platform with a common group of logged in user
                     $getkcUser = KarmaHelper::fetchUserGroup($user_id);
                     if (!empty($getkcUser)) {
                         $getkcUser = $getkcUser[0];
                     }
                 } else {
                     // fetch a user connection either KC or NON KC
                     $getsuggestion = KarmaHelper::getUserConnection($user_id, $location);
                     //get test users id
                     $getUser = KarmaHelper::getTestUsers();
                     if (!empty($getsuggestion)) {
                         foreach ($getsuggestion as $key => $value) {
                             $test_match = in_array($value->con_user_id, $getUser);
                             if ($value->con_user_id != "" && $test_match != 1) {
                                 $getKc = DB::table('users as u')->select(array('u.userstatus', 'u.id', 'u.fname', 'u.lname', 'u.linkedinurl', 'u.piclink', 'u.headline', 'u.email', 'u.karmascore', 'u.location'))->where('u.id', '=', $value->con_user_id)->where('u.userstatus', '=', 'approved')->get();
                                 if (!empty($getKc)) {
                                     $value->networkid = $getKc;
                                 }
                             }
                         }
                         $getsuggestion = $getsuggestion[array_rand($getsuggestion)];
                     }
                 }
                 //fetch random 5 unique notes
                 $getKarmanote = "";
                 $getKarmanote = KarmaHelper::getKarmanote();
                 $type = 19;
                 $meetingIncomplete = $myKarmaDataOfInCompleteState;
                 $meetingComplete = $myKarmaDataOfCompleteState;
                 $offeredHelp = $queryOfferHelp;
                 $user_id = $user_id;
                 $user_connection_onkc = $user_connection_onkc;
                 $totalPendingRequest = $totalPendingRequest;
                 $totalReceivedRequest = $totalReceivedRequest;
                 $totalintroductionInitiated = $totalintroductionInitiated;
                 $totagroupquestion = $totagroupquestion;
                 $getsuggestion = $getsuggestion;
                 $getkcUser = $getkcUser;
                 $getKarmanote = $getKarmanote;
                 // call a function to send email
                 if ($meetingIncomplete > 0 || $meetingComplete > 0 || $offeredHelp > 0) {
                     $sendLinkedinMessage = MessageHelper::dailyUpdateMykarmaScreen($type, $user_id, $meetingIncomplete, $meetingComplete, $offeredHelp, $user_connection_onkc, $totalPendingRequest, $totalReceivedRequest, $totalintroductionInitiated, $totagroupquestion, $getsuggestion, $getkcUser, $getKarmanote);
                 }
                 /*Queue::push('MessageSender@MyKarmaDailyUpdateScreen',array('type' =>19,'meetingIncomplete'=>$myKarmaDataOfInCompleteState,'meetingComplete'=>$myKarmaDataOfCompleteState,'offeredHelp'=>$queryOfferHelp,'user_id' => $user_id,'user_connection_onkc'=>$user_connection_onkc,'totalPendingRequest' => $totalPendingRequest,'totalReceivedRequest'=>$totalReceivedRequest,'totalintroductionInitiated'=>$totalintroductionInitiated,'totagroupquestion'=>$totagroupquestion,'getsuggestion'=>$getsuggestion,'getkcUser'=>$getkcUser,'getKarmanote'=>$getKarmanote));*/
             }
         }
         //endforeach
     }
     // endif
     Log::alert('Executed Daily Cron Job');
 }
 public static function sendActivationMessage($user_info)
 {
     $subject = "Welcome to KarmaCircles";
     $Content = "Your KarmaCircles account is activated now.";
     $type = "11";
     $user_id = $user_info->id;
     $location = $user_info->location;
     // fetch user connections on KC
     $getUserConnection = KarmaHelper::getUserConnection($user_id, $location);
     $user_connection_onkc = 0;
     if (!empty($getUserConnection)) {
         foreach ($getUserConnection as $key => $value) {
             if (isset($value->con_user_id)) {
                 $user_connection_onkc++;
             }
         }
     }
     // fetch pending karmanote requests
     $totalPendingRequest = 0;
     $PendingRequest = array();
     $PendingRequest = KarmaHelper::getPendingKarmaNotes($user_info->id);
     if (!empty($PendingRequest)) {
         $totalPendingRequest = count($PendingRequest);
     }
     //fetch pending KM requests only received no read no unread
     $totalReceivedRequest = 0;
     $GiverInMeeting = User::find($user_info->id)->Giver()->where('status', 'pending')->orderBy('updated_at', 'DESC')->get();
     if (!empty($GiverInMeeting)) {
         $totalReceivedRequest = count($GiverInMeeting);
     }
     //fetch pending karma intros
     $totalintroductionInitiated = 0;
     $IntroducerInitiated = User::find($user_info->id)->Introducer;
     if (!empty($IntroducerInitiated)) {
         foreach ($IntroducerInitiated as $key => $value) {
             $value['user_id_receiver'] = User::find($value['user_id_receiver'])->toArray();
             if (!empty($value['user_id_giver'])) {
                 $value['user_id_giver'] = User::find($value['user_id_giver'])->toArray();
             } else {
                 $value['user_id_giver'] = Connection::find($value['connection_id_giver'])->toArray();
             }
             if ($value->status == 'pending') {
                 $totalintroductionInitiated++;
             }
         }
     }
     // fetch queries that have been posted in the last 7 days within common groups including both public & private.
     $Usergroup = User::find($user_info->id)->Groups;
     $All_groups = '';
     $group_question = 0;
     $totagroupquestion = 0;
     $yesterday = Carbon::now()->subDays(1);
     $one_week_ago = Carbon::now()->subWeeks(1);
     if (!$Usergroup->isEmpty()) {
         foreach ($Usergroup as $key => $value) {
             $All_groups[] = $value->id;
         }
         if (!empty($All_groups)) {
             $group_question = DB::table('group_questions')->join('questions', 'group_questions.question_id', '=', 'questions.id')->select(array('questions.id'))->whereIn('group_questions.group_id', $All_groups)->where('questions.user_id', '!=', $user_info->id)->where('questions.queryStatus', '=', 'open')->where('questions.created_at', '>=', $one_week_ago)->where('questions.created_at', '<=', $yesterday)->orderBy('questions.created_at', 'DESC')->groupBy('question_id')->get();
             if (!empty($group_question)) {
                 $totagroupquestion = count($group_question);
             }
         }
     }
     // fetch weekly suggestion option value set from admin
     $weekly_suggestion = "KarmaNote";
     $weekly_suggestion = Adminoption::Where("option_name", "=", "Weekly Suggestion")->select("option_value")->first();
     if (!empty($weekly_suggestion)) {
         $weekly_suggestion = $weekly_suggestion->option_value;
     }
     $getkcUser = $getsuggestion = array();
     if ($weekly_suggestion == "KarmaMeeting") {
         // fetch a random users on KC platform with a common group of logged in user
         $getkcUser = KarmaHelper::fetchUserGroup($user_id);
         if (!empty($getkcUser)) {
             $getkcUser = $getkcUser[0];
         }
     } else {
         // fetch a user connection either KC or NON KC
         $getsuggestion = KarmaHelper::getUserConnection($user_id, $location);
         //get test users id
         $getUser = KarmaHelper::getTestUsers();
         if (!empty($getsuggestion)) {
             foreach ($getsuggestion as $key => $value) {
                 $test_match = in_array($value->con_user_id, $getUser);
                 if ($value->con_user_id != "" && $test_match != 1) {
                     $getKc = DB::table('users as u')->select(array('u.userstatus', 'u.id', 'u.fname', 'u.lname', 'u.linkedinurl', 'u.piclink', 'u.headline', 'u.email', 'u.karmascore', 'u.location'))->where('u.id', '=', $value->con_user_id)->where('u.userstatus', '=', 'approved')->get();
                     if (!empty($getKc)) {
                         $value->networkid = $getKc;
                     }
                 }
             }
             $getsuggestion = $getsuggestion[array_rand($getsuggestion)];
         }
     }
     $getkcUser = (array) $getkcUser;
     $getsuggestion = (array) $getsuggestion;
     /*echo"<pre>";print_r($getkcUser);echo"</pre>";
     		echo"<pre>";print_r($getsuggestion);echo"</pre>";
     		die;*/
     //fetch random 5 unique notes
     $getKarmanote = "";
     $getKarmanote = KarmaHelper::getKarmanote();
     // call send mail to user
     $to = $user_info->email;
     $url = URL::to('');
     $mail_subject = "Welcome to KarmaCircles";
     $count = 0;
     $fullname = $user_info->fname . ' ' . $user_info->lname;
     $html = "";
     if (!empty($getKarmanote)) {
         $html .= "<table width='100%:''> ";
         foreach ($getKarmanote as $value) {
             if (isset($value->user_idreceiver['piclink'])) {
                 $rcvrpiclink = $value->user_idreceiver['piclink'];
                 $plink = $url . '/profile/' . strtolower($value->user_idreceiver['fname'] . '-' . $value->user_idreceiver['lname']) . '/' . $value->user_idreceiver['id'];
                 $rcvrpic = "<span><a href='" . $plink . "' target='_blank'><img height='40' width='40' src='" . $rcvrpiclink . "'></a></span>";
             } else {
                 $rcvrpiclink = $url . "/images/default.png";
                 $plink = $url . '/profile/' . strtolower($value->user_idreceiver['fname'] . '-' . $value->user_idreceiver['lname']) . '/' . $value->user_idreceiver['id'];
                 $rcvrpic = "<span><a href='" . $plink . "' target='_blank'><img height='40' width='40' src='" . $rcvrpiclink . "'></a></span>";
             }
             if (isset($value->user_idgiver['piclink'])) {
                 $gvrpiclink = $value->user_idgiver['piclink'];
                 $plink = $url . '/profile/' . strtolower($value->user_idgiver['fname'] . '-' . $value->user_idgiver['lname']) . '/' . $value->user_idgiver['id'];
                 $gvrpic = "<span><a href='" . $plink . "' target='_blank'><img height='40' width='40' src='" . $gvrpiclink . "'></a></span>";
             } else {
                 $plink = $url . '/profile/' . strtolower($value->user_idgiver['fname'] . '-' . $value->user_idgiver['lname']) . '/' . $value->user_idgiver['id'];
                 if (isset($value->connection_idgiver['piclink'])) {
                     $gvrpiclink = $value->connection_idgiver['piclink'];
                 } else {
                     $gvrpiclink = $url . "/images/default.png";
                 }
                 $gvrpic = "<span><a href='" . $plink . "' target='_blank'><img height='40' width='40' src='" . $gvrpiclink . "'></a></span>";
             }
             if (!empty($value->user_idgiver)) {
                 $name = $value->user_idgiver['fname'] . ' ' . $value->user_idgiver['lname'];
             } else {
                 $name = $value->connection_idgiver['fname'] . ' ' . $value->connection_idgiver['lname'];
             }
             $html .= " <tr style='margin-bottom:5px;display:block;text-decoration:none;'>\n                            <td>\n                             \t" . $rcvrpic . "\n                            </td> \n                            <td> \n                              <span><img src=" . $url . "/images/icon002.png height='26' width='26'></span>\n                            </td> \n                            <td>\n                              " . $gvrpic . "\n                            </td> \n                            <td> \n                            <a target='_blank' style='text-decoration:none; color: #39bb95;font-size: 15px;'\n\t\t\t\t\t\t\thref=" . $url . "/meeting/" . strtolower($value->user_idreceiver['fname'] . "-" . $value->user_idreceiver['lname'] . "-" . $value->user_idgiver['fname'] . "-" . $value->user_idgiver['lname']) . '/' . $value->req_id . ">\n                              <p style='margin:0px;margin-left:10px;color: #39BB95;font-size: 15px;''>\n                              \t" . $value->user_idreceiver['fname'] . ' ' . $value->user_idreceiver['lname'] . "\n\t\t\t\t\tsent a KarmaNote to " . $name . "\n                              </p> \n\n                            </td>\n                          </tr>\n                        ";
         }
         $html .= "</table>";
     }
     Mail::send('emails.accountactive', array('type' => '11', 'user_id' => $user_id, 'user_connection_onkc' => $user_connection_onkc, 'totalPendingRequest' => $totalPendingRequest, 'totalReceivedRequest' => $totalReceivedRequest, 'totalintroductionInitiated' => $totalintroductionInitiated, 'totagroupquestion' => $totagroupquestion, 'getsuggestion' => $getsuggestion, 'getKcuser' => $getkcUser, 'getKarmanote' => $getKarmanote, 'url' => $url, 'UserDetail' => $user_info, 'html' => $html), function ($message) use($to, $mail_subject, $fullname) {
         $message->to($to, $fullname)->replyTo('*****@*****.**', "admin")->subject($mail_subject)->from('*****@*****.**', "KarmaCircles Team");
     });
     MessageHelper::sendWelcomeKNote($user_info);
 }
 public function ajaxdashboardSuggestion()
 {
     $getinvites = "";
     $getsuggestion = array();
     $user_info = Auth::user();
     $user_id = 0;
     $location = '';
     if (!empty($user_info)) {
         $user_id = $user_info->id;
         $location = $user_info->location;
         $ReceiverDetail = User::find($user_id);
     }
     $user_info = Auth::user();
     $getKcuser = KarmaHelper::getKcuser(0);
     $skipcount = '';
     //if(!empty($_REQUEST['skipcount']))
     $skipcount = $_REQUEST['skipcount'];
     if (empty($getKcuser)) {
         $getsuggestion = KarmaHelper::getUserConnection($user_id, $location);
         if (!empty($getsuggestion)) {
             foreach ($getsuggestion as $key => $value) {
                 if ($value->con_user_id != "") {
                     $getKcuser = DB::table('users as u')->select(array('u.userstatus', 'u.id', 'u.fname', 'u.lname', 'u.linkedinurl', 'u.piclink', 'u.headline', 'u.email', 'u.karmascore', 'u.location'))->where('u.id', '=', $value->con_user_id)->where('u.userstatus', '=', 'approved')->get();
                     if (!empty($getKcuser)) {
                         $value->networkid = $getKcuser;
                     }
                 }
             }
             $getsuggestion = $getsuggestion[array_rand($getsuggestion)];
             //$skipcount ++;
         }
         // fetch a user connection only nON kc
         $getinvites = KarmaHelper::getUserNonKcConnection($user_id, $location);
         if (!empty($getinvites)) {
             $getinvites = $getinvites[array_rand($getinvites)];
         }
     }
     $skipcountreq = $skipcount + 1;
     $skipcountintro = $skipcount + 2;
     $skipcountinvite = $skipcount + 3;
     $checkMeetingStatus = KarmaHelper::getMeetingStatusForWeb($user_id, $getKcuser[0]->id);
     $checkMeetingStatusOne = KarmaHelper::getMeetingStatusForWeb($user_id, $getKcuser[1]->id);
     $checkMeetingStatusTwo = KarmaHelper::getMeetingStatusForWeb($user_id, $getKcuser[2]->id);
     foreach ($getKcuser as $key => $value) {
         $giver_id = $value->id;
         //$MeetingRequestPending = KarmaHelper::karmaMeetingPendingCount($user_id,$giver_id);
         $MeetingRequestPending = '0';
         $MeetingRequestPendingArray[] = $MeetingRequestPending;
     }
     return View::make('ajaxdashboard_suggestion', array('CurrentUser' => $user_info, 'checkMeetingStatus' => $checkMeetingStatus, 'checkMeetingStatusOne' => $checkMeetingStatusOne, 'checkMeetingStatusTwo' => $checkMeetingStatusTwo, 'MeetingRequestPending' => $MeetingRequestPendingArray[0], 'MeetingRequestPendingOne' => $MeetingRequestPendingArray[1], 'MeetingRequestPendingTwo' => $MeetingRequestPendingArray[2], 'pageTitle' => 'Dashboard', 'getinvites' => $getinvites, 'getsuggestion' => $getsuggestion, 'getKcuser' => $getKcuser[0], 'getKcuserOne' => $getKcuser[1], 'getKcuserTwo' => $getKcuser[2], 'skipcountreq' => $skipcountreq, 'skipcountintro' => $skipcountintro, 'skipcountinvite' => $skipcountinvite));
 }