<?php

/* * **************************
 * File : report.php
 * Author : Cygnusa
 * @2018
 * ************************** */

include "Connection.php";
$conn = new Connection();


if (isset($_REQUEST["funName"])) {
    $db = $conn->getConnection();
    $fun = $_REQUEST["funName"];
    switch ($fun) {
        case "user_log":
            user_log();
            break;
        case "user_month_report":
            user_month_report();
            break;
        case "assessmentreport":
            assessmentreport();
            break;
        case "assessmentuserreport":
            assessmentuserreport();
            break;
        
        case "userwiseassessmentreport":
            userwiseassessmentreport();
            break;
        case "userresultassessmentreport":
            userresultassessmentreport();
            break;
        case "trainingcontentreport":
            trainingcontentreport();
            break;
        case "usersuppliercontentreport":
            usersuppliercontentreport();
            break;
        case "usercontentfilereport":
            usercontentfilereport();
            break;
        
    }
}

// USER LOG
function user_log() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
       // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
			
            $start_date = (isset($_REQUEST['start_date'])) ? date('Y-m-d',strtotime(escape_str($_REQUEST['start_date']))) : '';
            $end_date = (isset($_REQUEST['end_date'])) ? date('Y-m-d',strtotime(escape_str($_REQUEST['end_date']))) : '';
            $type = (isset($_REQUEST['type'])) ? escape_str($_REQUEST['type']) : '';
            
            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);


            //FILTER
            //START DATE
            $filtercond .= ($start_date != '') ? " AND l.created_on>='" . escape_str($start_date) . " 00:00:00'" : '';
            //END DATE
            $filtercond .= ($end_date != '') ? " AND l.created_on<='" . escape_str($end_date) . " 23:59:59'" : '';

            //FILTER TYPE
            if ($type != '') {
                switch ($type) {
                    case "app":
                    case "web":
                    case "admin":
                        $filtercond .= " AND l.device_type='" . escape_str($type) . "'";
                        break;
                }
            }
                    
            if (!empty($pn) && !empty($nr)) {
                   
                // Pagination
                $limit_cond .= " limit " . (($pn - 1) * $nr) . ", " . $nr;
                
            }

            //GET LOG
            $user_log_str = "SELECT SQL_CALC_FOUND_ROWS  u.`userid`, u.`smpin`, u.`emp_code`, u.`fname`, u.`lname`, u.`emailid`, u.`mobileno`,u.`role`,u.`dealer_code`,l.module,l.ip,MAX(l.created_on)as created_on FROM `" . TABLE_USER_LOG . "` as l JOIN " . TABLE_USER . " as u ON u.userid=l.userid  WHERE  l.module='SIGNIN' " . $filtercond . " GROUP BY u.userid ORDER BY l.created_on desc" . $limit_cond;
            $data_arr = $conn->getQueryValue($user_log_str);
                    
            
            $total_records_arr = $conn->getQueryValue("select FOUND_ROWS() as total_records");
            $total_records = $total_records_arr[0]->total_records;
            if (!empty($pn) && !empty($nr)) {

                $arr['total_records'] = $total_records;
            }
            //LOG
            $conn->insertUserLog($conn, 'REPORT LOG');
            if (!count($data_arr) > 0) {
                $arr['total_records']='0';
                $arr['report'] = array();
            }else
            {
                if(count($data_arr)>0)
                {
                    foreach($data_arr as $mkey=>$user_arr)
                    {
                        $data_arr[$mkey]->dealer=array();
                        $dealer_str = "SELECT * FROM `" . DMS_DEALER . "` WHERE dealer_code='".$user_arr->dealer_code."' LIMIT 1 ";
                        $dealer_arr = $conn->getQueryValue($dealer_str);
                        if(count($dealer_arr)>0)
                        {
                            $data_arr[$mkey]->dealer=$dealer_arr;
                        }
                    }
                }
                
                $arr['report'] = $data_arr;
            }
            $conn->insertUserLog($conn, 'USER LOG REPORT');
            $conn->message(true,$conn->getMessage($lang,'report','success1'),$arr );
        } else {
            $conn->message(false,$conn->getMessage($lang,'report','error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false,$conn->getMessage($lang,'report','error4'));
    }
}

// USER MONTH REPORT
function user_month_report() {
    global $conn;
    try {
		
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
                    
        if (isset($_SESSION["userid"])) {
            
            $month = (isset($_REQUEST['month'])) ? escape_str($_REQUEST['month']) : '';
            $year = (isset($_REQUEST['year'])) ? escape_str($_REQUEST['year']) : '';
            $type = (isset($_REQUEST['type'])) ? escape_str($_REQUEST['type']) : '';

            //FILTER TYPE
            if ($type != '') {
                switch ($type) {
                    case "app":
                    case "web":
                    case "admin":
                        $filtercond .= " AND  device_type='" . escape_str($type) . "'";
                       break;
                }
            }
            $date_data_arr = $conn->getMonthDates($month, $year);
                    
            $label_arr = $date_data_arr['date'];
            $x = 0;
                    
            $user_log_str = " select DATE(created_on)as label_date ,count(DISTINCT userid) as user_count FROM " . TABLE_USER_LOG . " WHERE module='SIGNIN' AND created_on>='" . $date_data_arr['start'] . " 00:00:00' AND  created_on<='" . $date_data_arr['last'] . " 23:59:59' " . $filtercond . " GROUP BY DATE(created_on) ORDER BY created_on";
            $arr = $conn->getQueryValue($user_log_str);
                    
                    
            if (count($arr) != count($date_data_arr['month'])) {
                    
                foreach ($arr as $val) {
                    $date_val_arr[$val->label_date] = $val->user_count;
                }
               
                foreach ($date_data_arr['month'] as $res_month) {
                    $nobj = new stdClass();
                    $nobj->label_date = $res_month;
                    $res_month_arr=@explode('-',$res_month);
                    $nobj->label_date_str =  $res_month_arr[2];
                    $nobj->user_count = (isset($date_val_arr[$nobj->label_date])) ? $date_val_arr[$nobj->label_date] : '0';
                    $rearr[] = $nobj;
                }
            } else {
                $rearr = $arr;
            }

            //LOG
            $conn->insertUserLog($conn, 'USER LOG MONTH REPORT');
            //echo $conn->message(true, $pageData->successMsg[0], array('data' => $rearr, 'date' => $label_arr, 'month' => $date_data_arr['month']));
            $arr=array('data' => $rearr, 'date' => $label_arr, 'month' => $date_data_arr['month']);
            $conn->message(true,$conn->getMessage($lang,'report','success1'),$arr );
                    
        } else {
            $conn->message(false,$conn->getMessage($lang,'report','error1'));
        }
    } catch (PDOException $e) {
       $conn->message(false,$conn->getMessage($lang,'report','error4'));
    }
}


// ASSESSMENT REPORT 
function assessmentreport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

            //$_SESSION["userid"]=1;    
        if (isset($_SESSION["userid"])) {
            $data_arr = array();
            $start_date = (isset($_REQUEST['start_date'])) ? escape_str($_REQUEST['start_date']) : '';
            $end_date = (isset($_REQUEST['end_date'])) ? escape_str($_REQUEST['end_date']) : '';
                    

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            //print_r($_REQUEST);

            $arr = array();

            //CONDITIONS
            $lcond = '';
            $t_cond = '';
            $user_cond = ''; 
            $lcond .= ($start_date != '') ? " AND a.publisheddate>='" . $start_date . "'" : '';
            $lcond .= ($end_date != '') ? " AND a.publisheddate<='" . $end_date . "'" : '';
                    

            //DATE WITH COND
            if ($start_date == '' && $end_date == '') {
                $lcond = "AND a.publisheddate<='" . NOW . "'";
            }

            $total_records = 0;              
            $get_assess_str = "SELECT  SQL_CALC_FOUND_ROWS a.`assessmentid`, a.`assessmentname`,a.`description`,a.`assessmenttype`,a.`assettypeid`,a. `assessmenttimetype` ,a.`assessmenttime`,a.`randomcond`,a.`created_on`,a.publisheddate,a.enddate,a.assessmentnotification,a.attemptslimit,a.`filter_role`, a.`filter_zone`, a.`filter_region`, a.`filter_dealer`,a.status FROM ".TABLE_ASSESSMENT." a  WHERE a.assettypeid='1' AND a.status='1' ".$lcond." ORDER BY a.assessmentid DESC";
            if (!empty($pn) && !empty($nr)) {
                //  echo "calling here";
                // Pagination
                $get_assess_str .= " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }
                    
                    
            $get_assess_arr = $conn->getQueryValue($get_assess_str);

            //echo "select FOUND_ROWS() as total_records";
            $total_records_arr = $conn->getQueryValue("select FOUND_ROWS() as total_records");
            $total_records = $total_records_arr[0]->total_records;
            //print_r($total_records_arr);

            if (count($get_assess_arr)>0)
            {
                //GET ALL STATUS
                $status_str = "SELECT * FROM ".TABLE_STATUS;						
                $status_res_arr=$conn->getQueryValue($status_str);

                //print_r($status_res_arr);
                if(count($status_res_arr)>0)
                {
                    foreach($status_res_arr  as $res)
                    {                            
                        $obj_key=$res->statusid;
                        $status_arr[$obj_key]=$res->status;
                    }
                }


                foreach ($get_assess_arr as $akey=>$res_assess)
                {
                    $get_assess_arr[$akey]->publishedtime=date('H:i',strtotime($res_assess->publisheddate));
                    $get_assess_arr[$akey]->endtime=date('H:i',strtotime($res_assess->endtime));

                    $get_assess_arr[$akey]->publisheddate=date('Y-m-d',strtotime($res_assess->publisheddate));
                    $get_assess_arr[$akey]->enddate=date('Y-m-d',strtotime($res_assess->enddate));

                     //CHECK ALREADY ATTENDTED
                    $assessment_result_str = "SELECT * FROM ".TABLE_ASSESSMENTRESULT." WHERE assessmentid='".$res_assess->assessmentid."'  ORDER BY arid DESC";
                    $assessment_result_arr=$conn->getQueryValue($assessment_result_str);                        
                    if(count($assessment_result_arr)>0)
                    {
                        //  3-Completed , 4 - In Progress
                        $get_assess_arr[$akey]->status =($res_assess->status=='1')?'3':'1';
                        //$res_assess->status = $get_assess_arr[$key]->status;    
                    } else {
                        $get_assess_arr[$akey]->status ='7';
                        //$res_assess->status = $get_assess_arr[$key]->status;   
                    }

                    //EXPIRED
                    if(strtotime($res_assess->enddate)<strtotime(NOW))
                    {
                        $get_assess_arr[$akey]->is_expired='1';
                        $get_assess_arr[$akey]->status = '8';
                        //$res_assessment->status = '8'; 
                    }

                    //STATUS STRING
                    $get_assess_arr[$akey]->statusname=(isset($status_arr[$res_assess->status]))?$status_arr[$res_assess->status]:"";

                }
                $data_arr=$get_assess_arr;
            }
                    
            if (!empty($pn) && !empty($nr)) {

                $arr['total_records'] = $total_records;
            }
            $arr['report'] = $data_arr;

            $conn->message(true,$conn->getMessage($lang,'report','success1'),$arr );
        } else {
            $conn->message(false,$conn->getMessage($lang,'report','error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false,$conn->getMessage($lang,'report','error4'));
    }
}


// ASSESSMENT USER REPORT
function assessmentuserreport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

            //$_SESSION["userid"]=1;    
        if (isset($_SESSION["userid"])) {
            
            $assessmentid = escape_str($_REQUEST["assessmentid"]);
            
            $assessment_user_str = "SELECT au.*,u.fname,u.lname,u.dealer_code FROM ".TABLE_ASSESSMENT_USER." as au JOIN ".TABLE_USER." as u ON au.userid=u.userid LEFT JOIN ".DMS_DEALER." as d ON u.dealer_code=d.dealer_code WHERE au.assessmentid='".$res_assess->assessmentid."'  ORDER BY au.id DESC";
            $assessment_user_arr=$conn->getQueryValue($assessment_user_str); 
                    
            $conn->message(true,$conn->getMessage($lang,'report','success1'),$arr );
        } else {
            $conn->message(false,$conn->getMessage($lang,'report','error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false,$conn->getMessage($lang,'report','error4'));
    }
}

function getuserdata($s3, $userid) {
    global $conn;
    $query_getuser_str = " SELECT userid,`emp_code`, `password`, `fname`, `lname`, `emailid`, `mobileno`, `role`, `dealer_code`, `address_1`, `address_2`, `address_3`, `profile_photo`, `dob`, `country`, `state`, `city`,  `language`, `status`, `last_password_change`, `is_first_login` FROM " . TABLE_USER . " WHERE userid='" . $userid . "' LIMIT 1 ";
    $query_getuser_arr = $conn->getQueryValue($query_getuser_str);
    if (count($query_getuser_arr) > 0) {
        if ($query_getuser_arr[0]->profile_photo != '') {
            //S3 URL
            $obj = new stdClass();
            $obj->key_name = S3_PROFILE_IMAGE_URL . $query_getuser_arr[0]->profile_photo;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);
            $query_getuser_arr[0]->profile_photo = $url;
            
            
        } else {
            $query_getuserarr['result'][0]->profile_photo = USER_NOIMAGE_URL;
        }

        return $query_getuser_arr[0];
    } else {
        return array();
    }
}

//ASSESSMENT USER REPORT LOG
function userwiseassessmentreport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $start_date = (isset($_REQUEST['start_date'])) ? escape_str($_REQUEST['start_date']) : '';
            $end_date = (isset($_REQUEST['end_date'])) ? escape_str($_REQUEST['end_date']) : '';
            $trainingid = (isset($_REQUEST['trainingid'])) ? escape_str($_REQUEST['trainingid']) : '';
            $category = (isset($_REQUEST['category'])) ? escape_str($_REQUEST['category']) : '';
            $dealer_code = (isset($_REQUEST['dealer_code'])) ? escape_str($_REQUEST['dealer_code']) : '';
            $supplier = (isset($_REQUEST['supplier'])) ? escape_str($_REQUEST['supplier']) : '';
            $userid = (isset($_REQUEST['userid'])) ? escape_str($_REQUEST['userid']) : '';


            //CONDITIONS
            $lcond = '';
            $t_cond = '';
            $user_cond = '';
            $lcond .= ($trainingid != '') ? " AND a.trainingid='" . $trainingid . "'" : '';
            $lcond .= ($category != '') ? " AND a.cateid='" . $category . "'" : '';
            $lcond .= ($start_date != '') ? " AND au.publisheddate>='" . $start_date . "'" : '';
            $lcond .= ($end_date != '') ? " AND au.publisheddate<='" . $end_date . "'" : '';
            $lcond .= ($userid != '') ? " AND au.userid='" . $userid . "'" : '';

            //GET ASSESSMENT                       
            $assessment_str = "SELECT a.assessmentid,a.assessmentname FROM `" . TABLE_ASSESSMENT . "` as a JOIN " . TABLE_ASSESSMENT_USER . " as au ON a.assessmentid=au.assessmentid " . $t_cond . " WHERE a.assettypeid='1' " . $lcond . "  GROUP BY a.assessmentid ";

            $assessment_arr = $conn->getQueryValue($assessment_str);
            if (count($assessment_arr) > 0) {
                foreach ($assessment_arr as $res_assess) {
                    //GET ASSESSMENT HIGHSCORE
                    $get_score_str = "SELECT au.highscore,au.highscorearid,end_time FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN (SELECT assessmentid,MAX(enddatetime)as end_time FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid='" . $res_assess->assessmentid . "' AND userid='" . $userid . "' )as ar ON au.assessmentid=au.assessmentid WHERE au.assessmentid='" . $res_assess->assessmentid . "' AND au.userid='" . $userid . "' LIMIT 1";
                    $get_score_arr = $conn->getQueryValue($get_score_str);

                    $res_assess->highscore = (count($get_score_arr) > 0 && isset($get_score_arr[0]->highscore) && $get_score_arr[0]->highscore > 0) ? $get_score_arr[0]->highscore : '0';
                    $res_assess->end_time = (count($get_score_arr) > 0 && isset($get_score_arr[0]->end_time) && $get_score_arr[0]->end_time != '0000-00-00 00:00:00') ? $get_score_arr[0]->end_time : '';

                    $res_assess->tot_ques_count = '0';
                    $res_assess->ans_ques_count = '0';
                    
                    if (count($get_score_arr) > 0) {
                        //GET ASSESSMENT USER RESULT
                        //$get_ques_count_str = "SELECT * FROM (SELECT COUNT(DISTINCT aqid) as tot_ques FROM  " . TABLE_ASSESSMENT_USERRESULT . " WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' GROUP BY assessmentid )as a,(SELECT COUNT(DISTINCT aqid) as ans_ques FROM  " . TABLE_ASSESSMENT_USERRESULT . " WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' AND choiseid!=0 GROUP BY assessmentid )as b";
                        
                       //$get_ques_count_str = "SELECT * FROM (SELECT SUM(total_question) as tot_ques FROM  " . TABLE_ASSESSMENT_SUPPLIER . " WHERE assessmentid='" . $res_assess->assessmentid . "' )as a,(SELECT COUNT(DISTINCT aqid) as ans_ques FROM  " . TABLE_ASSESSMENT_USERRESULT . " WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' AND choiseid!=0 GROUP BY assessmentid )as b";
                        
                        $get_ques_count_str = "SELECT tot_ques,ans_ques FROM (SELECT SUM(total_question) as tot_ques,assessmentid FROM  ".TABLE_ASSESSMENT_SUPPLIER." WHERE assessmentid='".$res_assess->assessmentid ."' ) as a LEFT JOIN (SELECT COUNT(DISTINCT aqid) as ans_ques,assessmentid FROM  ".TABLE_ASSESSMENT_USERRESULT." WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' AND choiseid!=0 GROUP BY assessmentid)as b ON a.assessmentid=b.assessmentid";
                        
                        $get_ques_count_arr = $conn->getQueryValue($get_ques_count_str);
                       
                       
                       

                        if (count($get_ques_count_arr) > 0) {
                            $res_assess->tot_ques_count = $get_ques_count_arr[0]->tot_ques;
                            $res_assess->ans_ques_count = ($get_ques_count_arr[0]->ans_ques)?$get_ques_count_arr[0]->ans_ques:'0';
                        }
                    }
                    //GET ASSESSMENT SUPPLIER
                  $get_supplier_str = "SELECT s.suppliername,s.supplierid,asu.total_question as tot_ques_count FROM (SELECT * FROM  `" . TABLE_ASSESSMENT_SUPPLIER . "` WHERE assessmentid='" . $res_assess->assessmentid . "') as asu JOIN " . TABLE_SUPPLIER . " as s ON asu.supplierid=s.supplierid WHERE  asu.assessmentid='" . $res_assess->assessmentid . "' ";
                   
                    $get_supplier_arr = $conn->getQueryValue($get_supplier_str);
                    if (count($get_supplier_arr) > 0) {
                        foreach ($get_supplier_arr as $nkey => $res_supplier) {
                            $get_supplier_arr[$nkey]->tot_ques_count = $res_supplier->tot_ques_count;
                            $get_supplier_arr[$nkey]->ans_ques_count = '0';                     
                    
                            $get_supplier_arr[$nkey]->score=getAssessmentSupplierScore($userid,$res_assess->assessmentid,$res_supplier->supplierid,$get_score_arr[0]->highscorearid);

                            //$get_sques_count_str = "SELECT * FROM (SELECT COUNT(DISTINCT aqid) as tot_ques FROM  " . TABLE_ASSESSMENT_USERRESULT . " WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' AND aqid IN (SELECT aqid FROM " . TABLE_ASSESSMENT_QUESTION . " WHERE asid='" . $res_supplier->supplierid . "') GROUP BY assessmentid )as a,(SELECT COUNT(DISTINCT aqid) as ans_ques FROM  " . TABLE_ASSESSMENT_USERRESULT . " WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' AND choiseid!=0 AND aqid IN (SELECT aqid FROM " . TABLE_ASSESSMENT_QUESTION . " WHERE asid='" . $res_supplier->supplierid . "') GROUP BY assessmentid )as b";
                            $get_sques_count_str = " SELECT COUNT(DISTINCT aqid) as ans_ques FROM  " . TABLE_ASSESSMENT_USERRESULT . " WHERE arid='" . $get_score_arr[0]->highscorearid . "' AND userid='" . $userid . "' AND choiseid!=0 AND aqid IN (SELECT aqid FROM " . TABLE_ASSESSMENT_QUESTION . " WHERE asid='" . $res_supplier->supplierid . "') GROUP BY assessmentid ";
                            $get_sques_count_arr = $conn->getQueryValue($get_sques_count_str);
                            if (count($get_sques_count_arr) > 0) {
                               // $get_supplier_arr[$nkey]->tot_ques_count = $get_sques_count_arr[0]->tot_ques;
                                $get_supplier_arr[$nkey]->ans_ques_count = $get_sques_count_arr[0]->ans_ques;
                            }
                        }
                        $res_assess->supplier = $get_supplier_arr;
                    }

                    $data_arr[] = $res_assess;
                }
            }
            echo $conn->message(true, $pageData->successMsg[0], $data_arr);
            exit;
        } else {
            echo $conn->message(false, $pageData->errorMsg[2]);
            exit;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

//ASSESSMENT USER RESULT REPORT LOG
function userresultassessmentreport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {

            $supplier = (isset($_REQUEST['supplier'])) ? escape_str($_REQUEST['supplier']) : '';
            $userid = (isset($_REQUEST['userid'])) ? escape_str($_REQUEST['userid']) : '';
            $assessmentid = (isset($_REQUEST['assessmentid'])) ? escape_str($_REQUEST['assessmentid']) : '';

            //CONDITIONS
            $lcond = " AND a.assessmentid='" . $assessmentid . "' AND au.userid='" . $userid . "'";
            $t_cond = '';
            $user_cond = '';
            
            $data_arr=array();

            //GET ASSESSMENT                       
            $assessment_str = "SELECT a.assessmentid,a.assessmentname,au.highscorearid FROM `" . TABLE_ASSESSMENT . "` as a JOIN " . TABLE_ASSESSMENT_USER . " as au ON a.assessmentid=au.assessmentid " . $t_cond . " WHERE a.assettypeid='1' " . $lcond . " LIMIT 1";
            
            
            
            
            $assessment_arr = $conn->getQueryValue($assessment_str);
            if (count($assessment_arr) > 0) {
                if ($supplier != '') {
                    $sup_cond = " JOIN (SELECT * FROM " . TABLE_ASSESSMENT_QUESTION . " WHERE assessmentid='" . $assessmentid . "' AND asid='" . $supplier . "')as aq ON ur.assessmentid=aq.assessmentid ";
                }

                $get_question_str = "SELECT ur.* FROM " . TABLE_ASSESSMENT_USERRESULT . " as ur " . $sup_cond . " WHERE ur.userid='" . $userid . "' AND ur.arid='" . $assessment_arr[0]->highscorearid . "' GROUP BY ur.aqid ORDER BY ur.userresultid ASC";
                $get_question_arr = $conn->getQueryValue($get_question_str);
                if (count($get_question_arr) > 0) {
                    $arid = $assessment_arr[0]->highscorearid;
                    $total_ques_score = 0;
                    $total_ans_score = 0;
                    foreach ($get_question_arr as $res_value) {
                        //GET THE QUESTION
                        $ques_str = "SELECT q.id as qid,q.`supplierid`, q.`questiontypeid`, q.`questiontext`, q.`qimage`, q.`qvideo`,q.score  FROM " . TABLE_QUESTION . " as q WHERE id='" . $res_value->qid . "' LIMIT 1";
                        $ques_arr = $conn->getQueryValue($ques_str);

                        if (count($ques_arr) > 0) {
                            //GET ALL QUESTION OPTIONS
                            $ques_option_str = "SELECT op.optid,op.opttext,op.score,op.iscorrect,'0' as selected  FROM " . TABLE_QUESTIONOPTION . " as op WHERE op.qid='" . $res_value->qid . "'";
                            $ques_option_arr = $conn->getQueryValue($ques_option_str);

                            //GET USER ANSWERED OPTIONS
                            $user_answer_str = "SELECT * FROM " . TABLE_ASSESSMENT_USERRESULT . "  WHERE userid='" . $userid . "' AND arid='" . $arid . "' AND aqid='" . $res_value->aqid . "'";
                            $user_answer_arr = $conn->getQueryValue($user_answer_str);

                            $ques_arr[0]->is_correct = '0';
                            $ques_arr[0]->ans_score = '0';
                            $correct_ans_arr = array();
                            $choise_arr = array();
                            if (count($ques_option_arr) > 0) {
                                if (count($user_answer_arr) > 0) {
                                    $choise_arr = array_column($user_answer_arr, 'choiseid');
                                }
                                $ques_ans = array();
                                $ques_ans_score = 0;
                                //print_r( $user_answer_arr);
                                foreach ($ques_option_arr as $mkey => $res_opt) {
                                   //print_r($ques_option_arr);
                                    if (count($user_answer_arr) > 0) {
                                        $ques_option_arr[$mkey]->selected = (in_array($res_opt->optid, $choise_arr)) ? '1' : '0';

                                        //ANSWERED ANS
                                        if ($ques_option_arr[$mkey]->selected == '1') {
                                            $ques_ans[] = $res_opt;
                                        }
                                    }
                                    //correct ans
                                    if ($res_opt->iscorrect == '1') {
                                        $correct_ans_arr[] = $res_opt->optid;
                                        
                                    }
                                }
								$ques_ans_score += $ques_arr[0]->score;
								$total_ques_score += $ques_arr[0]->score;
								

                                if (count($correct_ans_arr) == count($choise_arr)) {
                                    $result = array_diff($correct_ans_arr, $choise_arr);
                                    $ques_arr[0]->is_correct = (count($result) == 0) ? '1' : '0';

                                    if ($ques_arr[0]->is_correct == '1') {
                                        $ques_arr[0]->ans_score = $ques_ans_score;
                                        $total_ans_score += $ques_ans_score;
                                    }
                                }
                                $ques_arr[0]->answer = $ques_ans;
                            }
                        }

                        $data_arr[] = $ques_arr[0];
                    }
                }
                $percentage_val = ($total_ques_score > 0) ? round(($total_ans_score / $total_ques_score) * 100) : 0;
            }
            echo $conn->message(true, $pageData->successMsg[0], array('ques' => $data_arr, 'percentage' => $percentage_val));
            exit;
        } else {
            echo $conn->message(false, $pageData->errorMsg[2]);
            exit;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

//TRAINING CONTENT REPORT
function trainingcontentreport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {
            $data_arr = array();

            $start_date = (isset($_REQUEST['start_date'])) ? escape_str($_REQUEST['start_date']) : '';
            $end_date = (isset($_REQUEST['end_date'])) ? escape_str($_REQUEST['end_date']) : '';
            $trainingid = (isset($_REQUEST['trainingid'])) ? escape_str($_REQUEST['trainingid']) : '';
            $category = (isset($_REQUEST['category'])) ? escape_str($_REQUEST['category']) : '';
            $dealer_code = (isset($_REQUEST['dealer_code'])) ? escape_str($_REQUEST['dealer_code']) : '';

            //$supplier = (isset($_REQUEST['supplier'])) ? escape_str($_REQUEST['supplier']) : '';

            //CONDITIONS
            $lcond = '';
            $t_cond = '';
            $user_cond = '';
            $lcond .= ($trainingid != '') ? " AND a.trainingid='" . $trainingid . "'" : '';
            $lcond .= ($category != '') ? " AND a.cateid='" . $category . "'" : '';
            $lcond .= ($start_date != '') ? " AND au.publisheddate>='" . $start_date . "'" : '';
            $lcond .= ($end_date != '') ? " AND au.publisheddate<='" . $end_date . "'" : '';
            
            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);


            $get_training_user = "SELECT SQL_CALC_FOUND_ROWS u.`userid`, u.`emp_code`, u.`fname`,  u.`lname`,  u.`emailid`,  u.`mobileno`,  u.`role`,  u.`dealer_code`,  u.`address_1`,  u.`address_2`,  u.`address_3`,  u.`profile_photo`,  u.`dob`,  u.`country`,  u.`state`,  u.`city`,  u.`language`,  u.`status`,  u.`is_deleted`  FROM (SELECT * FROM `" . TABLE_TRAINING_PARTICIPANT . "` WHERE trainingid='" . $trainingid . "') as p JOIN " . TABLE_USER . " as u ON p.empid=u.userid " . $t_cond . "  GROUP BY p.empid ";
            
            if (!empty($pn) && !empty($nr)) {
                    
                // Pagination
                $get_training_user .= " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }            
            $data_arr = $conn->getQueryValue($get_training_user);
            
            $total_records_arr = $conn->getQueryValue("select FOUND_ROWS() as total_records");
            $total_records = $total_records_arr[0]->total_records;

            if (count($data_arr) > 0) {
                foreach ($data_arr as $key => $value) {
                    
                    $dealer_type=userDealertype($value->userid);
                    switch(strtoupper($dealer_type))
                    {
                        case "P":
                        case "K":
                            $user_cond=" AND ( dealer_type='".$dealer_type."' OR  dealer_type='O')";  
                        break;
                        default:
                            $user_cond=" AND dealer_type  IN('O','P','K')";  
                        break;
                    }

                    $get_total_topic_content = "SELECT COUNT(tacontentid) as content_count FROM " . VIEW_TRAINING_CONTENT . " WHERE trainingid='" . $trainingid . "'".$user_cond;
                    $get_total_topic_content_arr = $conn->getQueryValue($get_total_topic_content);

                    $total_content = (count($get_total_topic_content_arr) > 0) ? $get_total_topic_content_arr[0]->content_count : 0;
                    
                    
                   

                    $get_user_complete_count_str = "SELECT COUNT(id) as complete_count  FROM " . TABLE_USER_COMPLETE_CONTENT . " WHERE userid='" . $value->userid . "' AND trainingid='" . $trainingid . "'  AND status='2'  GROUP BY status ";
                    $get_user_complete_content_arr = $conn->getQueryValue($get_user_complete_count_str);

                    $user_completed_content = (count($get_user_complete_content_arr) > 0) ? $get_user_complete_content_arr[0]->complete_count : 0;

                    $data_arr[$key]->total_content = $total_content;
                    $data_arr[$key]->completed_content = $user_completed_content;
                    $data_arr[$key]->percentage = ($total_content>0 && $user_completed_content>0)? round(($user_completed_content/$total_content)*100):'0';
                }
            }
            $arr['report'] = $data_arr;
            if (!empty($pn) && !empty($nr)) {
                $arr['total_records'] = $total_records;
            }
            echo $conn->message(true, $pageData->successMsg[0], $arr);
            exit;
        } else {
            echo $conn->message(false, $pageData->errorMsg[2]);
            exit;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

//TRAINING SUPPLIER CONTENT REPORT
function usersuppliercontentreport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {
            $data_arr = array();

            $start_date = (isset($_REQUEST['start_date'])) ? date('Y-m-d',strtotime(escape_str($_REQUEST['start_date']))) : '';
            $end_date = (isset($_REQUEST['end_date'])) ? date('Y-m-d',strtotime(escape_str($_REQUEST['end_date']))) : '';
            $trainingid = (isset($_REQUEST['trainingid'])) ? escape_str($_REQUEST['trainingid']) : '';
            $category = (isset($_REQUEST['category'])) ? escape_str($_REQUEST['category']) : '';
            $dealer_code = (isset($_REQUEST['dealer_code'])) ? escape_str($_REQUEST['dealer_code']) : '';
            $userid = (isset($_REQUEST['userid'])) ? escape_str($_REQUEST['userid']) : '';
            
//            $pn = escape_str($_REQUEST["pn"]);
//            $nr = escape_str($_REQUEST["nr"]);
//            $key = escape_str($_REQUEST["key"]);

            //CONDITIONS
//            $lcond='';
//            $t_cond='';
//            $user_cond='';
//            $lcond.=($trainingid!='')?" AND a.trainingid='".$trainingid."'":'';
//            $lcond.=($category!='')?" AND a.cateid='".$category."'":'';
//            $lcond.=($start_date!='')?" AND au.publisheddate>='".$start_date."'":'';
//            $lcond.=($end_date!='')?" AND au.publisheddate<='".$end_date."'":'';



            $get_training_user = "SELECT   u.`userid`, u.`emp_code`, u.`fname`,  u.`lname`,  u.`emailid`,  u.`mobileno`,  u.`role`,  u.`dealer_code`,  u.`address_1`,  u.`address_2`,  u.`address_3`,  u.`profile_photo`,  u.`dob`,  u.`country`,  u.`state`,  u.`city`,  u.`language`,  u.`status`,  u.`is_deleted`  FROM (SELECT * FROM `" . TABLE_TRAINING_PARTICIPANT . "` WHERE trainingid='" . $trainingid . "') as p JOIN " . TABLE_USER . " as u ON p.empid=u.userid WHERE u.userid='" . $userid . "'  GROUP BY p.empid LIMIT 1 ";
            $user_data_arr = $conn->getQueryValue($get_training_user);

            $data_arr = array();
            if (count($user_data_arr) > 0) {
                //GET SUPPLIER                    
                $get_allsupplier_str = "SELECT supplierid FROM " . VIEW_TRAINING_CONTENT . "  WHERE trainingid='" . $trainingid . "' GROUP BY supplierid";
                $get_allsupplier_arr = $conn->getQueryValue($get_allsupplier_str);

                if (count($get_allsupplier_arr) > 0) {
                    foreach ($get_allsupplier_arr as $res_supplier) {
                        $get_allsupplier_str = "SELECT suppliername FROM " . TABLE_SUPPLIER . "  WHERE supplierid='" . $res_supplier->supplierid . "' LIMIT 1";
                        $get_allsupplier_arr = $conn->getQueryValue($get_allsupplier_str);
                        if (count($get_allsupplier_arr) > 0) {
                            $res_supplier->suppliername = $get_allsupplier_arr[0]->suppliername;
                        }
                        
                        $dealer_type=userDealertype($userid);
                        switch(strtoupper($dealer_type))
                        {
                            case "P":
                            case "K":
                                $user_cond=" AND ( dealer_type='".$dealer_type."' OR  dealer_type='O')";  
                            break;
                            default:
                                $user_cond=" AND  dealer_type IN('O','P','K')";  
                            break;
                        }

                        $get_total_topic_content = "SELECT COUNT(tacontentid) as content_count FROM " . VIEW_TRAINING_CONTENT . " WHERE trainingid='" . $trainingid . "' AND supplierid='" . $res_supplier->supplierid . "' ". $user_cond;
                        $get_total_topic_content_arr = $conn->getQueryValue($get_total_topic_content);

                        $total_content = (count($get_total_topic_content_arr) > 0) ? $get_total_topic_content_arr[0]->content_count : 0;

                        $get_user_complete_count_str = "SELECT COUNT(id) as complete_count  FROM " . TABLE_USER_COMPLETE_CONTENT . " WHERE userid='" . $userid . "' AND trainingid='" . $trainingid . "' AND supplierid='" . $res_supplier->supplierid . "' AND status='2'  GROUP BY status ";
                        $get_user_complete_content_arr = $conn->getQueryValue($get_user_complete_count_str);

                        $user_completed_content = (count($get_user_complete_content_arr) > 0) ? $get_user_complete_content_arr[0]->complete_count : 0;

                        $res_supplier->total_content = $total_content;
                        $res_supplier->completed_content = $user_completed_content;
                        $res_supplier->percentage=($res_supplier->total_content>0 && $res_supplier->completed_content>0)? round(($res_supplier->completed_content/$res_supplier->total_content)*100):'0';
                        

                        $data_arr[] = $res_supplier;
                    }
                }
            }

            echo $conn->message(true, $pageData->successMsg[0], $data_arr);
            exit;
        } else {
            echo $conn->message(false, $pageData->errorMsg[2]);
            exit;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

//TRAINING CONTENT FILE REPORT
function usercontentfilereport() {
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {
            $data_arr = array();

            $start_date = (isset($_REQUEST['start_date'])) ? escape_str($_REQUEST['start_date']) : '';
            $end_date = (isset($_REQUEST['end_date'])) ? escape_str($_REQUEST['end_date']) : '';
            $trainingid = (isset($_REQUEST['trainingid'])) ? escape_str($_REQUEST['trainingid']) : '';
            $category = (isset($_REQUEST['category'])) ? escape_str($_REQUEST['category']) : '';
            $dealer_code = (isset($_REQUEST['dealer_code'])) ? escape_str($_REQUEST['dealer_code']) : '';
            $userid = (isset($_REQUEST['userid'])) ? escape_str($_REQUEST['userid']) : '';
            $supplier = (isset($_REQUEST['supplier'])) ? escape_str($_REQUEST['supplier']) : '';
            //CONDITIONS
//            $lcond='';
//            $t_cond='';
//            $user_cond='';
//            $lcond.=($trainingid!='')?" AND a.trainingid='".$trainingid."'":'';
//            $lcond.=($category!='')?" AND a.cateid='".$category."'":'';
//            $lcond.=($start_date!='')?" AND au.publisheddate>='".$start_date."'":'';
//            $lcond.=($end_date!='')?" AND au.publisheddate<='".$end_date."'":'';

            $get_allsupplier_arr = array();
            $get_training_user = "SELECT  u.`userid`, u.`emp_code`, u.`fname`,  u.`lname`,  u.`emailid`,  u.`mobileno`,  u.`role`,  u.`dealer_code`,  u.`address_1`,  u.`address_2`,  u.`address_3`,  u.`profile_photo`,  u.`dob`,  u.`country`,  u.`state`,  u.`city`,  u.`language`,  u.`status`,  u.`is_deleted`  FROM (SELECT * FROM `" . TABLE_TRAINING_PARTICIPANT . "` WHERE trainingid='" . $trainingid . "') as p JOIN " . TABLE_USER . " as u ON p.empid=u.userid WHERE u.userid='" . $userid . "'  GROUP BY p.empid LIMIT 1 ";
            $user_data_arr = $conn->getQueryValue($get_training_user);
            $supplier_name = '';
            $data_arr = array();
            if (count($user_data_arr) > 0) {
                $get_allsupplier_str = "SELECT suppliername FROM " . TABLE_SUPPLIER . "  WHERE supplierid='" . $supplier . "' LIMIT 1";
                $get_allsupplier_arr = $conn->getQueryValue($get_allsupplier_str);
                $supplier_name = (count($get_allsupplier_arr) > 0) ? $get_allsupplier_arr[0]->suppliername : '';
                
                
                $dealer_type=userDealertype($userid);
                switch(strtoupper($dealer_type))
                {
                    case "P":
                    case "K":
                        $user_cond=" AND ( dealer_type='".$dealer_type."' OR  dealer_type='O')";  
                    break;
                    default:
                        $user_cond=" AND dealer_type IN('O','P','K')";  
                    break;
                }

                //GET CONTENT                    
                $get_all_content_str = "SELECT   `contentid`,tacontentid, `assettopicid` FROM " . VIEW_TRAINING_CONTENT . "  WHERE trainingid='" . $trainingid . "' AND supplierid='" . $supplier . "'  ".$user_cond;
                $get_all_content_arr = $conn->getQueryValue($get_all_content_str);


                //GET CONTENT                    
                $get_filetype_str = "SELECT * FROM " . TABLE_FILETYPE . "  ";
                $get_filetype_arr = $conn->getQueryValue($get_filetype_str);
                $filetype_arr = array();
                if (count($get_filetype_arr) > 0) {
                    foreach ($get_filetype_arr as $res_file) {
                        $id = $res_file->id;
                        $filetype_arr[$id] = $res_file;
                    }
                }

                if (count($get_all_content_arr) > 0) {
                    foreach ($get_all_content_arr as $res_content) {
                        $get_topic_str = "SELECT  a.title,b.topicname,c.filetypeid FROM  (SELECT  contentid,assettopicid,title FROM " . TABLE_ASSET_CONTENT . "  WHERE contentid='" . $res_content->contentid . "') as a JOIN (SELECT  assettopicid,topicname FROM " . TABLE_ASSET_TOPIC . "  WHERE assettopicid='" . $res_content->assettopicid . "') as b ON a.assettopicid=b.assettopicid  LEFT JOIN (SELECT  * FROM " . TABLE_ASSET_FILE . " WHERE contentid='" . $res_content->contentid . "' ) as c ON c.contentid=a.contentid  LIMIT 1";

                        $get_topic_arr = $conn->getQueryValue($get_topic_str);
                        $res_content->title = '';
                        $res_content->topicname = '';
                        $res_content->filetypeid = '';
                        $res_content->filetype = '';
                        if (count($get_topic_arr) > 0) {
                            $filetypeid = $get_topic_arr[0]->filetypeid;
                            $get_topic_arr[0]->filetype = $filetype_arr[$filetypeid]->filetype;
                            // $res_content->content=$get_topic_arr[0];
                            //ASSSIGN KEY TO MAIN OBJ
                            if (count($get_topic_arr[0]) > 0) {
                                foreach ($get_topic_arr[0] as $ckey => $cvalue) {
                                    $res_content->$ckey = $cvalue;
                                }
                            }
                        }

                        $get_complete_status_str = "SELECT id,status  FROM " . TABLE_USER_COMPLETE_CONTENT . " WHERE userid='" . $userid . "' AND trainingid='" . $trainingid . "' AND supplierid='" . $supplier . "' AND status='2'  AND  tacontentid='" . $res_content->tacontentid . "'  LIMIT 1";
                        $get_complete_status_arr = $conn->getQueryValue($get_complete_status_str);
                        $res_content->completed_status = (count($get_user_complete_content_arr) > 0) ? '1' : '0';


                        $data_arr[] = $res_content;
                    }
                }
            }

            echo $conn->message(true, $pageData->successMsg[0], array('supplier' => $supplier_name, 'data' => $data_arr));
            exit;
        } else {
            echo $conn->message(false, $pageData->errorMsg[2]);
            exit;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}



function getAssessmentSupplierScore($userid,$assessmentid,$supplierid,$arid)
{
    global $conn;
    $percentage_score=0;
    if($arid>0)
    {
        
        //GET ASSESSMENT QUESTION TOTAL SCORE
        $assessmentscore_str = "SELECT SUM(op.score) as totalscore FROM (SELECT * FROM ".TABLE_ASSESSMENT_QUESTION." WHERE assessmentid='".$assessmentid."' AND asid='".$supplierid."' ) aq JOIN  (SELECT * FROM ".TABLE_ASSESSMENT_USERRESULT." WHERE assessmentid='".$assessmentid."' AND arid='".$arid."' )as ur ON ur.aqid=aq.aqid  JOIN (SELECT * FROM ".TABLE_QUESTIONOPTION."  )as op ON aq.qid=op.qid  WHERE aq.assessmentid='".$assessmentid."' AND op.iscorrect=1 ";       
        $assessmentscore_arr=$conn->getQueryValue($assessmentscore_str);
        $total_score='0';
        if(count($assessmentscore_arr)>0)
        {
            $total_score=($assessmentscore_arr[0]->totalscore)?$assessmentscore_arr[0]->totalscore:'0';
        } 
        
                    
        
        $userscore_str = "SELECT SUM(op.score) as totalscore FROM ".TABLE_QUESTIONOPTION." as op JOIN (SELECT * FROM ".TABLE_ASSESSMENT_USERRESULT." WHERE assessmentid='".$assessmentid."' AND arid='".$arid."' )as ar ON ar.choiseid=op.optid JOIN (SELECT * FROM ".TABLE_ASSESSMENT_QUESTION." WHERE assessmentid='".$assessmentid."' AND asid='".$supplierid."' )as aq ON ar.aqid=aq.aqid JOIN ".TABLE_QUESTION." as q ON ar.qid=q.id WHERE ar.assessmentid='".$assessmentid."'  AND q.questiontypeid='1' AND op.iscorrect=1   AND ar.arid='".$arid."'";
                    
        $userscore_arr=$conn->getQueryValue($userscore_str);	
        $user_score='0';
        if(count($userscore_arr)>0)
        {
            $user_score=($userscore_arr[0]->totalscore)?$userscore_arr[0]->totalscore:'0';
        }
                    
        
        //CHECK MULTI CHOICE
        $get_multi_choice_ques_str = "SELECT ar.* FROM  (SELECT * FROM ".TABLE_ASSESSMENT_USERRESULT." WHERE assessmentid='".$assessmentid."' AND arid='".$arid."' ) as ar  JOIN ".TABLE_QUESTION." as q ON ar.qid=q.id JOIN (SELECT * FROM ".TABLE_ASSESSMENT_QUESTION." WHERE assessmentid='".$assessmentid."' AND asid='".$supplierid."' )as aq ON ar.aqid=aq.aqid  WHERE ar.assessmentid='".$assessmentid."'  AND q.questiontypeid='2' AND ar.arid='".$arid."'  GROUP BY ar.aqid ";            
        $get_multi_choice_arr=$conn->getQueryValue($get_multi_choice_ques_str);
        if(count($userscore_arr)>0)
        {
            foreach($get_multi_choice_arr as $res_multi)
            {
                //GET ALL QUESTION OPTIONS
                $ques_option_str ="SELECT op.optid,op.opttext,op.score,op.iscorrect FROM ".TABLE_QUESTIONOPTION." as op WHERE op.qid='".$res_multi->qid."'";
                $ques_option_arr=$conn->getQueryValue($ques_option_str);  

                //GET USER ANSWERED OPTIONS
                $user_answer_str ="SELECT * FROM ".TABLE_ASSESSMENT_USERRESULT."  WHERE userid='".$userid."' AND arid='".$arid."' AND aqid='".$res_multi->aqid."'";
                $user_answer_arr=$conn->getQueryValue($user_answer_str);

                $correct_ans_arr=array();
                $choise_arr=array();
                if(count($ques_option_arr)>0)
                {
                    if(count($user_answer_arr)>0)
                    {
                        $choise_arr=array_column($user_answer_arr,'choiseid');
                    }
                    $ques_score=0;
                    foreach($ques_option_arr as $mkey=>$res_opt)
                    {   
                        //correct ans
                        if($res_opt->iscorrect=='1')
                        {
                            $correct_ans_arr[]=$res_opt->optid;
                            $ques_score+=$res_opt->score;
                        }

                    }

                }

                //MUTIPLE CHOISE CORRECT ANSWER CHECK
                if(count($correct_ans_arr)==count($choise_arr))
                {
                    $result=array_diff($correct_ans_arr,$choise_arr);
                    $is_correct=(count($result)==0)?'1':'0';
                    if($is_correct=='1'){ $user_score+=$ques_score;}
                }

            }
        }
                    
        $percentage_score=($user_score>0 && $total_score>0)?round(($user_score/$total_score)*100):'0';
    }
    
    return  $percentage_score;
}

function userDealertype($userid)
{
    global $conn;
    $get_dealer_type_qry="SELECT d.dealer_type FROM ".TABLE_USER." u JOIN ".TABLE_DEALER." d ON u.dealer_code=d.dealercode WHERE u.userid='".$userid."' LIMIT 1";  
    $get_dealer_type_arr=$conn->getQueryValue($get_dealer_type_qry);
    if(count($get_dealer_type_arr)>0)
    {
        $dealer_type=$get_dealer_type_arr[0]->dealer_type;
    }
    $dealer_type='';
    return $dealer_type;
}


?>