<?php
class report_poll_person_stats extends c_report
{
 var $title='Результаты контроля знаний - статистика по сотруднику' ;
 var $personal_id=0 ;

 function get_cnt_items($usl,$options=array()){return(1) ;}

 function get_items($usl,$options=array()){return(array()) ;}

 function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;

    // получаем список всех ТЕМ вопросов, которые задавались сотруднику
    $theme_ids=execSQL('select t3.parent as theme_id,min(t1.c_data) as time_from,max(t1.c_data) as time_to,count(distinct(t1.question_id)),t4.obj_name as theme_name 
                        from poll_result t1 
                        left join poll_questions t2 on t2.pkey=t1.question_id 
                        left join poll_themes t3 on t3.pkey=t2.parent 
                        left join poll_themes t4 on t4.pkey=t3.parent 
                        where t1.personal_id='.$this->personal_id.' and not t3.parent is null  
                        group by t3.parent',0,1) ;

    $theme_stats=execSQL('select t3.parent as theme_id,t3.pkey as section_id,t1.correct,count(t1.pkey) as cnt,t3.obj_name as section_name 
                          from poll_result t1 
                          left join poll_questions t2 on t2.pkey=t1.question_id
                          left join poll_themes t3 on t3.pkey=t2.parent
                          where t1.personal_id='.$this->personal_id.' and not t3.pkey is null  
                          group by t3.parent,t2.parent,t1.correct ',0,1) ;

    $theme_stats_full=execSQL('select t3.parent as theme_id,t2.parent as section_id,count(t2.pkey) as cnt 
                               from poll_questions t2 
                               left join poll_themes t3 on t3.pkey=t2.parent 
                               group by t3.parent,t2.parent',0,1) ;

    $arr_result=array() ;

    if (_sizeof($theme_stats)) foreach($theme_stats as $rec)
    {  	    if ($rec['correct'])  { $arr_result[$rec['theme_id']]['cnt_questions_get_correct']+=$rec['cnt'] ;
                                    $arr_result[$rec['theme_id']]['section_stats'][$rec['section_id']]['cnt_questions_get_correct']+=$rec['cnt'] ;
                                  }
            $arr_result[$rec['theme_id']]['cnt_questions_get']+=$rec['cnt'] ;
            $arr_result[$rec['theme_id']]['section_stats'][$rec['section_id']]['cnt_questions_get']+=$rec['cnt'] ;
            $arr_result[$rec['theme_id']]['section_stats'][$rec['section_id']]['section_name']=$rec['section_name'] ;
    }
    if (_sizeof($theme_stats_full)) foreach($theme_stats_full as $rec)
    { if (isset($arr_result[$rec['theme_id']])) $arr_result[$rec['theme_id']]['cnt_questions']+=$rec['cnt'] ;
      if (isset($arr_result[$rec['theme_id']]['section_stats'][$rec['section_id']])) $arr_result[$rec['theme_id']]['section_stats'][$rec['section_id']]['cnt_questions']+=$rec['cnt'] ;
    }


    //damp_array($arr_result) ;

    // получаем value_out по первой оперии перед этой датой по всем счетам
    // или value_in по первой операции после этой даты по всем счетам
      //damp_array($this->filter) ;
    ?><table class="report_inv basic fz_small full auto"<?echo $id?>>
        <tr><th colspan="2">Тема/Раздел</th><th>Дата первого опроса</th><th>Дата последнего опроса</th><th>Число вопросов в теме/разделе</th><th>Число заданных вопросов</th><th>Процент правильных ответов</th><th>Процент покрытия темы/раздела</th></tr><?
        if (_sizeof($theme_ids)) foreach($theme_ids as $rec)
        {   $theme_id=$rec['theme_id'] ;
            $arr=TREE('poll_themes')->get_arr_parents_names($theme_id) ;
            $theme_full_name=implode(' / ',$arr) ;

            $proc1=($arr_result[$theme_id]['cnt_questions_get'])? $arr_result[$theme_id]['cnt_questions_get_correct']/$arr_result[$theme_id]['cnt_questions_get']*100:0 ;
            $proc1=($proc1>=1)? round($proc1):round($proc1,1) ;

            $proc2=($arr_result[$theme_id]['cnt_questions']*100)? $arr_result[$theme_id]['cnt_questions_get']/$arr_result[$theme_id]['cnt_questions']*100:0 ;
            $proc2=($proc2>=1)? round($proc2):round($proc2,2) ;

            ?><tr class=item>
                <td class="left" colspan="2"><?echo $theme_full_name?></td>
                <td class="center"><?echo date('d.m.Y H:i',$rec['time_from'])?></td>
                <td class="center"><?echo date('d.m.Y H:i',$rec['time_to'])?></td>
                <td class=""><?echo $arr_result[$theme_id]['cnt_questions']?></td>
                <td class=""><?echo $arr_result[$theme_id]['cnt_questions_get']?></td>
                <td class=""><?echo $proc1?>%</td>
                <td class=""><?echo $proc2?>%</td>
            </tr>
            <?
            if (_sizeof($arr_result[$theme_id]['section_stats'])) foreach($arr_result[$theme_id]['section_stats'] as $section_id=>$section_info)
                {   $proc1=$section_info['cnt_questions_get_correct']/$section_info['cnt_questions_get']*100 ;
                    $proc1=($proc1>=1)? round($proc1):round($proc1,1) ;

                    $proc2=$section_info['cnt_questions_get']/$section_info['cnt_questions']*100 ;
                    $proc2=($proc2>=1)? round($proc2):round($proc2,2) ;

                    ?><tr><td></td>
                        <td class="left"><?echo $section_info['section_name']?></td>
                        <td></td>
                        <td></td>
                        <td><?echo $section_info['cnt_questions']?></td>
                        <td><?echo $section_info['cnt_questions_get']?></td>
                        <td><?echo $proc1?>%</td>
                        <td><?echo $proc2?>%</td>

                    </tr>



                    <?

                }
            ?>
            <tr>





            </tr>
            <?
        }
      ?></table><?
   }

}

?>
