Example #1
0
function PrintSQL($sql)
{
    ?>
<div style="border:1px solid #CCCCCC"><?php 
    echo getFormattedSQL($sql);
    echo "</div>";
}
Example #2
0
	function Search($s) {
		//print_r($s);
		
		/*
			***************** steps to searching *****************
			1) build the search string
			2) run the query
			3) depending on the query type, either...
				a) display the query, then end
				b) display the results
		*/
		
		/* --------- [1] get the SQL search string ---------- */
		$sqlstring = BuildSQLString($s);

		if ($sqlstring == "") { return; }
		
		/* escape all the variables and put them back into meaningful variable names */
		foreach ($s as $key => $value) {
			if (is_scalar($value)) { $$key = mysql_real_escape_string($s[$key]); }
			else { $$key = $s[$key]; }
		}
		
		/* make modality lower case to conform with table names... MySQL table names are case sensitive when using the 'show tables' command */
		$s_studymodality = strtolower($s_studymodality);
		
		/* ---------- [2] run the query ----------- */
		$starttime = microtime(true);
		$result = MySQLQuery($sqlstring, __FILE__, __LINE__);
		$querytime = microtime(true) - $starttime;
		
		if ($s_resultorder == "debug") {
			?>
			<span class="sublabel">Query returned <?php 
echo mysql_num_rows($result);
?>
 rows in <?php 
echo number_format($querytime, 4);
?>
 sec</span>
			<div style="background-color: #EEEEEE"><?php 
echo $sqlstring;
?>
</div><br>
			<div style="background-color: #EEEEEE"><?php 
echo getFormattedSQL($sqlstring);
?>
</div>
			<br><br><br><br>
			<?
			return;
		}

		/* display the results */
		if (mysql_num_rows($result) > 0) {
		
			if ((mysql_num_rows($result) > 100000) && ($s_resultorder != "pipelinecsv")) {
				?>
				<div style="border: 2px solid darkred; background-color: #FFEEEE; text-align: left; padding:5px; border-radius: 5px">
				<b>Your search returned <? echo number_format(mysql_num_rows($result),0); ?> results... which is a lot</b>
				<br>
				Try changing the search criteria to return fewer results or select a .csv format
				</div>
				<?
				return;
			}

			/* generate a color gradient in an array (green to yellow to red) */
			$colors = GenerateColorGradient();
			$colors2 = GenerateColorGradient2();
			
			/* display the number of rows and the search time */
			?>
			<span class="sublabel">Query returned <? echo number_format(mysql_num_rows($result),0); ?> rows in <?php 
echo number_format($querytime, 4);
?>
 sec</span>
			<details>
				<summary style="font-size:9pt">View SQL query:</summary>
				<div style="background-color: #EEEEEE; font-family:courier new; font-size:10pt"><?php 
echo getFormattedSQL($sqlstring);
?>
<br></div>
			</details>
			<style>
			#preview {
				position:absolute;
				border:1px solid #ccc;
				background:gray;
				padding:0px;
				display:none;
				color:#fff;
			}
			</style>
			<script type="text/javascript">
			// Popup window code
			function newPopup(url) {
				popupWindow = window.open(
					url,'popUpWindow','height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=no')
			}
			</script>
			<?
			
			/* ---------- pipeline results ------------ */
			if (($s_resultorder == "pipeline") || ($s_resultorder == "pipelinecsv")) {
				if ($s_pipelineresulttype == "i") {
					/* get the result names first (due to MySQL bug which prevents joining in this table in the main query) */
					$sqlstringX = "select * from analysis_resultnames where result_name like '%$s_pipelineresultname%' ";
					$resultX = MySQLQuery($sqlstringX,__FILE__,__LINE__);
					while ($rowX = mysql_fetch_array($resultX, MYSQL_ASSOC)) {
						$resultnames[$rowX['resultname_id']] = $rowX['result_name'];
					}
					/* and get the result unit (due to the same MySQL bug) */
					$sqlstringX = "select * from analysis_resultunit where result_unit like '%$s_pipelineresultunit%' ";
					$resultX = MySQLQuery($sqlstringX,__FILE__,__LINE__);
					while ($rowX = mysql_fetch_array($resultX, MYSQL_ASSOC)) {
						$resultunit[$rowX['resultunit_id']] = $rowX['result_unit'];
					}
					
					/* ---------------- pipeline results (images) --------------- */
					while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
						//PrintVariable($row,'row');
					
						$step = $row['analysis_step'];
						$pipelinename = $row['pipeline_name'];
						$uid = $row['uid'];
						$subject_id = $row['subject_id'];
						$gender = $row['gender'];
						$study_id = $row['study_id'];
						$study_num = $row['study_num'];
						$type = $row['result_type'];
						$size = $row['result_size'];
						//$name = $row['result_name'];
						$name = $resultnames[$row['result_nameid']];
						$unit = $resultunit[$row['result_unitid']];
						$filename = $row['result_filename'];
						$swversion = $row['result_softwareversion'];
						$important = $row['result_isimportant'];
						$lastupdate = $row['result_lastupdate'];
						
						switch($type) {
							case "v": $thevalue = $value; break;
							case "f": $thevalue = $filename; break;
							case "t": $thevalue = $text; break;
							case "h": $thevalue = $filename; break;
							case "i": $thevalue = $filename; break;
						}
						$tables["$uid$study_num"][$name] = $thevalue;
						$tables["$uid$study_num"]['subjectid'] = $subject_id;
						$tables["$uid$study_num"]['studyid'] = $study_id;
						$tables["$uid$study_num"]['studynum'] = $study_num;
						$names[$name] = "blah";
					}
					//PrintVariable($tables,'Tables');
					?>
					<table cellspacing="0" class="multicoltable">
						<thead>
						<tr>
							<th>UID</th>
							<?
							foreach ($names as $name => $blah) {
								?>
								<th align="center" style="font-size:9pt"><?php 
echo $name;
?>
</th>
								<?
							}
						?>
						</tr>
						</thead>
						<?
							$maximgwidth = 1200/count($names);
							$maximgwidth -= ($maximgwidth*0.05); /* subtract 5% of image width to give a gap between them */
							if ($maximgwidth < 100) { $maximgwidth = 100; }
							foreach ($tables as $uid => $valuepair) {
								?>
								<tr style="font-weight: <?php 
echo $bold;
?>
">
									<td><a href="studies.php?id=<?php 
echo $tables[$uid]['studyid'];
?>
"><b><?php 
echo $uid;
?>
</b></a></td>
									<?
									foreach ($names as $name => $blah) {
										if ($tables[$uid][$name] == "") { $dispval = "-"; }
										else { $dispval = $tables[$uid][$name]; }
										list($width, $height, $type, $attr) = getimagesize("/mount$filename");
										$filesize = number_format(filesize("/mount$filename")/1000) . " kB";
									?>
										<td style="padding:2px"><a href="preview.php?image=/mount<?php 
echo $dispval;
?>
" class="preview"><img src="preview.php?image=/mount<?php 
echo $dispval;
?>
" style="max-width: <?php 
echo $maximgwidth;
?>
px"></a></td>
										
									<?
									}
									?>
								</tr>
								<?
							}
						?>
					</table>
					<br><br><br><br><br><br><br><br>
					<?
				}
				else {
				/* ---------------- pipeline results (values) --------------- */
				/* get the result names first (due to MySQL bug which prevents joining in this table in the main query) */
				$sqlstringX = "select * from analysis_resultnames where result_name like '%$s_pipelineresultname%' ";
				$resultX = MySQLQuery($sqlstringX,__FILE__,__LINE__);
				while ($rowX = mysql_fetch_array($resultX, MYSQL_ASSOC)) {
					$resultnames[$rowX['resultname_id']] = $rowX['result_name'];
				}
				/* and get the result unit (due to the same MySQL bug) */
				$sqlstringX = "select * from analysis_resultunit where result_unit like '%$s_pipelineresultunit%' ";
				$resultX = MySQLQuery($sqlstringX,__FILE__,__LINE__);
				while ($rowX = mysql_fetch_array($resultX, MYSQL_ASSOC)) {
					$resultunit[$rowX['resultunit_id']] = $rowX['result_unit'];
				}

				/* load the data into a useful table */
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					
					$step = $row['analysis_step'];
					$pipelinename = $row['pipeline_name'];
					$uid = $row['uid'];
					$subject_id = $row['subject_id'];
					$study_id = $row['study_id'];
					$study_num = $row['study_num'];
					$birthdate = $row['birthdate'];
					$gender = $row['gender'];
					$study_datetime = $row['study_datetime'];
					$type = $row['result_type'];
					$size = $row['result_size'];
					$name = $resultnames[$row['result_nameid']];
					$name2 = $resultnames[$row['result_nameid']];
					$unit = $resultunit[$row['result_unitid']];
					$unit2 = $resultunit[$row['result_unitid']];
					$text = $row['result_text'];
					$value = $row['result_value'];
					$filename = $row['result_filename'];
					$swversion = $row['result_softwareversion'];
					$important = $row['result_isimportant'];
					$lastupdate = $row['result_lastupdate'];
					
					/* calculate age at scan */
					list($year, $month, $day) = explode("-", $birthdate);
					$d1 = mktime(0,0,0,$month,$day,$year);
					list($year, $month, $day, $extra) = explode("-", $study_datetime);
					$d2 = mktime(0,0,0,$month,$day,$year);
					$ageatscan = number_format((($d2-$d1)/31536000),1);					
					
					if (strpos($unit,'^') !== false) {
						$unit = str_replace('^','<sup>',$unit);
						$unit .= '</sup>';
					}
					
					switch($type) {
						case "v": $thevalue = $value; break;
						case "f": $thevalue = $filename; break;
						case "t": $thevalue = $text; break;
						case "h": $thevalue = $filename; break;
						case "i":
							?>
							<a href="preview.php?image=/mount<?php 
echo $filename;
?>
" class="preview"><img src="images/preview.gif" border="0"></a>
							<?
							break;
					}
					if (substr($name, -(strlen($unit))) != $unit) {
						$name .= " <b>$unit</b>";
						$name2 .= " " . $row['result_unit'];
					}
					$tables[$uid][$name] = $thevalue;
					$tables[$uid][$name2] = $thevalue;
					$tables[$uid]['age'] = $ageatscan;
					$tables[$uid]['gender'] = $gender;
					$tables[$uid]['subjectid'] = $subject_id;
					$tables[$uid]['studyid'] = $study_id;
					$tables[$uid]['studynum'] = $study_num;
					//$names[$name] = "blah";
					if (($thevalue > $names[$name]['max']) || ($names[$name]['max'] == "")) { $names[$name]['max'] = $thevalue; }
					if (($thevalue < $names[$name]['min']) || ($names[$name]['min'] == "")) { $names[$name]['min'] = $thevalue; }
					
					if (($thevalue > $names2[$name2]['max']) || ($names2[$name2]['max'] == "")) { $names2[$name2]['max'] = $thevalue; }
					if (($thevalue < $names2[$name2]['min']) || ($names2[$name2]['min'] == "")) { $names2[$name2]['min'] = $thevalue; }
				}

				if ($s_resultorder == "pipelinecsv") {
					$csv = "uid,studynum,sex,age";
					foreach ($names2 as $name2 => $blah) {
						$csv .= ",$name2";
					}
					$csv .= "\n";
					foreach ($tables as $uid => $valuepair) {
						$csv .= $uid . ',' . $tables[$uid]['studynum'] . ',' . $tables[$uid]['gender'] . ',' . $tables[$uid]['age'];
						foreach ($names2 as $name2 => $blah) {
							$csv .= ',' . $tables[$uid][$name2];
						}
						$csv .= "\n";
					}
					$filename = "query" . GenerateRandomString(10) . ".csv";
					file_put_contents("/tmp/" . $filename, $csv);
					?>
					<br><br>
					<div width="50%" align="center" style="background-color: #FAF8CC; padding: 5px;">
					Download .csv file <a href="download.php?type=file&filename=<?php 
echo "/tmp/{$filename}";
?>
"><img src="images/download16.png"></a>
					</div>
					<?
				}
				else {
				?>
					<br><br><br><br><br>
					<br><br><br><br><br>
					<br><br><br><br><br>
					<style>
						tr.rowhover:hover { background-color: ffff96; }
						td.tdhover:hover { background-color: yellow; }
					</style>
					<table cellspacing="0">
						<tr>
							<td>UID</td>
							<td>Sex</td>
							<td>Age</td>
							<?
							$csv = "studyid,sex,age";
							foreach ($names as $name => $blah) {
								$csv .= ",$name";
								?>
								<td style="max-width:25px;"><span style="padding-left: 8px; font-size:10pt; white-space:nowrap; display: block; -webkit-transform: rotate(-70deg) translate3d(0,0,0); -moz-transform: rotate(-70deg);"><?php 
echo $name;
?>
</span></td>
								<?
							}
							$csv .= "\n";
						?>
						</tr>
						<?
							foreach ($tables as $uid => $valuepair) {
								?>
								<tr style="font-weight: <?php 
echo $bold;
?>
" class="rowhover">
									<td>
									<a href="studies.php?id=<?php 
echo $tables[$uid]['studyid'];
?>
"><b><?php 
echo $uid;
?>
</b><?php 
echo $tables[$uid]['studynum'];
?>
</a>
									</td>
									<td style="border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:9pt; padding:2px;"><?php 
echo $tables[$uid]['gender'];
?>
</td>
									<td style="border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:9pt; padding:2px;"><?php 
echo $tables[$uid]['age'];
?>
</td>
									<?
									$stats[0][$tables[$uid]['gender']]++;
									$stats[1][] = $tables[$uid]['age'];
									$csv .= $tables[$uid]['studyid'] . ',' . $tables[$uid]['gender'] . ',' . $tables[$uid]['age'];
									$i=2;
									foreach ($names as $name => $blah) {
										$val = $tables[$uid][$name];
										$range = $names[$name]['max'] - $names[$name]['min'];
										if (($val > 0) && ($range > 0)) {
											$cindex = round((($val - $names[$name]['min'])/$range)*100);
											//echo "[$val, $range, $cindex]<br>";
											if ($cindex > 100) { $cindex = 100; }
										}
										
										if ($tables[$uid][$name] == "") {
											$dispval = "-";
										}
										else {
											$dispval = $tables[$uid][$name];
											$stats[$i][] = $val;
											//$stats[$i]['numintotal'] ++;
										}
										$csv .= ',' . $tables[$uid][$name];
										if ($dispval != '-') {
											if (($dispval + 0) > 10000) { $dispval = number_format($dispval,0); }
											elseif (($dispval + 0) > 1000) { $dispval = number_format($dispval,2); }
											else { $dispval = number_format($dispval,4); }
										}
									?>
										<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px; background-color: <? if ($s_pipelinecolorize) { if (trim($dispval) == '-') { echo "#EEE"; } else { echo $colors[$cindex]; } } ?>"><?php 
echo $dispval;
?>
</td>
									<?
										$i++;
									}
									$csv .= "\n";
									?>
								</tr>
								<?
							}
							if ($s_pipelineresultstats == 1) {
								?>
								<tr class="rowhover">
									<td align="right"><b>N</b></td>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;">
									<?
										foreach ($stats[0] as $key => $value) { echo "$key -> $value<br>"; }
									?>
									</td>
									<?
									for($i=1;$i<count($stats);$i++) {
										$count = count($stats[$i]);
										?><td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"><?php 
echo $count;
?>
</td><?
									}
									?>
								</tr>
								<tr class="rowhover">
									<td align="right"><b>Min</b></td>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"></td>
									<?
									for($i=1;$i<count($stats);$i++) {
										$min = min($stats[$i]);
										?><td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"><?php 
echo $min;
?>
</td><?
									}
									?>
								</tr>
								<tr class="rowhover">
									<td align="right"><b>Max</b></td>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"></td>
									<?
									for($i=1;$i<count($stats);$i++) {
										$max = max($stats[$i]);
										?><td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"><?php 
echo $max;
?>
</td><?
									}
									?>
								</tr>
								<tr class="rowhover">
									<td align="right"><b>Mean</b></td>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"></td>
									<?
									for($i=1;$i<count($stats);$i++) {
										$avg = number_format(array_sum($stats[$i])/count($stats[$i]),2);
										?><td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"><?php 
echo $avg;
?>
</td><?
									}
									?>
								</tr>
								<tr class="rowhover">
									<td align="right"><b>Median</b></td>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"></td>
									<?
									for($i=1;$i<count($stats);$i++) {
										$median = number_format(median($stats[$i]),2);
										?><td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"><?php 
echo $median;
?>
</td><?
									}
									?>
								</tr>
								<tr class="rowhover">
									<td align="right"><b>Std Dev</b></td>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"></td>
									<?
									for($i=1;$i<count($stats);$i++) {
										$stdev = number_format(sd($stats[$i]),2);
										?><td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px;"><?php 
echo $stdev;
?>
</td><?
									}
									?>
								</tr>
								<?
							}
						?>
					</table>
					<? if ($s_pipelinecormatrix == 1) { ?>
					<br><br><br><br>
					<br><br><br><br>
					<b>Correlation Matrix (r)</b><br>
					<?
						foreach ($names as $name => $blah) {
							foreach ($tables as $uid => $valuepair) {
								$lists['age'][] = $tables[$uid]['age'];
								
								/* this loop gets the data into an array */
								foreach ($names as $name => $blah) {
									$lists[$name][] = $tables[$uid][$name];
								}
								
							}
						}
					?>
					<table cellspacing="0">
						<tr>
							<td>&nbsp;</td>
							<? foreach ($lists as $label => $vals1) { ?>
							<td style="max-width:25px;"><span style="padding-left: 8px; font-size:10pt; white-space:nowrap; display: block; -webkit-transform: rotate(-70deg) translate3d(0,0,0); -moz-transform: rotate(-70deg);"><?php 
echo $label;
?>
</span></td>
							<? } ?>
						</tr>
						<?
							$kashi = new Kashi();
							foreach ($lists as $label => $vals1) {
								for ($i=0;$i<count($vals1);$i++) {
									if ($vals1[$i] == 0) { $vals1[$i] = 0.000001; }
								}
								?>
								<tr class="rowhover">
									<td align="right" style="font-size:10pt"><?php 
echo $label;
?>
</td>
								<?
								foreach ($lists as $label => $vals2) {
									$starttime1 = microtime(true);
									/* compare vals1 to vals2 */
									//$coeff = Correlation($vals1,$vals2);
									for ($i=0;$i<count($vals2);$i++) {
										if ($vals2[$i] == 0) { $vals2[$i] = 0.000001; }
									}
									$coeff = $kashi->cor($vals1,$vals2);
									$coefftime = microtime(true) - $starttime1;
									
									$cindex = round((($coeff - (-1))/2)*100);
									//echo "[$val, $range, $cindex]<br>";
									if ($cindex > 100) { $cindex = 100; }
									/* display correlation coefficient */
									?>
									<td class="tdhover" style="text-align: right; border-left: 1px solid #AAAAAA; border-top: 1px solid #AAAAAA; font-size:8pt; padding:2px; background-color: <?php 
echo $colors2[$cindex];
?>
"><?php 
echo number_format($coeff, 3);
?>
</td>
									<?
									flush();
								}
								?>
								</tr>
								<?
							}
						?>
					</table>
					<?
						}
					}
				}
			}
			elseif ($s_resultorder == 'subject') {
				/* display only subject data */
				SearchStudy($result);
			}
			elseif ($s_resultorder == 'uniquesubject') {
				/* display only unique subject data */
				SearchSubject($result);
			}
			elseif ($s_resultorder == 'long') {
				/* display longitudinal data */
				SearchLongitudinal($result);
			}
			else {
				SearchDefault($result, $s, $colors, $colors2);
			}
		}
		else {
			?>
			<span class="sublabel">Query returned <? echo number_format(mysql_num_rows($result),0); ?> rows in <?php 
echo number_format($querytime, 4);
?>
 sec</span>
			<details>
				<summary style="font-size:9pt">View SQL query:</summary>
				<div style="background-color: #EEEEEE; font-family:courier new; font-size:10pt"><?php 
echo getFormattedSQL($sqlstring);
?>
<br></div>
			</details>
			<br>
			<?
		}
	}