<?php


class UserHelper extends Controller{


	


	


	


	public function check_user_login()


	{


		if(FALSE !== $this->loadSession()->hasSession("login") && FALSE !== $this->loadSession()->hasSession("user_id"))


		{


			return TRUE;


		}else{


			return FALSE;


		}


		


	}


    


	


    public function get_user_lang()


	{


		if(isset($_SESSION['login'],$_SESSION['job_type'],$_SESSION['user_id']) && $_SESSION['login'] == TRUE)


		{


			// get user id from session


			$user_id = $this->loadSession()->getSession("user_id");


			// get user data for header


			$dataView['userData'] = $this->loadModel("UserModel")->retriveDataById("id",$user_id);


			if($dataView['userData'] == NULL)


			{


				$this->redirect("error","index");


				exit();


			}


			


			$this->loadSession(array("lang"=>$dataView['userData']['user_language']))->setSession();


			return TRUE;


		}else if(isset($_SESSION['lang'])){


			$this->loadSession(array("lang"=>$_SESSION['lang']))->setSession();


			return TRUE;


		}


		$this->loadSession(array("lang"=>"ar"))->setSession();


		return TRUE;


	}


    public function get_user_stars_prec($user_id)


    {


        // set prec to 0


        $starsPres['star1Prec'] = 0;


        $starsPres['star2Prec'] = 0;


        $starsPres['star3Prec'] = 0;


        $starsPres['star4Prec'] = 0;


        $starsPres['star5Prec'] = 0;


        


        // get driver data from summery


        $summery = $this->loadModel("DriverSummerySheetModel")->retriveDataById("id_driver",$user_id);


        if(is_array($summery))


        {


            if($summery['total_users_rates'] > 0)


            {


                $starsPres['star1Prec'] = ($summery['star1_count']/$summery['total_users_rates']) * 100; 


                $starsPres['star2Prec'] = ($summery['star2_count']/$summery['total_users_rates']) * 100; 


                $starsPres['star3Prec'] = ($summery['star3_count']/$summery['total_users_rates']) * 100; 


                $starsPres['star4Prec'] = ($summery['star4_count']/$summery['total_users_rates']) * 100; 


                $starsPres['star5Prec'] = ($summery['star5_count']/$summery['total_users_rates']) * 100; 


            }


        }


        return $starsPres;


    }


    public function calculateDriverRating($driver_id)


    {


        $rating = 0;


        // get driver data from summery


        $summery = $this->loadModel("DriverSummerySheetModel")->retriveDataById("id_driver",$driver_id);


        if(is_array($summery))


        {


            if($summery['total_users_rates'] > 0)


            {


                $rating = round(($summery['star1_count'] + $summery['star2_count'] * 2 + $summery['star3_count'] * 3 + $summery['star4_count'] * 4 + $summery['star5_count'] * 5) / $summery['total_users_rates'],2);


            }


        }


        return $rating;


    }


    public function get_users_unreaded_messages_number()


    {


        $number = 0;


        if(isset($_SESSION['user_id']))


        {


            $user_id = $this->loadSession()->getSession("user_id");


            // get user data


            $dataView['userData'] = $this->loadModel("UserModel")->retriveDataById("id",$user_id);


            if($dataView['userData'] !== NULL)


            {


                if($dataView['userData']['job_type'] == 2)


                {


                    $count = $this->loadModel("MessagesModel")->retrieveDataByQuery("SELECT COUNT(id) AS unreaded FROM `messages` WHERE `id_driver`=? AND `passenger_replayed`=? AND `status` = ? AND `deleted`=?",array($user_id,1,1,1));


                    $number = $count[0]['unreaded'];


                }elseif($dataView['userData']['job_type'] == 3){


                    $count = $this->loadModel("MessagesModel")->retrieveDataByQuery("SELECT COUNT(id) AS unreaded FROM `messages` WHERE `id_passenger`=? AND `driver_replayed`=? AND `status` = ? AND `deleted`=?",array($user_id,1,1,1));


                    $number = $count[0]['unreaded'];


                }


            }


        }


        return $number;


    }


	


}


?>