PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/cms/modules/contest.lib.php

https://github.com/akash6190/pragyan
PHP | 179 lines | 136 code | 34 blank | 9 comment | 43 complexity | d0101e8ac253721a0e2608f68c04b579 MD5 | raw file
  1. <?php
  2. if(!defined('__PRAGYAN_CMS'))
  3. {
  4. header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
  5. echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
  6. echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
  7. exit(1);
  8. }
  9. class contest implements module, fileuploadable {
  10. private $moduleComponentId;
  11. private $userId;
  12. private $action;
  13. public function getHtml($userId, $moduleComponentId, $action) {
  14. $tihs->userId = $userId;
  15. $this->moduleComponentId = $moduleComponentId;
  16. $this->action = $action;
  17. if ($action == 'view') {
  18. return $this->actionView();
  19. }
  20. else if ($action == 'edit') {
  21. return $this->actionEdit();
  22. }
  23. }
  24. public function actionView() {
  25. $cid = $this->moduleComponentId;
  26. $uid = $this->userId;
  27. /*
  28. * View can be for:
  29. * viewing list of problems
  30. * given a problem id, viewing the problem
  31. * given subaction=submit, showing a submit box or submitting solution depending on post data
  32. * given subaction=status, showing submission status
  33. * filters: userid, problem
  34. * given subaction=ranklist, showing the ranklist for the contest.
  35. */
  36. $subaction = '';
  37. if (isset($_GET['subaction']))
  38. $subaction = $_GET['subaction'];
  39. if ($subaction == 'showproblem')
  40. return $this->getProblemPage($cid);
  41. else if ($subaction == 'showranklist')
  42. return $this->getRanklist($cid);
  43. else if ($subaction == 'showstatus')
  44. return $this->getStatus($cid);
  45. else if ($subaction == 'submit')
  46. return $this->getSubmitPage($cid);
  47. return $this->getContestPage($cid);
  48. }
  49. public function actionEdit() {
  50. }
  51. private function getContestPage($contestId) {
  52. $problemQuery = "SELECT * FROM `contest_problem` WHERE `cid` = '$contestId' AND `testable` = 1 ORDER BY `pid`";
  53. $problemResult = mysql_query($problemQuery);
  54. $html = '<table border="0">';
  55. $className = array('even', 'odd');
  56. $parity = 0;
  57. while ($problemRow = mysql_fetch_assoc($problemResult)) {
  58. $pcode = $problemRow['pcode'];
  59. $ptitle = $problemRow['pname'];
  60. $html .= "<tr class=\"{$className[$parity]}\"><td><a href=\"./+view&subaction=showproblem&pcode=$pcode\">{$problemRow['pcode']}</a></td><td><a href=\"./+view&subaction=showproblem&pcode=$pcode\">{$problemRow['ptitle']}</a></td></tr>\n";
  61. $parity = 1 - $parity;
  62. }
  63. $html .= '</table>';
  64. return $html;
  65. }
  66. private function getProblemId($contestId, $pcode) {
  67. $idQuery = "SELECT `pid` FROM `contest_problem` WHERE `cid` = '$contestId' AND `pcode` = '$pcode'";
  68. $idResult = mysql_query($idQuery);
  69. if (!$idResult) {
  70. displayerror('MySQL error while attempting to fetch problem id, on line ' . __LINE__ . ', ' . __FILE__);
  71. return -1;
  72. }
  73. $idRow = mysql_fetch_row($idResult);
  74. if ($idRow) return $idRow[0];
  75. else return -1;
  76. }
  77. private function getProblemPage($contestId) {
  78. $pcode = '';
  79. if (isset($_GET['pcode']))
  80. $pcode = $_GET['pcode'];
  81. else {
  82. displayerror('Error. Problem code not specified.');
  83. return '';
  84. }
  85. $problemId = $this->getProblemId($contestId, $pcode);
  86. if ($problemId < 0) {
  87. displayerror('Error. Invalid problem code specified. Could not find a problem in the current contest with the given problem code.');
  88. return '';
  89. }
  90. global $sourceFolder, $moduleFolder;
  91. $problemPageHtml = file_get_contents("$sourceFolder/$moduleFolder/contest/problems/$contestId/$pcode.html");
  92. $problemPageHtml .= $this->getSubmitForm($contestId, $problemId);
  93. return $problemPageHtml;
  94. }
  95. private function getRanklist($contestId) {
  96. }
  97. private function getStatus($contestId) {
  98. }
  99. function getPaginatedContent($selectQuery, $countQuery, $itemsPerPage, &$pageNumber, $sortField, $sortDirection, &$pageCount) {
  100. $startItem = 0;
  101. if ($itemsPerPage <= 0) $itemsPerPage = 20;
  102. $itemCountResult = mysql_query($countQuery);
  103. if (!$itemCountResult) return false;
  104. $itemCount = mysql_fetch_row($itemCountResult);
  105. $itemCount = $itemCount[0];
  106. $pageCount = ceil($itemCount / $itemsPerPage);
  107. if ($pageNumber <= 0) $pageNumber = 1;
  108. else if ($pageNumber > $pageCount) $pageNumber = $pageCount;
  109. $startItem = ($pageNumber - 1) * $itemsPerPage;
  110. if ($sortField != '' && ($sortDirection == 'ASC' || $sortDirection == 'DESC')) $selectQuery .= " ORDER BY `$sortField` $sortDirection";
  111. $selectQuery .= " LIMIT $startItem, $itemsPerPage";
  112. $selectResult = mysql_query($selectQuery);
  113. if (!$selectResult) {
  114. log_error(__FILE__, __LINE__, 'MySQL Error in query ' . $selectQuery . ': ' . mysql_error());
  115. return false;
  116. }
  117. $results = array();
  118. while ($selectRow = mysql_fetch_array($selectResult)) {
  119. $results[] = $selectRow;
  120. }
  121. mysql_free_result($selectResult);
  122. return $results;
  123. }
  124. function getSortableTableHeading($headings, $orderBy, $orderDir, $getData) {
  125. $html = '<tr>';
  126. for ($i = 0; $i < count($headings); ++$i) {
  127. $html .= '<th><a href="./&orderby=' . $headings[$i][0] . '&orderdir=';
  128. if ($headings[$i][0] == $orderBy)
  129. $html .= ($orderDir == 'ASC' ? 'desc' : 'asc');
  130. else
  131. $html .= 'asc';
  132. $html .= $getData . '">' . $headings[$i][1] . '</a></th>';
  133. }
  134. $html .= "</tr>\n";
  135. return $html;
  136. }
  137. function getPageNumberList($pageNumber, $pageCount, $getData) {
  138. $ret = '<ul class="pagenumbers">';
  139. for ($i = 1; $i <= $pageCount; ++$i)
  140. $ret .= '<li><a href="./&page=' . $i . $getData . '">' . "$i</a></li>\n";
  141. $ret .= "</ul>\n";
  142. return $ret;
  143. }
  144. }
  145. ?>