public static function save($data, $id = 0) { $model = new OjContestApplyModel(); if (0 == $id) { // 判断是否已经存在 $applyInfo = self::getDetail($data['contest_id'], $data['user_id']); $contestInfo = OjContestInterface::getById(array('id' => $data['contest_id'])); if (empty($contestInfo)) { throw new InterfaceException('竞赛不存在!'); } if ($contestInfo['type'] != ContestVars::TYPE_APPLY) { throw new InterfaceException('比赛不需要报名!'); } if ($contestInfo['end_time'] < time()) { throw new InterfaceException('比赛已经结束!'); } $data['status'] = ContestVars::APPLY_QUEUE; $data['is_diy'] = $contestInfo['is_diy']; if (!empty($applyInfo)) { if ($applyInfo['status'] == ContestVars::APPLY_ACCEPTED) { throw new InterfaceException('报名已通过,无法修改!'); } $model->updateById($applyInfo['id'], $data); return $applyInfo['id']; } else { $id = $model->insert($data); return $id; } } else { // 对某些字段特殊处理 $updateData = $data; $affects = $model->updateById($id, $updateData); return $affects; } }
public function defaultAction() { $pageSize = 20; // 获取参数 $page = Pager::get(); $status = (int) Request::getGET('status', -1); $contestId = (int) Request::getGET('contest-id', 0); $where = array(); $where[] = array('is_diy', '=', 0); if (!empty($contestId)) { $where[] = array('contest_id', '=', $contestId); } if ($status != -1) { $where[] = array('status', '=', $status); } // 获取数据 $offset = ($page - 1) * $pageSize; $applyList = OjContestApplyInterface::getList(array('where' => $where, 'limit' => $pageSize, 'offset' => $offset)); $allCount = 0; $userHash = array(); $contestHash = array(); if (!empty($applyList)) { $allCount = OjContestApplyInterface::getCount($where); $userIds = array_column($applyList, 'user_id'); $userHash = UserCommonInterface::getById(array('id' => $userIds)); $contestIds = array_unique(array_column($applyList, 'contest_id')); $contestHash = OjContestInterface::getById(array('id' => $contestIds)); } // 缓存部分的html $html = array(); $html['pager'] = $this->view->fetch(array('renderAllCount' => $allCount, 'renderPageSize' => $pageSize, 'renderRadius' => 8), 'widget/pager.php'); $this->renderFramework(array('html' => $html, 'applyList' => $applyList, 'contestHash' => $contestHash, 'userHash' => $userHash), 'contest/apply_list.php'); }
public function ajaxChangeStatusAction() { $applyId = Request::getPOST('apply-id'); $op = Request::getPOST('op'); if (!in_array($op, array(1, 2)) || empty($applyId)) { $this->renderError('参数错误!'); } $applyInfo = OjContestApplyInterface::getById(array('id' => $applyId)); if (empty($applyInfo)) { $this->renderError('报名信息不存在!'); } // 只能处理自己竞赛下的报名 $where = array(array('user_id', '=', $this->loginUserInfo['id']), array('is_diy', '=', 1)); $contestHash = OjContestInterface::getList(array('where' => $where)); $contestHash = Arr::listToHash('id', $contestHash); $contestIds = array_keys($contestHash); if (!in_array($applyInfo['contest_id'], $contestIds)) { $this->renderError('你没有权限操作!'); } if ($op == 1 && $applyInfo['status'] == ContestVars::APPLY_ACCEPTED || $op == 2 && $applyInfo['status'] == ContestVars::APPLY_REJECTED) { $msg = $op == 1 ? '已经通过!' : '已经拒绝!'; $this->renderError($msg); } if ($op == 1) { OjContestApplyInterface::accept(array('id' => $applyId)); } else { OjContestApplyInterface::reject(array('id' => $applyId)); } $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '操作成功!'); $this->renderAjax(0); }
public function ajaxApplyAction() { if (empty($this->loginUserInfo)) { $this->renderError('请登录!'); } $contestId = (int) Request::getPOST('contest-id'); $realName = trim(Request::getPOST('real-name')); $xuehao = trim(Request::getPOST('xuehao')); $xueyuan = (int) Request::getPOST('xueyuan'); $sex = (int) Request::getPOST('sex'); // 校验 if (empty($realName) || empty($xuehao) || empty($xueyuan) || empty($sex) || !array_key_exists($xueyuan, ContestVars::$XUEYUAN) || !in_array($sex, array(1, 2)) || mb_strlen($realName, 'utf8') < 2 || mb_strlen($realName, 'utf8') > 4) { $this->renderError('参数错误!'); } $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo)) { $this->renderError('比赛不存在!'); } if ($contestInfo['type'] != ContestVars::TYPE_APPLY) { $this->renderError('比赛不需要报名!'); } if ($contestInfo['end_time'] < time()) { $this->renderError('比赛已经结束!'); } $applyInfo = OjContestApplyInterface::getDetail(array('contest_id' => $contestId, 'user_id' => $this->loginUserInfo['id'])); if (!empty($applyInfo) && $applyInfo['status'] == ContestVars::APPLY_ACCEPTED) { $this->renderError('报名已通过,无法修改!'); } $data = array('contest_id' => $contestId, 'user_id' => $this->loginUserInfo['id'], 'real_name' => $realName, 'xuehao' => $xuehao, 'xueyuan' => $xueyuan, 'sex' => $sex); OjContestApplyInterface::save($data); $this->renderAjax(0); }
public function defaultAction() { $pageSize = 50; // 获取参数 $page = Pager::get(); $contestId = (int) Request::getGET('contest-id'); $status = (int) Request::getGET('status', -1); $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo) || $contestInfo['hidden'] || $contestInfo['type'] != ContestVars::TYPE_APPLY) { $this->renderError('竞赛不存在,或者竞赛不需要报名!'); } // 构建where $where = array(); $where[] = array('contest_id', '=', $contestId); if ($status != -1) { $where[] = array('status', '=', $status); } // 获取数据 $offset = ($page - 1) * $pageSize; $applyList = OjContestApplyInterface::getList(array('where' => $where, 'limit' => $pageSize, 'offset' => $offset)); $allCount = OjContestApplyInterface::getCount($where); // userHash $userIds = array_unique(array_column($applyList, 'user_id')); $userHash = UserCommonInterface::getById(array('id' => $userIds)); // 缓存部分的html $html = array(); $html['pager'] = $this->view->fetch(array('renderAllCount' => $allCount, 'renderPageSize' => $pageSize, 'renderRadius' => 8), 'widget/pager.php'); $this->renderFramework(array('html' => $html, 'applyList' => $applyList, 'contestInfo' => $contestInfo, 'userHash' => $userHash), 'contest/apply_list.php'); }
public function ajaxAddAction() { // 插入空数据 $data = array('is_diy' => 0, 'user_id' => $this->loginUserInfo['id']); OjContestInterface::save($data); $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '您成功创建了比赛,请编辑!'); $this->renderAjax(0); }
public function defaultAction() { $contestId = Request::getGET('contest-id'); $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo)) { $this->renderError('竞赛不存在!'); } $this->renderFramework(array('contestInfo' => $contestInfo), 'contest/detail.php'); }
public function ajaxSubmitAction() { // 获取参数 $contestId = (int) Request::getPOST('contest-id'); $title = trim(Request::getPOST('title')); $type = Request::getPOST('type'); $password = trim(Request::getPOST('password')); $notice = trim(Request::getPOST('notice')); $beginTime = strtotime(trim(Request::getPOST('begin-time'))); $endTime = strtotime(trim(Request::getPOST('end-time'))); $description = Request::getPOST('description', '', true); $problemHidden = Request::getPOST('problem-hidden', 0); // 参数校验1 if (empty($title) || mb_strlen($title, 'utf8') > 50) { $this->renderError('标题必填,限制50个字以内!'); } if (mb_strlen($notice, 'utf8') > 100) { $this->renderError('提示限制100个字以内!'); } if (!preg_match('/^[A-Za-z0-9_]{0,20}$/', $password)) { $this->renderError('密码格式不合法!'); } if ($type == ContestVars::TYPE_PASSWORD && empty($password)) { $this->renderError('密码不能为空!'); } if (empty($type) || !array_key_exists($type, ContestVars::$TYPE_FORMAT)) { $this->renderError('请选择比赛访问权限!'); } // 竞赛是否存在 $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo)) { $this->renderError('竞赛不存在!'); } // 权限 if ($contestInfo['user_id'] != $this->loginUserInfo['id']) { $this->renderError('你没有权限操作!'); } // 参数校验2 if ($contestInfo['is_active']) { $beginTime = $contestInfo['begin_time']; } if (empty($beginTime) || empty($endTime) || $beginTime >= $endTime) { $this->renderError('比赛时间不合法!'); } // 时间不能超过1年 if ($endTime - $beginTime > 366 * 86400) { $this->renderError('比赛时间不能超过1年!'); } $data = array('id' => $contestId, 'title' => $title, 'type' => $type, 'password' => $password, 'notice' => $notice, 'begin_time' => $beginTime, 'end_time' => $endTime, 'description' => $description, 'problem_hidden' => $problemHidden ? 1 : 0); OjContestInterface::save($data); $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '操作成功!'); $this->renderAjax(0); }
public function defaultAction() { list($rankHash, $mat, $userHash) = OjContestInterface::getRankBoard(array('id' => $this->contestInfo['id'])); // 如果是报名,获取报名列表 $applyHash = array(); if ($this->contestInfo['type'] == ContestVars::TYPE_APPLY) { $where = array(array('contest_id', '=', $this->contestInfo['id'])); $applyHash = OjContestApplyInterface::getList(array('where' => $where)); $applyHash = Arr::listToHash('user_id', $applyHash); } $this->renderFramework(array('rankHash' => $rankHash, 'mat' => $mat, 'userHash' => $userHash, 'applyHash' => $applyHash), 'rank/list.php'); }
public function defaultAction() { $contestId = Request::getGET('contest-id'); $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo)) { $this->renderError('竞赛不存在!'); } // 权限 if ($contestInfo['user_id'] != $this->loginUserInfo['id']) { $this->renderError('你没有权限查看!'); } $this->renderFramework(array('contestInfo' => $contestInfo), 'setup/contest/detail.php'); }
public function __construct() { parent::__construct(); // 校验登陆 if (empty($this->loginUserInfo)) { $this->login(); } // 获取$contestId $contestId = (int) Request::getREQUEST('contest-id', 0); if (empty($contestId)) { $this->render404('比赛ID不存在!'); } // 获取$contestInfo $this->contestInfo = OjContestInterface::getDetail(array('id' => $contestId)); if (empty($this->contestInfo) || $this->contestInfo['hidden']) { $this->render404('比赛不存在!'); } if ($this->contestInfo['type'] == ContestVars::TYPE_APPLY) { $this->applyInfo = OjContestApplyInterface::getDetail(array('contest_id' => $contestId, 'user_id' => $this->loginUserInfo['id'])); } // 管理员 $isOjAdmin = RootCommonInterface::allowed(array('user_id' => $this->loginUserInfo['id'], 'path' => '/hqoj/admin')); if ($isOjAdmin || $this->contestInfo['user_id'] == $this->loginUserInfo['id']) { $this->isContestAdmin = true; } // 如果未注册,未输入密码,比赛未开始,那么跳转到比赛首页 if (Router::$CONTROLLER != 'index') { if ($this->contestInfo['type'] == ContestVars::TYPE_APPLY) { if (empty($this->applyInfo) || $this->applyInfo['status'] != ContestVars::APPLY_ACCEPTED) { $this->setNotice('error', '您未通过报名!'); $url = '/?contest-id=' . $contestId; Url::redirect($url); } } else { if ($this->contestInfo['type'] == ContestVars::TYPE_PASSWORD) { if ($this->password != $this->contestInfo['password']) { $this->setNotice('error', '请输入密码!'); $url = '/?contest-id=' . $contestId; Url::redirect($url); } } } if (time() < $this->contestInfo['begin_time']) { $this->setNotice('error', '比赛未开始!'); $url = '/?contest-id=' . $contestId; Url::redirect($url); } } $this->view->assign(array('contestInfo' => $this->contestInfo, 'applyInfo' => $this->applyInfo, 'password' => $this->password, 'isContestAdmin' => $this->isContestAdmin)); }
public function ajaxAddAction() { // 如果非激活的比赛超过50,那么提示编辑非激活的比赛 $where = array(array('is_active', '=', 0), array('is_diy', '=', 1), array('user_id', '=', $this->loginUserInfo['id'])); $count = OjContestInterface::getCount($where); if ($count > 50) { $this->renderError('您有太多比赛没有编辑哦,请直接编辑!'); } // 插入空数据 $data = array('is_diy' => 1, 'user_id' => $this->loginUserInfo['id']); OjContestInterface::save($data); $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '您成功创建了DIY比赛,请编辑!'); $this->renderAjax(0); }
public function ajaxHideAction() { $contestId = Request::getPOST('contest-id'); $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo)) { $this->renderError('竞赛不存在!'); } if ($contestInfo['hidden']) { $this->renderError('竞赛已经隐藏!'); } OjContestInterface::hide(array('id' => $contestId)); $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '隐藏成功!'); $this->renderAjax(0); }
public function defaultAction() { $problemHash = OjContestInterface::getProblemHash(array('id' => $this->contestInfo['id'])); // 获取该用户比赛中提交的solutionList $where = array(array('contest_id', '=', $this->contestInfo['id']), array('user_id', '=', $this->loginUserInfo['id'])); $order = array('id' => 'ASC'); $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => $order)); // 构建hash,globalId => solutionInfo $userSolution = array(); foreach ($solutionList as $solutionInfo) { $globalId = $solutionInfo['problem_global_id']; if (!array_key_exists($globalId, $userSolution) || $solutionInfo['result'] == StatusVars::ACCEPTED) { $userSolution[$globalId] = $solutionInfo; } } $this->renderFramework(array('problemHash' => $problemHash, 'userSolution' => $userSolution), 'problem/list.php'); }
public function ajaxRemoveProblemAction() { $contestId = (int) Request::getPOST('contest-id'); $globalId = (int) Request::getPOST('global-id'); $problemInfo = OjProblemInterface::getById(array('id' => $globalId)); if (empty($problemInfo)) { $this->renderError('题目不存在!'); } $contestInfo = OjContestInterface::getById(array('id' => $contestId)); if (empty($contestInfo)) { $this->renderError('竞赛不存在!'); } // 更新数据 OjContestInterface::removeProblem(array('id' => $contestId, 'global_id' => $globalId)); $this->setNotice(FrameworkVars::NOTICE_SUCCESS, '移除成功!'); $this->renderAjax(0); }
public function defaultAction() { $pageSize = 20; // 获取参数 $page = Pager::get(); $title = Request::getGET('title'); $passed = (int) Request::getGET('passed', 0); $diy = (int) Request::getGET('diy', 0); // 构建where $where = array(); $where[] = array('hidden', '=', 0); if ($passed) { $where[] = array('end_time', '<=', time()); } else { $where[] = array('end_time', '>', time()); } if ($diy) { $where[] = array('is_diy', '=', 1); } else { $where[] = array('is_diy', '=', 0); } if (!empty($title)) { $where[] = array('title', 'LIKE', "%{$title}%"); } if ($passed) { $order = array('end_time' => 'DESC'); } else { $order = array('end_time' => 'ASC'); } // 获取数据 $offset = ($page - 1) * $pageSize; $tmpContestList = OjContestInterface::getList(array('where' => $where, 'order' => $order, 'limit' => $pageSize, 'offset' => $offset)); $allCount = OjContestInterface::getCount($where); // 将进行中的比赛提前 $contestList = array(); if (!$passed) { foreach ($tmpContestList as $i => $contestInfo) { if ($contestInfo['begin_time'] < time()) { $contestList[] = $contestInfo; unset($tmpContestList[$i]); } } foreach ($tmpContestList as $i => $contestInfo) { $contestList[] = $contestInfo; unset($tmpContestList[$i]); } } else { $contestList = $tmpContestList; } $userIds = array_unique(array_column($contestList, 'user_id')); $userHash = UserCommonInterface::getById(array('id' => $userIds)); // 格式化 foreach ($contestList as &$contestInfo) { // row_class $now = time(); if ($contestInfo['end_time'] < $now) { $contestInfo['row_css'] = 'passed'; } else { if ($contestInfo['begin_time'] > $now) { $contestInfo['row_css'] = 'pending'; } else { $contestInfo['row_css'] = 'running'; } } // type_format $type = $contestInfo['type']; if ($type == ContestVars::TYPE_PUBLIC) { $contestInfo['type_format'] = '<p class="red">公开</p>'; } else { if ($type == ContestVars::TYPE_APPLY) { $contestInfo['type_format'] = '<p class="orange">报名</p>'; } else { if ($type == ContestVars::TYPE_PASSWORD) { $contestInfo['type_format'] = '<p class="green">密码</p>'; } else { $contestInfo['type_format'] = '<p class="gray">未定义</p>'; } } } } // 缓存部分的html $html = array(); $html['pager'] = $this->view->fetch(array('renderAllCount' => $allCount, 'renderPageSize' => $pageSize, 'renderRadius' => 8), 'widget/pager.php'); // 输出 $this->renderFramework(array('contestList' => $contestList, 'userHash' => $userHash, 'html' => $html), 'contest/list.php'); }
public static function save($data, $id = 0, $trans = null) { if (0 == $id) { $globalId = $data['global_id']; $language = $data['language']; $userId = $data['user_id']; $source = $data['source']; $contestId = Arr::get('contest_id', $data, 0); // 设置默认语言 Cookie::set('default_language', $language, time() + 365 * 86400); $problemInfo = OjProblemInterface::getById(array('id' => $globalId)); if (empty($problemInfo)) { throw new InterfaceException('题目不存在!'); } if (!array_key_exists($data['language'], StatusVars::$LANGUAGE_SUPPORT[$problemInfo['remote']])) { throw new InterfaceException('编译器不支持!'); } // 连续提交判断 if (self::submitAlready($userId)) { throw new InterfaceException('提交频繁!'); } // 非法字符判断 if ($problemInfo['remote'] == StatusVars::REMOTE_HDU) { if (false === iconv('UTF-8', 'GBK', $source)) { throw new InterfaceException('代码中存在非法字符!'); } } // 开始事务 $innerTrans = $trans; if (null == $trans) { $innerTrans = new Trans(DbConfig::$SERVER_TRANS); $innerTrans->begin(); } $solutionModel = new OjSolutionModel($innerTrans); $submitTime = time(); $data = array('problem_global_id' => $globalId, 'remote' => $problemInfo['remote'], 'problem_id' => $problemInfo['problem_id'], 'problem_code' => $problemInfo['problem_code'], 'user_id' => $userId, 'submit_time' => $submitTime, 'submit_ip' => Http::getClientIp(), 'language' => $language, 'result' => StatusVars::QUEUE, 'code_length' => strlen($source), 'remote_uid' => -1); $userInfo = UserCommonInterface::getById(array('id' => $userId)); if (0 == $contestId) { $solutionId = $solutionModel->insert($data); UserCommonInterface::save(array('trans' => $innerTrans, 'id' => $userId, 'submit_all' => $userInfo['submit_all'] + 1)); OjProblemInterface::save(array('trans' => $innerTrans, 'id' => $problemInfo['id'], 'submit' => $problemInfo['submit'])); } else { $contestInfo = OjContestInterface::getDetail(array('id' => $contestId)); if (empty($contestInfo)) { throw new InterfaceException('竞赛不存在!'); } if ($submitTime > $contestInfo['end_time']) { throw new InterfaceException('比赛已经结束!'); } if ($submitTime < $contestInfo['begin_time']) { throw new InterfaceException('比赛未开始!'); } // 获取当前的submit_order $lastRow = OjSolutionInterface::getRow(array('where' => array(array('contest_id', '=', $contestId)), 'order' => array('id' => 'DESC'))); $data['contest_id'] = $contestId; $data['contest_submit_order'] = empty($lastRow) ? 1 : intval($lastRow['contest_submit_order']) + 1; $data['contest_submit_second'] = $submitTime - $contestInfo['begin_time']; $data['contest_end_time'] = $contestInfo['end_time']; $solutionId = $solutionModel->insert($data); // 激活比赛 if (!$contestInfo['is_active']) { OjContestInterface::save(array('id' => $contestId, 'trans' => $innerTrans, 'is_active' => 1)); } } // 如果是HQU题库,加入队列 if ($problemInfo['remote'] == StatusVars::REMOTE_HQU) { OjJudgeInterface::save(array('trans' => $innerTrans, 'problem_id' => $problemInfo['problem_id'], 'language' => $language, 'source' => $source, 'user_id' => $userId, 'solution_id' => $solutionId)); } // 保存代码 OjSolutionCodeInterface::save(array('trans' => $innerTrans, 'solution_id' => $solutionId, 'source' => $source)); if (null == $trans && null != $innerTrans) { // 没有外部事务,并且存在内部事务 $innerTrans->commit(); } // 设置重复提交缓存 $memcached = MemcachedPool::getMemcached(MemcachedConfig::$SERVER_COMMON); $key = MemcachedKeys::OJ_SECOND_SUBMIT_ . $userId; $memcached->set($key, true, time() + 5); return $solutionId; } else { // 开始事务 $innerTrans = $trans; if (null == $trans && array_key_exists('result', $data)) { // 开启内部事务条件 $innerTrans = new Trans(DbConfig::$SERVER_TRANS); $innerTrans->begin(); } $model = new OjSolutionModel($innerTrans); if (array_key_exists('result', $data)) { self::updateResult($id, $data['result'], $innerTrans); unset($data['result']); } // 对某些字段特殊处理 $updateData = $data; $affects = $model->updateById($id, $updateData); if (null == $trans && null != $innerTrans) { // 没有外部事务,并且存在内部事务 $innerTrans->commit(); } return $affects; } }