problem.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. requirePHPLib('form');
  3. requirePHPLib('judger');
  4. if (!validateUInt($_GET['id']) || !($problem = queryProblemBrief($_GET['id']))) {
  5. become404Page();
  6. }
  7. $problem_content = queryProblemContent($problem['id']);
  8. $contest = validateUInt($_GET['contest_id']) ? queryContest($_GET['contest_id']) : null;
  9. if ($contest != null) {
  10. genMoreContestInfo($contest);
  11. $problem_rank = queryContestProblemRank($contest, $problem);
  12. if ($problem_rank == null) {
  13. become404Page();
  14. } else {
  15. $problem_letter = chr(ord('A') + $problem_rank - 1);
  16. }
  17. }
  18. $is_in_contest = false;
  19. $ban_in_contest = false;
  20. if ($contest != null) {
  21. if (!hasContestPermission($myUser, $contest)) {
  22. if ($contest['cur_progress'] == CONTEST_NOT_STARTED) {
  23. become404Page();
  24. } elseif ($contest['cur_progress'] == CONTEST_IN_PROGRESS) {
  25. if ($myUser == null || !hasRegistered($myUser, $contest)) {
  26. becomeMsgPage("<h1>比赛正在进行中</h1><p>很遗憾,您尚未报名。比赛结束后再来看吧~</p>");
  27. } else {
  28. $is_in_contest = true;
  29. DB::update("update contests_registrants set has_participated = 1 where username = '{$myUser['username']}' and contest_id = {$contest['id']}");
  30. }
  31. } else {
  32. $ban_in_contest = !isProblemVisibleToUser($problem, $myUser);
  33. }
  34. }
  35. } else {
  36. if (!isProblemVisibleToUser($problem, $myUser)) {
  37. become404Page();
  38. }
  39. }
  40. $submission_requirement = json_decode($problem['submission_requirement'], true);
  41. $problem_extra_config = getProblemExtraConfig($problem);
  42. $custom_test_requirement = getProblemCustomTestRequirement($problem);
  43. if ($custom_test_requirement && Auth::check()) {
  44. $custom_test_submission = DB::selectFirst("select * from custom_test_submissions where submitter = '".Auth::id()."' and problem_id = {$problem['id']} order by id desc limit 1");
  45. $custom_test_submission_result = json_decode($custom_test_submission['result'], true);
  46. }
  47. if ($custom_test_requirement && $_GET['get'] == 'custom-test-status-details' && Auth::check()) {
  48. if ($custom_test_submission == null) {
  49. echo json_encode(null);
  50. } elseif ($custom_test_submission['status'] != 'Judged') {
  51. echo json_encode(array(
  52. 'judged' => false,
  53. 'html' => getSubmissionStatusDetails($custom_test_submission)
  54. ));
  55. } else {
  56. ob_start();
  57. $styler = new CustomTestSubmissionDetailsStyler();
  58. if (!hasViewPermission($problem_extra_config['view_details_type'], $myUser, $problem, $submission)) {
  59. $styler->fade_all_details = true;
  60. }
  61. echoJudgementDetails($custom_test_submission_result['details'], $styler, 'custom_test_details');
  62. $result = ob_get_contents();
  63. ob_end_clean();
  64. echo json_encode(array(
  65. 'judged' => true,
  66. 'html' => getSubmissionStatusDetails($custom_test_submission),
  67. 'result' => $result
  68. ));
  69. }
  70. die();
  71. }
  72. $can_use_zip_upload = true;
  73. foreach ($submission_requirement as $req) {
  74. if ($req['type'] == 'source code') {
  75. $can_use_zip_upload = false;
  76. }
  77. }
  78. function handleUpload($zip_file_name, $content, $tot_size) {
  79. global $problem, $contest, $myUser, $is_in_contest;
  80. $content['config'][] = array('problem_id', $problem['id']);
  81. if ($is_in_contest && $contest['extra_config']["contest_type"]!='IOI' && !isset($contest['extra_config']["problem_{$problem['id']}"])) {
  82. $content['final_test_config'] = $content['config'];
  83. $content['config'][] = array('test_sample_only', 'on');
  84. }
  85. $esc_content = DB::escape(json_encode($content));
  86. $language = '/';
  87. foreach ($content['config'] as $row) {
  88. if (strEndWith($row[0], '_language')) {
  89. $language = $row[1];
  90. break;
  91. }
  92. }
  93. if ($language != '/') {
  94. Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/');
  95. }
  96. $esc_language = DB::escape($language);
  97. $result = array();
  98. $result['status'] = "Waiting";
  99. $result_json = json_encode($result);
  100. if ($is_in_contest) {
  101. DB::query("insert into submissions (problem_id, contest_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, ${contest['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', 0)");
  102. } else {
  103. DB::query("insert into submissions (problem_id, submit_time, submitter, content, language, tot_size, status, result, is_hidden) values (${problem['id']}, now(), '${myUser['username']}', '$esc_content', '$esc_language', $tot_size, '${result['status']}', '$result_json', {$problem['is_hidden']})");
  104. }
  105. # file_get_contents("http://localhost:24270/submit");
  106. }
  107. function handleCustomTestUpload($zip_file_name, $content, $tot_size) {
  108. global $problem, $contest, $myUser;
  109. $content['config'][] = array('problem_id', $problem['id']);
  110. $content['config'][] = array('custom_test', 'on');
  111. $esc_content = DB::escape(json_encode($content));
  112. $language = '/';
  113. foreach ($content['config'] as $row) {
  114. if (strEndWith($row[0], '_language')) {
  115. $language = $row[1];
  116. break;
  117. }
  118. }
  119. if ($language != '/') {
  120. Cookie::set('uoj_preferred_language', $language, time() + 60 * 60 * 24 * 365, '/');
  121. }
  122. $esc_language = DB::escape($language);
  123. $result = array();
  124. $result['status'] = "Waiting";
  125. $result_json = json_encode($result);
  126. DB::insert("insert into custom_test_submissions (problem_id, submit_time, submitter, content, status, result) values ({$problem['id']}, now(), '{$myUser['username']}', '$esc_content', '{$result['status']}', '$result_json')");
  127. }
  128. if ($can_use_zip_upload) {
  129. $zip_answer_form = newZipSubmissionForm('zip_answer',
  130. $submission_requirement,
  131. 'uojRandAvaiableSubmissionFileName',
  132. 'handleUpload');
  133. $zip_answer_form->extra_validator = function() {
  134. global $ban_in_contest;
  135. if ($ban_in_contest) {
  136. return '请耐心等待比赛结束后题目对所有人可见了再提交';
  137. }
  138. return '';
  139. };
  140. $zip_answer_form->succ_href = $is_in_contest ? "/contest/{$contest['id']}/submissions" : '/submissions';
  141. $zip_answer_form->runAtServer();
  142. }
  143. $answer_form = newSubmissionForm('answer',
  144. $submission_requirement,
  145. 'uojRandAvaiableSubmissionFileName',
  146. 'handleUpload');
  147. $answer_form->extra_validator = function() {
  148. global $ban_in_contest;
  149. if ($ban_in_contest) {
  150. return '请耐心等待比赛结束后题目对所有人可见了再提交';
  151. }
  152. return '';
  153. };
  154. $answer_form->succ_href = $is_in_contest ? "/contest/{$contest['id']}/submissions" : '/submissions';
  155. $answer_form->runAtServer();
  156. if ($custom_test_requirement) {
  157. $custom_test_form = newSubmissionForm('custom_test',
  158. $custom_test_requirement,
  159. function() {
  160. return uojRandAvaiableFileName('/tmp/');
  161. },
  162. 'handleCustomTestUpload');
  163. $custom_test_form->appendHTML(<<<EOD
  164. <div id="div-custom_test_result"></div>
  165. EOD
  166. );
  167. $custom_test_form->succ_href = 'none';
  168. $custom_test_form->extra_validator = function() {
  169. global $ban_in_contest, $custom_test_submission;
  170. if ($ban_in_contest) {
  171. return '请耐心等待比赛结束后题目对所有人可见了再提交';
  172. }
  173. if ($custom_test_submission && $custom_test_submission['status'] != 'Judged') {
  174. return '上一个测评尚未结束';
  175. }
  176. return '';
  177. };
  178. $custom_test_form->ctrl_enter_submit = true;
  179. $custom_test_form->setAjaxSubmit(<<<EOD
  180. function(response_text) {custom_test_onsubmit(response_text, $('#div-custom_test_result')[0], '{$_SERVER['REQUEST_URI']}?get=custom-test-status-details')}
  181. EOD
  182. );
  183. $custom_test_form->submit_button_config['text'] = UOJLocale::get('problems::run');
  184. $custom_test_form->runAtServer();
  185. }
  186. ?>
  187. <?php
  188. $REQUIRE_LIB['mathjax'] = '';
  189. $REQUIRE_LIB['hljs'] = '';
  190. ?>
  191. <?php echoUOJPageHeader(HTML::stripTags($problem['title']) . ' - ' . UOJLocale::get('problems::problem')) ?>
  192. <?php
  193. $limit = getUOJConf("/var/uoj_data/{$problem['id']}/problem.conf");
  194. $time_limit = $limit['time_limit'];
  195. $memory_limit = $limit['memory_limit'];
  196. ?>
  197. <?php if ($contest): ?>
  198. <div class="page-header row">
  199. <h1 class="col-md-3 text-left"><small><?= $contest['name'] ?></small></h1>
  200. <h1 class="col-md-7 text-center"><?= $problem_letter ?>. <?= HTML::escape($problem['title']) ?></h1>
  201. <div class="col-md-2 text-right" id="contest-countdown"></div>
  202. </div>
  203. <?php if ($contest['cur_progress'] <= CONTEST_IN_PROGRESS): ?>
  204. <script type="text/javascript">
  205. checkContestNotice(<?= $contest['id'] ?>, '<?= UOJTime::$time_now_str ?>');
  206. $('#contest-countdown').countdown(<?= $contest['end_time']->getTimestamp() - UOJTime::$time_now->getTimestamp() ?>);
  207. </script>
  208. <?php endif ?>
  209. <?php else: ?>
  210. <h1 class="page-header text-center">#<?= $problem['id']?>. <?= HTML::escape($problem['title']) ?></h1>
  211. <?php endif ?>
  212. <div class="row">
  213. <div class="col-lg-9">
  214. <div class="d-flex justify-content-between align-items-center problem-title-bar">
  215. <div>
  216. <?= getClickZanBlock('P', $problem['id'], $problem['zan']) ?>
  217. </div>
  218. <?php if ($contest): ?>
  219. <a role="button" class="btn btn-info btn-sm" href="/contest/<?= $contest['id'] ?>/problem/<?= $problem['id'] ?>/statistics"><span class="glyphicon glyphicon-stats"></span> <?= UOJLocale::get('problems::statistics') ?></a>
  220. <?php else: ?>
  221. <a role="button" class="btn btn-info btn-sm" href="/problem/<?= $problem['id'] ?>/statistics"><span class="glyphicon glyphicon-stats"></span> <?= UOJLocale::get('problems::statistics') ?></a>
  222. <?php endif ?>
  223. </div>
  224. <ul class="nav nav-tabs" role="tablist">
  225. <li class="nav-item"><a class="nav-link active" href="#tab-statement" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-book"></span> <?= UOJLocale::get('problems::statement') ?></a></li>
  226. <li class="nav-item"><a class="nav-link" href="#tab-submit-answer" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-upload"></span> <?= UOJLocale::get('problems::submit') ?></a></li>
  227. <?php if ($custom_test_requirement): ?>
  228. <li class="nav-item"><a class="nav-link" href="#tab-custom-test" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-console"></span> <?= UOJLocale::get('problems::custom test') ?></a></li>
  229. <?php endif ?>
  230. <?php if (hasProblemCtrlPermission($myUser, $problem)): ?>
  231. <li class="nav-item"><a class="nav-link" href="/problem/<?= $problem['id'] ?>/manage/statement" role="tab"><?= UOJLocale::get('problems::manage') ?></a></li>
  232. <?php endif ?>
  233. <?php if ($contest): ?>
  234. <li class="nav-item"><a class="nav-link" href="/contest/<?= $contest['id'] ?>" role="tab"><?= UOJLocale::get('contests::back to the contest') ?></a></li>
  235. <?php endif ?>
  236. </ul>
  237. <div class="tab-content">
  238. <div class="tab-pane active" id="tab-statement">
  239. <article class="problem-statement top-buffer-md"><?= $problem_content['statement'] ?></article>
  240. </div>
  241. <div class="tab-pane" id="tab-submit-answer">
  242. <div class="top-buffer-sm"></div>
  243. <?php if ($can_use_zip_upload): ?>
  244. <?php $zip_answer_form->printHTML(); ?>
  245. <hr />
  246. <strong><?= UOJLocale::get('problems::or upload files one by one') ?><br /></strong>
  247. <?php endif ?>
  248. <?php $answer_form->printHTML(); ?>
  249. </div>
  250. <?php if ($custom_test_requirement): ?>
  251. <div class="tab-pane" id="tab-custom-test">
  252. <div class="top-buffer-sm"></div>
  253. <?php $custom_test_form->printHTML(); ?>
  254. </div>
  255. <?php endif ?>
  256. </div>
  257. </div>
  258. <div class="col-lg-3">
  259. <div class="problem-sidebar">
  260. <div class="card problem-meta-card">
  261. <div class="card-header"><span class="glyphicon glyphicon-info-sign"></span> 题目信息</div>
  262. <div class="card-body">
  263. <ul class="list-unstyled mb-0">
  264. <li class="d-flex justify-content-between">
  265. <span class="meta-label">难度</span>
  266. <span class="badge badge-danger">UKE</span>
  267. </li>
  268. <li class="d-flex justify-content-between">
  269. <span class="meta-label">控制组</span>
  270. <?php if ($problem['ctrl'] == ''): ?>
  271. <span>默认</span>
  272. <?php else: ?>
  273. <a href="<?= HTML::url('/user/profile/' . $problem['ctrl']) ?>"><?= HTML::escape(queryUser($problem['ctrl'])['email']) ?></a>
  274. <?php endif ?>
  275. </li>
  276. <li class="d-flex justify-content-between">
  277. <span class="meta-label">时间限制</span>
  278. <span><?=$time_limit!=null?"$time_limit s":"N/A"?></span>
  279. </li>
  280. <li class="d-flex justify-content-between">
  281. <span class="meta-label">空间限制</span>
  282. <span><?=$memory_limit!=null?"$memory_limit MB":"N/A"?></span>
  283. </li>
  284. <li class="d-flex justify-content-between">
  285. <span class="meta-label">数据大小</span>
  286. <span><?php echo printSize(queryProblemSize($problem['id'])) ?></span>
  287. </li>
  288. </ul>
  289. </div>
  290. </div>
  291. <div class="card problem-meta-card">
  292. <div class="card-header"><span class="glyphicon glyphicon-stats"></span> 提交统计</div>
  293. <div class="card-body">
  294. <ul class="list-unstyled mb-0">
  295. <li class="d-flex justify-content-between">
  296. <span class="meta-label">提交数</span>
  297. <span><?= isset($problem['submit_num']) ? $problem['submit_num'] : 'N/A' ?></span>
  298. </li>
  299. <li class="d-flex justify-content-between">
  300. <span class="meta-label">通过数</span>
  301. <span><?= isset($problem['ac_num']) ? $problem['ac_num'] : 'N/A' ?></span>
  302. </li>
  303. <li class="d-flex justify-content-between">
  304. <span class="meta-label">通过率</span>
  305. <span><?= isset($problem['submit_num']) && $problem['submit_num'] > 0 ? round($problem['ac_num'] / $problem['submit_num'] * 100, 1) . '%' : 'N/A' ?></span>
  306. </li>
  307. </ul>
  308. </div>
  309. </div>
  310. </div>
  311. </div>
  312. </div>
  313. <?php echoUOJPageFooter() ?>