<?php
class report_meeting_lessions extends c_report
{
 var $title='Отчет по собраниям' ;
 var $view_by_space_filter=1 ;
 var $event_id=0 ;
 var $group_id=0 ;
 //var $doc_types_names=array() ;
 //var $allow_export_to_csv=1 ;

 var $allow_export_to_csv=1 ;
 var $report_script=_DIR_EXT.'/events_meeting/report_meeting_lessions.php' ;
 var $arr_sign=array(1=>'Ведомость отсутствует',2=>'Ведомость создана, неподписана',3=>'Ведомость подписана') ;

 function init($create_options=array())
 {
    if (!$this->event_id) $this->event_id=execSQL_value('select pkey from obj_site_events_schedule where status=1 and enabled=1 and ext="events_meeting/i_event_meeting" order by pkey limit 1') ;
 }


 function setting($options=array())
 {
     $arr_groups=execSQL_row('select distinct(group_id),group_name from view_lessions where event_id='.$this->event_id.' order by group_name') ;
     $arr_accounts=execSQL_row('select distinct(account_id),account_name from view_lessions where event_id='.$this->event_id.' order by account_name') ;

      ?><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">
                          <? $obj_event=EVENTS()->get_event_obj_by_id($this->event_id);
                             $this->filter_section_select('lession_group_id','Место проведения занятия:',2,$arr_groups) ;
                             $this->filter_section_select_multiple('account_ids','Лектор:',2,$arr_accounts) ;
                             $this->filter_section_select('status','Статус:',2,$obj_event->arr_status_lession) ;
                             $this->filter_section_input_text('data_from','Дата занятия, от:',2,array('class'=>'datepicker')) ;
                             $this->filter_section_input_text('data_to','Дата занятия, до:',2,array('class'=>'datepicker')) ;
                             $this->filter_section_select('report1_sign','Ведомость прохождения:',2,$this->arr_sign) ;
                             //$this->filter_section_select('report2_sign','Журнал регистрации:',2,$arr_sign) ;
                          ?>
                        </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function panel_report_buttons()
  { if (MEMBER()->rol==1)
    { ?><button use_form="form_report" class="button v2" cmd="events_meeting/delete_check_lession">Удалить выбранные собрания</button>&nbsp;&nbsp;<?}
  }


function panel_info_filter()
{ $info=$this->get_arr_info_filters() ;
  $obj_event=EVENTS()->get_event_obj_by_id($this->event_id);
  $info['Место проведения занятия']=ACCOUNTS()->get_group_name(MEMBER()->cur_group_id) ; // выводим группы от местоположения текущего аккаунта
  if ($this->filter['lession_group_id'])  $info['Место проведения занятия']=ACCOUNTS()->get_group_name($this->filter['lession_group_id']) ; // дополнительная филтрация по группам
  if ($this->filter['account_id'])   $info['Лектор']='"'.ACCOUNTS()->get_member_name_by_id($this->filter['account_id']).'"' ;
  if ($this->filter['account_ids'])  $info['Лектор']='"'.ACCOUNTS()->get_members_names_by_ids($this->filter['account_ids'],array('ids_in_keys'=>1)).'"' ;
  if ($this->filter['status'])   $info['Статус']='"'.$obj_event->arr_status_lession[$this->filter['status']] ;
  if ($this->filter['report1_sign']) $info['Ведомость прохождения']=$this->arr_sign[$this->filter['report1_sign']] ;
  if ($this->filter['report2_sign']) $info['Журнал регистрации']=$this->arr_sign[$this->filter['report2_sign']] ;
  if ($this->filter['data_to'])   $info['дата до']='"'.$this->filter['data_to'].'"' ;
  if ($this->filter['data_to'])   $info['дата до']='"'.$this->filter['data_to'].'"' ;
  if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
  if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
  return $title ;
}

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     $usl[]='clss=226' ;
     $usl[]='event_id='.$this->event_id ;
     $usl[]='group_id in ('.ACCOUNTS()->tree[MEMBER()->cur_group_id]->get_list_child().')' ;
     if ($this->filter['lession_group_id']) $usl[]='group_id in ('.ACCOUNTS()->tree[$this->filter['lession_group_id']]->get_list_child().')' ;
     if ($this->filter['personal_id'])      $usl[]='personal_id='.$this->filter['personal_id'] ;
     if ($this->filter['account_id'])       $usl[]='account_id='.$this->filter['account_id'] ;
     if (isset($this->filter['account_ids']) and _sizeof($this->filter['account_ids']))       $usl[]='account_id in ('.implode(',',array_keys($this->filter['account_ids'])).')' ;
     if ($this->filter['status'])       $usl[]='status='.$this->filter['status'] ;
     if ($this->filter['report1_sign']==1)  $usl[]='(doc1_id=0 or doc1_id=0 is null)';
     if ($this->filter['report1_sign']==2)  $usl[]='doc1_id>0 and doc1_sign_data=0';
     if ($this->filter['report1_sign']==3)  $usl[]='doc1_id>0 and doc1_sign_data>0';
     if ($this->filter['report2_sign']==1)  $usl[]='(doc2_id=0 or doc2_id=0 is null)';
     if ($this->filter['report2_sign']==2)  $usl[]='doc2_id>0 and doc2_sign_data=0';
     if ($this->filter['report2_sign']==3)  $usl[]='doc2_id>0 and doc2_sign_data>0';
     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']) ;
     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl) ;
     return($usl_res) ;
  }

  function get_cnt_items($usl,$options=array())
  { $cnt=execSQL_value('select count(pkey)
                        from  view_lessions
                        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' ;
 	$list_rec=execSQL('select * from  view_lessions where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) EVENTS()->prepare_public_info_to_lession($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
    $personal_stats_all=array() ;  $personal_stats=array() ;
    if (_sizeof($list_recs))
    { $personal_stats_all=execSQL_row('select parent,count(pkey) as cnt from obj_site_lession_persons where parent in ('.implode(',',array_keys($list_recs)).') and clss=227 group by parent',0,1) ;
      $personal_stats=execSQL('select parent,status,count(pkey) as cnt from obj_site_lession_persons where parent in ('.implode(',',array_keys($list_recs)).') and clss=227 group by parent,status',0,1) ;
      $personal_stats=group_by_field('parent',$personal_stats,'status') ;
    }
      //damp_array($list_recs) ;
    //damp_array($personal_stats) ;
    $i=1 ;

    $obj_event=EVENTS()->get_event_obj_by_id($this->event_id);
    ?><form id="form_report"><table class="basic fz_small full auto"<?echo $id?>>
        <tr><?if (MEMBER()->rol==1){?><th>Выбор</th><?}?><th>№</th><th>ID</th><th>Дата/время<br>создания</th><th>Место занятия</th><th>Лектор</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)
          {   $rec['__href']='/window/events_meeting/lession/'.$rec['pkey'].'/' ;
              ?><tr>
                  <?if (MEMBER()->rol==1){?><td><?if ($rec['status']==6){?><input type="checkbox" name="_check[<?echo $rec['pkey']?>]" value="1"></td><?}}?>
                  <td><?echo $i ; $i++ ;?></td>
                  <td><a href="<?echo $rec['__href']?>" class="_new_window" title="window_event"><?echo $rec['pkey']?></a></td>
                  <td><?echo date('d.m.Y H:i',$rec['c_data'])?><??></td>
                  <td><?echo $rec['group_name']?><??></td>
                  <td><?echo $rec['account_name']?><??></td>
                  <td class="left"><a href="<?echo $rec['__href']?>" class="_new_window" title="window_event"><?echo $rec['obj_name']?></a></td>
                  <td><?if ($rec['time_from']) echo date('d.m.Y H:i',$rec['time_from']) ;?></td>
                  <td><?if ($rec['time_to']) echo date('d.m.Y H:i',$rec['time_to']) ;?></td>
                  <td><?if ($rec['time_from'] and $rec['time_to']) echo show_time($rec['time_to']-$rec['time_from']) ;?></td>
                  <td><? echo $obj_event->arr_status_lession[$rec['status']] ;  ; ?></td>
                  <td><?echo ($personal_stats_all[$rec['pkey']])? $personal_stats_all[$rec['pkey']]:'-'?></td>
                  <td><?echo ($personal_stats[$rec['pkey']][4]['cnt'])? $personal_stats[$rec['pkey']][4]['cnt']:'-'?></td>
                  <td><? if ($rec['doc1_id']){?><a href="/window/docs/pdf/<?echo $rec['doc1_id'] ?>/" class="new_window" title="doc_<?echo $rec['doc1_id']?>"><?echo $rec['doc1_name']?></a><?if ($rec['doc1_sign_data']){?>&nbsp;&nbsp;&nbsp;<i class="fa fa-lock"></i><?}}?></td>
              </tr><?
          }
          ?></table></form><?
      //damp_array($rec) ;
  }


    function print_template_CSV($list_recs,$options=array())
    {  $personal_stats_all=array() ;  $personal_stats=array() ;
       if (_sizeof($list_recs))
       { $personal_stats_all=execSQL_row('select parent,count(pkey) as cnt from obj_site_lession_persons where parent in ('.implode(',',array_keys($list_recs)).') and clss=227 group by parent',0,1) ;
         $personal_stats=execSQL('select parent,status,count(pkey) as cnt from obj_site_lession_persons where parent in ('.implode(',',array_keys($list_recs)).') and clss=227 group by parent,status',0,1) ;
         $personal_stats=group_by_field('parent',$personal_stats,'status') ;
       }
       $obj_event=EVENTS()->get_event_obj_by_id($this->event_id);
           //damp_array($list_recs,1,-1) ;
         $i=$options['from'] ;
         if (_sizeof($list_recs)) foreach($list_recs as $rec)
           { $data=array() ;
             $data['ID']=$rec['pkey'] ;
             $data['Дата/время создания']=date('d.m.Y H:i',$rec['c_data']) ;
             $data['Место занятия']=$rec['group_name'] ;
             $data['Лектор']=$rec['account_name'] ;
             $data['Темы занятия']=$rec['obj_name'] ;
             $data['Время начала занятия']=($rec['time_from'])? date('d.m.Y H:i',$rec['time_from']):'-' ;
             $data['Время окончания занятия']=($rec['time_to'])? date('d.m.Y H:i',$rec['time_to']):'-' ;
             $data['Длительность занятия']=($rec['time_to'])? show_time($rec['time_to']-$rec['time_from']):'-' ;
             $data['Статус занятия']=$obj_event->arr_status_lession[$rec['status']] ;
             $data['Зарегистрировано участников']=($personal_stats_all[$rec['pkey']])? $personal_stats_all[$rec['pkey']]:'-' ;
             $data['Расписалось участников']=($personal_stats[$rec['pkey']][4]['cnt'])? $personal_stats[$rec['pkey']][4]['cnt']:'-' ;
             $data['Ведомость прохождения']=$rec['doc1_name'] ;
             $data['Статус ведомости прохождения']=($rec['doc1_sign_data'])? 'Подписана':'Не подписана' ;
             //$data['Темы собрания']=(_sizeof($arr_themes_grp[$rec['parent']]))? implode(', ',$arr_themes_grp[$rec['parent']]):'' ;


             $this->put_data_to_CSV($data,$i) ;
             $i++ ;
           }

       }


}

?>
