<?php
include_once(_DIR_EXT."/events_poll/i_event_poll.php");
include_once(_DIR_TO_CLASS."/c_page.php");
class c_page_cab_events_poll_card extends c_page
{
  public $h1='Карточка мероприятия по проверке знаний' ;
  var $rec_event ;
  var $rec_lession ;
  var $obj_event ;
  var $rec_theme ;

 function select_obj_info()
 { if (_CUR_ID)
   { $this->rec_event=EVENTS()->get_event_by_id(_CUR_ID) ;
     if ($this->rec_event['pkey'])
     { $this->obj_event=EVENTS()->get_event_obj_by_ext($this->rec_event['ext']) ;
       $this->obj_event->rec_event=$this->rec_event ;
       $this->h1=$this->obj_event->name.' "'.$this->rec_event['obj_name'].'"' ;
         //if (MEMBER()->rol!=1 and _CUR_PAGE_DIR==$this->rec_event['__href']) redirect_301_to($this->rec_event['__href'].'new_lession/') ;
     }
     if (!$this->rec_event['pkey']) $this->result=404 ;
   } else $this->result=404 ;

 }

 function menu_config()
 { if ($this->no_event_menu) { parent::menu_config() ; return ; }
   $arr_cnt=EVENTS()->get_event_personals_status($this->rec_event['pkey']) ;
   $cnt_all=execSQL_value('select count(pkey) from '.$this->rec_event['viewer_name']) ;
   ////damp_array($arr_cnt,1,-1);
   $arr_cnt[1]=$cnt_all-$arr_cnt[2]-$arr_cnt[3];

   if ($this->rec_event['pkey'])
      { $this->menu[$this->rec_event['__href']]='Карточка' ;

        $this->menu[$this->rec_event['__href'].'personals/']=array('title'=>'Все участники','info_value'=>(($cnt_all)? '<span>'.$cnt_all.'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/none/']=array('title'=>'Не опрошены','info_value'=>(($arr_cnt[1])? '<span>'.$arr_cnt[1].'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/part/']=array('title'=>'Частично опрошены','info_value'=>(($arr_cnt[2])? '<span>'.$arr_cnt[2].'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/full/']=array('title'=>'Опрошены','info_value'=>(($arr_cnt[3])? '<span>'.$arr_cnt[3].'</span>':'')) ;

        $this->menu[$this->rec_event['__href'].'results/']=array('title'=>'Ход опроса','_info_value'=>(($arr_cnt[1])? '<span>'.$arr_cnt[1].'</span>':'')) ;

        $this->menu[$this->rec_event['__href'].'db/']='Исходные данные' ;
        $this->page_type='window' ;
      }
   elseif ($this->rec_event['pkey'])
      { $this->menu[$this->rec_event['__href'].'edit/step1/']='Параметры' ;
        $this->page_type='window' ;
      }
   else parent::menu_config() ;
 }

    function second_menu_config()
   { $this->second_menu[$this->rec_event['__href']]=array('title'=>'Карточка опросв') ;
     $this->second_menu[$this->rec_event['__href'].'edit/step1/']=array('title'=>'Редактировать') ;
     //$this->second_menu[$this->rec_event['__href'].'log/']='История' ;
     $this->second_menu[$this->rec_event['__href'].'db/']='Исходные данные' ;
     //$this->second_menu[$this->rec_event['__href'].'check/']='Проверки' ;
   }

 function block_main()
 {
   $this->page_title() ;
   $this->panel_event_info() ;
   //$this->panel_events_themes() ;
   ?><br><?
   //$this->panel_stats_by_org() ;


   /*$recs=execSQL_row('select obj_name,count(pkey) from
                  poll_themes
                  group by  obj_name
                  having  count(pkey)>1
                  ',2) ;

   if (_sizeof($recs)) foreach($recs as $name=>$count)
   {  $themes_ids=execSQL_line('select pkey from poll_themes where obj_name="'.$name.'" order by pkey') ;
      $sql='update poll_questions set parent='.$themes_ids[0].' where parent in ('.implode(',',$themes_ids).')' ;
      echo $sql.'<br>' ;
      //execSQL_update($sql) ;
      unset($themes_ids[0]) ;
      $sql='delete from  poll_themes where pkey in ('.implode(',',$themes_ids).')' ;
      echo $sql.'<br>' ;
      //execSQL_update($sql) ;
   }


   */
   /*
   $recs=execSQL('select parent,obj_name,count(pkey) from
                       poll_questions
                       group by  parent,obj_name
                       having  count(pkey)>1
                       ',2,1) ;

     if (_sizeof($recs)) foreach($recs as $rec)
        {  $ids=execSQL_line('select pkey from poll_questions where parent='.$rec['parent'].' and obj_name="'.$rec['obj_name'].'" order by pkey desc') ;
           $sql='update poll_result set question_id='.$ids[0].' where question_id in ('.implode(',',$ids).')' ;
           echo $sql.'<br>' ;
           //execSQL_update($sql) ;
           unset($ids[0]) ;
           $sql='delete from  poll_questions where pkey in ('.implode(',',$ids).')' ;
           echo $sql.'<br>' ;
           //execSQL_update($sql) ;
        }





   $recs=execSQL('select t1.pkey,t1.obj_name from
                  poll_questions t1
                  left join poll_result t2 on t2.question_id=t1.pkey
                  where t2.pkey is null
                  ',2) ;       */
 }

    function panel_event_info()
        {   // данные по участникам мероприятия с учетом группы текущего аккаунта
           $arr_personals_status=EVENTS()->get_event_personals_status($this->rec_event['pkey']) ;
           $personals_count=array_sum($arr_personals_status) ;
           //damp_array($this->rec_event) ;

           ?><table class="basic auto fz_small setting">
                <tr><th>Статус:</th><td><?echo $this->rec_event['_status']?></td></tr>
                <tr><th>Срок действия, от:</th><td><?echo date('d.m.Y H:i',$this->rec_event['date_from'])?></td></tr>
                <tr><th>Срок действия, до:</th><td><?echo date('d.m.Y H:i',$this->rec_event['date_to'])?></td></tr>
                <?/*?><tr><th>Условие отбора участников:</th><td><?PERSONAL()->get_personals_filter_info_to_item($this->rec_event['_reffer'])?></td></tr><?*/?>
                <tr><th>Условие отбора участников:</th><td><?echo $this->rec_event['_ext_info']['info_filter']?></td></tr>
                <tr><th>Место проведения:</th><td><?echo implode('<br>',$this->rec_event['_ext_info']['_group_ids'])?></td></tr>
            </table>
            <style type="text/css">
                table.basic.setting{}
                table.basic.setting th{text-align:left;font-weight:normal;}
                table.basic.setting td{text-align:left;font-weight:bold;font-size:15px;color:black;}
                table.basic.setting td p{text-align:left;font-weight:normal;}
            </style>
           <?
        }

 function panel_stats_by_org()
 {
   $themes_ids=implode(',',$this->rec_event['_ext_info']['themes_ids']) ;
   $tiket_ids=array() ;
   //echo '$themes_ids='.print_r($themes_ids,true).'<br>' ;
   if ($themes_ids)
 	{ $str_ids=$themes_ids;
 	  // получаем все разделы которые внутри указанной темы/раздела или сам раздел
 	  $recs_rasdels=execSQL_line('select pkey from poll_themes where pkey in ('.$str_ids.') or parent1 in ('.$str_ids.') or parent2 in ('.$str_ids.')  or parent3 in ('.$str_ids.')  or parent4 in ('.$str_ids.')  or parent5 in ('.$str_ids.')  or parent6 in ('.$str_ids.')  or parent7 in ('.$str_ids.')  or parent8 in ('.$str_ids.') or parent9 in ('.$str_ids.')',0,1) ;
 	  if (_sizeof($recs_rasdels)) $tiket_ids=execSQL_line('select pkey,obj_name from poll_questions where parent in ('.implode(',',$recs_rasdels).')',0) ;

 	  //$use_rasdel=$recs_rasdels[array_rand($recs_rasdels)] ;
 	  //echo '$use_rasdels='.$use_rasdel.'<br>' ;
    }

     //t1.question_id in ('.implode(',',$tiket_ids).')

   $stats=execSQL('select t2._org_id,t3.obj_name,t1.correct,count(t1.pkey) as cnt_questions 
            from  poll_result t1
            left join  view_personals t2 on t2.pkey=t1.personal_id
            left join  obj_site_personals t3 on t3.pkey=t2._org_id
            left join poll_questions t4 on t4.pkey=t1.question_id 
            left join obj_site_events_pass t5 on t5.pkey=t1.poll_id
            where not t2._org_id is null
                  and t2.enabled=1 
                  and t5.event_id='.$this->rec_event['pkey'].'
                  and not t4.pkey is null
            group by t2._org_id,t1.correct
           ',0,1) ;

     $stats2=array() ;
   if (_sizeof($stats))  foreach($stats as $rec)
   { $stats2[$rec['_org_id']]['all']+=$rec['cnt_questions'] ;
     if ($rec['correct'])  $stats2[$rec['_org_id']]['correct']=$rec['cnt_questions'] ;
     $stats2[$rec['_org_id']]['org_name']=$rec['obj_name'] ;
   }
   ?><table class="basic fz_small full"><tr><th>Организация</th><th>Задано вопросов</th><th>Правильных ответов</th><th>Процент правильных ответов</th></tr><?
   if (_sizeof($stats2)) foreach($stats2 as $rec)
   { ?><tr>
         <td><?echo $rec['org_name']?></td>
         <td><?echo $rec['all']?></td>
         <td><?echo $rec['correct']?></td>
         <td><?echo round($rec['correct']/$rec['all']*100)?>%</td>

      </tr><?

   }
   ?></table><?

   //damp_array($stats2) ;


   /*  $recs=execSQL('select t1._org_id,t1._org_name,t1._otdel_id,t1._otdel_name,t1.event_status,count(t1.pkey) as cnt
                           from view_event_personals t1
                           group by t1._org_id,t1._otdel_id,t1.event_status
                           order by t1._org_name,t1._otdel_name
                            ',1,1) ;

   ?><h2>Статистика проверки знаний по организациям:</h2><?
   include_once(_DIR_EXT.'/events_poll/report_poll_info_by_orgs.php') ;
   $report_obj=new report_poll_info_by_orgs() ;
   $report_obj->event_id=$this->rec_event['pkey'] ;
   $report_obj->filter['group_id']=MEMBER()->cur_group_id ;
   $report_obj->setting() ;
   //$report_obj->view() ;   */
 }


  function panel_events_themes()
  {   //damp_array($this->obj_event) ;
      $themes_ids=$this->rec_event['_ext_info']['themes_ids'] ;

      ?><div id="panel_events_themes"><h2>Темы проверки знаний</h2><?
      if (_sizeof($themes_ids)) $recs_themes=execSQL('select * from poll_themes where pkey in ('.implode(',',$themes_ids).') order by indx') ;

       ?><table class="basic fz_small full auto"><tr class="head"><th>№</th><th>ID</th><th>Наименование</th><th>Методические материалы</th></tr><?
           if (_sizeof($recs_themes)) foreach($recs_themes as $rec)
           {   $arr=TREE('poll_themes')->get_arr_parents_names($rec['pkey']) ;

               ?><tr>
                 <td style="width:10%;" class="indx center"><?echo $rec['indx']?></td>
                 <td style="width:10%;" class="indx center"><?echo $rec['pkey']?></td>
                 <td style="width:35%;" class="left"><?echo implode(' / ',$arr)?></td>
                 <td style="width:20%;text-align:left;"><?
                 ?></td>
               </tr>
             <?
           }
           ?>
            </table>
            <style type="text/css">
               table.basic td.indx{text-align:center;}
               table.basic td.indx:hover i{display:inline;cursor:pointer;}
               table.basic ul{margin:0 0 0 20px;;padding:0;}
               table.basic ul li{padding:0;margin:0;}
            </style>
            </div>
           <?
  }
}



?>
