forked from DOMjudge/domjudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
718 lines (641 loc) · 24.1 KB
/
common.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
/**
* Common functions shared between team/public/jury interface
*
* Part of the DOMjudge Programming Contest Jury System and licenced
* under the GNU GPL. See README and COPYING for details.
*/
/** Text symbol used in output to represent a circle */
define('CIRCLE_SYM', '●');
function parseRunDiff($difftext){
$line = strtok($difftext,"\n"); //first line
if(sscanf($line, "### DIFFERENCES FROM LINE %d ###\n", $firstdiff) != 1)
return htmlspecialchars($difftext);
$return = $line . "\n";
// Add second line 'team ? reference'
$line = strtok("\n");
$return .= $line . "\n";
// We determine the line number width from the '_' characters and
// the separator position from the character '?' on the second line.
$linenowidth = mb_strrpos($line, '_') + 1;
$midloc = mb_strpos($line, '?') - ($linenowidth+1);
$line = strtok("\n");
while(mb_strlen($line) != 0){
$linenostr = mb_substr($line, 0, $linenowidth);
$diffline = mb_substr($line, $linenowidth+1);
$mid = mb_substr($diffline, $midloc-1, 3);
switch($mid){
case ' = ':
$formdiffline = "<span class='correct'>".htmlspecialchars($diffline)."</span>";
break;
case ' ! ':
$formdiffline = "<span class='differ'>".htmlspecialchars($diffline)."</span>";
break;
case ' $ ':
$formdiffline = "<span class='endline'>".htmlspecialchars($diffline)."</span>";
break;
case ' > ':
case ' < ':
$formdiffline = "<span class='extra'>".htmlspecialchars($diffline)."</span>";
break;
default:
$formdiffline = htmlspecialchars($diffline);
}
$return = $return . $linenostr . " " . $formdiffline . "\n";
$line = strtok("\n");
}
return $return;
}
/**
* Print a list of submissions from contests contained in the $cdatas
* contest data array, either all or only those that match
* <key> [= <value>] pairs specified in $restrictions:
* - 'verified' if set, only list submissions that are verified
* - 'judged' if set, only list submissions with completed judgings
* - 'teamid', 'probid', 'langid', 'categoryid', 'judgehost' can be
* set to an ID to filter on that respective team, language, etc.
* Output is limited to the number $limit, or unlimited by default.
* If $highlight is a submission ID, then that one is highlighted.
*/
function putSubmissions($cdatas, $restrictions, $limit = 0, $highlight = null)
{
global $DB, $username;
/* We need two kind of queries: one for all submissions, and one
* with the results for the valid ones.
*/
$cids = array_keys($cdatas);
$verifyclause = '';
if ( isset($restrictions['verified']) ) {
if ( $restrictions['verified'] ) {
$verifyclause = 'AND (j.verified = 1) ';
} else {
$verifyclause = 'AND (j.verified = 0 OR (j.verified IS NULL AND s.judgehost IS NULL)) ';
}
}
$judgedclause = '';
if ( isset($restrictions['judged']) ) {
if ( $restrictions['judged'] ) {
$judgedclause = 'AND (j.result IS NOT NULL) ';
} else {
$judgedclause = 'AND (j.result IS NULL) ';
}
}
$rejudgingclause = '';
if ( isset($restrictions['rejudgingdiff']) ) {
if ( $restrictions['rejudgingdiff'] ) {
$rejudgingclause = 'AND (j.result != jold.result) ';
} else {
$rejudgingclause = 'AND (j.result = jold.result) ';
}
}
if ( isset($restrictions['old_result']) && !isset($restrictions['rejudgingid']) ) {
error('cannot specify restriction on old_result without specifying a rejudgingid');
}
// Special case the rejudgingid restriction by showing the
// corresponding judging and the old (active) judging result:
$sqlbody =
'FROM submission s
LEFT JOIN team t USING (teamid)
LEFT JOIN problem p USING (probid)
LEFT JOIN contestproblem cp USING (probid, cid)
LEFT JOIN language l USING (langid) ' .
(isset($restrictions['rejudgingid']) ?
'LEFT JOIN judging j ON (s.submitid = j.submitid AND j.rejudgingid = %i)
LEFT JOIN judging jold ON (j.prevjudgingid IS NULL AND s.submitid = jold.submitid AND jold.valid = 1 OR j.prevjudgingid = jold.judgingid) ' :
'LEFT JOIN judging j ON (s.submitid = j.submitid AND j.valid = 1) %_ ') .
'WHERE s.cid IN (%Ai) ' . $verifyclause . $judgedclause . $rejudgingclause .
(isset($restrictions['teamid']) ? 'AND s.teamid = %i ' : '%_ ') .
(isset($restrictions['categoryid']) ? 'AND t.categoryid = %i ' : '%_ ') .
(isset($restrictions['probid']) ? 'AND s.probid = %i ' : '%_ ') .
(isset($restrictions['langid']) ? 'AND s.langid = %s ' : '%_ ') .
(isset($restrictions['judgehost']) ? 'AND s.judgehost = %s ' : '%_ ') .
(isset($restrictions['rejudgingid']) ? 'AND (s.rejudgingid = %i OR ' .
' j.rejudgingid = %i) ' : '%_ %_ ') .
(isset($restrictions['old_result']) ? 'AND jold.result = %s ' : '%_ ') .
(isset($restrictions['result']) ? 'AND j.result = %s ' : '%_ ');
// No contests; automatically nothing found and the query can not be run...
if ( empty($cids) ) {
echo "<p class=\"nodata\">No submissions</p>\n\n";
return;
}
$res = $DB->q('SELECT s.submitid, s.teamid, s.probid, s.langid, s.cid,
s.submittime, s.judgehost, s.valid, t.name AS teamname,
cp.shortname, p.name AS probname, l.name AS langname,
j.result, j.judgehost, j.verified, j.jury_member, j.seen ' .
(isset($restrictions['rejudgingid']) ? ', jold.result AS oldresult ' : '') .
$sqlbody .
'ORDER BY s.submittime DESC, s.submitid DESC ' .
($limit > 0 ? 'LIMIT 0, %i' : '%_'), @$restrictions['rejudgingid'], $cids,
@$restrictions['teamid'], @$restrictions['categoryid'],
@$restrictions['probid'], @$restrictions['langid'],
@$restrictions['judgehost'],
@$restrictions['rejudgingid'], @$restrictions['rejudgingid'],
@$restrictions['old_result'], @$restrictions['result'],
$limit);
// nothing found...
if( $res->count() == 0 ) {
echo "<p class=\"nodata\">No submissions</p>\n\n";
return;
}
if ( IS_JURY ) {
echo addForm('submission.php');
}
// print the table with the submissions.
// table header
echo "<table class=\"list sortable\">\n<thead>\n<tr>" .
(IS_JURY ? "<th scope=\"col\" class=\"sorttable_numeric\">ID</th>" : '') .
(IS_JURY && count($cids) > 1 ? "<th scope=\"col\" class=\"sorttable_numeric\">contest</th>" : '') .
"<th scope=\"col\">time</th>" .
(IS_JURY ? "<th scope=\"col\">team</th>" : '') .
"<th scope=\"col\">problem</th>" .
"<th scope=\"col\">lang</th>" .
"<th scope=\"col\">result</th>" .
(IS_JURY ? "<th scope=\"col\">verified</th><th scope=\"col\">by</th>" : '') .
(IS_JURY && isset($restrictions['rejudgingid']) ?
"<th scope=\"col\">old result</th>" : '') .
"</tr>\n</thead>\n<tbody>\n";
// print each row with links to detailed information
$iseven = $subcnt = $corcnt = $igncnt = $vercnt = $quecnt = 0;
while( $row = $res->next() ) {
$sid = (int)$row['submitid'];
// always provide link if this is Jury. For team, provide link
// to a different page, provided that the result is actually
// present and valid.
if ( IS_JURY ) {
// If rejudging list, link to the new rejudging:
$linkurl = 'submission.php?id=' . $sid .
(isset($restrictions['rejudgingid']) ?
'&rejudgingid=' . $restrictions['rejudgingid'] : '');
$link = ' href="' . $linkurl . '"';
} elseif ( $row['submittime'] < $cdatas[$row['cid']]['endtime'] &&
$row['result'] && $row['valid'] &&
(!dbconfig_get('verification_required',0) || $row['verified']) ) {
$link = ' href="submission_details.php?id=' . $sid . '"';
} else {
$link = '';
}
echo "<tr class=\"" .
( $iseven ? 'roweven': 'rowodd' );
$iseven = !$iseven;
if ( $row['valid'] ) {
$subcnt++;
} else {
$igncnt++;
echo ' sub_ignore';
}
if ( $sid == $highlight ) {
echo ' highlight';
}
if (!IS_JURY && !$row['seen'] ) {
echo ' unseen';
}
echo '">';
if ( IS_JURY ) {
echo "<td><a$link>s$sid</a></td>";
}
if ( IS_JURY && count($cids) > 1 ) {
echo "<td><a$link>c${row['cid']}</a></td>";
}
echo "<td><a$link>" . printtime($row['submittime']) . "</a></td>";
if ( IS_JURY ) {
echo '<td title="t' .
htmlspecialchars($row['teamid']) . '">' .
"<a$link>" . htmlspecialchars(str_cut($row['teamname'],30)) . '</a></td>';
}
echo '<td class="probid" title="' . htmlspecialchars($row['probname']) . '">' .
"<a$link>" . htmlspecialchars($row['shortname']) . '</a></td>';
echo '<td class="langid" title="' . htmlspecialchars($row['langname']) . '">' .
"<a$link>" . htmlspecialchars($row['langid']) . '</a></td>';
echo "<td class=\"result\"><a$link>";
if ( difftime($row['submittime'],$cdatas[$row['cid']]['endtime']) >= 0 ) {
echo printresult('too-late');
if ( IS_JURY && $row['result'] ) {
echo " (" . printresult($row['result']) . ")";
}
} else if ( ! $row['result'] ||
( !IS_JURY && ! $row['verified'] &&
dbconfig_get('verification_required', 0) ) ) {
echo printresult($row['judgehost'] || !IS_JURY ? '' : 'queued');
} else {
echo printresult($row['result']);
}
echo "</a></td>";
if ( IS_JURY ) {
// only display verification if we're done with judging
unset($verified, $jury_member);
$claim = FALSE;
if ( empty($row['result']) ) {
$verified = ' ';
$jury_member = ' ';
} else {
$verified = printyn($row['verified']);
if ( empty($row['jury_member']) ) {
$jury_member = ' ';
} else {
$jury_member = htmlspecialchars($row['jury_member']);
}
if ( !$row['verified'] ) {
$vercnt++;
if ( empty($row['jury_member']) ) {
$claim = TRUE;
} else {
$verified = 'claimed';
}
}
}
echo "<td><a$link>$verified</a></td><td>";
if ( $claim ) {
echo "<a class=\"button\" href=\"$linkurl&claim=1\">claim</a>";
} else {
if ( !$row['verified'] && $jury_member==$username ) {
echo "<a class=\"button\" href=\"$linkurl&unclaim=1\">unclaim</a>";
} else {
echo "<a$link>$jury_member</a>";
}
}
echo "</td>";
if ( isset($restrictions['rejudgingid']) ) {
echo "<td class=\"result\"><a href=\"submission.php?id=$sid\">" .
printresult($row['oldresult']) . "</a></td>";
}
}
echo "</tr>\n";
if ( $row['result'] == 'correct' ) $corcnt++;
}
echo "</tbody>\n</table>\n\n";
if ( IS_JURY ) {
echo addEndForm();
if ( $limit > 0 ) {
$query_extras = array('subcnt' => '',
'corcnt' => ' AND j.result LIKE \'correct\'',
'igncnt' => ' AND s.valid = 0',
'vercnt' => ' AND verified = 0 AND result IS NOT NULL',
'quecnt' => ' AND result IS NULL');
foreach ( $query_extras as $cnt => $query_extra ) {
$$cnt = $DB->q('VALUE SELECT count(s.submitid) ' . $sqlbody . $query_extra,
@$restrictions['rejudgingid'], $cids,
@$restrictions['teamid'], @$restrictions['categoryid'],
@$restrictions['probid'], @$restrictions['langid'],
@$restrictions['judgehost'],
@$restrictions['rejudgingid'], @$restrictions['rejudgingid'],
@$restrictions['old_result'], @$restrictions['result']);
}
}
echo "<p>Total correct: $corcnt, submitted: $subcnt";
if ( $vercnt > 0 ) echo ", unverified: $vercnt";
if ( $igncnt > 0 ) echo ", ignored: $igncnt";
if ( $quecnt > 0 ) echo ", judgement pending: $quecnt";
echo "</p>\n\n";
}
return;
}
/**
* Output team information (for team and public interface)
*/
function putTeam($teamid) {
global $DB;
$team = $DB->q('MAYBETUPLE SELECT t.*, c.name AS catname,
a.name AS affname, a.country FROM team t
LEFT JOIN team_category c USING (categoryid)
LEFT JOIN team_affiliation a ON (t.affilid = a.affilid)
WHERE teamid = %i', $teamid);
if ( empty($team) ) error ("No team found by this id.");
$countryflag = "../images/countries/" . urlencode($team['country']) . ".png";
$teamimage = "../images/teams/" . urlencode($team['teamid']) . ".jpg";
echo "<h1>Team ".htmlspecialchars($team['name'])."</h1>\n\n";
if ( is_readable($teamimage) ) {
echo '<img id="teampicture" src="' . $teamimage .
'" alt="Picture of team ' .
htmlspecialchars($team['name']) . '" />';
}
?>
<table>
<tr><td>Name: </td><td><?php echo htmlspecialchars($team['name'])?></td></tr>
<tr><td>Category:</td><td><?php echo htmlspecialchars($team['catname'])?></td></tr>
<?php
if ( !empty($team['members']) ) {
echo '<tr><td>Members:</td><td>' .
nl2br(htmlspecialchars($team['members'])) . "</td></tr>\n";
}
if ( !empty($team['affilid']) ) {
echo '<tr><td>Affiliation:</td><td>';
echo htmlspecialchars($team['affname']);
echo "</td></tr>\n";
if ( !empty($team['country']) ) {
echo '<tr><td>Country:</td><td>';
if ( is_readable($countryflag) ) {
echo '<img src="' . $countryflag . '" alt="' .
htmlspecialchars($team['country']) . '" /> ';
}
echo htmlspecialchars($team['country']) . "</td></tr>\n";
}
}
if ( !empty($team['room']) ) {
echo '<tr><td>Location:</td><td>' .
htmlspecialchars($team['room']) . "</td></tr>\n";
}
echo "</table>\n\n";
}
/**
* Output clock
*/
function putClock() {
global $cdata, $username;
echo '<div id="clock">';
// timediff to end of contest
if ( difftime(now(), $cdata['starttime']) >= 0 &&
difftime(now(), $cdata['endtime']) < 0 ) {
$left = "time left: " . printtimediff(now(),$cdata['endtime']);
} else if ( difftime(now(), $cdata['activatetime']) >= 0 &&
difftime(now(), $cdata['starttime']) < 0 ) {
$left = "time to start: " . printtimediff(now(),$cdata['starttime']);
} else {
$left = "";
}
echo "<span id=\"timeleft\">" . $left . "</span>";
global $cid, $cdatas;
// Show a contest selection form, if there are contests
if ( IS_JURY || count($cdatas) > 1 ) {
echo "<div id=\"selectcontest\">\n";
echo addForm('change_contest.php', 'get', 'selectcontestform');
$contests = array_map(function($c) { return $c['shortname']; }, $cdatas);
if ( IS_JURY ) {
$values = array(
// -1 because setting cookies to null/'' unsets then and that is not what we want
-1 => '- No contest'
);
}
foreach ( $contests as $contestid => $name ) {
$values[$contestid] = $name;
}
echo 'contest: ' . addSelect('cid', $values, $cid, true);
echo addEndForm();
echo "<script type=\"text/javascript\">
document.getElementById('cid').addEventListener('change', function() {
document.getElementById('selectcontestform').submit();
});
</script>
";
echo "</div>\n";
}
if ( logged_in() ) {
echo "<div id=\"username\">logged in as " . $username
. ( have_logout() ? " <a href=\"../auth/logout.php\">×</a>" : "" )
. "</div>";
}
echo "</div>";
echo "<script type=\"text/javascript\">
var initial = " . time() . ";
var activatetime = " . ( isset($cdata['activatetime']) ? $cdata['activatetime'] : -1 ) . ";
var starttime = " . ( isset($cdata['starttime']) ? $cdata['starttime'] : -1 ) . ";
var endtime = " . ( isset($cdata['endtime']) ? $cdata['endtime'] : -1 ) . ";
var offset = 0;
var date = new Date(initial*1000);
var timeleftelt = document.getElementById(\"timeleft\");
setInterval(function(){updateClock();},1000);
updateClock();
</script>\n";
}
/**
* Output a footer for pages containing the DOMjudge version and server host/port/time
*/
function putDOMjudgeVersion() {
echo "<hr /><address>DOMjudge/" . DOMJUDGE_VERSION .
" at ".$_SERVER['SERVER_NAME']." Port ".$_SERVER['SERVER_PORT'].", page generated <span id=\"timecur\">" . strftime('%a %d %b %Y %T %Z') . "</span></address>\n";
}
/**
* Check whether the logged in user has DOMjudge administrator level,
* as defined in passwords.php. If not, error and stop further execution.
*/
function requireAdmin() {
if (!checkrole('admin')) {
error("This function is only accessible to administrators.");
}
}
/**
* Translate error codes from PHP's file upload function into
* concrete error strings.
*/
function checkFileUpload($errorcode) {
switch ( $errorcode ) {
case UPLOAD_ERR_OK: // everything ok!
return;
case UPLOAD_ERR_INI_SIZE:
error('The uploaded file is too large (exceeds the upload_max_filesize directive).');
case UPLOAD_ERR_FORM_SIZE:
error('The uploaded file is too large (exceeds the MAX_FILE_SIZE directive).');
case UPLOAD_ERR_PARTIAL:
error('The uploaded file was only partially uploaded.');
case UPLOAD_ERR_NO_FILE:
error('No file was uploaded.');
case UPLOAD_ERR_NO_TMP_DIR:
error('Missing a temporary folder. Contact staff.');
case UPLOAD_ERR_CANT_WRITE:
error('Failed to write file to disk. Contact staff.');
case UPLOAD_ERR_EXTENSION:
error('File upload stopped by extension. Contact staff.');
default:
error('Unknown error while uploading: '. $_FILES['code']['error'] .
'. Contact staff.');
}
}
/**
* Outputs a problem description text, either as download or inline.
* It is assumed that the headers have not been sent yet, and this
* function terminates the PHP script execution.
*/
function putProblemText($probid)
{
global $DB, $cdata;
$prob = $DB->q("MAYBETUPLE SELECT cid, shortname, problemtext, problemtext_type
FROM problem INNER JOIN contestproblem USING (probid)
WHERE OCTET_LENGTH(problemtext) > 0
AND probid = %i AND cid = %i", $probid, $cdata['cid']);
if ( empty($prob) ||
!(IS_JURY ||
($prob['cid']==$cdata['cid'] && difftime($cdata['starttime'],now())<=0)) ) {
error("Problem p$probid not found or not available");
}
switch ( $prob['problemtext_type'] ) {
case 'pdf':
$mimetype = 'application/pdf';
break;
case 'html':
$mimetype = 'text/html';
break;
case 'txt':
$mimetype = 'text/plain';
break;
default:
error("Problem p$probid text has unknown type");
}
$filename = "prob-$prob[shortname].$prob[problemtext_type]";
header("Content-Type: $mimetype; name=\"$filename\"");
header("Content-Disposition: inline; filename=\"$filename\"");
header("Content-Length: " . strlen($prob['problemtext']));
echo $prob['problemtext'];
exit(0);
}
/**
* Outputs bulleted list of problem statements for this contest
*/
function putProblemTextList()
{
global $cid, $cdata, $DB;
$fdata = calcFreezeData($cdata);
if ( ! have_problemtexts() ) {
echo "<p class=\"nodata\">No problem texts available for this contest.</p>\n\n";
} elseif ( !$fdata['cstarted'] ) {
echo "<p class=\"nodata\">Problem texts will appear here at contest start.</p>\n\n";
} else {
// otherwise, display list
$res = $DB->q('SELECT probid,shortname,name,color,problemtext_type
FROM problem INNER JOIN contestproblem USING (probid)
WHERE cid = %i AND allow_submit = 1 AND
problemtext_type IS NOT NULL ORDER BY shortname', $cid);
if ( $res->count() > 0 ) {
echo "<ul>\n";
while($row = $res->next()) {
print '<li> ' .
'<img src="../images/' . urlencode($row['problemtext_type']) .
'.png" alt="' . htmlspecialchars($row['problemtext_type']) .
'" /> <a href="problem.php?id=' . urlencode($row['probid']) . '">' .
'Problem ' . htmlspecialchars($row['shortname']) . ': ' .
htmlspecialchars($row['name']) . "</a></li>\n";
}
echo "</ul>\n";
}
}
}
function putSampleDataList() {
global $cid, $cdata, $DB;
$fdata = calcFreezeData($cdata);
if ( ! have_sampletestcases() ) {
echo "<p class=\"nodata\">No sample data available for this contest.</p>\n\n";
} elseif ( !$fdata['cstarted'] ) {
echo "<p class=\"nodata\">Sample data will appear here at contest start.</p>\n\n";
} else {
// otherwise, display list
$res = $DB->q('SELECT
p.probid,cp.shortname,p.name,t.testcaseid,t.description
FROM problem p
INNER JOIN contestproblem cp USING (probid)
LEFT JOIN testcase t USING (probid)
WHERE cid = %i AND allow_submit = 1 AND
t.sample = 1 ORDER BY p.probid,shortname', $cid);
$currentprobid = -1;
if ( $res->count() > 0 ) {
while($row = $res->next()) {
if ($row['probid'] != $currentprobid) {
echo '<h4>Problem ' . htmlspecialchars($row['shortname']) . ': ' .
htmlspecialchars($row['name']) . "</h4>\n";
$currentprobid = $row['probid'];
$i = 1;
}
echo "<ul>\n";
print '<li> ' .
'<img src="../images/txt.png" alt="" /> ' .
'Testcase ' . $i . ": " . htmlspecialchars($row['description']) . ' ' .
'(<a href="testcase.php?id=' . urlencode($row['testcaseid']) . '&fetch=input">input</a>)' .
'(<a href="testcase.php?id=' . urlencode($row['testcaseid']) . '&fetch=output">output</a>)' .
"</li>\n";
echo "</ul>\n";
$i++;
}
} else {
echo "<p class=\"nodata\">No sample data available for this contest.</p>\n\n";
}
}
}
function putSampleData($testcaseid, $fetch) {
global $DB, $cdata;
$testcase = $DB->q("MAYBETUPLE SELECT cid, shortname, testcaseid,
$fetch, OCTET_LENGTH($fetch) as size
FROM problem INNER JOIN contestproblem USING (probid)
LEFT JOIN testcase USING (probid)
WHERE sample = 1
AND testcaseid = %i AND cid = %i", $testcaseid, $cdata['cid']);
if ( empty($testcase) ||
!(IS_JURY ||
($testcase['cid']==$cdata['cid'] && difftime($cdata['starttime'],now())<=0)) ) {
error("Testcase t$testcaseid not found or not available");
}
$ext = substr($fetch,0,-3);
$filename = "$testcase[shortname]-t$testcase[testcaseid].$ext";
header("Content-Type: text/plain; name=\"$filename\"");
header("Content-Disposition: inline; filename=\"$filename\"");
header("Content-Length: " . strlen($testcase['size']));
echo $testcase[$fetch];
exit(0);
}
/**
* Returns true if at least one problem in the current contest has a
* problem statement text in the database.
*/
function have_problemtexts()
{
global $DB, $cid;
return $DB->q('VALUE SELECT COUNT(*) FROM problem
INNER JOIN contestproblem USING (probid)
WHERE problemtext_type IS NOT NULL
AND cid = %i', $cid) > 0;
}
/**
* Returns true if at least one problem in the current contest has a
* sample testcase in the database.
*/
function have_sampletestcases()
{
global $DB, $cid;
return $DB->q('VALUE SELECT COUNT(*) FROM problem
INNER JOIN contestproblem USING (probid)
LEFT JOIN testcase USING (probid)
WHERE sample = 1
AND cid = %i', $cid) > 0;
}
/**
* Maps domjudge language id to Ace language id
*/
function langidToAce($langid) {
switch ($langid) {
case 'c':
case 'cpp':
case 'cxx':
return 'c_cpp';
case 'pas':
return 'pascal';
case 'hs':
return 'haskell';
case 'pl':
return 'perl';
case 'bash':
return 'sh';
case 'py2':
case 'py3':
return 'python';
case 'adb':
return 'ada';
case 'plg':
return 'prolog';
case 'rb':
return 'ruby';
}
return $langid;
}
/**
* Output JavaScript function that contains the language extensions as
* configured in the database so the frontend can use them to automatically
* detect the language from the filename extension.
*/
function putgetMainExtension($langdata) {
echo "function getMainExtension(ext)\n{\n";
echo "\tswitch(ext) {\n";
foreach ( $langdata as $langid => $langdata ) {
$exts = json_decode($langdata['extensions']);
if ( !is_array($exts) ) continue;
foreach ( $exts as $ext ) {
echo "\t\tcase '" . $ext . "': return '" . $langid . "';\n";
}
}
echo "\t\tdefault: return '';\n\t}\n}\n\n";
}