<?php 
    # $Id: format_SHOW.php,v 1.7 2007/01/08 14:49:30 jarda Exp $

    /* Page that displays data of the specified benchmark format
    */

    require_once('structuredPage.inc.php');

    class PageClass extends ViewStructuredPage
    {
        function checkEnvironment()
        {
            if (!$_GET['id'])
                return False;

            return True;
        }

        
        function handleInvalidEnvironment()
        {
            header("Location: formats_LISTING.php"); /* Redirect browser */
            exit();
        }


        function getTitle()
        {
            return 'Format information';
        }
    

        function printContents() {
            $db = connect();

            $query = "SELECT * FROM format WHERE id=$_GET[id]";
            $result = $db->Execute($query);

            if (!$result->RecordCount())
                die('Unknow format ID');

            $row = $result->FetchRow();

            # basic information {{{
            echo "<table>\n";
            echo "<tr><td>Format:</td><td>$row[name]</td></tr>\n";
            echo "<tr><td>Description:</td><td>$row[description]</td></tr>\n";
            # }}}
            
            echo "</table><br>\n";

            # supported attributes {{{
            echo "<b>Supported attributes:</b>";

            $query = "SELECT * FROM formatAttribute, attribute WHERE formatId=$_GET[id] AND attribute.id = formatAttribute.attributeId";
            $result = $db->Execute($query);

            echo "<table>\n";

            while ($row = $result->FetchRow()) {
                $this->addAttributeTableRow($row);
            }

            echo "</table><br>\n";
            # }}}

            # number of instances {{{
            $instanceCount = $this->getInstanceCount($_GET['id']);
            echo "<tr><td>Number of benchmark instances:</td><td>$instanceCount</td></tr><br><br>\n";
            # }}}

            # benchmark sets {{{
            echo "Benchmark sets that contain the format:";

            $query = "SELECT name, id FROM benchmarkSet WHERE id IN " .
                     "(SELECT benchmarkSetId FROM benchmarkInstance " .
                     "WHERE benchmarkInstance.formatId = $_GET[id] " .
                     "GROUP BY benchmarkSetId)";

            $result = $db->Execute($query);

            echo "<table>\n";

            while ($row = $result->FetchRow()) {
                $this->addBenchmarkSetTableRow($row);
            }

            echo "</table><br>\n";
            # }}}

        }


        /* Returns number of benchmark instances that have the specified format
        */
        function getInstanceCount($formatId)
        {
            $db = connect();
            
            $query = "SELECT COUNT(*) FROM benchmarkInstance WHERE formatId = $formatId";

            $recordSet = $db->Execute($query);
            $row = $recordSet->FetchRow();

            return $row['count'];
        }

        
        /* Adds a row to the attribute table
        */
        function addAttributeTableRow($row)
        {
            echo "<tr><td>$row[name]</td></tr>\n";
        }

        
        /* Adds a row to the benchmark set table
        */
        function addBenchmarkSetTableRow($row)
        {
            echo "<tr><td><a href=\"benchmarkSet_SHOW.php?id=$row[id]\">$row[name]</a></td></tr>\n";
        }

    }


    launchPage();

    # vim:enc=iso8859-2:fenc=iso8859-2:
?>
