<?php

class Xena
{
    public function __construct()
    {
        $this->_db = Application::instance()->getDB();
    }
    public function getLRUsers( $exid, $uid = NULL, $type = NULL )
    {
        $suid = ( int ) $_SESSION[ 'user' ][ 'id' ];
        $uid = ( int ) $uid;
        
        $basesql = '
            SELECT
                "person"."name" as user,
                "exercise"."name" as exercise,
                "LRs"."time",
                "LRs"."folder_name",
                "LRs"."nb_min",
                "LRs"."Be",
                "LRs"."LR",
                "LRs"."id",
                "LRs"."modeltype",
                "LRs"."afis_rank",
                "LRs"."afis_score",
                "LRs"."afis_dev",
                "LRs"."afis_db_size",
                "LRs"."afis_db_gpselect",
                "LRs"."afis_db_fingerselect"
            FROM
                "public"."LRs",
                "public"."person",
                "public"."exercise"
            WHERE
                "LRs"."user" = "person"."id" AND
                "LRs"."exercise" = "exercise"."id" AND
                "exercise"."id" = ' . pg_escape_string( $exid ) . '
        ';
        $order = '
            ORDER BY
                "person"."name" ASC,
                "LRs"."time" DESC;
        ';
        
        if( $type != NULL )
            $basesql .= ' AND "LRs"."modeltype" = \'' . pg_escape_string( $type ) . '\'';
            
            if( $_SESSION[ 'user' ][ 'isadmin' ] )
            {
                $sql = $basesql . $order;
                return $this->_db->query( $sql );
                
            } else if( $suid == $uid ) {
                $usercond = ' AND "person"."id" = ' . pg_escape_string( $uid );
                
                $sql = $basesql . $usercond . $order;
                
                return $this->_db->query( $sql );
                
            } else {
                throw new Exception( "Access denied" );
            }
    }
    
    public function deleteLR( $id )
    {
        if( $_SESSION[ 'user' ][ 'isadmin' ] )
        {
            if( Application::get( "tutor_can_delete_lrs" ) )
            {
                return $this->_db->query( 'DELETE FROM "LRs" WHERE id = ' . pg_escape_string( $id ) . ';' );
            } else {
                throw new Exception( "Deletion not allowed" );
            }
        } else {
            throw new Exception( "Access denied" );
        }
    }
    
    public function lr()
    {
        $data = array (
            "data" => $_POST[ 'data' ],
            "dburl" => Application::get( 'db_uri' ),
            
            "userName" => $_POST[ 'userName' ],
            "userId" => $_POST[ 'userId' ],
            "userEmail" => $_POST[ 'userEmail' ],
            
            "exid" => $_POST[ 'exid' ],
            "caseName" => $_POST[ 'caseName' ],
            "markId" => $_POST[ 'markId' ],
            "printId" => $_POST[ 'printId' ],
            "ESLR" => $_POST[ 'phase' ] == "analysis" ? "ESLR" : "LR",
            
            "gpselect" => $_POST[ 'gpselect' ],
            "fingerselect" => $_POST[ 'fingerselect' ],
            "gpimpact" => $_POST[ 'gpimpact' ],
            "alllrs" => $_POST[ 'alllrs' ]
        );
        
        $options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded",
                'method'  => 'POST',
                'content' => http_build_query( $data )
            )
        );
        
        $url = Application::get( 'Xena_api_post' );
        $context = stream_context_create( $options );
        
        try {
            $result = file_get_contents( $url, false, $context );
            $result = json_decode( $result );
            
            if( ! $result->error )
                return $result->id;
            else
                return $result;
        
        } catch ( Exception $e ) {
            throw new Exception( "Xena API unreachable" );
        }
    }
    
    public function getlrpdf( $id )
    {
        header( "Content-Type:application/octet-stream" );
        header( 'Content-Transfer-Encoding: binary' );
        
        $res = $this->_db->query( 'SELECT * FROM public."LRs" WHERE id = ' . $id . ' LIMIT 1;' );
        
        header( 'Content-Disposition: attachment;filename="' . ( int ) $id . '.pdf"' );
        
        echo pg_unescape_bytea( $res[ 0 ][ 'pdf' ] );
    }
};

?>