Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header("content-type: application/javascript");
- require_once 'config.php';
- /*
- Not used, but might be handy later.
- -----------------------------------
- function total_responses(){
- $stmt = $mysqli->prepare("select COUNT(*) from responces");
- $stmt->execute();
- $stmt->bint_result($num_rows);
- $stmt->fetch();
- return $num_rows;
- }
- function responces_for_age($age){
- $stmt = $mysqli->prepare("select COUNT(responces.id) from responces join images on images.id=responces.image_id where age=?");
- $stmt->bind_param("i", $age);
- $stmt->execute();
- $stmt->bind_result($num_rows);
- $stmt->fetch();
- return $num_rows;
- }
- function average_score(){
- $stmt = $mysqli->prepare("select AVG(score) from responces");
- $stmt->execute();
- $stmt->bind_result($avg_score);
- $stmt->fetch();
- return $avg_score;
- }
- function average_score_in_time_period($time_start, $time_end){
- $stmt = $mysqli->prepare("select AVG(score) from responces where datetime >= ? or datetime <= ?");
- $stmt->bind_param("ii", $time_start, $time_end);
- $stmt->execute();
- $stmt->bind_param($avg_score);
- return $avg_score;
- }
- function average_score_for_age_in_time_period($age, $time_start, $time_end){
- $stmt = $mysqli->prepare("select AVG(score) from responces join images on images.id=responces.image_id where age=? and (datetime >= ? or datetime <= ?)");
- $stmt->bind_param("ii", $time_start, $time_end);
- $stmt->execute();
- $stmt->bind_param($avg_score);
- return $avg_score;
- }
- */
- function average_score_for_age($age){
- $stmt = $mysqli->prepare("select AVG(score) from responces join images on images.id=responces.image_id where age=?");
- $stmt->bind_param("i", $age);
- $stmt->execute();
- $stmt->bind_result($avg_score);
- $stmt->fetch();
- return $avg_score;
- }
- function heighest_scoring_age_in_time_period($time_start, $time_end){
- $stmt = $mysqli->prepare("select age from images join responce on images.id=responce.image_id where datetime >= ? or datetime <= ? order by AVG(responce.score) desc limit 1");
- $stmt->bind_param("ii", $time_start, $time_end);
- $stmt->execute();
- $stmt->bind_param($age);
- return $age;
- }
- function score_against_avg_age(){
- $graph = array();
- for ($a = MIN_AGE; $a <= MAX_AGE; $a++){
- array_push($graph, array(
- "age" => $a,
- "score" => average_score_for_age($a)//should rewrite as stored function, as this is a lot of mysql transactions; slow!
- ));
- }
- return $graph;
- }
- function highest_scoring_age_againts_time(){
- //find oldest responce
- $stmt = $mysqli->prepare("select UNIX_TIMESTAMP(datetime) from images order by datetime asc limit 1");
- $stmt->execute();
- $stmt->bind_result($start_datetime);
- $stmt->fetch();
- //create time periods
- $age_diff = MAX_AGE - MIN_AGE;
- $now = time();
- $total_time_elapsed = $now - $start_datetime;
- $time_period = intval($total_time_elapsed / $age_diff);
- $graph = array();
- while ($age_diff != 0) {
- $end_time = $start_datetime + $time_period;
- array_push($graph, array(
- "startTime" => $start_datetime,
- "endTime" => $end_time,
- "age" => heighest_scoring_age_in_time_period($start_datetime, $end_time)// this could also do with being an sql function.
- ));
- $start_datetime = $end_time;
- $age_diff--;
- }
- return $graph;
- }
- function output_graph($graph){
- echo json_encode($graph);
- }
- //inspect request and serve graph data
- if($_POST['graph']){
- switch ($_POST['graph']) {
- case 'scoreAgainstAvgAge':
- output_graph(scoreAgainstAvgAge());
- break;
- case 'highestScoringAgeAgaintsTime':
- output_graph(highest_scoring_age_againts_time());
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement