contest_inside.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. requirePHPLib('form');
  3. if (!validateUInt($_GET['id']) || !($contest = queryContest($_GET['id']))) {
  4. become404Page();
  5. }
  6. genMoreContestInfo($contest);
  7. if (!hasContestPermission(Auth::user(), $contest)) {
  8. if ($contest['cur_progress'] == CONTEST_NOT_STARTED) {
  9. header("Location: /contest/{$contest['id']}/register");
  10. die();
  11. } elseif ($contest['cur_progress'] == CONTEST_IN_PROGRESS) {
  12. if ($myUser == null || !hasRegistered(Auth::user(), $contest)) {
  13. header("Location: /contest/{$contest['id']}/register");
  14. die();
  15. }
  16. }
  17. }
  18. if (isset($_GET['tab'])) {
  19. $cur_tab = $_GET['tab'];
  20. } else {
  21. $cur_tab = 'dashboard';
  22. }
  23. $tabs_info = array(
  24. 'dashboard' => array(
  25. 'name' => UOJLocale::get('contests::contest dashboard'),
  26. 'url' => "/contest/{$contest['id']}"
  27. ),
  28. 'submissions' => array(
  29. 'name' => UOJLocale::get('contests::contest submissions'),
  30. 'url' => "/contest/{$contest['id']}/submissions"
  31. ),
  32. 'standings' => array(
  33. 'name' => UOJLocale::get('contests::contest standings'),
  34. 'url' => "/contest/{$contest['id']}/standings"
  35. )
  36. );
  37. if (hasContestPermission(Auth::user(), $contest)) {
  38. $tabs_info['backstage'] = array(
  39. 'name' => UOJLocale::get('contests::contest backstage'),
  40. 'url' => "/contest/{$contest['id']}/backstage"
  41. );
  42. }
  43. if (!isset($tabs_info[$cur_tab])) {
  44. become404Page();
  45. }
  46. if (isset($_POST['check_notice'])) {
  47. $result = DB::query("select * from contests_notice where contest_id = '${contest['id']}' order by time desc limit 10");
  48. $ch = array();
  49. $flag = false;
  50. try {
  51. while ($row = DB::fetch($result)) {
  52. if (new DateTime($row['time']) > new DateTime($_POST['last_time'])) {
  53. $ch[] = $row['title'].': '.$row['content'];
  54. }
  55. }
  56. } catch (Exception $e) {
  57. }
  58. global $myUser;
  59. $result=DB::query("select * from contests_asks where contest_id='${contest['id']}' and username='${myUser['username']}' order by reply_time desc limit 10");
  60. try {
  61. while ($row = DB::fetch($result)) {
  62. if (new DateTime($row['reply_time']) > new DateTime($_POST['last_time'])) {
  63. $ch[] = $row['question'].': '.$row['answer'];
  64. }
  65. }
  66. } catch (Exception $e) {
  67. }
  68. if ($ch) {
  69. die(json_encode(array('msg' => $ch, 'time' => UOJTime::$time_now_str)));
  70. } else {
  71. die(json_encode(array('time' => UOJTime::$time_now_str)));
  72. }
  73. }
  74. if (isSuperUser($myUser)) {
  75. if (CONTEST_PENDING_FINAL_TEST <= $contest['cur_progress'] && $contest['cur_progress'] <= CONTEST_TESTING) {
  76. $start_test_form = new UOJForm('start_test');
  77. $start_test_form->handle = function() {
  78. global $contest;
  79. $result = DB::query("select id, problem_id, content from submissions where contest_id = {$contest['id']}");
  80. while ($submission = DB::fetch($result, MYSQLI_ASSOC)) {
  81. if (!isset($contest['extra_config']["problem_{$submission['problem_id']}"])) {
  82. $content = json_decode($submission['content'], true);
  83. if (isset($content['final_test_config'])) {
  84. $content['config'] = $content['final_test_config'];
  85. unset($content['final_test_config']);
  86. }
  87. if (isset($content['first_test_config'])) {
  88. unset($content['first_test_config']);
  89. }
  90. $esc_content = DB::escape(json_encode($content));
  91. DB::update("update submissions set judge_time = NULL, result = '', score = NULL, status = 'Waiting Rejudge', content = '$esc_content' where id = {$submission['id']}");
  92. }
  93. }
  94. DB::query("update contests set status = 'testing' where id = {$contest['id']}");
  95. };
  96. $start_test_form->submit_button_config['class_str'] = 'btn btn-danger btn-block';
  97. $start_test_form->submit_button_config['smart_confirm'] = '';
  98. $start_test_form->submit_button_config['text'] = '重测所有提交';
  99. $start_test_form->runAtServer();
  100. }
  101. if ($contest['cur_progress'] >= CONTEST_PENDING_FINAL_TEST) {
  102. $publish_result_form = new UOJForm('publish_result');
  103. $publish_result_form->handle = function() {
  104. // time config
  105. set_time_limit(0);
  106. ignore_user_abort(true);
  107. global $contest;
  108. $contest_data = queryContestData($contest);
  109. calcStandings($contest, $contest_data, $score, $standings, true);
  110. $ratings = array();
  111. $rating_rds = array();
  112. $rating_vols = array();
  113. if (!isset($contest['extra_config']['unrated'])) {
  114. calcRatingGlicko2($standings, $ratings, $rating_rds, $rating_vols);
  115. error_log("RATING");
  116. error_log(print_r($standings, true));
  117. error_log(print_r($ratings, true));
  118. } else {
  119. for ($i = 0; $i < count($standings); $i++) {
  120. $ratings[$i] = $standings[$i][2][1];
  121. $rating_rds[$i] = $standings[$i][2][2];
  122. $rating_vols[$i] = $standings[$i][2][3];
  123. }
  124. }
  125. for ($i = 0; $i < count($standings); $i++) {
  126. $user = queryUser($standings[$i][2][0]);
  127. $change = $ratings[$i] - $user['rating'];
  128. $user_link = getUserLink($user['username']);
  129. if ($change != 0) {
  130. $tail = '<strong style="color:red">' . ($change > 0 ? '+' : '') . $change . '</strong>';
  131. $content = <<<EOD
  132. <p>${user_link} 您好:</p>
  133. <p class="indent2">您在 <a href="/contest/{$contest['id']}">{$contest['name']}</a> 这场比赛后的Rating变化为${tail},当前Rating为 <strong style="color:red">{$ratings[$i]}</strong>。</p>
  134. EOD;
  135. } else {
  136. $content = <<<EOD
  137. <p>${user_link} 您好:</p>
  138. <p class="indent2">您在 <a href="/contest/{$contest['id']}">{$contest['name']}</a> 这场比赛后Rating没有变化。当前Rating为 <strong style="color:red">{$ratings[$i]}</strong>。</p>
  139. EOD;
  140. }
  141. sendSystemMsg($user['username'], 'Rating变化通知', $content);
  142. DB::update("update user_info set rating = {$ratings[$i]}, rating_rd = {$rating_rds[$i]}, rating_vol = {$rating_vols[$i]} where username = '{$standings[$i][2][0]}'");
  143. DB::query("update contests_registrants set rank = {$standings[$i][3]} where contest_id = {$contest['id']} and username = '{$standings[$i][2][0]}'");
  144. }
  145. DB::query("update contests set status = 'finished' where id = {$contest['id']}");
  146. };
  147. $publish_result_form->submit_button_config['class_str'] = 'btn btn-danger btn-block';
  148. $publish_result_form->submit_button_config['smart_confirm'] = '';
  149. $publish_result_form->submit_button_config['text'] = '公布成绩';
  150. $publish_result_form->runAtServer();
  151. }
  152. }
  153. if ($cur_tab == 'dashboard') {
  154. if ($contest['cur_progress'] <= CONTEST_IN_PROGRESS) {
  155. $post_question = new UOJForm('post_question');
  156. $post_question->addVTextArea('qcontent', '问题', '',
  157. function($content) {
  158. if (!Auth::check()) {
  159. return '您尚未登录';
  160. }
  161. if (!$content || strlen($content) == 0) {
  162. return '问题不能为空';
  163. }
  164. if (strlen($content) > 140 * 4) {
  165. return '问题太长';
  166. }
  167. return '';
  168. },
  169. null
  170. );
  171. $post_question->handle = function() {
  172. global $contest;
  173. $content = DB::escape($_POST['qcontent']);
  174. $username = Auth::id();
  175. DB::query("insert into contests_asks (contest_id, question, username, post_time, is_hidden) values ('{$contest['id']}', '$content', '$username', now(), 1)");
  176. };
  177. $post_question->runAtServer();
  178. } else {
  179. $post_question = null;
  180. }
  181. } elseif ($cur_tab == 'backstage') {
  182. if (isSuperUser(Auth::user())) {
  183. $post_notice = new UOJForm('post_notice');
  184. $post_notice->addInput('title', 'text', '标题', '',
  185. function($title) {
  186. if (!$title) {
  187. return '标题不能为空';
  188. }
  189. return '';
  190. },
  191. null
  192. );
  193. $post_notice->addTextArea('content', '正文', '',
  194. function($content) {
  195. if (!$content) {
  196. return '公告不能为空';
  197. }
  198. return '';
  199. },
  200. null
  201. );
  202. $post_notice->handle = function() {
  203. global $contest;
  204. $title = DB::escape($_POST['title']);
  205. $content = DB::escape($_POST['content']);
  206. DB::insert("insert into contests_notice (contest_id, title, content, time) values ('{$contest['id']}', '$title', '$content', now())");
  207. };
  208. $post_notice->runAtServer();
  209. } else {
  210. $post_notice = null;
  211. }
  212. if (hasContestPermission(Auth::user(), $contest)) {
  213. $reply_question = new UOJForm('reply_question');
  214. $reply_question->addHidden('rid', '0',
  215. function($id) {
  216. global $contest;
  217. if (!validateUInt($id)) {
  218. return '无效ID';
  219. }
  220. $q = DB::selectFirst("select * from contests_asks where id = $id");
  221. if ($q['contest_id'] != $contest['id']) {
  222. return '无效ID';
  223. }
  224. return '';
  225. },
  226. null
  227. );
  228. $reply_question->addVSelect('rtype', [
  229. 'public' => '公开',
  230. 'private' => '非公开',
  231. 'statement' => '请仔细阅读题面(非公开)',
  232. 'no_comment' => '无可奉告(非公开)',
  233. 'no_play' => '请认真比赛(非公开)',
  234. ], '回复类型', 'private');
  235. $reply_question->addVTextArea('rcontent', '回复', '',
  236. function($content) {
  237. if (!Auth::check()) {
  238. return '您尚未登录';
  239. }
  240. switch ($_POST['rtype']) {
  241. case 'public':
  242. case 'private':
  243. if (strlen($content) == 0) {
  244. return '回复不能为空';
  245. }
  246. break;
  247. }
  248. return '';
  249. },
  250. null
  251. );
  252. $reply_question->handle = function() {
  253. global $contest;
  254. $content = DB::escape($_POST['rcontent']);
  255. $is_hidden = 1;
  256. switch ($_POST['rtype']) {
  257. case 'statement':
  258. $content = '请仔细阅读题面';
  259. break;
  260. case 'no_comment':
  261. $content = '无可奉告 ╮(╯▽╰)╭ ';
  262. break;
  263. case 'no_play':
  264. $content = '请认真比赛 ( ̄口 ̄)!!';
  265. break;
  266. case 'public':
  267. $is_hidden = 0;
  268. break;
  269. default:
  270. break;
  271. }
  272. DB::update("update contests_asks set answer = '$content', reply_time = now(), is_hidden = {$is_hidden} where id = {$_POST['rid']}");
  273. };
  274. $reply_question->runAtServer();
  275. } else {
  276. $reply_question = null;
  277. }
  278. }
  279. function echoDashboard() {
  280. global $contest, $post_notice, $post_question, $reply_question;
  281. $myname = Auth::id();
  282. $contest_problems = DB::selectAll("select contests_problems.problem_id, best_ac_submissions.submission_id from contests_problems left join best_ac_submissions on contests_problems.problem_id = best_ac_submissions.problem_id and submitter = '{$myname}' where contest_id = {$contest['id']} order by contests_problems.problem_id asc");
  283. for ($i = 0; $i < count($contest_problems); $i++) {
  284. $contest_problems[$i]['problem'] = queryProblemBrief($contest_problems[$i]['problem_id']);
  285. }
  286. $contest_notice = DB::selectAll("select * from contests_notice where contest_id = {$contest['id']} order by time desc");
  287. if (Auth::check()) {
  288. $my_questions = DB::selectAll("select * from contests_asks where contest_id = {$contest['id']} and username = '{$myname}' order by post_time desc");
  289. $my_questions_pag = new Paginator([
  290. 'data' => $my_questions
  291. ]);
  292. } else {
  293. $my_questions_pag = null;
  294. }
  295. $others_questions_pag = new Paginator([
  296. 'col_names' => array('*'),
  297. 'table_name' => 'contests_asks',
  298. 'cond' => "contest_id = {$contest['id']} and username != '{$myname}' and is_hidden = 0",
  299. 'tail' => 'order by reply_time desc',
  300. 'page_len' => 10
  301. ]);
  302. uojIncludeView('contest-dashboard', [
  303. 'contest' => $contest,
  304. 'contest_notice' => $contest_notice,
  305. 'contest_problems' => $contest_problems,
  306. 'post_question' => $post_question,
  307. 'my_questions_pag' => $my_questions_pag,
  308. 'others_questions_pag' => $others_questions_pag
  309. ]);
  310. }
  311. function echoBackstage() {
  312. global $contest, $post_notice, $reply_question;
  313. $questions_pag = new Paginator([
  314. 'col_names' => array('*'),
  315. 'table_name' => 'contests_asks',
  316. 'cond' => "contest_id = {$contest['id']}",
  317. 'tail' => 'order by post_time desc',
  318. 'page_len' => 50
  319. ]);
  320. if ($contest['cur_progress'] < CONTEST_TESTING) {
  321. $contest_data = queryContestData($contest, ['pre_final' => true]);
  322. calcStandings($contest, $contest_data, $score, $standings);
  323. $standings_data = [
  324. 'contest' => $contest,
  325. 'standings' => $standings,
  326. 'score' => $score,
  327. 'contest_data' => $contest_data
  328. ];
  329. } else {
  330. $standings_data = null;
  331. }
  332. uojIncludeView('contest-backstage', [
  333. 'contest' => $contest,
  334. 'post_notice' => $post_notice,
  335. 'reply_question' => $reply_question,
  336. 'questions_pag' => $questions_pag,
  337. 'standings_data' => $standings_data
  338. ]);
  339. }
  340. function echoMySubmissions() {
  341. global $contest, $myUser;
  342. $show_all_submissions_status = Cookie::get('show_all_submissions') !== null ? 'checked="checked" ' : '';
  343. $show_all_submissions = UOJLocale::get('contests::show all submissions');
  344. echo <<<EOD
  345. <div class="checkbox text-right">
  346. <label for="input-show_all_submissions"><input type="checkbox" id="input-show_all_submissions" $show_all_submissions_status/> $show_all_submissions</label>
  347. </div>
  348. <script type="text/javascript">
  349. $('#input-show_all_submissions').click(function() {
  350. if (this.checked) {
  351. $.cookie('show_all_submissions', '');
  352. } else {
  353. $.removeCookie('show_all_submissions');
  354. }
  355. location.reload();
  356. });
  357. </script>
  358. EOD;
  359. if (Cookie::get('show_all_submissions') !== null) {
  360. echoSubmissionsList("contest_id = {$contest['id']}", 'order by id desc', array('judge_time_hidden' => ''), $myUser);
  361. } else {
  362. echoSubmissionsList("submitter = '{$myUser['username']}' and contest_id = {$contest['id']}", 'order by id desc', array('judge_time_hidden' => ''), $myUser);
  363. }
  364. }
  365. function echoStandings() {
  366. global $contest;
  367. $contest_data = queryContestData($contest);
  368. calcStandings($contest, $contest_data, $score, $standings);
  369. if ($contest['cur_progress'] >= CONTEST_FINISHED && hasContestPermission(Auth::user(), $contest)) {
  370. echo <<<EOD
  371. <div>
  372. <a class="btn btn-info" href="/contest/{$contest['id']}/export_standings">下载排名</a>
  373. </div>
  374. EOD;
  375. }
  376. uojIncludeView('contest-standings', [
  377. 'contest' => $contest,
  378. 'standings' => $standings,
  379. 'score' => $score,
  380. 'contest_data' => $contest_data
  381. ]);
  382. }
  383. function echoContestCountdown() {
  384. global $contest;
  385. $rest_second = $contest['end_time']->getTimestamp() - UOJTime::$time_now->getTimestamp();
  386. $time_str = UOJTime::$time_now_str;
  387. $contest_ends_in = UOJLocale::get('contests::contest ends in');
  388. echo <<<EOD
  389. <div class="card border-info">
  390. <div class="card-header bg-info">
  391. <h3 class="card-title">$contest_ends_in</h3>
  392. </div>
  393. <div class="card-body text-center countdown" data-rest="$rest_second"></div>
  394. </div>
  395. <script type="text/javascript">
  396. checkContestNotice({$contest['id']}, '$time_str');
  397. </script>
  398. EOD;
  399. }
  400. function echoContestJudgeProgress() {
  401. global $contest;
  402. if ($contest['cur_progress'] < CONTEST_TESTING) {
  403. $rop = 0;
  404. $title = UOJLocale::get('contests::contest pending final test');
  405. } else {
  406. $total = DB::selectCount("select count(*) from submissions where contest_id = {$contest['id']}");
  407. $n_judged = DB::selectCount("select count(*) from submissions where contest_id = {$contest['id']} and status = 'Judged'");
  408. $rop = $total == 0 ? 100 : (int)($n_judged / $total * 100);
  409. $title = UOJLocale::get('contests::contest final testing');
  410. }
  411. echo <<<EOD
  412. <div class="card border-info">
  413. <div class="card-header bg-info">
  414. <h3 class="card-title">$title</h3>
  415. </div>
  416. <div class="card-body">
  417. <div class="progress bot-buffer-no">
  418. <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="$rop" aria-valuemin="0" aria-valuemax="100" style="width: {$rop}%; min-width: 20px;">{$rop}%</div>
  419. </div>
  420. </div>
  421. </div>
  422. EOD;
  423. }
  424. function echoContestFinished() {
  425. $title = UOJLocale::get('contests::contest ended');
  426. echo <<<EOD
  427. <div class="card border-info">
  428. <div class="card-header bg-info">
  429. <h3 class="card-title">$title</h3>
  430. </div>
  431. </div>
  432. EOD;
  433. }
  434. $page_header = HTML::stripTags($contest['name']) . ' - ';
  435. ?>
  436. <?php echoUOJPageHeader(HTML::stripTags($contest['name']) . ' - ' . $tabs_info[$cur_tab]['name'] . ' - ' . UOJLocale::get('contests::contest')) ?>
  437. <div class="text-center">
  438. <h1><?= $contest['name'] ?></h1>
  439. <?= getClickZanBlock('C', $contest['id'], $contest['zan']) ?>
  440. </div>
  441. <div class="row">
  442. <?php if ($cur_tab == 'standings'): ?>
  443. <div class="col-sm-12">
  444. <?php else: ?>
  445. <div class="col-sm-9">
  446. <?php endif ?>
  447. <?= HTML::tablist($tabs_info, $cur_tab) ?>
  448. <div class="top-buffer-md">
  449. <?php
  450. if ($cur_tab == 'dashboard') {
  451. echoDashboard();
  452. } elseif ($cur_tab == 'submissions') {
  453. echoMySubmissions();
  454. } elseif ($cur_tab == 'standings') {
  455. echoStandings();
  456. } elseif ($cur_tab == 'backstage') {
  457. echoBackstage();
  458. }
  459. ?>
  460. </div>
  461. </div>
  462. <?php if ($cur_tab == 'standings'): ?>
  463. <div class="col-sm-12">
  464. <hr />
  465. </div>
  466. <?php endif ?>
  467. <div class="col-sm-3">
  468. <?php
  469. if ($contest['cur_progress'] <= CONTEST_IN_PROGRESS) {
  470. echoContestCountdown();
  471. } elseif ($contest['cur_progress'] <= CONTEST_TESTING) {
  472. echoContestJudgeProgress();
  473. } else {
  474. echoContestFinished();
  475. }
  476. ?>
  477. <?php if ($cur_tab == 'standings'): ?>
  478. </div>
  479. <div class="col-sm-3">
  480. <?php endif ?>
  481. <?php if (!isset($contest['extra_config']['contest_type']) || $contest['extra_config']['contest_type']=='OI'):?>
  482. <p>此次比赛为OI赛制。</p>
  483. <p><strong>注意:比赛时只显示测样例的结果。</strong></p>
  484. <?php elseif ($contest['extra_config']['contest_type']=='IOI'):?>
  485. <p>此次比赛为IOI赛制。</p>
  486. <p><strong>注意:比赛时显示测试所有数据的结果,但无法看到详细信息。</strong></p>
  487. <?php endif?>
  488. <a href="/contest/<?=$contest['id']?>/registrants" class="btn btn-info btn-block"><?= UOJLocale::get('contests::contest registrants') ?></a>
  489. <?php if (isSuperUser($myUser)): ?>
  490. <a href="/contest/<?=$contest['id']?>/manage" class="btn btn-primary btn-block">管理</a>
  491. <?php if (isset($start_test_form)): ?>
  492. <div class="top-buffer-sm">
  493. <?php $start_test_form->printHTML(); ?>
  494. </div>
  495. <?php endif ?>
  496. <?php if (isset($publish_result_form)): ?>
  497. <div class="top-buffer-sm">
  498. <?php $publish_result_form->printHTML(); ?>
  499. </div>
  500. <?php endif ?>
  501. <?php endif ?>
  502. <?php if ($contest['extra_config']['links']) { ?>
  503. <?php if ($cur_tab == 'standings'): ?>
  504. </div>
  505. <div class="col-sm-3">
  506. <div class="card border-info">
  507. <?php else: ?>
  508. <div class="card border-info top-buffer-lg">
  509. <?php endif ?>
  510. <div class="card-header bg-info">
  511. <h3 class="card-title">比赛资料</h3>
  512. </div>
  513. <div class="list-group">
  514. <?php foreach ($contest['extra_config']['links'] as $link) { ?>
  515. <a href="/blogs/<?=$link[1]?>" class="list-group-item"><?=$link[0]?></a>
  516. <?php } ?>
  517. </div>
  518. </div>
  519. <?php } ?>
  520. </div>
  521. </div>
  522. <?php echoUOJPageFooter() ?>