<?php
include_once(_DIR_EXT.'/personal/report_personals.php') ;
class report_quiz_section_personals extends report_personals
{
    var $title='Участники КЗ тема';
    var $view_by_space_filter=0;
    var $allow_org_ids=1;
    var $quiz_section_id=0;
    var $rec_section=0;

    function init($create_options=array())
    {   if($this->quiz_section_id)
        {
            $this->rec_section=QUIZ()->get_theme_by_id($this->quiz_section_id) ;
            $this->personals_filter_viewer=$this->rec_section['viewer_name'] ;
        }
        parent::init($create_options);
        $this->debug=0 ;

    }


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

        $sections_ids=QUIZ()->get_child_sections($this->quiz_section_id,1);
        $questions_stats=execSQL('select personal_id,correct,count(t1.pkey) as cnt
                              from quiz_result t1
                              left join '.$this->viewer_name.' t2 on t2.pkey=t1.personal_id
                              left join quiz_questions t3 on t3.pkey=t1.question_id
                              where not t2.pkey is null and t3.parent in ('.implode(',',$sections_ids).')
                              group by personal_id,correct
                              ',0,1);
        $stats2=array();
        if(_sizeof($questions_stats)) foreach($questions_stats as $rec_stat)
        {
            $stats2[$rec_stat['personal_id']]['all']+=$rec_stat['cnt'];
            if($rec_stat['correct']) $stats2[$rec_stat['personal_id']]['correct_1']=$rec_stat['cnt'];
            if(!$rec_stat['correct']) $stats2[$rec_stat['personal_id']]['correct_0']=$rec_stat['cnt'];
        }
        //damp_array($stats2);

        ?>
        <table class="list_personal_table_img basic fz_small full auto"<? echo $id ?>>
        <tr><th>ID</th>
            <th>Пропуск</th>
            <th>ФИО</th>
            <th>Дата рождения</th>
            <th>Возраст</th>
            <th>Организация</th>
            <th>Подразделение</th>
            <th>Должность</th>
            <th>Задано вопросов</th>
            <th>Правильные<br>ответы</th>
            <th>Неправильные<br>ответы</th>
            <th>Средний балл</th>
        </tr><?
        if(_sizeof($list_recs)) foreach($list_recs as $rec)
        {
            $stats=$stats2[$rec['pkey']];
            ?>
            <tr class=item>
            <td class="id"><? echo $rec['pkey'] ?></td>
            <td class="id"><? echo $rec['propusk'] ?></td>
            <td class="left">
                <a href="<? echo $rec['__href_window'] ?>" class="new_window" target="_blank" title="win_second"><? echo $rec['obj_name'] ?></a>
                <br><? echo '<span class="nowrap small">'.$rec['uid'].'</span>' ?>
                <? if(!$rec['enabled']) echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$rec['fired_data']).')' ?>
            </td>
            <td class="dr"><? echo $rec['dr'] ?></td>
            <td class="age"><? echo $rec['age'] ?></td>
            <td class="left"><? echo $rec['_org_name'] ?></td>
            <td class="left"><? echo $rec['_otdel_name'] ?></td>
            <td class="left"><? echo _IL('IL_works')->recs[$rec['working']]['obj_name'];
                if($rec['working2']) echo '<br><strong>Совмещение: </strong>'._IL('IL_works')->recs[$rec['working2']]['obj_name'];

                ?>
            </td>
            <td><? echo ($stats['all'])? $stats['all']:'-' ?></td>
            <td><? echo ($stats['correct_1'])? $stats['correct_1']:'-' ?></td>
            <td><? echo ($stats['correct_0'])? $stats['correct_0']:'-' ?></td>
            <td><? echo ($stats['all'])? round($stats['correct_1']*5/$stats['all'],1):'-' ?></td>
            </tr><?
        }
        ?></table><?
        //print_2x_arr($list_recs) ;
    }

    function print_template_HTML_small($list_recs,$options=array())
    {
        $id=($options['id'])? 'id='.$options['id']:'';
        $arr_personal_ids=array();
        if($options['cur_group'])
        {
            $rec_group=PERSONAL()->get_group_by_id($options['cur_group']);
            $personal_ids=get_linked_ids_to_rec($rec_group,array('clss'=>211));
            if($personal_ids) $arr_personal_ids=explode(',',$personal_ids);
        }

        if($this->quiz_section_id)
        {
            $arr_child_ids=QUIZ()->get_child_sections($this->quiz_section_id);
            print_r($arr_child_ids);
        }

        ?>
        <table class="list_personal_table_img basic fz_small full auto"<? echo $id ?>>
        <colgroup>
            <col id="c1">
            <col id="c2">
            <col id="c3">
            <col id="c4">
        </colgroup>
        <tr>
            <th>Выбор</th>
            <th>ФИО</th>
            <th>Организация, подразделение, должность</th>
            <th>Группа сотрудника</th>
        </tr><?
        if(_sizeof($list_recs)) foreach($list_recs as $rec)
        { ?>
            <tr class=item>
            <td><? if(!in_array($rec['pkey'],$arr_personal_ids)) {
                    ?>
                    <input type="<? echo ($options['check_type'])? $options['check_type']:'checkbox' ?>" name="_check[]" value="<? echo $rec['_reffer'] ?>"><? } else echo '<span class="red">Сотрудник уже в группе</span>' ?>
            </td>
            <td class="left">
                <a href="<? echo $rec['__href_window'] ?>" class="new_window" target="_blank" title="win_second"><? echo $rec['obj_name'] ?></a>
                <br><? echo $rec['dr'] ?>
                <br><? echo $rec['age'].' '.get_case_by_count_1_2_5($rec['age'],'год,года,лет') ?>
                <br><? echo '<span class="nowrap small">'.$rec['uid'].'</span>' ?>
                <? if(!$rec['enabled']) echo '<br><br><span class="red bold">УВОЛЕН</span> ('.date('d.m.Y H:i',$rec['fired_data']).')' ?>
            </td>
            <td class="left"><? echo $rec['_org_name'] ?><br><? echo $rec['_otdel_name'] ?>
                <br><? echo _IL('IL_works')->recs[$rec['working']]['obj_name'] ?></td>
            <td class="left"><? echo $rec['_groups_names'] ?></td>
            </tr><?
        }
        ?></table><?
        //damp_array($rec) ;
    }

    function print_template_CSV($list_recs,$options=array())
    {
        $i=$options['from'];
        if(_sizeof($list_recs)) foreach($list_recs as $rec)
        {
            $data=array();
            $data['№']=$i+1;
            $data['Пропуск']=$rec['propusk'];
            //$data['Табельный номер']=$rec['tabnumber']  ;
            $data['ФИО']=$rec['obj_name'];
            $data['UID']=$rec['uid'];
            $data['Дата рождения']=$rec['dr'];
            $data['Возраст']=$rec['age'];
            $data['Организация']=$rec['_org_name'];
            $data['Подразделение']=$rec['_otdel_name'];
            $data['Должность']=$rec['_work_name'];
            $data['Группа']=$rec['_groups_names'];
            // вносим данные в csv
            $this->put_data_to_CSV($data,$i);
            $i++;
        }
    }
}

?>
