<?php
include_once('c_quiz.php') ;
class c_page_cab_quiz_info extends c_page_cab_quiz
{
 public $h1='КЗ - Информация' ;

 function block_main()
 {
 	/*$session_quiz_max_id=execSQL_value('select max(pkey) from quiz_sessions') ;
 	execSQL_update('update quiz_result set pass_id=quiz_id where quiz_id>'.$session_quiz_max_id,2) ;
 	execSQL_update('update quiz_result set quiz_id=0 where quiz_id>'.$session_quiz_max_id,2) ;

 	//execSQL_update('delete from quiz_sessions where pkey>171') ;
 	//execSQL_update('update quiz_result set quiz_id=0 where quiz_id>171') ;
    $this->sync_quiz_result_to_quiz_sessions() ;

	 execSQL_update('update quiz_result t1
                  right join quiz_sessions t2 on t2.mo_id=t1.mo_id
                  set t1.pass_id=t2.pass_id
                  where (t1.pass_id=0 or t1.pass_id is null)'
                 ,2) ;

	 execSQL_update('update quiz_result t1
                  right join obj_site_events_pass t2 on t2.pkey=t1.pass_id
                  set t1.event_id=t2.event_id
                  where t1.pass_id>0 and (t1.event_id=0 or t1.event_id is null)'
                 ,2) ;
*/




     $tables=array(   'quiz_sessions'=>'Основная таблица сессий КЗ',
                      'quiz_sessions_questions'=>'Cессии КЗ - заданные вопросы',
                      'quiz_result'=>'Результаты ответов',
                      'quiz_stats'=>'Статистика контроля знаний',
                      'quiz_themes'=>'Темы/разделы проверки знаний',
                      'quiz_themes_text'=>'Темы/разделы проверки знаний - тексты',
                      'quiz_questions'=>'Вопросы проверки знаний',
                      'quiz_answers'=>'Ответы проверки знаний',
                      'quiz_content'=>'Контент проверки знаний',
                      'quiz_learned'=>'Усваиваемость вопросов',
                   ) ;
     include_once(_DIR_EXT.'/aspmo_debug/c_debug_info.php')  ;
     $page=new c_page_cab_debug_info() ;
     $page->panel_info_db_small($tables) ;
 }

 function sync_quiz_result_to_quiz_sessions()
 {

     $recs=execSQL('select t1.*,
                    t3.terminal_pkey,t3.terminal_id,t3.member_id as personal_id,t3.group_id,t3.reader,t3.c_data,t3.e_data,t3.reason,t3.equipment,t3.video,t3.DTW_id,
                    t3.c_data_m,t3.c_data_h,t3.c_data_d, t3.error,t3.error_reason,t3.message_terminal,t3.terminal_comment
                    from quiz_result t1
                    left join quiz_sessions t2 on  t2.mo_id=t1.mo_id
                    left join obj_site_mo_session t3 on  t3.pkey=t1.mo_id
                    where t2.pkey is null and t1.mo_id>0
                   ') ;
     if (!_sizeof($recs)) return ;
     echo 'Найдено <strong>'._sizeof($recs).'</strong> записей в quiz_result, сессии которых отсутстуют в quiz_sessions<br>' ;
     $recs_mo=group_by_field('mo_id',$recs) ;
     if (_sizeof($recs_mo)) foreach($recs_mo as $mo_id=>$quiz_results)
     {
         $mo_info=$quiz_results[0] ;
         $rec_session_kz=array('mo_id'          => $mo_id,
                               'DTW_id'        => $mo_info['DTW_id'],
                               'pass_id'        => $mo_info['pass_id'],
                               'terminal_id'    => $mo_info['terminal_id'],
                               'terminal_pkey'  => $mo_info['terminal_pkey'],
                               'personal_id'    => $mo_info['personal_id'],
                               'group_id'       => $mo_info['group_id'],
                               'reader'         => ($mo_info['reader'])? $mo_info['reader']:'',
                                'c_data'        => $mo_info['c_data'],
                                'e_data'        => $mo_info['e_data'],
                                'c_data_m'        => $mo_info['c_data_m'],
                                'c_data_h'        => $mo_info['c_data_h'],
                                'c_data_d'        => $mo_info['c_data_d'],
                 	            'reason'        => $mo_info['reason'],
                 	            'equipment'     => $mo_info['equipment'],
                 	            'video'         => $mo_info['video'],
                 	            'error'         => $mo_info['error'],
                 	            'error_reason'         => $mo_info['error_reason'],
                 	            'message_terminal'         => $mo_info['message_terminal'],
                 	            'terminal_comment'         => $mo_info['terminal_comment'],
                 	            'status'         => 2,
                 	            'clss'         => 278,
                 	            'indx'         => 1
                               //'pass_id'        => $rec_event_pass['event_pass_id']
                              );
         multiSQL_insert('quiz_sessions')->append($rec_session_kz);
         $arr_sessions[]=$rec_session_kz ;
     }

     //print_2x_arr($arr_sessions);
     multiSQL_insert('quiz_sessions')->flush();

     execSQL_update('update quiz_result t1
                     right join quiz_sessions t2 on t2.mo_id=t1.mo_id
                     set t1.quiz_id=t2.pkey
                     where (t1.quiz_id=0 or t1.quiz_id is null)'
                    ,2) ;




 }



}

?>
