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 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 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 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;
     }
 }