PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/website/profile.php

https://github.com/flagcapper/aichallenge
PHP | 322 lines | 296 code | 9 blank | 17 comment | 56 complexity | 8873bd2a227195b0cfab16067729c34a MD5 | raw file
  1. <?php
  2. // ini_set('error_reporting', E_ALL);
  3. // ini_set('display_errors', true);
  4. require_once('header.php');
  5. require_once('mysql_login.php');
  6. require_once('nice.php');
  7. if (isset($_GET["user"])) {
  8. $user_id = $_GET["user"];
  9. if(!filter_var($user_id, FILTER_VALIDATE_INT)) {
  10. $user_id = NULL;
  11. }
  12. }
  13. if (!isset($user_id)) {
  14. require_once('footer.php');
  15. die();
  16. }
  17. require_once('profile_submissions_widget.php');
  18. require_once('profile_games_widget.php');
  19. require_once('game_list.php');
  20. $rank = NULL;
  21. $skill = NULL;
  22. $userresult = contest_query("select_profile_user", $user_id);
  23. if ($userresult) {
  24. $userdata = mysql_fetch_assoc($userresult);
  25. if ($userdata['rank']) {
  26. $rank = nice_rank($userdata["rank"],
  27. $userdata["rank_change"]);
  28. $skill = nice_skill($userdata['skill'],
  29. $userdata['mu'],
  30. $userdata['sigma'],
  31. $userdata['skill_change'],
  32. $userdata['mu_change'],
  33. $userdata['sigma_change']);
  34. }
  35. }
  36. $rank = ($rank == NULL)?"Not Ranked":$rank;
  37. $skill = ($skill == NULL)?"No Skillz":$skill;
  38. $username = htmlentities($userdata["username"], ENT_COMPAT, 'UTF-8');
  39. $created = nice_date($userdata["created"]); // date("M jS Y",$userdata["created"]);
  40. $country_id = htmlentities($userdata["country_id"], ENT_COMPAT, 'UTF-8');
  41. $country_name = htmlentities($userdata["country_name"], ENT_COMPAT, 'UTF-8');
  42. $country_name = $country_name == NULL ?
  43. "Unknown" : htmlentities($country_name, ENT_COMPAT, 'UTF-8');
  44. $flag_filename = $userdata["flag_filename"];
  45. $flag_filename = $flag_filename == NULL ? "" : "<img alt=\"$country_name\" width=\"16\" height=\"11\" title=\"$country_name\" src=\"flags/$flag_filename\" />";
  46. $org_id = htmlentities($userdata["org_id"], ENT_COMPAT, 'UTF-8');
  47. $org_name = htmlentities($userdata["org_name"], ENT_COMPAT, 'UTF-8');
  48. $bio = str_replace("\n","<br />",str_replace("\r","", htmlentities($userdata["bio"], ENT_COMPAT, 'UTF-8')));
  49. if ($org_name == NULL) {
  50. $org_name = "None";
  51. }
  52. if (logged_in_with_valid_credentials() &&
  53. (current_user_id() == $user_id || logged_in_as_admin())) {
  54. $logged_in = true;
  55. $sid = session_id();
  56. $update_key = sha1(
  57. $sid . $userdata["activation_code"] . $userdata["email"]);
  58. } else {
  59. $logged_in = false;
  60. }
  61. if (!$userresult) {
  62. echo "<p>Invalid User ID</p>";
  63. } else {
  64. $title="Profile for ".$username;
  65. echo " <h2>Profile for $username</h2>";
  66. /*
  67. * Top Profile Info section ====================================================
  68. */
  69. echo "<div class=\"profile\">";
  70. if ($logged_in) {
  71. echo <<< EOT
  72. <script>
  73. function toggle_change_org() {
  74. if (document.getElementById('orgchange').style.display == 'none') {
  75. document.getElementById('orgchange').style.display='inline';
  76. document.getElementById('org_ctxt').innerHTML = 'Cancel';
  77. } else {
  78. document.getElementById('orgchange').style.display='none';
  79. document.getElementById('org_ctxt').innerHTML = 'Change';
  80. }
  81. }
  82. function toggle_change_country() {
  83. if (document.getElementById('countrychange').style.display == 'none') {
  84. document.getElementById('countrychange').style.display='inline';
  85. document.getElementById('country_ctxt').innerHTML = 'Cancel';
  86. } else {
  87. document.getElementById('countrychange').style.display='none';
  88. document.getElementById('country_ctxt').innerHTML = 'Change';
  89. }
  90. }
  91. function toggle_change_bio() {
  92. if (document.getElementById('bio_submit').style.display == 'none') {
  93. document.getElementById('bio_submit').style.display = 'inline';
  94. document.getElementById('bio_span').style.display = 'none';
  95. document.getElementById('bio_edit').style.display = 'inline';
  96. document.getElementById('bio_ctxt').innerHTML = 'Cancel';
  97. } else {
  98. document.getElementById('bio_submit').style.display = 'none';
  99. document.getElementById('bio_edit').style.display = 'none';
  100. document.getElementById('bio_span').style.display = 'inline';
  101. document.getElementById('bio_ctxt').innerHTML = 'Edit';
  102. }
  103. }
  104. </script>
  105. <form method="post" action="save_profile.php">
  106. <input type="hidden" name="update_key" value="$update_key" />
  107. EOT;
  108. }
  109. echo <<<EOT
  110. <p><strong>Country:</strong>&nbsp;
  111. <a href="country_profile.php?country=$country_id">$flag_filename
  112. $country_name</a>
  113. EOT;
  114. if ($logged_in) {
  115. echo <<<EOT
  116. <span style="padding-left: 1em; font-size: smaller">
  117. <a href="#" id="country_ctxt" onclick="toggle_change_country()">Change</a>
  118. <span id="countrychange" style="display: none">
  119. <select name="user_country" style="width:210px;">
  120. EOT;
  121. $query = "SELECT * FROM country ORDER BY country_id";
  122. $result = mysql_query($query);
  123. while ($row = mysql_fetch_assoc($result)) {
  124. $option_id = $row['country_id'];
  125. $option_name = $row['name'];
  126. if ($option_id == $country_id) {
  127. echo "<option selected value=$option_id>$option_name</option>";
  128. } else {
  129. echo "<option value=$option_id>$option_name</option>";
  130. }
  131. if ($option_id == 11) {
  132. echo "<option value=999>---</option>";
  133. }
  134. }
  135. echo <<<EOT
  136. </select><input type="submit" value="Save" />
  137. </span></span></p>
  138. EOT;
  139. }
  140. echo <<<EOT
  141. <p>
  142. <strong>Affiliation:</strong>&nbsp;
  143. <a href="organization_profile.php?org=$org_id">$org_name</a>
  144. EOT;
  145. if ($logged_in) {
  146. echo <<<EOT
  147. <span style="padding-left: 1em; font-size: smaller">
  148. <a href="#" id="org_ctxt" onclick="toggle_change_org()">Change</a>
  149. <span id="orgchange" style="display: none">
  150. <select name="user_organization" style="width:210px;">
  151. <option value="0">Other</option>
  152. <option value="1">University of Waterloo</option>
  153. <option value="999">---</option>
  154. EOT;
  155. $query = "SELECT * FROM organization WHERE org_id > 1 ORDER BY name";
  156. $result = mysql_query($query);
  157. while ($row = mysql_fetch_assoc($result)) {
  158. $option_id = $row['org_id'];
  159. $option_name = $row['name'];
  160. if ($option_id == $org_id) {
  161. echo "<option selected value=$option_id>$option_name</option>";
  162. } else {
  163. echo "<option value=$option_id>$option_name</option>";
  164. }
  165. }
  166. echo <<<EOT
  167. </select><input type="submit" value="Save" /></span></span>
  168. EOT;
  169. }
  170. echo <<<EOT
  171. </p>
  172. <p><strong>Joined:</strong>&nbsp;$created</p>
  173. EOT;
  174. if ($bio != NULL) {
  175. echo <<<EOT
  176. <p><strong>About Me:</strong><br />
  177. <span id="bio_span">$bio</span>
  178. EOT;
  179. if ($logged_in) {
  180. echo <<<EOT
  181. <textarea id="bio_edit" style="display: none" name="user_bio" cols="40" rows="3">$bio</textarea>
  182. <input id="bio_submit" style="display: none" type="submit" value="Save" />
  183. <span style="padding-left: 1em; font-size: smaller">
  184. <a href="#" id="bio_ctxt" onclick="toggle_change_bio()">Edit</a>
  185. </span>
  186. </p>
  187. <p>
  188. <a href="change_password.php">Change Password</a>
  189. </p>
  190. </form>
  191. EOT;
  192. } else {
  193. echo "</p>";
  194. }
  195. } elseif ($logged_in) {
  196. echo <<<EOT
  197. <p><strong>About Me:</strong><br />
  198. <span id="bio_span">You currently have no information entered.</span>
  199. <textarea id="bio_edit" style="display: none" name="user_bio" cols="40" rows="3"></textarea>
  200. <input id="bio_submit" style="display: none" type="submit" value="Save" />
  201. <span style="font-size: smaller">
  202. <a href="#" id="bio_ctxt" onclick="toggle_change_bio()">Edit</a>
  203. </span>
  204. </p>
  205. <p>
  206. <a href="change_password.php">Change Password</a>
  207. </p>
  208. </form>
  209. EOT;
  210. }
  211. echo "</div>";
  212. /*
  213. * Rank, Skill and Next Game section ====================================================
  214. */
  215. echo "<div class=\"rank\">";
  216. echo "<p><strong>Rank:</strong> <span class=\"stats\">$rank</span> <strong>Skill:</strong> <span class=\"stats\">$skill</span></p>";
  217. if ($logged_in && ($user_id == current_user_id() or logged_in_as_admin())) {
  218. $in_game_result = contest_query("select_in_game", $user_id);
  219. if ($in_game_result and mysql_num_rows($in_game_result) > 0) {
  220. echo "<p><strong>In Game:</strong> Playing in a game right now.</p>";
  221. } else {
  222. $next_game_result = contest_query("select_next_game_in", $user_id);
  223. if ($next_game_result) {
  224. while ($next_game_row = mysql_fetch_assoc($next_game_result)) {
  225. echo "<p><strong>Next Game:</strong> ".$next_game_row["players_ahead"]." players are ahead.<br />";
  226. echo "The current player rate is about ".$next_game_row["players_per_minute"]." players per minute.<br />";
  227. echo "The current game rate is about ".$next_game_row["games_per_minute"]." games per minute.<br />";
  228. if ($next_game_row["players_per_minute"] == 0) {
  229. echo "Next game could take awhile...";
  230. } else {
  231. echo "Next game should be within ".$next_game_row["next_game_in_adjusted"]." minutes.";
  232. }
  233. echo "<br />Page refreshed at ".
  234. nice_datetime("now") .".";
  235. echo "</p>";
  236. }
  237. } else {
  238. echo "<p><strong>Next Game:</strong> The current game rate is unavailable. :'(</p>";
  239. }
  240. }
  241. }
  242. echo "</div>";
  243. echo "<h3><span>Latest Games</span><div class=\"divider\" /></h3>";
  244. echo get_user_game_list($user_id, 0, True, 'profile_games.php');
  245. echo "<p></p>";
  246. /*
  247. * Submission activation / deactivation section
  248. */
  249. if (logged_in_with_valid_credentials() && (logged_in_as_admin() || current_user_id() == $user_id)) {
  250. $status_result = contest_query("select_submission_status", $user_id);
  251. if ($status_row = mysql_fetch_assoc($status_result)) {
  252. if ($status_row['status'] == 100 || $status_row['status'] == 40) {
  253. echo "<div class=\"activate\">";
  254. echo "<form method=\"post\" action=\"update_submission.php\">";
  255. if ($status_row['status'] == 100) {
  256. echo "<p>Your current submission was deactivated on ".$status_row['shutdown_date']." (".
  257. nice_ago($status_row['shutdown_date']).")</p>";
  258. }
  259. echo "<input type=\"hidden\" name=\"update_key\" value=\"$update_key\" />
  260. <input type=\"submit\" name=\"activate\" value=\"Activate\" />";
  261. if ($status_row['status'] == 40) {
  262. echo "<input type=\"submit\" name=\"deactivate\" value=\"Deactivate\" />";
  263. echo "<p>Your current submission will be deactivated on ".$status_row['shutdown_date']." (".
  264. nice_ago($status_row['shutdown_date']).")</p>";
  265. }
  266. echo "<p><em>Inactive submissions will not be chosen as a seed player for a new matchup, but may still be chosen as an opponent in a game.</em><p>";
  267. echo "</form>";
  268. echo "</div>";
  269. }
  270. }
  271. }
  272. echo "<h3><span>Recent Submissions</span><div class=\"divider\" /></h3>";
  273. echo getSubmissionTableString($user_id, true, 10, "profile_submissions.php?user=$user_id&page=1");
  274. }
  275. if (logged_in_with_valid_credentials() && logged_in_as_admin()) {
  276. $username = current_username();
  277. echo <<<EOT
  278. <form method="post" action="disable_account.php"
  279. onSubmit='return confirm("Are you sure you want do disable this account?")'>
  280. <h2>Disable Account:</h2>
  281. <p>Reason: <input name="reason" type="text" />&nbsp;by&nbsp;$username.</p>
  282. <input type="submit" value="Disable Account" />
  283. <input type="hidden" name="user_id" value="$user_id" />
  284. </form>
  285. EOT;
  286. }
  287. echo '
  288. <script>
  289. $(function () {
  290. $(".games").tablesorter({
  291. /*textExtraction: function (node) {
  292. node = $(node);
  293. if (node.attr("class") === "number") {
  294. var n = parseFloat(node.text());
  295. return isNaN(n) ? 0.0 : n;
  296. } else {
  297. return node.text();
  298. }
  299. }*/
  300. });
  301. });
  302. </script>
  303. ';
  304. include 'footer.php';
  305. ?>