PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/list_view.php

http://ditag.googlecode.com/
PHP | 425 lines | 294 code | 112 blank | 19 comment | 51 complexity | 1b0b039abf3e8ca1b6ba6b2fd770b3ce MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. session_start();
  3. //print_r($_SESSION);
  4. include_once("db_login.php");
  5. $cnx_status = connect_to_db();
  6. if (!$cnx_status) {
  7. die("Could not connect to database: " . mysql_error());
  8. }
  9. //echo "AUTH IS " . $_SESSION["auth"] . "<br />";
  10. if (!$_SESSION['auth']) {
  11. echo "Please <a href=\"login_form.php\">log in</a> to the MoDD website.";
  12. echo "</body></html>";
  13. exit;
  14. }
  15. // need to figure out WHO is doing something
  16. // and WHAT they are doing
  17. //
  18. // then set variables for this to refer to later.
  19. $my_uid = $_SESSION["uid"];
  20. if (isset($_POST['user_submit'])) {
  21. $view_uid = $_POST['user_list'];
  22. if (strcmp($view_uid,"none") != 0){
  23. $_SESSION["view_uid"] = $view_uid;
  24. }
  25. else {
  26. unset($_SESSION["view_uid"]);
  27. }
  28. }
  29. $self = $_SERVER['PHP_SELF'];
  30. $who = NULL;
  31. $what = NULL;
  32. $ADMIN = "admin";
  33. $USER = "user";
  34. $PROV = "provider";
  35. $VIEW = "view";
  36. $SAVE = "save";
  37. $EDIT = "edit";
  38. $the_username = $_SESSION["username"];
  39. // by default, you're in a 'view' state
  40. $what = $VIEW;
  41. $who = getPermissions($my_uid);
  42. if ($_POST['edit']) {
  43. $what = $EDIT;
  44. }
  45. else if ($_POST['save']) {
  46. $what = $VIEW;
  47. }
  48. if ( strcmp($what,$VIEW)==0 ) {
  49. if (strcmp($who,$USER) == 0) {
  50. saveUserComments();
  51. } else if (strcmp($who,$PROV)==0) {
  52. saveProvComments();
  53. }
  54. }
  55. render_header();
  56. render_start();
  57. if ( strcmp($who,$ADMIN)==0 ) {
  58. render_stats();
  59. }
  60. if ( (strcmp($who,$PROV)==0) or (strcmp($who,$ADMIN)==0) ) {
  61. render_user_list();
  62. if (isset($_SESSION['view_uid'])) {
  63. showSamplesFor($_SESSION["view_uid"]);
  64. }
  65. }
  66. else if (strcmp($who, $USER)==0) {
  67. showSamplesFor($_SESSION["uid"]);
  68. }
  69. function showSamplesFor($show_uid) {
  70. global $who, $what, $PROV, $USER, $ADMIN, $VIEW, $SAVE, $EDIT;
  71. $list_components = array();
  72. $select = ' SELECT ';
  73. $from = ' FROM ';
  74. $query = $select . " sample_id,time,glucose,user_comment,cp_comment " . $from . " `sample` " .
  75. " WHERE " . " (`sample`.user_id=$show_uid) ORDER BY time";
  76. $sample_ids = mysql_query($query);
  77. while ($result_row = mysql_fetch_row($sample_ids)) {
  78. $sample_id = $result_row[0];
  79. $list_components[$sample_id] = array();
  80. $list_components[$sample_id]["time"] = $result_row[1];
  81. $list_components[$sample_id]["glucose"] = $result_row[2];
  82. $list_components[$sample_id]["user_comment"] = $result_row[3];
  83. $list_components[$sample_id]["cp_comment"] = $result_row[4];
  84. //get all the audio files
  85. $audio_query = $select . " location,audio_id " . $from . " `audio` " . " WHERE " .
  86. " (`audio`.sample_id=$sample_id) ";
  87. $audio_locs = mysql_query($audio_query);
  88. $audio_files = array();
  89. while ($res_row_2 = mysql_fetch_array($audio_locs, MYSQL_ASSOC)) {
  90. //echo "Got for item location:" . $res_row_2[0] . " & length:" . $res_row_2[1] ."<br />";
  91. array_push($audio_files, array($res_row_2["location"], $res_row_2["audio_id"]));
  92. }
  93. $list_components["$sample_id"]["audio"] = $audio_files;
  94. // get all the image files
  95. $image_query = $select . " location " . $from . " `image` " . " WHERE " .
  96. " (`image`.sample_id=$sample_id) ";
  97. $image_locs = mysql_query($image_query);
  98. $image_files = array();
  99. while ($res_row_2 = mysql_fetch_array($image_locs, MYSQL_ASSOC)) {
  100. //echo "Got for item location:" . $res_row_2[0] . " & length:" . $res_row_2[1] ."<br />";
  101. array_push($image_files, $res_row_2["location"]);
  102. }
  103. $list_components["$sample_id"]["image"] = $image_files;
  104. }
  105. $name_query = "SELECT firstname, lastname FROM user WHERE user.user_id=$show_uid";
  106. $name_res = mysql_query($name_query);
  107. $name_data = mysql_fetch_array($name_res, MYSQL_ASSOC);
  108. echo ("<h3> Samples for $name_data[firstname] $name_data[lastname] </h3>\n");
  109. echo(
  110. '<table width="100%" border="1" align="center">
  111. <tr>
  112. <th width="5%" class="table_header" scope="col">Sample ID </th>
  113. <th width="10%" class="table_header" scope="col">Time</th>
  114. <th width="5%" class="table_header" scope="col">Glucose</th>
  115. <th width="20%" class="table_header" scope="col">Images</th>
  116. <th width="15%" class="table_header" scope="col">Audio</th>');
  117. if ( strcmp($what,$VIEW) == 0 ) {
  118. $button_name = "edit";
  119. $button_val = "Edit";
  120. }
  121. else if (strcmp($what, $EDIT) == 0) {
  122. $button_name = "save";
  123. $button_val = "Save";
  124. }
  125. /* The user comments column */
  126. if (strcmp($who, $USER) == 0) {
  127. echo("<th width='25%' class='table_header' scope='col'>" .
  128. "<form action=\" " . $_SERVER[PHP_SELF] . "\" method=\"POST\">\n" .
  129. "Comments <input type=\"submit\" name=$button_name value=$button_val>\n" .
  130. "</th>");
  131. }
  132. else {
  133. echo ("<th width='25%' class='table_header' scope='col'> Comments </th> \n");
  134. }
  135. /* end user comments column*/
  136. /* The provider comments column */
  137. if (strcmp($who, $PROV) == 0) {
  138. echo("<th width='25%' class='table_header' scope='col'>" .
  139. "<form action=\" " . $_SERVER[PHP_SELF] . "\" method=\"POST\">\n" .
  140. "Care Provider's Comments <input type=\"submit\" name=$button_name value=$button_val>\n" .
  141. "</th></tr>");
  142. }
  143. else {
  144. echo ("<th width='25%' class='table_header' scope='col'> Care Provider's Comments </th></tr> \n");
  145. }
  146. /* end provider comments column */
  147. $even_row = false;
  148. foreach ($list_components as $id => $data) {
  149. $row_tag = ($even_row) ? "class=\"even_row\"" : "class=\"odd_row\"";
  150. echo "<tr $row_tag>";
  151. echo "<td ALIGN=\"center\"><b>$id</b></td>";
  152. echo "<td>$data[time]</td>";
  153. echo "<td>$data[glucose]</td>";
  154. // show each image.
  155. echo "<td>";
  156. foreach ($data["image"] as $img) {
  157. echo "<img src=\"samples/sample_$id/image/$img\" style=\"padding:10px\"/><br />";
  158. }
  159. echo "</td>";
  160. echo "<td>";
  161. foreach ($data["audio"] as $aud) {
  162. $file_path = "samples/sample_$id/audio/";
  163. $audio_file_location = $aud[0];
  164. $audio_file_id = $aud[1];
  165. $file_parts = explode(".",$audio_file_location);
  166. if ( (file_exists($file_path.$file_parts[0].".mp3")) and
  167. (filesize($file_path.$file_parts[0].".mp3") > 0) ) {
  168. echo "<a href=\"scripts/get_file.php?sample_id=$id&file_type=mp3&file_id=$audio_file_id\">".
  169. "$file_parts[0].mp3</a><br />";
  170. } else {
  171. echo "<a href=\"scripts/get_file.php?sample_id=$id&file_type=amr&file_id=$audio_file_id\">".
  172. "$file_parts[0].mp3</a><br />";
  173. }
  174. }
  175. echo "</td>";
  176. echo "<td>";
  177. if ( (strcmp($what,$EDIT) == 0) && (strcmp($who,$USER) == 0) ) {
  178. echo "<textarea name=\"user_sample$id\" cols=\"30\" rows=\"10\">$data[user_comment]</textarea>";
  179. }
  180. else {
  181. echo "$data[user_comment]";
  182. }
  183. echo "</td>";
  184. echo "<td>";
  185. if ( (strcmp($what,$EDIT) == 0) && (strcmp($who,$PROV)==0) ){
  186. echo "<textarea name=\"cp_sample$id\" cols=\"30\" rows=\"10\">$data[cp_comment]</textarea>";
  187. }
  188. else {
  189. echo "$data[cp_comment]";
  190. }
  191. echo "</tr>";
  192. $even_row = !$even_row;
  193. } // end bracket for loop that goes through all the samples
  194. echo("</table>");
  195. echo "</form>";
  196. render_end();
  197. }
  198. function render_stats() {
  199. // TODO: ADD DELETE ACCOUNT BUTTON
  200. $user_stats_query = "SELECT user_id, username, firstname, lastname, last_log_time FROM user ".
  201. "WHERE permission=\"U\"";
  202. $user_stats_res = mysql_query($user_stats_query);
  203. echo("<h3> Registered Users </h3>");
  204. echo("<table BORDER=\"1\" WIDTH=\"75%\">\n");
  205. echo("<tr>\n");
  206. echo("<th>User ID</th>\n");
  207. echo("<th>Username</th>\n");
  208. echo("<th>First name</th>\n");
  209. echo("<th>Last name</th>\n");
  210. echo("<th>Last log</th>\n");
  211. echo("<th>Last sample</th>\n");
  212. echo("<th>Number samples</th>\n");
  213. echo("</tr>\n");
  214. while ($user_row = mysql_fetch_array($user_stats_res, MYSQL_ASSOC)) {
  215. $this_user_id = $user_row["user_id"];
  216. echo("<tr>\n");
  217. echo("<td>$this_user_id</td>\n");
  218. echo("<td>$user_row[username]</td>\n");
  219. echo("<td>$user_row[firstname]</td>\n");
  220. echo("<td>$user_row[lastname]</td>\n");
  221. echo("<td>$user_row[last_log_time]</td>\n");
  222. $last_sample_query = "SELECT time FROM sample WHERE user_id=$this_user_id ORDER BY time DESC";
  223. $last_sample_res = mysql_query($last_sample_query);
  224. $last_sample_data = mysql_fetch_array($last_sample_res);
  225. $last_sample = $last_sample_data[0];
  226. $num_samples_query = "SELECT COUNT(*) FROM sample WHERE user_id=$this_user_id";
  227. $num_samples_res = mysql_query($num_samples_query);
  228. $num_sample_data = mysql_fetch_array($num_samples_res);
  229. $num_samples = $num_sample_data[0];
  230. echo("<td>$last_sample</td>\n");
  231. echo("<td>$num_samples</td>\n");
  232. echo("</tr>");
  233. }
  234. echo("</table>");
  235. echo("<br /><HR WIDTH=\"100%\">");
  236. }
  237. function render_header() {
  238. echo (
  239. '<html xmlns="http://www.w3.org/1999/xhtml">
  240. <head>
  241. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  242. <title>My Samples</title>
  243. <link href="moddweb.css" rel="stylesheet" type="text/css">
  244. </head>');
  245. }
  246. function render_start() {
  247. $name = $_SESSION["name"];
  248. echo ('<body>');
  249. echo ('<table width="100%" border="0">');
  250. echo ("<tr><td WIDTH=\"80%\">&nbsp;</td>");
  251. echo ("<td ALIGN=right><a href=\"change_password.php\">change password</a></td>");
  252. echo ("<td ALIGN=right><a href=\"goodbye.php\">log out</a></td></tr>");
  253. echo ('<tr><td WIDTH="2%"><h2 class="nice_box"> Welcome, ' . $name . '!</h2></td></tr>');
  254. echo ("</table>");
  255. }
  256. function render_end() {
  257. echo ('</body></html>');
  258. }
  259. function render_user_list() {
  260. /* determine if you need to give admin the list of users records to view */
  261. $query_res = mysql_query("SELECT user_id,firstname,lastname,permission FROM user");
  262. $reg_users = array();
  263. while ($row = mysql_fetch_array($query_res, MYSQL_ASSOC)) {
  264. //echo $row["firstname"];
  265. if (strcmp($row["permission"],"U") == 0) {
  266. $reg_users[$row["user_id"]] = $row["firstname"] . " " . $row["lastname"];
  267. }
  268. }
  269. echo "<br /> <br />";
  270. echo('<form action="' . $self . '" method="POST">
  271. <label>Select a user to view: </label><br />
  272. <SELECT NAME="user_list">
  273. <option VALUE="none"></option>');
  274. foreach ($reg_users as $key => $value) {
  275. echo('<OPTION VALUE="' . $key . '">' . $value . "</option>");
  276. }
  277. echo ("</SELECT>
  278. <input type=\"submit\" name=\"user_submit\" value=\"View\">
  279. </form>");
  280. }
  281. function saveUserComments() {
  282. foreach ($_POST as $key => $value) {
  283. if (strpos($key, "user_sample") === 0) {
  284. $comment_id = substr($key,11);
  285. $comment_query = "UPDATE sample SET user_comment=\"$value\" WHERE sample_id=$comment_id";
  286. $res = mysql_query($comment_query);
  287. }
  288. }
  289. }
  290. function saveProvComments() {
  291. foreach ($_POST as $key => $value) {
  292. if (strpos($key, "cp_sample") === 0) {
  293. $comment_id = substr($key,9);
  294. $comment_query = "UPDATE sample SET cp_comment=\"$value\" WHERE sample_id=$comment_id";
  295. $res = mysql_query($comment_query);
  296. }
  297. }
  298. }
  299. function getPermissions($a_uid) {
  300. global $PROV, $ADMIN, $USER;
  301. $prmsn_query = "SELECT permission FROM user where user_id=$a_uid";
  302. $res = mysql_query($prmsn_query);
  303. $prmsn_array = mysql_fetch_array($res, MYSQL_ASSOC);
  304. switch ($prmsn_array['permission']) {
  305. case "P":
  306. return $PROV;
  307. case "A";
  308. return $ADMIN;
  309. case "U":
  310. return $USER;
  311. }
  312. }
  313. ?>