* @author Hamid Zarabi-Zadeh */ // must be run within Dokuwiki if (!defined('DOKU_INC')) { die(); } class syntax_plugin_judge extends DokuWiki_Syntax_Plugin { /** * @return string Syntax mode type. container indicates that this plugin tag can be nested in listblock, table, quote, hr tags. */ public function getType() { return 'container'; } /** * @return block indicates that open paragraphs need to be closed before plugin output. it can't be used inside open paragraph. */ public function getPType() { return 'block'; } /** * @return int Sort order - Low numbers go before high numbers */ public function getSort() { return 10; } /** * Connect lookup pattern to lexer. SpecialPattern indicates that this pattern don't have exit pattern and pattern data is not to be processed. * * @param string $mode Parser mode */ public function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{[judge|داوری|scoreboard][ ]*[^}]*\}\}', $mode, 'plugin_judge'); } /** * Handle matches of the judge syntax * * @param string $match The text matched by the patterns * @param int $state Type of pattern which triggered this call to handle() * @param int $pos The character position of the matched text. * @param Doku_Handler $handler The handler * @return array Data for the renderer */ public function handle($match, $state, $pos, Doku_Handler $handler) { if (substr($match, 2, 10) == "scoreboard") { /** * Strip markup from start and end $match */ $match = substr($match, 13, -2); return $this->scoreboard_handle($match); } elseif (substr($match, 2, 5) == "judge") { /** * Strip markup from start and end $match */ $match = substr($match, 8, -2); return $this->judge_handle($match); } elseif (substr($match, 2, 10) == "داوری") { /** * Strip markup from start and end $match */ $match = substr($match, 13, -2); return $this->judge_handle($match); } } public function scoreboard_handle($match) { /** * Extract problem names into array */ $parameters = explode(' ', $match); return array('mode' => "scoreboard", 'questions' => $parameters); } public function judge_handle($match) { global $ID, $conf; /** * Extract problem parameters into array */ $parameters = explode(' ', $match); /** * Set problem parameters */ foreach ($parameters as $parameter) { if (strpos($parameter, '=') == false) { /** * Find variable type and set it */ switch ($parameter) { case "test-case": $type = $parameter; break; case "output-only": $type = $parameter; break; case "tester.cpp": $method = $parameter; break; case "tester.py": $method = $parameter; break; case "diff": $method = $parameter; break; case "برنامه-نویسی": $type = "test-case"; break; case "فقط-خروجی": $type = "output-only"; break; case "java": $language = "Java"; break; case "Java": $language = "Java"; break; case "python2": $language = "Python 2"; break; case "python3": $language = "Python 3"; break; case "cpp": $language = "C++"; break; case "c++": $language = "C++"; break; case "C++": $language = "C++"; break; case "c": $language = "C"; break; case "C": $language = "C"; break; default: if (is_numeric($parameter)) { $runtime = $parameter; } else { $problem_name = $parameter; } } } else { list($key, $value) = explode('=', $parameter); switch ($key) { /** * Remove key string if included */ case "problem": $problem_name = $value; break; case "type": $type = $value; break; case "time": $runtime = $value; break; case "method": switch ($value) { case "tester.cpp": $method = $value; break; case "tester.py": $method = $value; break; case "diff": $method = $value; } case "language": switch ($value) { case "java": $language = "Java"; break; case "Java": $language = "Java"; break; case "python2": $language = "Python 2"; break; case "python3": $language = "Python 3"; break; case "cpp": $language = "C++"; break; case "c++": $language = "C++"; break; case "C++": $language = "C++"; break; case "c": $language = "C"; break; case "C": $language = "C"; break; } } } } /** * Set default value for parameters */ if ($problem_name == null) { $problem_name = $ID; } $judge = "داوری:" . $problem_name; if ($type === null) { $type = "test-case"; } if ($runtime === null) { $runtime = 0; } if ($language === null) { $language = "all"; } if ($method === null) { $method = "diff"; } if ($conf['useslash']) { $problem_name = str_replace('/', ':', $problem_name); } return array('mode' => "judge", 'problem_name' => $problem_name, 'type' => $type, 'runtime' => $runtime, 'language' => $language, 'judge' => $judge, 'method' => $method); } /** * Render xhtml output or metadata * * @param string $mode Renderer mode (supported modes: xhtml) * @param Doku_Renderer $renderer The renderer * @param array $data The data from the handler() function * @return bool If rendering was successful. */ public function render($mode, Doku_Renderer $renderer, $data) { if ($mode != 'xhtml') { return false; } if ($data["mode"] == "judge") { $renderer->doc .= $this->render_judge($data); } elseif ($data["mode"] == "scoreboard") { $renderer->doc .= $this->render_scoreboard($data); } return true; } public function render_judge($data) { global $conf, $USERINFO; $html = ''; /** * Show plugin to logged in user */ if ($_SERVER['REMOTE_USER']) { /** * Show plugin if problem_name page exists */ if (page_exists($data['problem_name'])) { $page_answer_exist = page_exists($data['judge']); $media_files = array_filter(glob($conf['mediadir'] . "/" . str_replace("%3A", "/", urlencode($data['judge'])) . "/" . "*")); $media_answer_exist = !empty($media_files); if (($page_answer_exist || $media_answer_exist) && in_array("user", $USERINFO['grps'])) { $html .= '

' . $this->getLang('btn_submit_answer') . '

'; if ($data['type'] === "output-only") { $html .= '
'; } elseif ($data['type'] === "test-case") { $html .= ' '; } $html .= '
'; $html .= '
'; define('DBFILE', dirname(__FILE__) . '/submissions.sqlite'); date_default_timezone_set('Asia/Tehran'); $db = new SQLite3(DBFILE); $submissions = $db->querySingle('SELECT COUNT(*) FROM submissions WHERE problem_name = "' . $data['problem_name'] . '"AND username="' . $_SERVER['REMOTE_USER'] . '";'); if (!empty($submissions)) { $html .= '
'; } else { $html .= ' '; } }; if (in_array($this->getConf('editors_group'), $USERINFO['grps']) || in_array("admin", $USERINFO['grps'])) { if (page_exists($data['problem_name'])) { if (auth_quickaclcheck($data['judge']) >= AUTH_EDIT) { if ($data['type'] === "test-case") { if ($media_answer_exist) { $html .= ''; } elseif ($page_answer_exist) { $html .= ''; } else { $html .= ''; } } else { if ($page_answer_exist) { $html .= ''; } else { $html .= ''; } } } } else { $html .= ''; } } } return $html; } public function render_scoreboard($data) { $crud = plugin_load('helper', 'judge_crud', true); $html = '
'; $i = 1; foreach ($data["questions"] as &$problem_name) { $submissions = $crud->tableRender(array('problem_name' => $problem_name, 'type' => "test-case", 'user' => null), "html", $i, "timestamp"); $html .= $submissions["submissions_table"]; $i = $submissions["count"]; } $html .= '
' . $this->getLang('count_number') . '' . $this->getLang('question_name') . '' . $this->getLang('sender') . '' . $this->getLang('timestamp') . '' . $this->getLang('language') . '' . $this->getLang('status') . '
'; return $html; } } // vim:ts=4:sw=4:et: