Location: PHPKode > projects > SvnDashboard > 1.1.3.140/phpLib/modules/PageTimer.mod.php
<?php
//=============================================================================
//
// Description: The PageTimer module allows you to keep track of start/stop
//              times at various sections of a page.
//
// Change-Log:
//     2008-12-23 - Brian Mullen: Initial Creation
//
//=============================================================================

class PageTimer extends Module
{
    //=============================================================================
    // Class: PageTimer
    // Author: Brian Mullen
    // Date: 2008-12-23
    //
    // Description:	Methods for keeping track of page times
    //
    // Dependencies: Module, EventData
    //
    //===========================================================================

    private $_page;//container for start/stop times on a page

    public function __construct( EventData $preCreate = null, EventData $postCreate = null, EventData $preDestroy = null, EventData $postDestroy = null, ModuleLoader $loader = null, $onFail = null )
    {
        //=====================================================================================
        // Function: __construct
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Initialize the pageTimer module
        //
        // Params:
        //     $preCreate {EventData} Optional: data to be passed to the preCreate god event
		//
		//     $postCreate {EventData} Optional: data to be passed to the preCreate god event
		//
		//     $preDestroy {EventData} Optional: data to be passed to the preCreate god event
		//
		//     $postDestroy {EventData} Optional: data to be passed to the preCreate god event
		//
		//     $loader {ModuleLoader} Optional: reference to the module loader
		//
		//     $onFail {string} Optional: how to handle failure of module loading
		//
		// Returns: {PageTimer}
		//=====================================================================================

        //call parent constructor
        parent::__construct( $preCreate, $postCreate, $preDestroy, $postDestroy, $loader, $onFail );

        //init variables
        $this->_page = array( );

        //register events
        $this->registerEvents( );
    }

    public function __destruct( )
    {
        //=====================================================================================
        // Function: __destruct
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Cleans up variable allocation
        //
        // Params: None
        //
		// Returns: None
		//=====================================================================================

        //destroy variables
        unset( $this->_page );

        //call parent destructor
        parent::__destruct( );
    }

    private function registerEvents( )
    {
        $this->eventManager->registerEvent( 'PAGETIMER_ONTIMERADD' );
    }

    public function loadPageList( array $list )
    {
        $this->_page = $list;
    }

    public function addPageTime( $page, $section, $start, $stop )
    {
        //=====================================================================================
        // Function: addPageTime
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Adds a start/stop time to the specified section of a page
        //
        // Params:
        //    $page {string}: name of page
        //
        //    $section {string}: name of the page section
        //
        //    $start {int}: php start time of the page section
        //
        //    $stop {int}: php stop time of the page section
        //
		// Returns: None
		//=====================================================================================

        //init page container if it doesnt exist
        if( !isset( $this->_page[ $page ] ) )
        {
            $this->_page[ $page ] = array( );
        }
        //init section container if it doesnt exist
        if( !isset( $this->_page[ $page ][ $section ] ) )
        {
            $this->_page[ $page ][ $section ] = array( );
        }
        //add start/stop times to page
        $this->_page[ $page ][ $section ][ ] = array( 'start' => $start, 'stop' => $stop );

        $eventData = new EventData( 'PAGETIMER_ONTIMERADD', array( 'page' => $page, 'section' => $section, 'startTime' => $start, 'stopTime' => $stop, 'caller' => $this ) );
        $this->eventManager->fireEvent( PAGETIMER_ONTIMERADD, $eventData );
    }

    public function getPageList( )
    {
        //=====================================================================================
        // Function: getPageList
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Returns a list of all pages specified
        //
        // Params:
        //     $page {string}: name of page
        //
		// Returns: {array}
		//=====================================================================================

        return array_keys( $this->_page );
    }

    public function getPageSectionList( $page )
    {
        //=====================================================================================
        // Function: getPageSectionList
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Returns a list of all sections for the specified page
        //
        // Params:
        //     $page {string}: name of page
        //
		// Returns: {array}
		//=====================================================================================

        if( isset( $this->_page[ $page ] ) )
        {
            return array_keys( $this->_page[ $page ] );
        }
        else
        {
            return array( );
        }
    }

    public function getPageSectionTimes( $page, $section )
    {
        //=====================================================================================
        // Function: getPageSectionTimes
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Returns all the times in one list for the specified page and section
        //
        // Params:
        //     $page {string}: name of page
        //
        //     $section {string}: name of section
        //
		// Returns: {array}
		//=====================================================================================

        $pageTimes = $this->getPageTimes( $page );
        if( isset( $pageTimes[ $section ] ) )
        {
            return $pageTimes[ $section ];
        }
        else
        {
            return array( );
        }
    }

    public function getPageTimes( $page )
    {
        //=====================================================================================
        // Function: getPageTimes
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Returns all the times in one list for the specified page
        //
        // Params:
        //     $page {string}: name of page
        //
		// Returns: {array}
		//=====================================================================================

        $times = array( );
        if( isset( $this->_page[ $page ] ) )
        {
            foreach( $this->_page[ $page ] as $time )
            {
                $times[ ] = $time;
            }
        }
        return $times;
    }

    public function getAllTimes( )
    {
        //=====================================================================================
        // Function: getAllTimes
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Returns all the times in one list
        //
        // Params: None
        //
		// Returns: {array}
		//=====================================================================================

        $times = array( );
        foreach( $this->_page as $section )
        {
            foreach( $section as $time )
            {
                $times[ ] = $time;
            }
        }
        return $times;
    }

    public function getTimeBreakout( )
    {
        //=====================================================================================
        // Function: getTimeBreakout
        // Author: Brian Mullen
        // Date: 2008-12-23
        //
        // Description: Returns all the times for each section for each page
        //
        // Params: None
        //
		// Returns: {array}
		//=====================================================================================

        return $this->_page;
    }

	public function getAllTotals( )
	{
		$totals = array( );

		foreach ($this->_page as $question => $questionTimes)
		{
			$totals[$question] = $this->getQuestionTotals( $question );
		}

		return $totals;
	}

	public function getQuestionTotals( $question )
	{
		if (isset( $this->_page[$question]))
		{
			foreach ($this->_page[$question] as $section => $sectionTimes)
			{
				$totals[$section] = $this->getSectionTotals( $question, $section );
			}

			$sectionsTotal = array_sum($totals);
			$totalElapsed = $this->getPageTotalTime( $question );
			$view = ($totalElapsed - $sectionsTotal);
			$view = ($view < 0 ? 0 : $view); // -- ensure positive view time

			$totals['process'] = $sectionsTotal;
			$totals['view'] = $view;
			$totals['totalElapsed'] = $totalElapsed;
		}
		else
		{
			$totals = false;
		}

		return $totals;
	}

	public function getSectionTotals( $question, $section )
	{
		if (isset( $this->_page[$question][$section] ))
		{
			foreach ($this->_page[$question][$section] as $times)
			{
				$total = $times['stop'] - $times['start'];
				if ($total < 0)
				{
					$total = 0;
				}
			}
		}
		else
		{
			$total = false;
		}

		return $total;
	}

	public function getAllPageTotalTimes( )
	{
		$totals = array( );
		foreach ($this->_page as $question => $questionTimes) // -- loop through all questions
		{
			$totals[$question] = $this->getPageTotalTime( $question );
		}

		return $totals;
	}

	public function getPageTotalTime( $question )
	{
		if (isset( $this->_page[$question]['precode'] ))
		{
			$total = 0;
			foreach ($this->_page[$question]['precode'] as $index => $sectionTime) // -- loop through all start times for this question
			{
				if (isset( $this->_page[$question]['postcode'][$index] )) // -- this question has a stop time
				{
					$qStart = $this->_page[$question]['precode'][$index]['start'];
					$qStop = $this->_page[$question]['postcode'][$index]['stop'];

					$indexTotal = $qStop - $qStart;
					$indexTotal = ($indexTotal < 0 ? 0 : $indexTotal); // -- ensure no negative times are recorded

					$total += $indexTotal;
				}
			}
		}
		else
		{
			$total = false;
		}

		return $total;
	}
}

?>
Return current item: SvnDashboard