<?php   
defined('C5_EXECUTE') or die("Access Denied."); 
/*
Concrete Bricks game by John Liddiard (aka JohntheFish)
www.jlunderwater.co.uk
This software is licensed under the terms described in the concrete5.org marketplace. 
Please find the add-on there for the latest license copy.
*/
?><div class="concrete_bricks_game_top_scores_table"><div class="concrete_bricks_game_top_scores_table_inner"><?php 
if (empty($best_scores)  || count($best_scores)<1){
	echo '<h2 class="concrete_bricks_game_top_scores_heading">';
	echo t('Sorry, no scores have been recorded.');
	echo '</h2>';
} else {
	if (isset($options['show_heading'])){

		if (!empty($current_user_name)){
			echo '<h2 class="concrete_bricks_game_top_scores_heading">';
			echo t('Concrete Bricks Top Scores For ');
			echo '<span class="concrete_bricks_game_user_name">';
			echo $current_user_name;	
			echo '</span></h2>';
		}
		else {
			echo '<h2 class="concrete_bricks_game_top_scores_heading">';
			echo t('Concrete Bricks Top Scores');
			echo '</h2>';
		}
	}
	
	$table_stuff;
	$border_stuff = ' style="padding:2px 5px;" ';
	$th_stuff = ' style="padding:2px 5px;" ';
	if (isset($options['show_borders'])){
		$border_stuff = ' style="border:1px solid silver; padding:2px 5px;" ';
		$th_stuff = ' style="border:1px solid silver; padding:2px 5px;" ';
		$table_stuff = ' style="border-collapse:collapse; " ';
	}

	echo '<table '.$table_stuff.'>';
	if (isset($options['show_col_headings'])){
		echo '<thead><tr>';
		if (! isset($options['current_user_only'])){?>
			<th> </th>
			<?php 	
		}

		?><th<?php  echo $th_stuff?>><?php  echo t('Score');?></th><?php 

		if (isset($options['time_taken'])){?>
			<th<?php  echo $th_stuff?>><?php  echo t('In');?></th>
			<?php 	
		}

		if (isset($options['completed_lines'])){?>
			<th<?php  echo $th_stuff?>><?php  echo t('Lines');?></th>
			<?php 	
		}

		if (isset($options['level_achieved'])){?>
			<th<?php  echo $th_stuff?>><?php  echo t('Level');?></th>
			<?php 	
		}

		if (isset($options['GameDate'])){?>
			<th<?php  echo $th_stuff?>><?php  echo t('When');?></th>
			<?php 	
		}

		if (isset($options['comment'])){?>
			<th<?php  echo $th_stuff?>><?php  echo t('Comment');?></th>
			<?php 	
		}
		echo '</tr></thead>';
	}
	
	foreach ($best_scores as $a_score){
		echo '<tr>';
		if (! isset($options['current_user_only'])){
			if (isset($a_score['uID'])){
				$for_user = User::getByUserID($a_score['uID']);
				if (	isset($for_user) && 
						gettype($for_user)=='object'
						){
					$a_score_user_name = $for_user->getUserName();
				}
			}
			if (empty($a_score_user_name)){
				$a_score_user_name = $a_score['name'];
			}
			if (empty($a_score_user_name)){
				$a_score_user_name = t('Unknown Player');
			}?>
			<td<?php  echo $border_stuff?>><?php  echo $a_score_user_name;?></td>
			<?php 	
		}

		?>
		<td<?php  echo $border_stuff?>><?php  echo $a_score['score'];?></td>		
		<?php 
		
		if (isset($options['time_taken'])){
			$pretty_duration = '';
			$seconds = (int)($a_score['time_taken']/1000);
			$minutes = (int)($seconds/60);
			$hours = (int)($minutes/60);
			if ($hours>0){
				$minutes=$minutes%60;
				$pretty_duration .= $hours.t('h');
			}
			if ($hours>0 || $minutes>0){
				$seconds=$seconds%60;
				$pretty_duration .= ' '.$minutes.t('m');
			}
			$pretty_duration .=' '.$seconds.t('s');
			
		
			?>
			<td<?php  echo $border_stuff?>><?php  echo $pretty_duration;?></td>
			<?php 	
		}

		if (isset($options['completed_lines'])){?>
			<td<?php  echo $border_stuff?>><?php  echo $a_score['completed_lines'];?></td>
			<?php 	
		}

		if (isset($options['level_achieved'])){?>
			<td<?php  echo $border_stuff?>><?php  echo $a_score['level_achieved'];?></td>
			<?php 	
		}

		if (isset($options['GameDate'])){?>
			<td<?php  echo $border_stuff?>><?php  
			echo $this->controller->selectedTime($a_score['GameDate'], $options['dt_format_option']);
			?></td>
			<?php 	
		}

		if (isset($options['comment'])){?>
			<td<?php  echo $border_stuff?>><?php  echo $a_score['comment'];?></td>
			<?php 	
		}
		echo '</tr>';
	}
	echo '</table>';

}
?></div></div>
<?php 
/* 
Respond to score update event and reload results table by ajax,
so not included during an ajax update.
*/
if (empty($ajax_mode)){
?>
<script type="text/javascript">
$(document).ready(function(){var a="<?php  echo $action_ajax_update_table ?>";var b="<?php  echo $validate_ajax_update_table ?>";var d="<?php  echo $blockID ?>";var c="<?php  echo $collectionID ?>";$(".concrete_bricks_game_top_scores_table").live("concrete_bricks_game_scores_updated",function(){$(".concrete_bricks_game_top_scores_table").load(a+" .concrete_bricks_game_top_scores_table_inner",{ccm_token:b,blockid:d,collectionid:c})})});
</script><?php  }