contest_inside.php 18 KB

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