<?php
include_once(_DIR_EXT.'/HTML/c_page_JSON.php');
class c_services_quiz_tree extends c_page_JSON
{
    public $tree_root=0 ; // с какого раздела будет раскрываться дерево каталога, 0 - будет показан корневой элемент, 1 - будут показываны сразу разделы второго уровня

    function themes_get_tree_content()
    {
        if($_GET['id']=='#') $id=$this->tree_root ;
        else  $id=(int)$_GET['id'];

        $recs_sections=execSQL('select pkey,obj_name,parent,level,clss,
                                  (select count(t2.pkey) from quiz_themes t2 where t2.parent=t1.pkey and t2.clss in (1,472,481)) as cnt_subsections, 
                                  (select count(t3.pkey) from quiz_questions t3 where t3.parent=t1.pkey) as cnt_questions 
                                  from quiz_themes t1 
                                  where parent='.$id);
        $recs_questions=execSQL('select pkey,obj_name,parent,clss
                          from quiz_questions t1 
                          where parent='.$id);

        $res=array();
        if(_sizeof($recs_sections)) foreach($recs_sections as $rec)
        {
            $has_children=($rec['cnt_subsections'] or $rec['cnt_questions'])? true:false;
            $perent_uid=($rec['parent']==$this->tree_root)? '#':$rec['parent'].'_'.$rec['tkey'];
            $type='clss_'.$rec['clss'];
            $uid=$rec['pkey'].'_'.$rec['tkey'];
            $res[]=array('id'=>$uid,
                         'reffer'=>$rec['_reffer'],
                         'text'=>$rec['obj_name'],
                         'parent'=>$perent_uid,
                         'type'=>$type,
                         'children'=>$has_children,
                         'state'=>array('opened'=>false,'selected'=>false));
        }
        if(_sizeof($recs_questions)) foreach($recs_questions as $rec)
        {
            $perent_uid=($rec['parent']==$this->tree_root)? '#':$rec['parent'].'_'._DOT('quiz_themes')->pkey;
            $type='clss_'.$rec['clss'];
            $uid=$rec['pkey'].'_'.$rec['tkey'];
            $res[]=array('id'=>$uid,
                         'reffer'=>$rec['_reffer'],
                         'text'=>$rec['obj_name'],
                         'parent'=>$perent_uid,
                         'type'=>$type,
                         'children'=>false,
                         'state'=>array('opened'=>false,'selected'=>false));
        }
        return ($res);
    }

}
