<?php
class report_poll_results extends c_report
{
 var $title='Ход проверки контроля знаний' ;
 var $view_by_space_filter=1 ;
 var $event_id=0 ;
 var $lession_id=0 ;
 var $rec_lession=array() ;
 var $rec_event=array() ;
 var $event_ext=array() ;
 var $allow_export_to_csv=1 ;
 var $report_script=_DIR_EXT.'/events_poll/report_poll_results.php' ;

 function init($options=array())
 { if (!function_exists('EVENTS')) return ;
   //if ($this->filter['event_id']) $this->event_id=$this->filter['event_id'];
   if (!$this->event_id) $this->event_id=execSQL_value('select pkey from obj_site_events_schedule where status!=5 and enabled=1 and ext="events_poll/i_event_poll" order by pkey limit 1') ;
   if ($this->event_id) $this->rec_event=EVENTS()->get_event_by_id($this->event_id)  ;
 }

 function setting($options=array())
 {    $arr_orgs_ids=PERSONAL()->get_arr_orgs_to_cur_account('',['viewer_name'=>$this->rec_event['viewer_name']]) ;
      $arr_otdel_ids=(_sizeof($this->filter['dep_ids']))? PERSONAL()->get_arr_otdels_to_cur_account($this->filter['dep_ids'],$this->rec_event['_ext_info']['usl_select']):array() ;
      $arr_workd_ids=PERSONAL()->get_arr_works_to_cur_account($this->rec_event['_ext_info']['usl_select'],$this->filter['dep_ids'],$this->filter['otdel_ids']) ;
      //$arr_events=execSQL('select pkey,obj_name,status from obj_site_events_schedule where clss=222 and status!=5 and ext="events_poll/i_event_poll" and obj_name!="" order by obj_name') ;
      if ($this->event_id) $arr_themes=execSQL('select pkey,obj_name from obj_site_events_themes where parent='.$this->event_id.' and obj_name!="" order by obj_name') ;
      $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
      //echo _CUR_REPORT_DIR ;
      ?><div id="panel_filter">
             <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                  <fieldset>
                      <div class="row">
                          <? //$this->filter_section_select('event_id','Cобрание:',2,$arr_events) ;
                             //$this->filter_section_select_multiple('theme_ids','Темы собрания:',2,$arr_themes) ;
                             $this->filter_section_input_text('name','ФИО:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                             ///$this->filter_section_select('lession_group_id','Место проведения собрания:',2,$arr_groups) ;
                             $this->filter_section_input_text('data_from','Дата, от:',2,array('class'=>'datetimepicker')) ;
                             $this->filter_section_input_text('data_to','Дата, до:',2,array('class'=>'datetimepicker')) ;
                          ?>
                        </div>
                      <div class="row">
                          <?$this->filter_section_select_multiple('dep_ids','Организация:',2,$arr_orgs_ids) ;
                            $this->filter_section_select_multiple('otdel_ids','Подразделение:',2,$arr_otdel_ids) ;
                            $this->filter_section_select_multiple('working_ids','Должность:',2,$arr_workd_ids) ;
                            //$this->filter_section_select('personal_mode','Отбор сотрудников:',2,$this->personal_mode) ;
                          ?>
                      </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     $usl[]='t5.event_id='.$this->event_id ;
      //damp_array($this) ;
     //if ($this->event_id and $this->rec_event['_ext_info']['usl_select']) $usl[]=$this->rec_event['_ext_info']['usl_select'] ;

     if (MEMBER()->personal_viewer_name) $usl['account_personal_filter']='pkey in (select pkey from '.MEMBER()->personal_viewer_name.')' ;

     if ($this->filter['personal_id'])        $usl[]='t2.pkey='.$this->filter['personal_id'] ;
     if ($this->filter['name'])            $usl[]='t2.obj_name like "%'.$this->filter['name'].'%"' ;
     if ($this->filter['dep_ids'])         $usl[]='t2.parent2 in ('.implode(',',array_keys($this->filter['dep_ids'])).')' ;
     if ($this->filter['otdel_ids'])       $usl[]='t2.parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;
     if (_sizeof($this->filter['working_ids'])) $usl[]='working in ('.implode(',',array_keys($this->filter['working_ids'])).')' ;
      if ($this->filter['data_from'])        $usl[]='t1.c_data>='.strtotime($this->filter['data_from']) ;
      if ($this->filter['data_to'])          $usl[]='t1.c_data<='.strtotime($this->filter['data_to']) ;



     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     //damp_array($_usl_select_events,1,-1) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $title='' ;
   $info=$this->get_arr_info_filters() ;
   if ($this->filter['lession_group_id'])   $info['Место проведения инструктажа']=ACCOUNTS()->get_member_name_by_id($this->filter['lession_group_id'])  ;
   if ($this->filter['personal_mode'])   $info['Отбор сотрудников']=$this->personal_mode[$this->filter['personal_mode']];
   if ($this->filter['theme_ids'])
   { $arr_names=execSQL_line('select obj_name from obj_site_events_themes where pkey  in ('.implode(', ',array_keys($this->filter['theme_ids'])).')') ;
     $info['Темы занятий']=implode(', ',$arr_names) ;
   }

   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<div class=center>'.implode(' ',$_str).'</div>' ;
   return $title ;
   }



  function get_cnt_items($usl,$options=array())
  { $options['debug']=0 ;
    $cnt=execSQL_value('select count(t1.pkey)
                        from  poll_result t1
                        left join view_personals t2 on t2.pkey=t1.personal_id 
                        left join poll_questions t3 on t3.pkey=t1.question_id 
                        left join poll_themes t4 on t4.pkey=t1.theme_id 
                        left join obj_site_events_pass t5 on t5.pkey=t1.poll_id
                        where '.$usl.'
                        ',$options) ;
    return($cnt) ;

  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    //$options['debug']=1 ;
    $options['no_indx']=1 ;
    $order=($options['order'])? ' order by '.$options['order']:' order by t1.pkey desc' ;

 	$list_recs=execSQL('select t1.pkey as poll_pkey,t1.c_data as poll_data,t1.theme_id,t1.question_id,t1.correct,t1.poll_id,t3.obj_name as question_text,t4.obj_name as theme_text,t6.pkey as rasdel_id,t6.obj_name as rasdel_text,t5.event_id,t2.* 
                        from  poll_result t1    
                        left join view_personals t2 on t2.pkey=t1.personal_id 
                        left join poll_questions t3 on t3.pkey=t1.question_id 
                        left join poll_themes t6 on t6.pkey=t3.parent 
                        left join poll_themes t4 on t4.pkey=t1.theme_id 
                        left join obj_site_events_pass t5 on t5.pkey=t1.poll_id 
                        where '.$usl.$order.$limit,$options) ;
    //print_2x_arr($list_recs) ;
    return($list_recs) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { //print_2x_arr($list_recs) ;
    $id=($options['id'])? 'id='.$options['id']:'' ;
    $i=1 ;
    include(_DIR_EXT.'/personal/panel_personal_info.php') ;

    ?><table class="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></tr><?
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   ?><tr>
                  <td><?echo $rec['poll_pkey']?></td>
                  <td><?echo date('d.m.Y H:i',$rec['poll_data'])?></td>
                  <td class="left"><? echo $rec['theme_text']?></td>
                  <td class="left"><? echo $rec['rasdel_text']?></td>
                  <td class="left"><? echo $rec['question_text']?></td>
                  <td class="center"><?echo ($rec['correct'])? 'Да':'Нет'?></td>
                  <td class="left"><?panel_personal_info_full($rec)?></td>
                  <td class="left"><?echo '<strong>'.$rec['_org_name'].'</strong><br><div class="small">'.$rec['_otdel_name'].'</div>'?></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>
              </tr><?
              $i++;
          }
      ?></table><?
  }

    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['Дата проверки']=date('d.m.Y',$rec['poll_data'])  ;
                $data['Время проверки']=date('H:i',$rec['poll_data'])  ;
                $data['ID темы']=$rec['theme_id']  ;
                $data['Тема']=$rec['theme_text']  ;
                $data['ID раздела']=$rec['rasdel_id']  ;
                $data['Раздел']=$rec['rasdel_text']  ;
                $data['ID вопроса']=$rec['question_id']  ;
                $data['Вопрос']=$rec['question_text']  ;
                $data['Правильный ответ']=($rec['correct'])? 'Да':'Нет'  ;
                $data['GUID сотрудника']=$rec['uid']  ;
        	    $data['ФИО сотрудника']=$rec['obj_name']  ;
                $data['Дата рождения']=$rec['dr']  ;
                $data['Возраст']=$rec['age']  ;
                $data['Пол']=($rec['pol'])? 'Женский':'Мужской'  ;
        	    $data['Организация']=$rec['_org_name'] ;
        	    $data['Подразделение']=$rec['_otdel_name'] ;
        	    $data['Должность']= _IL('IL_works')->recs[$rec['working']]['obj_name'] ;
                // вносим данные в csv
                $this->put_data_to_CSV($data,$i) ;
        	    $i++ ;
    	}

         }


}

?>
