Skip to content

Instantly share code, notes, and snippets.

@jimmyandrade
Created May 23, 2015 23:20
Show Gist options
  • Save jimmyandrade/2310c3d80ac963e9fa40 to your computer and use it in GitHub Desktop.
Save jimmyandrade/2310c3d80ac963e9fa40 to your computer and use it in GitHub Desktop.
Classe "Histórico"
<?php
namespace Taekwondo\Admin_Pages;
class Historico {
public static function gerar_grafico($id, $colunas, $dados, $config ) {
?>
<script>
google.setOnLoadCallback(desenharGrafico<?php echo esc_js( $id ); ?>);
function desenharGrafico<?php echo esc_js( $id ); ?>() {
var data = new google.visualization.DataTable();
<?php
foreach ( $colunas as $tipo => $rotulo ) {
?>
data.addColumn( '<?php echo esc_js( $rotulo ); ?>', '<?php echo esc_js( $tipo ); ?>' );
<?php
}
?>
data.addRows(<?php echo json_encode( $dados ); ?>);
var options = <?php echo json_encode( $config ); ?>;
var chart = new google.charts.Line(document.getElementById('<?php echo esc_js( $id ); ?>'));
chart.draw(data, options);
}
</script>
<div id="<?php echo esc_attr( $id ); ?>"></div>
<?php
}
public static function callback() {
$id_usuario_endereco = filter_input( INPUT_GET, 'post' );
$id_usuario_atual = get_current_user_id();
$args_usuario_relacionado = array(
'post_type' => 'atleta',
'posts_per_page' => 1,
);
if ( intval( $id_usuario_endereco ) > 0 ) {
$args_usuario_relacionado['p'] = $id_usuario_endereco;
}
else {
// @codingStandardsIgnoreStart
$args_usuario_relacionado['meta_key'] = 'usuario_relacionado';
$args_usuario_relacionado['meta_value'] = strval( $id_usuario_atual );
// @codingStandardsIgnoreEnd
}
$atleta_usuario_atual = new \WP_Query( $args_usuario_relacionado );
if ( ! $atleta_usuario_atual->have_posts() ) {
return;
}
?>
<script>
google.load('visualization', '1.1', {packages: ['line']});
</script>
<div class="wrap">
<h2>Histórico do atleta</h2>
<?php
$atleta_usuario_atual->the_post();
$data_atual = new \DateTime();
$data_de_nascimento = \DateTime::createFromFormat( 'Ymd', get_field( 'data_de_nascimento' ) );
$diferenca_datas = $data_atual->diff( $data_de_nascimento );
$args_registros_atleta = array(
'post_type' => 'registro',
// @codingStandardsIgnoreStart
'meta_key' => 'atleta',
'meta_value' => get_the_ID(),
// @codingStandardsIgnoreEnd
);
$registros_atleta = new \WP_Query( $args_registros_atleta );
?>
<h3><?php the_title(); ?></h3>
<?php edit_post_link(); ?>
<p><?php echo wp_kses( $diferenca_datas->y, 'post' ); ?> anos, <?php the_field( 'sexo' ) ?> </p>
<?php
$dados_matriz_historico = array();
$alturas = array();
$config_matriz_historico = array(
'chart' => array(
'title' => 'Histórico do atleta',
'subtitle' => '',
),
'width' => 600,
'height' => 500,
);
$colunas_matriz_historico = array(
'Data do registro' => 'string',
'Altura' => 'number',
'Peso' => 'number',
'Força (membros superiores)' => 'number',
'Força (membros inferiores)' => 'number',
'Equilíbrio' => 'number',
'Velocidade' => 'number',
'Agilidade' => 'number',
'Flexibilidade' => 'number',
'Resistência Abdominal' => 'number',
);
while ( $registros_atleta->have_posts() ) {
$registros_atleta->the_post();
$linha_dados = Historico::gerar_linha_dados( array(
'altura' => 'float',
'peso' => 'float',
'forca_ms' => 'float',
'forca_mi' => 'float',
'equilibrio' => 'int',
'velocidade' => 'int',
'agilidade' => 'int',
'flexibilidade' => 'int',
'resistencia_abdominal' => 'int',
) );
$dados_matriz_historico[] = $linha_dados;
} // end while
Historico::gerar_grafico( 'historico', $colunas_matriz_historico, $dados_matriz_historico, $config_matriz_historico );
?>
</div>
<?php
} // end function
public static function gerar_linha_dados(array $colunas = array()) {
global $post;
$linha = array();
$linha[] = get_the_date();
foreach ( $colunas as $nome => $tipo ) {
switch ( $tipo ) {
case 'float':
$linha[] = get_field( $nome ) ? floatval( get_field( $nome ) ) : 0;
break;
case 'int':
$linha[] = get_field( $nome ) ? intval( get_field( $nome ) ) : 0;
break;
default:
$linha[] = get_field( $nome ) ? get_field( $nome ) : 0;
break;
}
}
return $linha;
}
} // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment