From 317f4fb579d6978607e83ca1a64d973e89a5c45e Mon Sep 17 00:00:00 2001 From: mattbk Date: Fri, 30 Oct 2015 08:45:51 -0500 Subject: [PATCH 1/8] misc --- index.php | 7 ++++++- views/finish.html.twig | 2 ++ views/results.html.twig | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index e979ff0..8493b6b 100755 --- a/index.php +++ b/index.php @@ -94,6 +94,11 @@ switch ($mode) { ) or die('Could not prepare query'); $query->execute() or die('Could not execute query:
'.mysqli_error($db)); $query->close(); + + //Debugging I think? + $variables['debug'] = $_SESSION['results']; + + break; case 'results': $download = isset($_GET['download']); @@ -165,5 +170,5 @@ if (!empty ($template)) { echo $twig->render($template, $variables); } else { - //header('HTTP/1.0 404 Not Found'); + header('HTTP/1.0 404 Not Found'); } \ No newline at end of file diff --git a/views/finish.html.twig b/views/finish.html.twig index 8285ab9..86c095c 100644 --- a/views/finish.html.twig +++ b/views/finish.html.twig @@ -6,4 +6,6 @@

Thank you for your participation.

Return to homepage »

+ + {% endblock %} \ No newline at end of file diff --git a/views/results.html.twig b/views/results.html.twig index 46031a5..dc42f33 100644 --- a/views/results.html.twig +++ b/views/results.html.twig @@ -35,8 +35,8 @@ +
{{ dump(stats) }}
+
{{ dump(debug) }}
-->

Raw results

Download results

From 86c46fb7364c77cb616000e7592a28bfec612458 Mon Sep 17 00:00:00 2001 From: mattbk Date: Fri, 30 Oct 2015 09:09:57 -0500 Subject: [PATCH 2/8] Learning nested foreach loops See comments in code. --- index.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 8493b6b..cc1f709 100755 --- a/index.php +++ b/index.php @@ -95,9 +95,19 @@ switch ($mode) { $query->execute() or die('Could not execute query:
'.mysqli_error($db)); $query->close(); - //Debugging I think? + + //Loop through $results better + //This is not done yet, but shows an example of a loop you could put a query statement in. + foreach ($_SESSION['results'] as $name => $value) { + foreach ($value as $n2 => $v2) { + echo $name.$n2.$v2; + } + } + + //Debugging I think? $variables['debug'] = $_SESSION['results']; - + + break; case 'results': From 39805f9621316a7c32de59acf0cbf7dd66ae3df6 Mon Sep 17 00:00:00 2001 From: mattbk Date: Fri, 30 Oct 2015 13:53:39 -0500 Subject: [PATCH 3/8] Got most results into a new EVA table Still need to figure out timestamps and have the user enter an identifier at the beginning. --- index.php | 33 +++++++++++++++++++++++++++++---- views/finish.html.twig | 2 -- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/index.php b/index.php index cc1f709..9fdd5bf 100755 --- a/index.php +++ b/index.php @@ -98,12 +98,37 @@ switch ($mode) { //Loop through $results better //This is not done yet, but shows an example of a loop you could put a query statement in. - foreach ($_SESSION['results'] as $name => $value) { - foreach ($value as $n2 => $v2) { - echo $name.$n2.$v2; + //I think this will allow running only one query rather than several: http://stackoverflow.com/a/10054657/2152245 + //However, if you want to define the number of phases/scenes in settings.json only (and not in a db setup script), + // you have to use an EVA table here with fields like userid, phase, measure [xccordinate, ycoordinate, or responsetime], value [value for measure] at minimum. + // then need to rewrite the query to pull them out of the db correctly, since each userid will have one row per measure per phase. + foreach ($_SESSION['results'] as $phasename => $phasevalue) { + foreach ($phasevalue as $measure => $measurevalue) { + //Building an INSERT query: + //Include userid (once collection form is added into the start page) + //Include a timestamp that actually works + + //Test output on the screen + //echo ""."".$_SERVER['REMOTE_ADDR'].$phasename.$measure.$measurevalue."
"; + + // DB fields: uid, date, host, phase, measure, value + // http://php.net/manual/en/function.implode.php + $columnstoimplode = array("uid", "datetime", "host", "phase", "measure", "value"); + // Note that backticks (`) go around field names... + $columns = "`".implode("`, `", $columnstoimplode)."`"; + //print_r($columns); + $valuestoimplode = array("", "unix_timestamp()", $_SERVER['REMOTE_ADDR'], $phasename, $measure, $measurevalue); + $values = "'".implode("', '", $valuestoimplode)."'"; + //print_r($values); + // Build and execute query + $sql = "INSERT INTO results ("; + $sql .= $columns; + $sql .= ") VALUES ($values)"; + $db->query($sql) or die('Could not execute query:
'.mysqli_error($db)); } } - + + //Debugging I think? $variables['debug'] = $_SESSION['results']; diff --git a/views/finish.html.twig b/views/finish.html.twig index 86c095c..8285ab9 100644 --- a/views/finish.html.twig +++ b/views/finish.html.twig @@ -6,6 +6,4 @@

Thank you for your participation.

Return to homepage »

- - {% endblock %} \ No newline at end of file From ab3a6763c0a9a24a4c704013d2eed8fa4a9e0d0e Mon Sep 17 00:00:00 2001 From: mattbk Date: Fri, 30 Oct 2015 14:14:02 -0500 Subject: [PATCH 4/8] Solved timestamp issue, removed debug statements --- index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 9fdd5bf..a71929e 100755 --- a/index.php +++ b/index.php @@ -116,10 +116,10 @@ switch ($mode) { $columnstoimplode = array("uid", "datetime", "host", "phase", "measure", "value"); // Note that backticks (`) go around field names... $columns = "`".implode("`, `", $columnstoimplode)."`"; - //print_r($columns); - $valuestoimplode = array("", "unix_timestamp()", $_SERVER['REMOTE_ADDR'], $phasename, $measure, $measurevalue); + // Set up timestamp so you can tell participants apart. http://alvinalexander.com/php/php-date-formatted-sql-timestamp-insert + $timestamp = date('Y-m-d G:i:s'); + $valuestoimplode = array("", $timestamp, $_SERVER['REMOTE_ADDR'], $phasename, $measure, $measurevalue); $values = "'".implode("', '", $valuestoimplode)."'"; - //print_r($values); // Build and execute query $sql = "INSERT INTO results ("; $sql .= $columns; From 06407aa70733c39724214fe839ce0ddf11992338 Mon Sep 17 00:00:00 2001 From: mattbk Date: Fri, 30 Oct 2015 16:09:45 -0500 Subject: [PATCH 5/8] Rethinking results table structure See comments in file. --- index.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/index.php b/index.php index a71929e..4406269 100755 --- a/index.php +++ b/index.php @@ -78,6 +78,7 @@ switch ($mode) { case 'finish': $template = 'finish.html.twig'; // Store the result. +// This section will be removed $query = $db->prepare('insert into vcd_results values (null, unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); $query->bind_param( 'siiiiiiiii', @@ -94,14 +95,19 @@ switch ($mode) { ) or die('Could not prepare query'); $query->execute() or die('Could not execute query:
'.mysqli_error($db)); $query->close(); - - + // End remove section + + //Store the result. //Loop through $results better - //This is not done yet, but shows an example of a loop you could put a query statement in. - //I think this will allow running only one query rather than several: http://stackoverflow.com/a/10054657/2152245 + //This will allow running only one query rather than several: http://stackoverflow.com/a/10054657/2152245 //However, if you want to define the number of phases/scenes in settings.json only (and not in a db setup script), - // you have to use an EVA table here with fields like userid, phase, measure [xccordinate, ycoordinate, or responsetime], value [value for measure] at minimum. + // you have to use an EAV table here with fields like userid, phase, measure [xccordinate, ycoordinate, or responsetime], value [value for measure] at minimum. // then need to rewrite the query to pull them out of the db correctly, since each userid will have one row per measure per phase. + //Criticism of EAV: https://www.simple-talk.com/sql/database-administration/five-simple--database-design-errors-you-should-avoid/, + // + //Below needs to be rewritten; because I have `phase` as a field, I can also have `xcoordinate`, `ycoordinate`, and `responsetime` + // as values as well. Table should have a field list of: uid, datetime, host, userid, phase, xcoordinate, ycoordinate, responsetime. + // SELECT query should then return one row per userid per phase/scene. foreach ($_SESSION['results'] as $phasename => $phasevalue) { foreach ($phasevalue as $measure => $measurevalue) { //Building an INSERT query: @@ -127,12 +133,9 @@ switch ($mode) { $db->query($sql) or die('Could not execute query:
'.mysqli_error($db)); } } - - - //Debugging I think? - $variables['debug'] = $_SESSION['results']; - + //Debugging + $variables['debug'] = $_SESSION['results']; break; case 'results': @@ -155,11 +158,15 @@ switch ($mode) { $template = 'results.html.twig'; } + // Only shows up when debug=true in settings.json. if ($filtered) { $variables['filtered'] = true; $results = $db->query("select * from vcd_results where result_host != '192.168.0.1' order by result_date asc"); } + else { +//This query needs to be rewritten to use the results table (EAV version), +// but hopefully format the data in the same way (in the query itself, if possible?) $results = $db->query("select * from vcd_results order by result_date asc"); } From 8d064d69229973093b2f44bc6d40058f78957726 Mon Sep 17 00:00:00 2001 From: mattbk Date: Fri, 30 Oct 2015 17:02:14 -0500 Subject: [PATCH 6/8] Table structure change Saving this while it works. --- index.php | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/index.php b/index.php index 4406269..86c670f 100755 --- a/index.php +++ b/index.php @@ -100,38 +100,35 @@ switch ($mode) { //Store the result. //Loop through $results better //This will allow running only one query rather than several: http://stackoverflow.com/a/10054657/2152245 - //However, if you want to define the number of phases/scenes in settings.json only (and not in a db setup script), - // you have to use an EAV table here with fields like userid, phase, measure [xccordinate, ycoordinate, or responsetime], value [value for measure] at minimum. - // then need to rewrite the query to pull them out of the db correctly, since each userid will have one row per measure per phase. - //Criticism of EAV: https://www.simple-talk.com/sql/database-administration/five-simple--database-design-errors-you-should-avoid/, - // - //Below needs to be rewritten; because I have `phase` as a field, I can also have `xcoordinate`, `ycoordinate`, and `responsetime` + //Because I have `phase` as a field, I can also have `xcoordinate`, `ycoordinate`, and `responsetime` // as values as well. Table should have a field list of: uid, datetime, host, userid, phase, xcoordinate, ycoordinate, responsetime. - // SELECT query should then return one row per userid per phase/scene. + // SELECT query should then return one row per userid per phase/scene. foreach ($_SESSION['results'] as $phasename => $phasevalue) { + $ready = array(); + $ready[] = $phasename; foreach ($phasevalue as $measure => $measurevalue) { - //Building an INSERT query: - //Include userid (once collection form is added into the start page) - //Include a timestamp that actually works + //Put results in the right order for database + array_push($ready, $measurevalue); + } + //Building an INSERT query: + //Include userid (once collection form is added into the start page) + // DB fields: uid, date, host, phase, measure, value + // http://php.net/manual/en/function.implode.php + $columnstoimplode = array("uid", "datetime", "host", "phase", "xcoordinate", "ycoordinate","responsetime"); + // Note that backticks (`) go around field names... + $columns = "`".implode("`, `", $columnstoimplode)."`"; + // Set up timestamp so you can tell participants apart. http://alvinalexander.com/php/php-date-formatted-sql-timestamp-insert + $timestamp = date('Y-m-d G:i:s'); + $valuestoimplode = array("", $timestamp, $_SERVER['REMOTE_ADDR']); + $valuestoimplode = array_merge($valuestoimplode,$ready); + $values = "'".implode("', '", $valuestoimplode)."'"; + print_r($values); + // Build and execute query + $sql = "INSERT INTO results ("; + $sql .= $columns; + $sql .= ") VALUES ($values)"; + $db->query($sql) or die('Could not execute query:
'.mysqli_error($db)); - //Test output on the screen - //echo ""."".$_SERVER['REMOTE_ADDR'].$phasename.$measure.$measurevalue."
"; - - // DB fields: uid, date, host, phase, measure, value - // http://php.net/manual/en/function.implode.php - $columnstoimplode = array("uid", "datetime", "host", "phase", "measure", "value"); - // Note that backticks (`) go around field names... - $columns = "`".implode("`, `", $columnstoimplode)."`"; - // Set up timestamp so you can tell participants apart. http://alvinalexander.com/php/php-date-formatted-sql-timestamp-insert - $timestamp = date('Y-m-d G:i:s'); - $valuestoimplode = array("", $timestamp, $_SERVER['REMOTE_ADDR'], $phasename, $measure, $measurevalue); - $values = "'".implode("', '", $valuestoimplode)."'"; - // Build and execute query - $sql = "INSERT INTO results ("; - $sql .= $columns; - $sql .= ") VALUES ($values)"; - $db->query($sql) or die('Could not execute query:
'.mysqli_error($db)); - } } //Debugging From 75f0e29bbaeb9793e5fcc68f75bf8bdf05911fc7 Mon Sep 17 00:00:00 2001 From: mattbk Date: Sun, 1 Nov 2015 08:52:46 -0600 Subject: [PATCH 7/8] Score is now recorded in database Saving this so I can move forward. --- index.php | 96 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/index.php b/index.php index 86c670f..c9c0ad5 100755 --- a/index.php +++ b/index.php @@ -104,17 +104,33 @@ switch ($mode) { // as values as well. Table should have a field list of: uid, datetime, host, userid, phase, xcoordinate, ycoordinate, responsetime. // SELECT query should then return one row per userid per phase/scene. foreach ($_SESSION['results'] as $phasename => $phasevalue) { + //Initialize arrays $ready = array(); + //First piece of $ready is the phase/scene name $ready[] = $phasename; + //Loop through xccordinate, ycoordinate, responsetime foreach ($phasevalue as $measure => $measurevalue) { //Put results in the right order for database array_push($ready, $measurevalue); } + + //Calculate the score. Compare results with targets in settings.json. + if (($phasevalue['xcoordinate'] >= $settings->elementLocations->{$phasename }->topleft->x) + && ($phasevalue['xcoordinate'] < $settings->elementLocations->{$phasename}->bottomright->x) + && ($phasevalue['ycoordinate'] >= $settings->elementLocations->{$phasename}->topleft->y) + && ($phasevalue['ycoordinate'] < $settings->elementLocations->{$phasename}->bottomright->y)) { + //Put the correct score in the ready array + array_push($ready, '1'); + } + else { + //Put the incorrect score in the ready array + array_push($ready, '0'); + } + //Building an INSERT query: //Include userid (once collection form is added into the start page) - // DB fields: uid, date, host, phase, measure, value - // http://php.net/manual/en/function.implode.php - $columnstoimplode = array("uid", "datetime", "host", "phase", "xcoordinate", "ycoordinate","responsetime"); + // DB fields are listed here: + $columnstoimplode = array("uid", "datetime", "host", "phase", "xcoordinate", "ycoordinate","responsetime","score"); // Note that backticks (`) go around field names... $columns = "`".implode("`, `", $columnstoimplode)."`"; // Set up timestamp so you can tell participants apart. http://alvinalexander.com/php/php-date-formatted-sql-timestamp-insert @@ -122,7 +138,7 @@ switch ($mode) { $valuestoimplode = array("", $timestamp, $_SERVER['REMOTE_ADDR']); $valuestoimplode = array_merge($valuestoimplode,$ready); $values = "'".implode("', '", $valuestoimplode)."'"; - print_r($values); + //print_r($values); // Build and execute query $sql = "INSERT INTO results ("; $sql .= $columns; @@ -131,13 +147,13 @@ switch ($mode) { } - //Debugging $variables['debug'] = $_SESSION['results']; break; case 'results': $download = isset($_GET['download']); +// Check if filtering by local IP should be enabled. if ($settings->debug) { $variables['allow_filtering'] = true; $filtered = isset($_GET['filtered']) || $download; @@ -155,25 +171,25 @@ switch ($mode) { $template = 'results.html.twig'; } - // Only shows up when debug=true in settings.json. if ($filtered) { $variables['filtered'] = true; - $results = $db->query("select * from vcd_results where result_host != '192.168.0.1' order by result_date asc"); + // $results = $db->query("select * from vcd_results where result_host != '192.168.0.1' order by result_date asc"); + $results = $db->query("select * from results where host != '192.168.0.1' order by datetime asc"); } else { //This query needs to be rewritten to use the results table (EAV version), // but hopefully format the data in the same way (in the query itself, if possible?) - $results = $db->query("select * from vcd_results order by result_date asc"); + //$results = $db->query("select * from vcd_results order by result_date asc"); + $results = $db->query("select * from results order by datetime asc"); } - + if ($results === false) { header('HTTP/1.0 500 Internal Server Error'); exit; - } + } $variables['records'] = $results->num_rows; - $variables['data'] = array(); $variables['stats'] = array(); @@ -181,29 +197,47 @@ switch ($mode) { $data = array(); $stats = array(); - // Prepare data - foreach ($record as $name => $value) { - list($cat, $key) = explode('_', $name); - $data[$cat][$key] = $value; - } - $variables['data'][] = $data; + //Skip empty results (created during debugging) + if ($record['responsetime'] > 0) { + //Put your results in one array + $variables['data'][] = $record; + } + //here - // Build statistics - foreach ($settings->phases as $phase) { - $stats[$phase] = array( - 'correct' => (($data[$phase]['xcoordinate'] >= $settings->elementLocations->{$phase}->topleft->x) - && ($data[$phase]['xcoordinate'] <= $settings->elementLocations->{$phase}->bottomright->x) - && ($data[$phase]['ycoordinate'] >= $settings->elementLocations->{$phase}->topleft->y) - && ($data[$phase]['ycoordinate'] <= $settings->elementLocations->{$phase}->bottomright->y)), - 'time' => $data[$phase]['responsetime'] - ); - } - $variables['stats'][] = $stats; + + + //Skip empty results (created during debugging) + if ($record['responsetime'] > 0) { + foreach ($record as $name => $value) { + + } } - $results->free(); - break; -} + +} //End while to loop through results from database. + + echo "
";
+	   print_r($variables['data']);
+    echo "
"; + +// Build statistics +//Stats are calculated before results are put into the database +// foreach ($variables['data'] as $key=>$value) { +// //print_r($value['phase']); +// $stats[] = array( +// 'correct' => (($value['xcoordinate'] >= $settings->elementLocations->{$value['phase'] }->topleft->x) +// && ($value['xcoordinate'] < $settings->elementLocations->{$value['phase']}->bottomright->x) +// && ($value['ycoordinate'] >= $settings->elementLocations->{$value['phase']}->topleft->y) +// && ($value['ycoordinate'] < $settings->elementLocations->{$value['phase']}->bottomright->y)), +// 'time' => $value['responsetime'] +// ); +// } +// $variables['stats'] = $stats; + + $results->free(); + break; +} //End case to select page. + if (!empty ($template)) { echo $twig->render($template, $variables); From 3c0cb388e64834c7476d2734d046c6f2fc646606 Mon Sep 17 00:00:00 2001 From: mattbk Date: Sun, 1 Nov 2015 09:34:49 -0600 Subject: [PATCH 8/8] Raw results are working and downloadable --- index.php | 28 ++----------------- views/results.html.twig | 61 ++++++++++++++++++++--------------------- views/results.txt.twig | 7 ++--- 3 files changed, 36 insertions(+), 60 deletions(-) diff --git a/index.php b/index.php index c9c0ad5..7087e93 100755 --- a/index.php +++ b/index.php @@ -164,7 +164,7 @@ switch ($mode) { if ($download) { header('Content-Type: text/tab-separated-values'); - header('Content-Disposition: attachment; filename=vcd-results.txt'); + header('Content-Disposition: attachment; filename="results.txt"'); $template = 'results.txt.twig'; } else { @@ -173,16 +173,11 @@ switch ($mode) { if ($filtered) { $variables['filtered'] = true; - // $results = $db->query("select * from vcd_results where result_host != '192.168.0.1' order by result_date asc"); $results = $db->query("select * from results where host != '192.168.0.1' order by datetime asc"); - } - + } else { -//This query needs to be rewritten to use the results table (EAV version), -// but hopefully format the data in the same way (in the query itself, if possible?) - //$results = $db->query("select * from vcd_results order by result_date asc"); $results = $db->query("select * from results order by datetime asc"); - } + } if ($results === false) { header('HTTP/1.0 500 Internal Server Error'); @@ -216,23 +211,6 @@ switch ($mode) { } //End while to loop through results from database. - echo "
";
-	   print_r($variables['data']);
-    echo "
"; - -// Build statistics -//Stats are calculated before results are put into the database -// foreach ($variables['data'] as $key=>$value) { -// //print_r($value['phase']); -// $stats[] = array( -// 'correct' => (($value['xcoordinate'] >= $settings->elementLocations->{$value['phase'] }->topleft->x) -// && ($value['xcoordinate'] < $settings->elementLocations->{$value['phase']}->bottomright->x) -// && ($value['ycoordinate'] >= $settings->elementLocations->{$value['phase']}->topleft->y) -// && ($value['ycoordinate'] < $settings->elementLocations->{$value['phase']}->bottomright->y)), -// 'time' => $value['responsetime'] -// ); -// } -// $variables['stats'] = $stats; $results->free(); break; diff --git a/views/results.html.twig b/views/results.html.twig index dc42f33..e80de42 100644 --- a/views/results.html.twig +++ b/views/results.html.twig @@ -4,7 +4,9 @@ + {% if allow_filtering %}
Filtered results @@ -12,6 +14,7 @@
{% endif %} + + -->

Raw results

-

Download results

+

Download results

+ + + - - - - - - - - - - - - - + + + + + {% for record in data %} - - - - - - - - - - + + + + + + + + {% endfor %}
#NotableUnnotedRenoted
xytimexytimeuiddatehostphase x y timescore
{{ loop.index }}.{{ record.notable.xcoordinate }}{{ record.notable.ycoordinate }}{{ record.notable.responsetime }}{{ record.unnoted.xcoordinate }}{{ record.unnoted.ycoordinate }}{{ record.unnoted.responsetime }}{{ record.renoted.xcoordinate }}{{ record.renoted.ycoordinate }}{{ record.renoted.responsetime }}{{record.uid}}.{{ record.datetime }}{{ record.host }}{{ record.phase }}{{ record.xcoordinate }}{{ record.ycoordinate }}{{ record.responsetime }}{{ record.score }}
+ {% endblock %} \ No newline at end of file diff --git a/views/results.txt.twig b/views/results.txt.twig index 8cbaae2..a374d69 100644 --- a/views/results.txt.twig +++ b/views/results.txt.twig @@ -1,5 +1,4 @@ -notable unnoted renoted -x y time x y time x y time -{% for result in data %} -{{result.notable.xcoordinate}} {{result.notable.ycoordinate}} {{result.notable.responsetime}} {{result.unnoted.xcoordinate}} {{result.unnoted.ycoordinate}} {{result.unnoted.responsetime}} {{result.renoted.xcoordinate}} {{result.renoted.ycoordinate}} {{result.renoted.responsetime}} +uid datetime host phase xcoordinate ycoordinate responsetime score +{% for record in data %} +{{record.uid}} {{record.datetime}} {{record.host}} {{record.phase}} {{record.xcoordinate}} {{record.ycoordinate}} {{record.responsetime}} {{record.score}} {% endfor %}