<?php
include_once('c_poll_question_card.php');
class c_page_cab_poll_question_card_db extends c_page_cab_poll_question_card
{
    public $h1='Опрос - карточка билета - Исходные данные';
    var $use_textarea_autosize=1;

    function block_main()
    {
        $this->page_title();
        $this->panel_db_info();
    }

    function panel_db_info()
    {
        ?><strong>view_poll_questions</strong><br><?
        print_2x_arr([$this->rec_question]);

        ?><strong>poll_tree</strong><br><?
        $recs=execSQL('select * from poll_tree where pkey='.$this->rec_question['pkey']) ;
        print_2x_arr($recs);

        ?><strong>poll_questions</strong><br><?
        $recs=execSQL('select * from poll_questions where pkey='.$this->rec_question['pkey']) ;
        print_2x_arr($recs);

        ?><strong>poll_answers</strong><br><?
        $recs=execSQL('select * from poll_answers where parent='.$this->rec_question['pkey']) ;
        print_2x_arr($recs);

        ?><strong>poll_content</strong><br><?
        $recs=execSQL('select * from poll_content where parent='.$this->rec_question['pkey']) ;
        print_2x_arr($recs);

        //$page=$this->get_page_obj_by_url(_CUR_PAGE_DIR) ;
        ?><button class="v1" cmd="append_space_result">Проверить наличие записей результатов опроса для уклонистов</button><?

    }

    function CMD_append_space_result()
     {
           // уклонисты по первому вопросу
         if (!$this->rec_question['is_second'])
         { $recs=execSQL(' select sq.pkey as sq_id,sq.question_id,sq.c_data,
                            s.pkey as poll_id,s.personal_id,s.mo_id,s.event_id,s.pass_id,
                            r.pkey as result_id,r.answer_id,r.answer_ball
                            from poll_sessions_questions sq 
                            left join poll_sessions s on s.pkey=sq.parent
                            left join poll_result r on r.personal_id=s.personal_id and r.question_id=sq.question_id and r.poll_id=s.pkey
                            where s.personal_id>0 and sq.question_id='.$this->rec_question['pkey'].' and r.pkey is null
                            order by sq.pkey desc
                          ') ;
            echo 'Будет добавлено <strong>'._sizeof($recs).'</strong> результатов уклонистов по первому вопросу<br>' ;
         }
         else
         {
           // уклонисты по второму вопросу
            $recs=execSQL(' select sq.pkey as sq_id,sq.question_id,
                             s.pkey as poll_id,s.personal_id,s.mo_id,s.event_id,s.pass_id,
                             r1.pkey as result1_id,r1.answer_id as answer1_id,r1.c_data,
                             r2.pkey as result2_id,r2.answer_id as answer2_id
                             from poll_sessions_questions sq 
                             left join poll_sessions s on s.pkey=sq.parent
                             left join poll_result r1 on r1.personal_id=s.personal_id and r1.question_id=sq.question_id and r1.poll_id=s.pkey
                             left join poll_result r2 on r2.personal_id=s.personal_id and r2.question_id='.$this->rec_question['pkey'].' and r2.poll_id=s.pkey
                             where s.personal_id>0 and r1.answer_id='.$this->rec_question['parent'].' and r2.pkey is null
                             order by sq.pkey desc
                           ') ;

           echo 'Будет добавлено <strong>'._sizeof($recs).'</strong> результатов уклонистов по второму вопросу<br>' ;
		 }



            if (_sizeof($recs)) foreach($recs as $rec)
            {
                $data=array('clss' => 576,
                            'c_data' => $rec['c_data'],
                            'r_data' => $rec['c_data'],
                            'indx' => 1,
                            'personal_id' => $rec['personal_id'],
                            'mo_id' => $rec['mo_id'],
                            'poll_id' => $rec['poll_id'],
                            'event_id' => $rec['event_id'],
                            'pass_id' => $rec['pass_id'],
                            'session_question_id' => $rec['sq_id'],
                            'question_id' => $this->rec_question['pkey'],
                            'answer_id' => 0,
                            'answer_ball' => 0,
                           ) ;
                $recs2[]=$data ;
                multiSQL_insert('poll_result')->append($data);
            }

          multiSQL_insert('poll_result')->flush();
          print_2x_arr($recs2);
        }




}

?>
