<?
function panel_theme_export($theme_id)
{  //$arr=TREE('quiz_themes')->get_arr_parents_names($rec_section['pkey'],array('from_level'=>2)) ;
   //echo '<h1>'.implode(' / ',$arr).' - Новый билет</h1>'  ;
   $rec_theme=QUIZ()->get_theme_by_id($theme_id) ;
   if (!$rec_theme['pkey']) { echo 'Тема не найдена' ; return ; }
   $recs_sections=execSQL('select * from quiz_themes where parent='.$rec_theme['pkey']) ;
   $recs_questions=execSQL('select * from quiz_questions where parent in (select pkey from quiz_themes where parent='.$rec_theme['pkey'].')',1) ;
   $recs_answers=execSQL('select * from quiz_answers where parent in (select pkey from quiz_questions where parent in (select pkey from quiz_themes where parent='.$rec_theme['pkey'].'))',1) ;
   $recs_content=execSQL('select * from quiz_content where parent in (select pkey from quiz_questions where parent in (select pkey from quiz_themes where parent='.$rec_theme['pkey'].'))',1) ;
   $result['thema']=array('id'=>$rec_theme['pkey'],'name'=>$rec_theme['obj_name'])  ;
   if (_sizeof($recs_sections)) foreach($recs_sections as $id=>$rec) $result['sections'][$id]=array('name'=>$rec['obj_name'],'parent'=>$rec['parent']) ;
   if (_sizeof($recs_questions)) foreach($recs_questions as $id=>$rec) $result['tikets'][$id]=array('text'=>$rec['obj_name'],'section_id'=>$rec['parent'],'timeout'=>$rec['timeout']) ;
   if (_sizeof($recs_answers)) foreach($recs_answers as $id=>$rec) $result['answers'][$id]=array('indx'=>$rec['indx'],'text'=>$rec['obj_name'],'tiket_id'=>$rec['parent'],'status'=>$rec['status']) ;
   if (_sizeof($recs_content)) foreach($recs_content as $rec)
     {   $rec_content=array('name'=>$rec['obj_name'],'tiket_id'=>$rec['parent']) ;
         if (file_exists(_DIR_TO_ROOT.$rec['dir'].$rec['file_name'])) $rec_content['data']=base64_encode(file_get_contents(_DIR_TO_ROOT.$rec['dir'].$rec['file_name'])) ;
         $result['contents'][]=$rec_content ;
   }
   $json_content=json_encode($result) ;
   echo '<br>Размер файла <strong>'.show_size(strlen($json_content)).'</strong><br>' ;
   $fname='quiz_theme_'.safe_file_names($rec_theme['obj_name']).'.json' ;
   $fdir=_TEMP_DIR.'/'.$fname ;
   file_put_contents($fdir,$json_content) ;
   $result['Разделов']=_sizeof($recs_sections) ;
   $result['Билетов']=_sizeof($recs_questions) ;
   $result['Ответов']=_sizeof($recs_answers) ;
   $result['Файлов']=_sizeof($recs_content) ;
   $result['Файл экпорта']=$fname ;
   echo '<a target=_blank href="/temp/'.$fname.'">'.$fname.'</a>' ;
}


?>
