<?php

    global $tpl;

    js::$i->put('js/psychogra.js');
    
    $Proces = procesy::get( core::$i->id );
    $Temat = tematy::get( $Proces->temat_id );
    
    if( $Proces->id )
    {
        if( core::$i->id <= 2 ) $query = db::$i->q("SELECT * FROM `kryteria` WHERE `temat_id`='".( $Temat->id )."' AND `proces_id` IS NULL" , true );
        else $query = db::$i->q("SELECT * FROM `kryteria` WHERE `proces_id`='".( $Proces->id )."'" , true );        
        
        
        if( db::$i->rows )
        {
            while( $item = db::$i->f( $query ))
            {
                $Kryteria[] = $item;
            }
            
        } else 
        {
            header("Location:".core::$i->url . '/index.php?mode=user&function=game');
            exit;
        }
        
        foreach( $Kryteria as $Kryterium )
        {
            $KryteriaLista[$Kryterium['id']] = $Kryterium['nazwa'];
            $KryteriaGrafika[$Kryterium['id']] = $Kryterium['image'];
            $Koszyk[] = $Kryterium['id'];
        }
        
        $Pairs = ArrayTool::AllPairs( $Koszyk );
        shuffle( $Pairs );
        //debug::printr( $Pairs );
        
        //debug::printr( $Kryteria );
        $tpl->assign(array(
            'Pairs' => $Pairs,
            'KryteriaLista' => $KryteriaLista,
            'KryteriaGrafika' => $KryteriaGrafika,
            'temat_id' => $Temat->id,
            'proces_id' => $Proces->id,
            'ShowDetails' => ( isset( $_REQUEST['details'] ) ? true : false )
        ));
        
        
            
    } 
    else 
    {
        $Tematy = tematy::getList();
        
        $Proces = procesy::unfinished( user::$i->id );
        if( $Proces->Count )
        {
            foreach( $Proces->Items as $ItemKey => $Item )
            {
                list( $Proces->Items[$ItemKey]->GameResults ) = db::$i->row("
                    SELECT COUNT(1) 
                    FROM `wagi` 
                    WHERE 
                        `user`='".( user::$i->id )."' 
                        AND 
                        `temat_id`='".( $Item->temat_id )."'
                ");
            }
        }
        
        $tpl->assign(array(
            'Procesy' => $Proces
        ));
    }
    
    
    module::$i->on();

?>