<?php
/**
 * Created by PhpStorm.
 * User: Jure
 * Date: 8.5.2014
 * Time: 8:46
 */


include_once("_dirinfo.php");

include_once(PATH_TO_ROOT . "init.php");
include_once(PATH_TO_ROOT . "config.php");
include_once("../../../../_ajaxManipulators/_class/class.gridData.php");
include_once("../../../class/NfieldDownloadParser.php");

class StatisticInterviewers extends GridDataManipulator
{

    function selectAction()
    {
        $projectName = mysql_real_escape_string($_POST['project_text']);
        $nfieldProjectDataCheck = $projectName.'_data_check';
        $nfieldProjectDataRaw = $projectName.'_data_raw';

        $var = file_get_contents("http://sorspbx.rmplus.si/warpitDevelopment/nfield/Helpers/interviewersData.php?nfield=1");

        $interviewersArray = json_decode(trim($var),1);

        //Control interviewer
        $sql = "SELECT count(*) as n, data_value, IF(check_status > 0,1,2) as check_status
			FROM ".$nfieldProjectDataCheck." as a
		LEFT JOIN ".$nfieldProjectDataRaw." as b ON a.`id_structure` =  b.`id_structure` AND b.`data_type` = 2
			GROUP BY data_value,IF(check_status > 0,1,2)";


        $res = $this->db->SQLexecute($sql);
        while ($zad = $this->db->fetchAssoc($res)) {
            $stat[$zad['data_value']][$zad['check_status']] = $zad['n'];
        }
        foreach ($stat as $ank=>$pod) {

            foreach($interviewersArray['rows'] as $key){

                if($ank == $key['ClientInterviewerId']){
                    $row['AnketarImePriimek'] = $key['FirstName'].' '.$key['LastName'];
                }
            }
            $row['control'] = $pod['1'];
            $row['allSurvey'] = ($pod['2'] + $pod['1']);
            $row['procent'] = round($pod['1'] / ($pod['2'] + $pod['1']) * 100,2);

            $this->arr[] = $row;
        }
        //Summary
        $row['AnketarImePriimek'] = '<b>Skupno</b>';
        $row['control'] = '<b>'.$this->getSum($this->arr,'control').'</b>';
        $row['allSurvey'] = '<b>'.$this->getSum($this->arr,'allSurvey').'</b>';
        $row['procent'] = '<b>'.$this->getProcentSum(strip_tags ($row['control']),strip_tags ($row['allSurvey'])).'</b>';
        $this->arr[] = $row;

        $allData = array(
            'success' => 'true',
            'results' => sizeof($this->arr),
            'rows' => $this->arr
        );

        return json_encode($allData);
        //return parent::selectAction();
    }
    private function getProcentSum($sumControl,$sumSurvey){

        $result = ($sumControl * 100) / $sumSurvey;
        return round($result,2);
    }

    private function getSum($array,$title){

        $counter = 0;
        foreach($array as $a){
            foreach($a as $key=>$value){
                if($key == $title){$counter+= $value;}
            }
        }
        return $counter;
    }
    function exportAction()
    {
        if ($_REQUEST['group_id'] != 0)
            $this->addWhere("id_group = " . $_POST['group_id']);
        if (isset($_POST['template_id'])) {
            //we call the function which prepares the $_POST paramaters to create the appropriate SQL
            $this->createTemplateSQL();
        }
        return parent::exportAction();
    }
}

$db->ChangeDatabase($_REQUEST['database']);


$manipulator = new StatisticInterviewers($db, $_REQUEST['RMtable']);

$comm = $_REQUEST['action'] . 'Action';
if (method_exists($manipulator, $comm))
    echo $manipulator->$comm();
else {
    Echo '{success:false,error:"Unknown action!"}';
}


?>
