Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. <?php
  2. /*
  3. * Created 03-2016 by Marcel Minke (marcel.minke@limesurvey.org)
  4. *
  5. * This script is from within a survey using an Ajax call and gets data from DB to push it to a charting tool.
  6. */
  7.  
  8. // SETTINGS
  9. $surveytable = 'lime_old_survey_834857_20161123121133';
  10.  
  11. //if re-activated
  12. $tokentable = 'lime_old_tokens_834857_20161123121133';
  13.  
  14. //SETTINGS END
  15.  
  16.  
  17. //check if debug mode is enabled
  18. if(isset($_GET['debug']))
  19. {
  20. $debug = intval($_GET['debug']);
  21. }
  22. else
  23. {
  24. $debug = 0;
  25. }
  26. //check if panel details and to be used column name was passed
  27. if(!(isset($_GET['token']) AND $_GET['token'] != "" AND isset($_GET['panelid']) AND $_GET['panelid'] != "" AND isset($_GET['datacolumn']) and $_GET['datacolumn'] != ""))
  28. {
  29. //log error
  30. debug_to_console($debug, "Not all required parameters were passed for token ".$_GET['token']." Panel: ".$_GET['panelid']." - Column: ".$_GET['datacolumn'] );
  31. exit();
  32. }
  33.  
  34.  
  35.  
  36. //error reporting
  37. ini_set('display_errors',1);
  38. error_reporting(E_ALL & ~E_NOTICE);
  39.  
  40. /*
  41. echo "<pre>";
  42. print_r($dataarray);
  43. echo "</pre>";
  44. */
  45.  
  46. /*
  47. * ------------ START WORKFLOW --------------
  48. */
  49.  
  50. //basepath needs to be defined in order to include config file (there is a check for this!)
  51. define("BASEPATH", dirname(dirname(__FILE__)));
  52.  
  53. //push all config details (config.details returns them as array) into this array
  54. $config = require_once("application/config/config.php");
  55.  
  56.  
  57.  
  58. // DB CONNECTION DETAILS
  59. $username = $config['components']['db']['username'];
  60. $password = $config['components']['db']['password'];
  61. $dbprefix = $config['components']['db']['tablePrefix'];
  62. //the DB connection string looks like this:
  63. //'mysql:host=localhost;port=3306;dbname=etcvps88_devm;'
  64. //so we need to check for the last "=", add 1 and get the string without the ending ";" character:
  65. $database = substr($config['components']['db']['connectionString'],strrpos($config['components']['db']['connectionString'],"=")+1,-1);
  66.  
  67. //connect to DB
  68. $connect = mysqli_connect("localhost",$username,$password,$database) or die( "Unable connect to database '$database'");
  69.  
  70. //set charset to deal with special characters
  71. mysqli_set_charset($connect,'utf8');
  72.  
  73.  
  74. //sanitize passed data
  75. $panelid = sanitizeInputDB($_GET['panelid'], $connect);
  76. $datacolumn = sanitizeInputDB($_GET['datacolumn'], $connect);
  77. $token = sanitizeInputDB($_GET['token'], $connect);
  78.  
  79. //adjust panel information for query -> sometimes we need to check for > 1 value!
  80. $panelquery = "";
  81. if($panelid == "su" OR $panelid == "ne" OR $panelid == "fe" OR $panelid == "nu" OR $panelid == "anp" )
  82. {
  83. $panelquery = " (attribute_1='su' OR attribute_1='ne' OR attribute_1='fe' OR attribute_1='nu' OR attribute_1='anp') ";
  84. }
  85. elseif($panelid == "ga" OR $panelid == "r" OR $panelid == "di")
  86. {
  87. $panelquery = " (attribute_1='ga' OR attribute_1='r' OR attribute_1='di') ";
  88. }
  89. elseif($panelid == "par" OR $panelid == "pat")
  90. {
  91. $panelquery = " (attribute_1='par' OR attribute_1='pat') ";
  92. }
  93. else
  94. {
  95. $panelquery = " (attribute_1='$panelid') ";
  96. }
  97.  
  98. /*
  99. * Overview:
  100. * Panel 1 (neonatal panel) = ne, fe and s = " surgeons, nurses, neonatologists and fetal medics"
  101. * Panel 2 (non-neonatal panel) = nu pae and r = "paediatricians, dieticians and researchers"
  102. * Panel 3 (lay panel) = par and pat = "parents and people who were born with gastroschisis"
  103. */
  104.  
  105. //get data for current TOKEN
  106. $tokenquery = "SELECT $datacolumn FROM $surveytable
  107. WHERE token='$token' AND submitdate IS NOT NULL";
  108. $tokenresult = mysqli_query($connect, $tokenquery) or die("Couldn't query token data: <br/>$tokenquery<br />");
  109.  
  110. debug_to_console($debug, mysqli_real_escape_string($connect, $tokenquery));
  111.  
  112. //loop through data
  113. while($tokendata = mysqli_fetch_array($tokenresult,MYSQLI_NUM))
  114. {
  115. $tokenanswer = intval($tokendata[0]);
  116. }
  117.  
  118.  
  119. //get AVG data -> NOTE that responses have to be <=9 because 10=don't know option
  120. $avgquery = "SELECT $datacolumn FROM $surveytable, $tokentable
  121. WHERE $panelquery AND $datacolumn!='10' AND submitdate IS NOT NULL AND $surveytable.token = $tokentable.token ORDER BY $datacolumn ASC";
  122. $avgresult = mysqli_query($connect, $avgquery) or die("Couldn't query MEDIAN data: <br/>$avgquery<br />");
  123.  
  124. debug_to_console($debug, mysqli_real_escape_string($connect, $avgquery));
  125.  
  126. //loop through data
  127. while($avgdata = mysqli_fetch_array($avgresult,MYSQLI_NUM))
  128. {
  129. $medianarray[] = intval($avgdata[0]);
  130. }
  131. $median = calculate_median($medianarray);
  132.  
  133.  
  134.  
  135.  
  136. //get RESPONSES
  137. $query = "SELECT $datacolumn FROM $surveytable, $tokentable
  138. WHERE $panelquery AND submitdate IS NOT NULL AND $surveytable.token = $tokentable.token";
  139. $result = mysqli_query($connect, $query) or die("Couldn't query panel data: <br/>$query<br />");
  140.  
  141. debug_to_console($debug, mysqli_real_escape_string($connect, $query));
  142.  
  143. $dataarray = array();
  144.  
  145. //pre-populate with zero values
  146. for($i = 1; $i <= 9; $i++)
  147. {
  148. $dataarray[$i] = 0;
  149. }
  150.  
  151. //loop through data
  152. while($data = mysqli_fetch_array($result,MYSQLI_NUM))
  153. {
  154. //only scores from 1 to 9 are valid (10 is "don't know")
  155. if($data[0] < 10 && is_numeric($data[0]))
  156. {
  157. $dataarray[$data[0]]++;
  158. }
  159.  
  160. }
  161.  
  162. $count = count($dataarray);
  163. $total = array_sum($dataarray);
  164.  
  165.  
  166. //calculate percentage values
  167. for($i = 1; $i <= 9; $i++)
  168. {
  169. if($dataarray[$i] != 0 && $total != 0)
  170. {
  171. $dataarray[$i] = round(($dataarray[$i] / $total)*100,0);
  172. }
  173.  
  174. }
  175.  
  176. //add token data detail at the very first position
  177. $dataarray[0] = $tokenanswer;
  178.  
  179. //add median at last position
  180. $dataarray[10] = $median;
  181.  
  182. ksort($dataarray);
  183. debug_to_console($debug, "We have $count values which sum up to $total and have a median of $median.<br />For token $token answer $tokenanswer was given for question $datacolumn");
  184.  
  185. debug_to_console($debug, $dataarray);
  186.  
  187.  
  188. //free result set
  189. mysqli_free_result($result);
  190.  
  191. //close connection
  192. mysqli_close($connect);
  193.  
  194. /*
  195. echo "<pre>";
  196. print_r($dataarray);
  197. echo "</pre>";
  198. */
  199.  
  200. echo json_encode($dataarray);
  201.  
  202.  
  203.  
  204. function sanitizeInputDB($data, $connect)
  205. {
  206. if (!isset($data)) {
  207. return null;
  208. }
  209.  
  210. $data = trim($data);
  211. $data = stripslashes($data);
  212. $data = htmlspecialchars($data);
  213. $data = mysqli_real_escape_string($connect, $data);
  214.  
  215. return $data;
  216. }
  217.  
  218. function debug_to_console( $debug, $data ) {
  219.  
  220. if(isset($debug) && $debug==1)
  221. {
  222. if ( is_array( $data ) )
  223. $output = "<script>console.log( 'Debugging (Array): " . implode( ',', $data) . "' );</script>";
  224. else
  225. $output = "<script>console.log( 'Debugging: " . $data . "' );</script>";
  226.  
  227. echo $output;
  228. }
  229. }
  230.  
  231. function calculate_median($arr) {
  232. $count = count($arr); //total numbers in array
  233. $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
  234. if($count % 2) { // odd number, middle is the median
  235.  
  236. $median = $arr[$middleval];
  237. } else { // even number, calculate avg of 2 medians
  238. $low = $arr[$middleval];
  239. $high = $arr[$middleval+1];
  240. $median = (($low+$high)/2);
  241.  
  242. //round UP:
  243. $median = round($median, 0, PHP_ROUND_HALF_UP);
  244. }
  245. return $median;
  246. }
  247.  
  248. /*
  249. function debug($text, $_GET['db'])
  250. {
  251. if(isset($_GET['debug']) && intval($_GET['debug'])==1)
  252. {
  253. echo "<br />$text<br />";
  254. }
  255. }
  256. */
  257. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement