public function retriveGroupMessage()
 {
     $input = Input::all();
     $user = Auth::user();
     $messages = DB::table('conversation_message')->where('conversation_id', $input['conversationID'])->orderBy('id', 'desc')->get();
     //$scope.messages = $messages;
     $userid = $user->id;
     $userList = ConversationUser::where('conversation_id', '=', $input['conversationID'])->get();
     $userNotificationStatus = GroupNotificationStatus::where('conversation_id', '=', $input['conversationID'])->where('user_id', '=', $user->id)->get();
     $userListArray = array();
     $j = 0;
     foreach ($userList as $key => $value) {
         # code...
         //echo($value->user_id);
         $userInfo = DB::select('select * from users where id = ?', array($value->user_id));
         $userListArray[$j]['id'] = $userInfo[0]->id;
         $userListArray[$j]['full_name'] = $userInfo[0]->firstname . ' ' . $userInfo[0]->lastname;
         $j++;
     }
     $i = 0;
     $tmessages['user_list'] = json_encode($userListArray);
     $tmessages['status'] = false;
     if (isset($userNotificationStatus[0])) {
         if ($userNotificationStatus[0]->status == 1) {
             # code...
             $tmessages['status'] = true;
         }
     }
     foreach ($messages as $message) {
         $user = DB::select('select * from users where id = ?', array($message->sender_id));
         //print_r($user[0]->picture);
         $m = new \MomentPHP\MomentPHP($message->created_at);
         $momentFromVo = $m->fromNow();
         $tmessages['mlist'][$i]['userid'] = $user[0]->id;
         $tmessages['mlist'][$i]['pic'] = base64_decode($user[0]->picture);
         $tmessages['mlist'][$i]['fname'] = $user[0]->firstname . " " . $user[0]->lastname;
         $tmessages['mlist'][$i]['message'] = $message->message;
         $tmessages['mlist'][$i]['created_at'] = $momentFromVo;
         $tmessages['mlist'][$i]['id'] = $message->id;
         $tmessages['mlist'][$i]['current_user'] = $userid;
         $i++;
     }
     return json_encode($tmessages);
     //
 }
    print curl_error($ch);
} else {
    curl_close($ch);
}
$columns = json_decode($resp)->results["0"]->data_columns;
$data = json_decode($resp)->results["0"]->data;
$todayActions = array();
$tomorrowActions = array();
$weekendActions = array();
$nextweekActions = array();
//    echo $nwd_start->format("d.m.Y") . ' ' . $dateOfEndOfWeek;
foreach ($data as $key => $value) {
    $a_date = substr($value[array_search("ACTION_DATE", $columns)], 0, 10);
    $inst = new MomentPHP\MomentPHP($a_date);
    $inst2 = new MomentPHP\MomentPHP($a_date);
    $inst3 = new MomentPHP\MomentPHP();
    $inst3->add(1, 'day');
    //        echo $a_date . '  ---  {' . $inst3->format('d.m.Y') . '}  :  ';
    //        echo $wkd_start->format('d.m.Y') . '  <>  ' . $wkd_end->format('d.m.Y');
    //        echo $a_date . ' --  ';
    $inst2Format = $inst2->format('d.m.Y');
    if ($a_date == $today) {
        array_push($todayActions, $value);
    } else {
        if ($a_date == $inst3->format('d.m.Y')) {
            array_push($tomorrowActions, $value);
        } else {
            if ($inst2Format == $wkd_start->format('d.m.Y') || $inst2Format == $wkd_end->format('d.m.Y')) {
                array_push($weekendActions, $value);
            } else {
                if ($inst2 >= $nwd_start && $inst2 <= $dateOfEndOfWeek3) {
 public function searchConversationByID()
 {
     $input = Input::all();
     $user = Auth::user();
     $messages = array();
     $umessages = Message::where('to', '=', $user['id'])->where('from', $input['id'])->get();
     $imessages = Message::where('to', '=', $input['id'])->where('from', $user['id'])->get();
     //echo($input['id']);
     $i = 0;
     foreach ($umessages as $umessage) {
         if (true) {
             $tou = DB::select('select * from users where id = ?', array($umessage->to));
             $fromu = DB::select('select * from users where id = ?', array($umessage->from));
             $messages['user']['firstname'] = ucfirst($tou[0]->firstname);
             $messages['user']['lastname'] = ucfirst($tou[0]->lastname);
             $messages['list'][$i]['id'] = $umessage->id;
             $messages['list'][$i]['message'] = $umessage->message;
             $messages['list'][$i]['from'] = $umessage->from;
             $messages['list'][$i]['to'] = $umessage->to;
             $messages['list'][$i]['pic'] = base64_decode($fromu[0]->picture);
             $messages['list'][$i]['tousername'] = $tou[0]->firstname . " " . $tou[0]->lastname;
             $messages['list'][$i]['fromusername'] = $fromu[0]->firstname . " " . $fromu[0]->lastname;
             if ($umessage->from == $user['id']) {
                 $messages['list'][$i]['type'] = 'current_user';
             }
             $m = new \MomentPHP\MomentPHP($umessage->updated_at);
             $momentFromVo = $m->fromNow();
             $messages['list'][$i]['updated_at'] = $momentFromVo;
             $i++;
         }
     }
     foreach ($imessages as $imessage) {
         if (true) {
             $tou = DB::select('select * from users where id = ?', array($imessage->to));
             $fromu = DB::select('select * from users where id = ?', array($imessage->from));
             $messages['user']['firstname'] = ucfirst($tou[0]->firstname);
             $messages['user']['lastname'] = ucfirst($tou[0]->lastname);
             $messages['list'][$i]['id'] = $imessage->id;
             $messages['list'][$i]['message'] = $imessage->message;
             $messages['list'][$i]['from'] = $imessage->from;
             $messages['list'][$i]['to'] = $imessage->to;
             $messages['list'][$i]['pic'] = base64_decode($fromu[0]->picture);
             $messages['list'][$i]['tousername'] = $tou[0]->firstname . " " . $tou[0]->lastname;
             $messages['list'][$i]['fromusername'] = $fromu[0]->firstname . " " . $fromu[0]->lastname;
             if ($imessage->from == $user['id']) {
                 $messages['list'][$i]['type'] = 'current_user';
             }
             $m = new \MomentPHP\MomentPHP($imessage->updated_at);
             $momentFromVo = $m->fromNow();
             $messages['list'][$i]['updated_at'] = $momentFromVo;
             $i++;
         }
     }
     //print_r($messages);
     return $messages;
 }
    public function notificationEmailCron()
    {
        $userObj = new stdClass();
        // read user data from the database and check if the user are not inside admin group
        $users = DB::table('users')->where('notification_email', 1)->where('suspended', 0)->get(array('id', 'username', 'firstname', 'lastname', 'email', 'created_at', 'updated_at'));
        if (sizeof($users) < 1) {
            print_r('no users found<br/>');
        }
        // loop thru all the users with email_notification enable
        foreach ($users as $user) {
            // init role assignment to check wether the user is admin or normal user
            $role_assignments = DB::table('role_assignments')->where('user_id', $user->id)->where('role_id', 1)->get();
            //print_r($role_assignments);
            if (count($role_assignments) > 0) {
                continue;
            }
            // building up the userObj
            $userObj->id = $user->id;
            $userObj->username = $user->username;
            $userObj->firstname = $user->firstname;
            $userObj->lastname = $user->lastname;
            $userObj->email = $user->email;
            $userObj->created_at = $user->created_at;
            $userObj->updated_at = $user->updated_at;
            $groupid = null;
            $group = null;
            $notifications = null;
            // init user task object
            $userTaskObj = new UserTask();
            $fiveDaysAgo = new DateTime('now - 5 days');
            $oneDaysAgo = new DateTime('now - 1 days');
            $groupid = DB::table('group_users')->where('user_id', $user->id)->pluck('group_id');
            // select group_name based on user_id from user table
            $group = DB::table('groups')->where('id', $groupid)->get(array('id', 'name'));
            // set the user group to the 1st group
            // currently the user has one group only but the user may have more group in the future
            // so initially set the user group to the 1st element of the array
            $userObj->groupid = $group[0]->id;
            $userObj->groupname = $group[0]->name;
            // get the latest 5 notifications belong to the user
            $notifications = DB::table('notifications')->where('user_id', $user->id)->orderBy('id', 'desc')->take(5)->get();
            // get the user outgoing notifications in the last 1 day
            $outGoingNotifications = DB::table('notifications')->where('source_user_id', $user->id)->where('created_at', '>', $oneDaysAgo->format(DateTime::ISO8601))->orderBy('id', 'desc')->get();
            // check the user post in the last 1 day
            $wallPost = DB::table('posts')->where('user_id', $user->id)->where('group_id', $groupid)->where('created_at', '>', $oneDaysAgo->format(DateTime::ISO8601))->orderBy('id', 'desc')->get();
            $totaltasks = $userTaskObj->totalTasks($userObj->groupid);
            // completed tasks
            $completedTasks = DB::table('user_tasks')->where('user_id', $user->id)->where('group_id', $groupid)->where('complete', 1)->orderBy('id', 'desc')->get(array('id', 'updated_at'));
            // check to see if user is first in group to complete missions and the 1st in the team to finish
            $results = DB::select('select ut.user_id, count(1) finished, max(ut.updated_at) as complete_at, u.username, u.id
				from user_tasks ut JOIN users u ON u.id = user_id
				where ut.group_id = ?
					and ut.complete = 1
				group by ut.user_id
	            having count(1) = ?
				order by max(ut.updated_at) LIMIT 1', array($groupid, $totaltasks));
            // set object value
            $userObj->number_of_completed_tasks = count($completedTasks);
            $userObj->completed_tasks = $completedTasks;
            $userObj->days_since_last_completed_task = 0;
            $userObj->missions_completed = 'false';
            $userObj->first_to_complete_all_missions = 'false';
            $userObj->number_of_total_tasks = $totaltasks;
            $userObj->first_to_complete_all_missions_username = "";
            //Get days passed since user created
            $timeStartDate = $userObj->created_at;
            $dayPassed = daysDiff(new DateTime($timeStartDate), new DateTime('now')) + 1;
            // check if the user has completed all their tasks
            if (count($completedTasks) == $totaltasks && $totaltasks > 0) {
                $userObj->missions_completed = 'true';
                if (count($results > 0) && $results[0]->user_id == $user->id) {
                    $userObj->first_to_complete_all_missions = 'true';
                }
            } else {
                if (count($completedTasks) > 0) {
                    // get last completed task
                    $lastCompletedTask = reset($completedTasks);
                    $userObj->days_since_last_completed_task = daysDiff(new DateTime($lastCompletedTask->updated_at), new DateTime('now'));
                }
            }
            print '$userObj->days_since_last_completed_task: ' . $userObj->days_since_last_completed_task . '<br>';
            $userObj->inactive_user = '******';
            $userObj->inactive_user_on_missions = 'false';
            // begin pendingTask related code
            // get pending_task of user
            $pendingTask = DB::table('user_tasks')->where('user_id', $user->id)->where('group_id', $groupid)->where('complete', 0)->orderBy('id', 'desc')->first();
            $userObj->pending_task = $pendingTask;
            // check if user has not used the app by checking pending tasks or out going notifications
            //TODO: add check for $userObj->days_since_last_completed_task in IF
            if (count($outGoingNotifications) == '0' && count($wallPost) == '0' && !isset($userObj->pending_task) && daysDiff(new DateTime($userObj->created_at), new DateTime('now')) > 0 && $userObj->missions_completed != true) {
                // check if the user has any completed tasks
                $userObj->inactive_user = '******';
            } elseif ($userObj->days_since_last_completed_task > 1) {
                $userObj->inactive_user_on_missions = 'true';
            }
            print '$userObj->inactive_user: '******'<br>';
            $userObj->pending_task_everyday = 'false';
            // if there is a pending task that is at least 1 day old
            if ($pendingTask) {
                $pendingDaysOld = daysDiff(new DateTime($pendingTask->created_at), new DateTime('now'));
                print 'pendingDaysOld: ' . $pendingDaysOld . '<br>';
                if ($pendingDaysOld > 0) {
                    $userObj->pending_task_everyday = 'true';
                }
            }
            print 'count($outGoingNotifications): ' . count($outGoingNotifications) . '<br>';
            print 'count($wallPost): ' . count($wallPost) . '<br>';
            print 'isset($userObj->pending_task): ' . isset($userObj->pending_task) . '<br>';
            print 'daysDiff(new DateTime($userObj->created_at), new DateTime(now)): ' . daysDiff(new DateTime($userObj->created_at), new DateTime('now')) . '<br>';
            print 'userObj->pending_task_everyday: ' . $userObj->pending_task_everyday . '<br>';
            print '$userObj->completed_tasks: ' . $userObj->number_of_completed_tasks . '<br>';
            print '$userObj->missions_completed: ' . $userObj->missions_completed . '<br>';
            print 'first_to_complete_all_missions: ' . $userObj->first_to_complete_all_missions . '<br>';
            print 'inactive_user_on_missions: ' . $userObj->inactive_user_on_missions . '<br>';
            // end pendingTask related code
            $noNotificationFlag = true;
            if ($notifications) {
                $noNotificationFlag = false;
                $userObj->notifications = $notifications;
            }
            $userObj->notification_content = "";
            // check if there is any notification belongs to the user
            // call push notification object
            $pushNotificationObject = new PushNotificationController();
            $mailContent = "Hi " . $userObj->firstname . "<br/><br/>";
            // set nothing to send flag to check whether to send an email
            $nothingToSend = true;
            // if there is a pending task the last 1 day
            if ($userObj->pending_task_everyday == 'true') {
                // sendput push notification
                //$pushNotificationObject->sendPushNotification(SITE_URL."/#/mission", "Just a friendly reminder that you have a pending Mission and your team mates need you! If you haven't already, \nplease log in to On A Roll 21 and let them know how you roll.", "mobile", $userObj->id);
                $mailContent .= "Just a friendly reminder that you have a pending Mission and your team mates need you! If you haven't already, please log in to On A Roll 21&trade; and let them know how you roll.<br/><br/>";
                $nothingToSend = false;
            } elseif ($userObj->inactive_user == 'true') {
                // send out push notification
                //$pushNotificationObject->sendPushNotification(SITE_URL."/#/journal", "Just a friendly reminder that you've not accepted any Missions or participated in your teams Wall. \nYour team mates need you! If you haven't already, please log in to On A Roll 21 and let them know how you roll...", "mobile", $userObj->id);
                $mailContent .= "Just a friendly reminder that you've not accepted any Missions or participated in your teams Wall. Your team mates need you! If you haven't already, please log in to On A Roll 21 and let them know how you roll...<br><br>";
                $nothingToSend = false;
            } else {
                if ($userObj->inactive_user_on_missions == 'true') {
                    // send out push notification
                    //$pushNotificationObject->sendPushNotification(SITE_URL."/#/journal", "Just a friendly reminder that you've not recently accepted any Missions. \nYour team mates need you! If you haven't already, please log in to On A Roll 21 and let them know how you roll...", "mobile", $userObj->id);
                    $mailContent .= "Just a friendly reminder that you've not recently accepted any Missions. Your team mates need you! If you haven't already, please log in to On A Roll 21 and let them know how you roll...<br><br>";
                    $nothingToSend = false;
                } elseif ($userObj->missions_completed == 'true') {
                    $mailContent .= "You are awesome! You've completed all missions and achieved your goal! <br/><br/>";
                    $nothingToSend = false;
                }
            }
            // check if the user is the first to complete all missions
            if ($dayPassed <= $totaltasks) {
                if ($userObj->first_to_complete_all_missions == 'true') {
                    $mailContent .= "And you are the first in your team to do so! You win a CHOCOLATE COOKIE :) <br/><br/>";
                    // sending email
                    $nothingToSend = false;
                } else {
                    // check if the record exists
                    if (count($results) > 0) {
                        $mailContent .= "Congratulations to " . $results[0]->username . " for being the first to reach their goal in team " . $userObj->groupname . "! <br/><br/>";
                        $nothingToSend = false;
                    }
                }
            }
            if (isset($mailContent) && !$noNotificationFlag) {
                // if newest notification is older than 1 day display no new notifications message
                $newestNotification = reset($notifications);
                $daysSinceNewest = daysDiff(new DateTime($newestNotification->created_at), new DateTime('now'));
                print 'daysSinceNewest: ' . $daysSinceNewest . '<br>';
                if ($daysSinceNewest > 1) {
                    $mailContent .= "You currently have no new notifications.<br/>";
                    $mailContent .= "Your passed notifications in Team " . $userObj->groupname . ":<br/><br/>";
                } else {
                    $mailContent .= "Your latest notifications in Team " . $userObj->groupname . ":<br/><br/>";
                }
                $nothingToSend = false;
            }
            if (isset($notifications)) {
                // add notification into the notification collection
                foreach ($notifications as $key => $value) {
                    $m = new \MomentPHP\MomentPHP($value->created_at);
                    $momentFromVo = $m->fromNow();
                    $userFirstname = DB::table('users')->where('id', $value->source_user_id)->pluck("firstname");
                    // message type
                    if ($value->type == "message") {
                        $mailContent .= $userFirstname . ' sent you a message: ' . $value->message . ' - ' . $momentFromVo . '  <br/>';
                    }
                    // new roller type
                    if ($value->type == "newgroupmember") {
                        $mailContent .= $userFirstname . ' has joined your team - ' . $momentFromVo . '  <br/>';
                    }
                    // new like
                    if ($value->type == "newlike") {
                        $mailContent .= $userFirstname . ' liked your post - ' . $momentFromVo . ' <br/>';
                    }
                    // new lcomment
                    if ($value->type == "newcomment") {
                        $mailContent .= $userFirstname . ' commented on your post - ' . $momentFromVo . ' <br/>';
                    }
                    // group first to complete
                    if ($value->type == "firstcomplete") {
                        $mailContent .= $value->message . ' - ' . $momentFromVo . '  <br/>';
                    }
                }
            }
            // if nothing to send skip to the next user
            if ($nothingToSend) {
                continue;
            }
            // build up the notification email object
            $notificationEmailObj['content'] = $mailContent;
            $notificationEmailObj['email'] = $userObj->email;
            echo $mailContent;
            echo "--------------------------------------</br>" . $userObj->email;
            if (isset($mailContent)) {
                // send out email notification
                $baseController = new BaseController();
                $baseController->notificationEmail($userObj->email, $notificationEmailObj, "Another Day Rolls By!..", "", "notification");
            }
        }
        return "OK - Task Done";
    }