contest_inside.php 18 KB

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