• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/modules/contest/submit.php

Go to the documentation of this file.
00001 <?php
00002 if(!defined('__PRAGYAN_CMS'))
00003 { 
00004         header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
00005         echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
00006         echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
00007         exit(1);
00008 }
00009 function getLanguageSelectionBox($languageList) {
00010         if ($languageList == '')
00011                 $languageList = array(
00012                         'C',
00013                         'C++',
00014                         'Java',
00015                         'C#'
00016                 );
00017         else
00018                 $languageList = split('@', $languageList);
00019 
00020         $html = '<select name="language" id="language">';
00021         for ($i = 0; $i < count($languageList); ++$i) {
00022                 $html .= '<option value="' . $languageList[$i] . '">' . $languageList[$i] . '</option>';
00023         }
00024         $html .= '</select>';
00025 
00026         return $html;
00027 }
00028 
00029 function showSubmitForm($contestId, $problemId = -1) {
00030         global $userId;
00031 
00032         if ($userId < 0)
00033                 return 'You need to <a href="./+login">login</a> to submit a solution.';
00034 
00035         if ($contestCode == '')
00036                 $problemCode = '';
00037         else
00038                 if (!isContestOpen($contestCode)) {
00039                         display_error('Error. The specified contest is not open yet. Please check back later.');
00040                         return '';
00041                 }
00042 
00043         $problemSelect = '<tr><td><label for="problemcode">Problem Code:</label></td><td><input type="text" name="problemcode" id="problemcode" value="' . $problemCode . '" /></td></tr>';
00044         $contestSelect = '<tr><td><label for="contestcode">Contest:</label></td><td><input type="text" name="contestcode" id="contestcode" value="' . $contestCode . '" /></td></tr>';
00045 
00046         $languageList = '';
00047         if ($contestCode != '') {
00048                 $contestRow = getContestRow($contestCode);
00049                 if ($contestRow) {
00050                         $contestId = $contestRow['cid'];
00051                         if ($problemCode != '') {
00052                                 $problemRow = getProblemRow($contestId, $problemCode);
00053                                 if ($problemRow) {
00054                                         $languageList = $problemRow['plang'];
00055                                 }
00056                         }
00057                 }
00058         }
00059 
00060         $languageSelect = getLanguageSelectionBox($languageList);
00061         $languageSelect = '<tr><td><label for="languagebox">Language:</label></td><td>' . $languageSelect . '</td></tr>';
00062 
00063         $codeBox = <<<CODEBOX
00064                 <tr><td><label for="codebox">Enter your code here:</label></td><td><textarea name="codebox" id="codebox" rows="10" cols="80"></textarea></td></tr>
00065                 <tr><td><label for="fileuploadbox">Or upload your code here:</label></td><td><input type="hidden" name="MAX_FILE_SIZE" value="30000" /><input type="file" name="fileuploadbox" id="fileuploadbox" /></td></tr>
00066 CODEBOX;
00067 
00068         return <<<SUBMITFORM
00069         <form name="solutionsubmit" method="POST" action="">
00070                 <table border="0">
00071                         $loginBox
00072                         $contestSelect
00073                         $problemSelect
00074                         $languageSelect
00075                         $codeBox
00076                         $fileUploadBox
00077                         <tr><td></td><td><input type="submit" name="btnSubmit" value="Submit Solution" /></td></tr>
00078                 </table>
00079         </form>
00080 SUBMITFORM;
00081 }
00082 
00083 function submitPostSolution($contestCode, $problemCode) {
00084         global $userId;
00085 
00086         $contestRow = getContestRow($contestCode);
00087         if ($contestRow === false) {
00088                 display_error('Error. Could not find the specified contest.');
00089                 return '';
00090         }
00091 
00092         $problemRow = getProblemRow($problemCode);
00093         if ($problemRow === false) {
00094                 display_error('Error. Could not find the specified problem.');
00095                 return '';
00096         }
00097 
00098         $allowableLanguages = split('@', $problemRow['plang']);
00099 
00100         if (!isset($_POST['language']) || !in_array($_POST['language'], $allowableLanguages)) {
00101                 display_error('Error. No language specified, or submissions in the specified language are not allowed for this problem.');
00102                 return '';
00103         }
00104 
00105         $uploadedSource = '';
00106         if (isset($_POST['codebox']) && $_POST['codebox'] != '')
00107                 $uploadedSource = $_POST['codebox'];
00108         if (isset($_FILES['fileuploadbox']) && $_FILES['fileuploadbox']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['fileuploadbox']['tmp_name'])) {
00109                 $src = file_get_contents($_FILES['fileuploadbox']['tmp_name']);
00110                 if ($src !== false) {
00111                         $uploadedSource = $src;
00112                         unlink($_FILES['fileuploadbox']['tmp_name']);
00113                 }
00114         }
00115 
00116         if ($uploadedSource == '') {
00117                 display_error('Error. Empty submission.');
00118                 return '';
00119         }
00120 
00121         $time = time();
00122         $insertQuery = 'INSERT INTO `problem`(`pid`, `uid`, `lang`, `stid`, `status`, `code`, `runout`, `time`, `score`) VALUES ' .
00123                 "({$problemRow['pid']}, {$userId}, '{$_POST['language']}', 1, 'Waiting', '$uploadedSource', '', $time, 0)";
00124         if (mysql_query($insertQuery))
00125                 return "<p>Your solution has been submitted. Please check the <a href=\"../../status/\">status page</a> for more.</p>";
00126         else {
00127                 display_error("Error. Could not insert submission into database.");
00128                 return '';
00129         }
00130 }
00131 
00132 function submitSolution($contestCode, $problemCode) {
00133         global $userId;
00134 
00135         if ($userId > 0 && isset($_POST['codebox'])) {
00136                 if (!isContestOpen($contestCode)) {
00137                         display_error('Error. The specified contest is not open just yet. Please check back later.');
00138                         return '';
00139                 }
00140 
00141                 return submitPostSolution($contestCode, $problemCode);
00142         }
00143         else
00144                 return showSubmitForm($contestCode, $problemCode);
00145 }

Generated on Sun Jan 2 2011 04:55:31 for Pragyan CMS by  doxygen 1.7.1