contest_inside.php 18 KB

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