<?php
class report_lessions_trainingOld extends c_report
{
 var $title='Журнал обучения' ;
 var $view_by_space_filter=1 ;
 var $event_id=1 ;
 var $lession_id=0 ;
 var $rec_lession=array() ;
 var $rec_event=array() ;
 //var $allow_export_to_csv=1 ;

 function init($options=array())
 { if (!$GLOBALS['_SETTING']['modules']['event']) return ;
   if ($this->filter['lession_id']) $this->rec_lession=EVENTS()->get_lession_by_id($this->filter['lession_id'])  ;
   if ($this->rec_lession['event_id']) $this->rec_event=EVENTS()->get_event_by_id($this->rec_lession['event_id'])  ;
   else                                $this->rec_event=EVENTS()->get_event_by_id($this->event_id)  ;
   //damp_array($this) ;
 }
 function setting($options=array())
 {
     list($arr_orgs_ids,$arr_otdel_ids,$arr_workd_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter,['filter_reffer'=>$this->rec_event['_reffer']]) ;

      ?><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_input_text('name','ФИО:',3,array('auto_complete'=>"autocomplette_personal_name")) ;
                             $this->filter_section_select_multiple('org_ids','Организация:',3,$arr_orgs_ids) ;
                             $this->filter_section_select_multiple('otdel_ids','Подразделение:',3,$arr_otdel_ids) ;
                             $this->filter_section_select_multiple('working_ids','Должность:',3,$arr_workd_ids) ;
                          ?>
                        </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     //$usl[]='clss=226' ;
     $usl[]='event_id='.$this->event_id ;
     if ($this->filter['lession_id']) $usl[]='lession_id='.$this->filter['lession_id'] ;
     if ($this->filter['personal_id'])        $usl[]='personal_id='.$this->filter['personal_id'] ;
     if ($this->filter['data_from'])        $usl[]='c_data>='.strtotime($this->filter['data_from']) ;
     if ($this->filter['data_to'])          $usl[]='c_data<='.strtotime($this->filter['data_to']) ;

      if ($this->filter['name'])            $usl[]='obj_name like "%'.$this->filter['name'].'%"' ;
      if ($this->filter['org_ids'])         $usl[]='parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
      if ($this->filter['otdel_ids'])       $usl[]='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 (MEMBER()->personal_viewer_name) $usl['account_personal_filter']='pkey in (select pkey from '.MEMBER()->personal_viewer_name.')' ;


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



  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(distinct(pkey))
                        from  view_lession_personals
                        where '.$usl,$options) ;
    return($cnt) ;

  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    //$options['debug']=2 ;
    //$order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
    $order=($options['order'])? ' order by '.$options['order']:' order by pkey desc' ;
    // за счет индекса pkey получаем уникальные записи по сотрудникм
 	$list_rec=execSQL('select * from  view_lession_personals where '.$usl.$order.$limit,$options) ;
    //if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) EVENTS()->prepare_public_info_to_event($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
    $debug=($_GET['debug'])?  $_GET['debug']:0;
    $i=1 ;
    $rec_event=EVENTS()->get_event_by_id($this->event_id) ;
    $t=$rec_event['date_from'] ;    $arr_dates=array() ;
    while($t<=$rec_event['date_to'])
    { $tt=getdate($t) ;
      $day_from=mktime(0,0,0,$tt['mon'],$tt['mday'],$tt['year']) ;
      $day_to=mktime(23,59,59,$tt['mon'],$tt['mday'],$tt['year']) ;
      $arr_dates[date('d.m.Y',$day_from)]=array('from'=>$day_from,'to'=>$day_to) ;
      $t=$day_to+1 ;
    }
    //damp_array($arr_dates) ;


    //damp_array($rec_event) ;
    $arr_themes_by_date=array() ;
    $recs_lession=execSQL('select t1.pkey,t1.obj_name,t1.theme_id,t2.status,t2.time_from 
                           from obj_site_lession_themes t1
                           left join  obj_site_lession t2 on t2.pkey=t1.parent
                           where t1.event_id='.$this->event_id.' 
                           having t2.status=4',0) ;
    if (_sizeof($recs_lession)) foreach($recs_lession as $rec) $arr_themes_by_date[date('d.m.Y',$rec['time_from'])][$rec['theme_id']]++ ;
    {
      //$arr_themes
    }
    if ($debug) damp_array($arr_themes_by_date,1,-1) ;


    // собираем темы всех занятий, которые были успешно прочитаны сотрудникам
    // пока тут и темы неоконченных занятий
    $arr_lession_themes=array() ;
    $arr_themes=execSQL('select parent as lession_id,theme_id from obj_site_lession_themes where event_id='.$this->event_id,0,1) ;
    if (_sizeof($arr_themes)) foreach($arr_themes as $rec)  $arr_lession_themes[$rec['lession_id']][$rec['theme_id']]=1 ;
    if ($debug) damp_array($arr_lession_themes,1,-1) ;

    // статус прослушанного занятия - 4
    $arr_pers_day_themes=array() ;
    $recs_lession_by_pers=execSQL('select personal_id,c_data,status,parent as lession_id from obj_site_lession_persons t1 where event_id='.$this->event_id.' and personal_id in ('.implode(',',array_keys($list_recs)).') and status=4',0,1) ;

    // надо посчитать сколько тем прослушал сотрудник на основании информации по посещенным занятиям
    if (_sizeof($recs_lession_by_pers)) foreach($recs_lession_by_pers as $rec)
    {   if (_sizeof($arr_lession_themes[$rec['lession_id']])) foreach($arr_lession_themes[$rec['lession_id']] as $theme_id=>$t)
            $arr_pers_day_themes[$rec['personal_id']][date('d.m.Y',$rec['c_data'])][$theme_id]++ ;
    }
    if ($debug) damp_array($arr_pers_day_themes,1,-1) ;

    //$recs_personals=execSQL('select * from obj_site_events_pass where event_id='.$this->event_id,2) ;
    //damp_array($arr_lession_themes) ;
    ?><h2>Сведения об учащихся группы</h2>
      <table class="basic fz_small full auto"<?echo $id?>><tr><th>№</th><th>Фамилия, Имя, Отчество</th><th>Год рождения</th><th>Образование</th><th>Профессия</th><th>Место работы</th><th>Дополнительные сведения</th></tr>
      <? $i=1;if (_sizeof($list_recs)) foreach($list_recs as $rec_person)
      { ?><tr><td><?echo $i?></td>
              <td class="left"><?echo $rec_person['obj_name']?></td>
              <td><?echo $rec_person['dr']?></td>
              <td></td>
              <td class="left"><?echo _IL('IL_works')->recs[$rec_person['working']]['obj_name']?></td>
              <td class="left"><?echo $rec_person['_org_name']?></td>
              <td></td>
         </tr><? $i++ ;

      }?></table><?


    ?><br><h2>Наименование курса: "<?echo $rec_event['obj_name']?>"</h2>

      <table class="basic fz_small full auto"<?echo $id?>>
        <tr><th rowspan="2">№</th><th rowspan="2">ФИО</th><th colspan="<?echo _sizeof($arr_dates)?>">Дата занятий</th></tr>
        <tr><?if (_sizeof($arr_dates)) foreach($arr_dates as $date=>$arr_range) {?><th><?echo $date?></th><?}?></tr>
        <?$i=1 ; if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   ?><tr>
                  <td><?echo $i?></td>
                  <td class="left"><a href="/window/esmo_magnit_instruction/card/<?echo $this->event_id?>/personal/<?echo $rec['pkey']?>/" target="_blank"><?echo $rec['obj_name']?></a>
                  <?if (_sizeof($arr_dates)) foreach($arr_dates as $date=>$arr_range) {?><td>
                    <?  // в массиве $arr_pers_day_themes собраны темы, прослушанные сотрудником в течении дня
                        // если сотрудник в этот день прослушал все темы, что были прочитаны в этот день - ставим галочку
                        // $theme_ids - список тем, котоые были прочитаны в этот день  $date
                        $theme_ids=$arr_themes_by_date[$date]  ;
                        if (_sizeof($theme_ids) and _sizeof($arr_pers_day_themes[$rec['pkey']][$date])==_sizeof($theme_ids)) echo '<i class="fa fa-check"></i>' ;
                    ?></td><?}?>
              </tr><?
              $i++;
          }
      ?></table>

      <?
      $recs_lession=execSQL('select * from obj_site_events_themes where parent='.$this->event_id.' order by indx') ;
      ?>
      <br><h2>Преподаватель: ""</h2>
      <table class="basic fz_small full auto"<?echo $id?>><tr><th>Дата</th><th>№ темы</th><th>Тема занятий</th><th>Количество часов</th><th>Подпись</th></tr>
       <? $i=1;if (_sizeof($recs_lession)) foreach($recs_lession as $rec_lession)
       { ?><tr><td><?echo $i?></td>
               <td class="left"></td>
               <td class="left"><?echo $rec_lession['obj_name']?></td>
               <td><?echo $rec_lession['length']?></td>
               <td></td>
          </tr><? $i++ ;

       }?></table><?
      //damp_array($rec) ;
  }


}

?>
