public static function getRankBoard($id)
 {
     $setInfo = self::getById($id);
     if (empty($setInfo)) {
         throw new InterfaceException('专题不存在!');
     }
     $globalIds = json_decode($setInfo['problem_set'], true);
     if (empty($globalIds)) {
         return array(array(), array(), array());
     }
     // 取出提交记录
     $where = array(array('problem_global_id', 'IN', $globalIds), array('result', '>=', 4));
     $order = array('id' => 'ASC');
     $solutionList = OjSolutionInterface::getList(array('field' => 'id,user_id,problem_global_id,result', 'where' => $where, 'order' => $order));
     // 计算$rankHash
     $rankHash = array();
     $mat = array();
     $firstBlood = array();
     // 标记第一滴血
     foreach ($solutionList as $solutionInfo) {
         $globalId = $solutionInfo['problem_global_id'];
         $userId = $solutionInfo['user_id'];
         $result = $solutionInfo['result'];
         if (!isset($rankHash[$userId])) {
             $rankHash[$userId]['solved'] = 0;
             $rankHash[$userId]['all_fail_count'] = 0;
         }
         if (!isset($mat[$userId][$globalId])) {
             $mat[$userId][$globalId] = array();
             $mat[$userId][$globalId]['fail_count'] = 0;
         }
         // 已经通过的,直接continue
         if (isset($mat[$userId][$globalId]['accepted'])) {
             continue;
         }
         // 如果这条solution是accepted
         if ($result == StatusVars::ACCEPTED) {
             $mat[$userId][$globalId]['accepted'] = 1;
             $rankHash[$userId]['solved']++;
             // 标记1血
             if (!isset($firstBlood[$globalId])) {
                 $mat[$userId][$globalId]['first_blood'] = 1;
                 $firstBlood[$globalId] = 1;
             }
             continue;
         } else {
             $mat[$userId][$globalId]['fail_count']++;
             $rankHash[$userId]['all_fail_count']++;
         }
     }
     uasort($rankHash, array('OjProblemSetLogic', 'cmp'));
     // 获取用户信息
     $userIds = array_unique(array_column($solutionList, 'user_id'));
     $userHash = UserCommonInterface::getById(array('id' => $userIds));
     return array($rankHash, $mat, $userHash);
 }
 public function defaultAction()
 {
     $contestId = Request::getGET('contest-id');
     $contestInfo = OjContestInterface::getDetail(array('id' => $contestId));
     if (empty($contestInfo)) {
         $this->renderError('竞赛不存在!');
     }
     $problemHash = OjProblemInterface::getById(array('id' => $contestInfo['global_ids']));
     $solutionList = OjSolutionInterface::getList(array('field' => 'problem_global_id', 'where' => array(array('contest_id', '=', $contestId))));
     $submitGlobalIds = array_unique(array_column($solutionList, 'problem_global_id'));
     $this->renderFramework(array('contestInfo' => $contestInfo, 'problemHash' => $problemHash, 'globalIds' => $contestInfo['global_ids'], 'submitGlobalIds' => $submitGlobalIds), 'contest/set_problem.php');
 }
 public function defaultAction()
 {
     $pageSize = 100;
     // 获取参数
     $page = Pager::get();
     $remote = (int) Request::getGET('remote', 0);
     $keyword = Request::getGET('keyword');
     $searchType = (int) Request::getGET('search-type', 1);
     // 构建where
     $where = array();
     $where[] = array('remote', '=', $remote);
     $where[] = array('hidden', '=', 0);
     if (!empty($keyword)) {
         if ($searchType == 1) {
             $where[] = array('OR' => array(array('problem_code', '=', $keyword), array('title', 'LIKE', "%{$keyword}%")));
         } else {
             if ($searchType == 2) {
                 $where[] = array('OR' => array(array('problem_code', '=', $keyword), array('source', 'LIKE', "%{$keyword}%")));
             }
         }
     }
     // 获取数据
     $order = array('problem_code' => 'ASC');
     $offset = ($page - 1) * $pageSize;
     $problemList = OjProblemInterface::getList(array('where' => $where, 'order' => $order, 'limit' => $pageSize, 'offset' => $offset));
     $allCount = OjProblemInterface::getCount($where);
     // 获取用户解决的题目
     $userSolution = array();
     if ($this->loginUserInfo) {
         $globalIds = array_column($problemList, 'id');
         $where = array(array('user_id', '=', $this->loginUserInfo['id']), array('contest_id', '=', 0), array('problem_global_id', 'IN', $globalIds));
         $solutionList = OjSolutionInterface::getList(array('where' => $where));
         foreach ($solutionList as $solutionId => $solutionInfo) {
             $globalId = $solutionInfo['problem_global_id'];
             if (!array_key_exists($globalId, $userSolution) || $solutionInfo['result'] == StatusVars::ACCEPTED) {
                 $userSolution[$globalId] = $solutionInfo;
             }
         }
     }
     $userHash = array();
     if ($allCount > 0) {
         $userIds = array_unique(array_column($problemList, 'user_id'));
         $userHash = UserCommonInterface::getById(array('id' => $userIds));
     }
     // 缓存部分的html
     $html = array();
     $html['pager'] = $this->view->fetch(array('renderAllCount' => $allCount, 'renderPageSize' => $pageSize, 'renderRadius' => 10), 'widget/pager.php');
     $tpl = $remote ? 'problem/list_remote.php' : 'problem/list.php';
     // 输出
     $this->renderFramework(array('html' => $html, 'problemList' => $problemList, 'userSolution' => $userSolution, 'userHash' => $userHash), $tpl);
 }
 public function defaultAction()
 {
     $username = Request::getGET('username');
     if (empty($username)) {
         $this->renderError();
     }
     // 校验用户
     $userInfo = UserCommonInterface::getByLoginName(array('login_name' => $username));
     if (empty($userInfo)) {
         $this->renderError();
     }
     // 获取solutionList
     $where = array(array('user_id', '=', $userInfo['id']));
     $order = array('remote' => 'ASC', 'problem_code' => 'ASC');
     $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => $order));
     // 计算排名,先按题数,再按照提交次数
     $where = array(array('OR' => array(array('solved_all', '>', $userInfo['solved_all']), array(array('solved_all', '=', $userInfo['solved_all']), array('submit_all', '>', $userInfo['submit_all'])), array(array('solved_all', '=', $userInfo['solved_all']), array('submit_all', '=', $userInfo['submit_all']), array('id', '<', $userInfo['id'])))));
     $prevCount = UserCommonInterface::getCount($where);
     $rank = intval($prevCount) + 1;
     // 计算解决的题目
     $solvedProblemList = array();
     $visited = array();
     // 标记数组
     foreach ($solutionList as $solutionInfo) {
         $remote = $solutionInfo['remote'];
         $problemCode = $solutionInfo['problem_code'];
         if (!isset($visited[$remote][$problemCode]) && $solutionInfo['result'] == StatusVars::ACCEPTED) {
             $problemInfo = array('remote' => $remote, 'problem_code' => $problemCode);
             $solvedProblemList[] = $problemInfo;
             $visited[$remote][$problemCode] = 1;
         }
     }
     // 计算未解决的题目
     $unSolvedProblemList = array();
     $visited2 = array();
     foreach ($solutionList as $solutionInfo) {
         $remote = $solutionInfo['remote'];
         $problemCode = $solutionInfo['problem_code'];
         if (!isset($visited2[$remote][$problemCode]) && !isset($visited[$remote][$problemCode])) {
             $problemInfo = array('remote' => $remote, 'problem_code' => $problemCode);
             $unSolvedProblemList[] = $problemInfo;
             $visited2[$remote][$problemCode] = 1;
         }
     }
     // 输出
     $this->renderFramework(array('rank' => $rank, 'userInfo' => $userInfo, 'solvedProblemList' => $solvedProblemList, 'unSolvedProblemList' => $unSolvedProblemList), 'user/my.php');
 }
 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 defaultAction()
 {
     $setId = (int) Request::getGET('set-id');
     if (empty($setId)) {
         $this->renderError();
     }
     $setInfo = OjProblemSetInterface::getById(array('id' => $setId));
     if (empty($setInfo)) {
         $this->renderError();
     }
     if (!$this->isOjAdmin && $setInfo['hidden'] && $this->loginUserInfo['id'] != $setInfo['user_id']) {
         Cookie::delete('current_set');
         $this->renderError('您没有权限查看!');
     }
     // 设置当前的set
     Cookie::set('current_set', $setId);
     $problemJson = $setInfo['problem_set'];
     $globalIds = (array) json_decode($problemJson, true);
     // 按照$globalIds顺序
     $problemList = OjProblemInterface::getById(array('id' => $globalIds));
     // 获取用户解决的题目
     $userSolution = array();
     if ($this->loginUserInfo) {
         $where = array(array('problem_global_id', 'IN', $globalIds), array('user_id', '=', $this->loginUserInfo['id']));
         $solutionList = OjSolutionInterface::getList(array('where' => $where));
         foreach ($solutionList as $solutionInfo) {
             $globalId = $solutionInfo['problem_global_id'];
             if (!array_key_exists($globalId, $userSolution) || $solutionInfo['result'] == StatusVars::ACCEPTED) {
                 $userSolution[$globalId] = $solutionInfo;
             }
         }
     }
     // userInfo
     $userInfo = UserCommonInterface::getById(array('id' => $setInfo['user_id']));
     $this->renderFramework(array('setInfo' => $setInfo, 'problemList' => $problemList, 'userSolution' => $userSolution, 'userInfo' => $userInfo), 'set/set_problem.php');
 }
 public static function getRankBoard($id)
 {
     $contestInfo = self::getDetail($id);
     if (empty($contestInfo)) {
         throw new InterfaceException('专题不存在!');
     }
     // 取出提交记录
     $where = array(array('contest_id', '=', $id), array('result', '>=', 4), array('problem_global_id', 'IN', $contestInfo['global_ids']));
     $order = array('id' => 'ASC');
     $solutionList = OjSolutionInterface::getList(array('field' => 'id,user_id,problem_global_id,result,contest_submit_second', 'where' => $where, 'order' => $order, 'include_contest' => true));
     // 计算$rankHash
     $rankHash = array();
     $mat = array();
     $firstBlood = array();
     // 标记第一滴血
     foreach ($solutionList as $solutionInfo) {
         $globalId = $solutionInfo['problem_global_id'];
         $userId = $solutionInfo['user_id'];
         $result = $solutionInfo['result'];
         if (!isset($rankHash[$userId])) {
             $rankHash[$userId]['solved'] = 0;
             $rankHash[$userId]['cost_second'] = 0;
         }
         if (!isset($mat[$userId][$globalId])) {
             $mat[$userId][$globalId] = array('accepted' => 0, 'first_blood' => 0, 'fail_count' => 0, 'pass_second' => 0);
         }
         // 已经通过的,直接continue
         if ($mat[$userId][$globalId]['accepted']) {
             continue;
         }
         // 如果这条solution是accepted
         if ($result == StatusVars::ACCEPTED) {
             // 记录到mat
             $mat[$userId][$globalId]['accepted'] = 1;
             $mat[$userId][$globalId]['pass_second'] = $solutionInfo['contest_submit_second'];
             if (!isset($firstBlood[$globalId])) {
                 $mat[$userId][$globalId]['first_blood'] = 1;
                 $firstBlood[$globalId] = 1;
             }
             // 记录到rankHash
             $rankHash[$userId]['solved']++;
             $rankHash[$userId]['cost_second'] += $mat[$userId][$globalId]['pass_second'] + $mat[$userId][$globalId]['fail_count'] * 20 * 60;
             continue;
         } else {
             $mat[$userId][$globalId]['fail_count']++;
         }
     }
     uasort($rankHash, array('OjContestLogic', 'cmp'));
     // 获取用户信息
     $userIds = array_unique(array_column($solutionList, 'user_id'));
     $userHash = UserCommonInterface::getById(array('id' => $userIds));
     return array($rankHash, $mat, $userHash);
 }
 public function defaultAction()
 {
     $pageSize = 15;
     $username = Request::getGET('username');
     $remote = (int) Request::getGET('remote', -1);
     $problemCode = Request::getGET('problem-code');
     // 因为ZOJ的题号是code
     $language = (int) Request::getGET('language', -1);
     $result = (int) Request::getGET('result', -1);
     $contestId = (int) Request::getGET('contest-id');
     $globalId = (int) Request::getGET('global-id');
     // 获取id
     $maxId = (int) Request::getGET('max-id', -1);
     $minId = (int) Request::getGET('min-id', -1);
     // 获取userInfo,username转换为userId
     $userInfo = array();
     if (!empty($username)) {
         $userInfo = UserCommonInterface::getByLoginName(array('login_name' => $username));
     }
     // 构建where
     $where = array();
     if ($this->isOjAdmin) {
         if ($contestId) {
             $where[] = array('contest_id', '=', $contestId);
         }
     }
     if (!empty($username)) {
         $where[] = array('user_id', '=', Arr::get('id', $userInfo, 0));
     }
     if ($remote != -1) {
         $where[] = array('remote', '=', $remote);
     }
     if (!empty($problemCode)) {
         $where[] = array('problem_code', '=', $problemCode);
     }
     if (!empty($globalId)) {
         $where[] = array('problem_global_id', '=', $globalId);
     }
     if ($language != -1) {
         $where[] = array('language', '=', $language);
     }
     if ($result != -1) {
         $where[] = array('result', '=', $result);
     }
     // 获取solutionList
     if ($maxId != -1) {
         $where[] = array('id', '<=', $maxId);
         $order = array('id' => 'DESC');
         $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => $order, 'limit' => $pageSize, 'include_contest' => $this->isOjAdmin));
     } else {
         if ($minId != -1) {
             $where[] = array('id', '>=', $minId);
             $order = array('id' => 'ASC');
             $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => $order, 'limit' => $pageSize, 'include_contest' => $this->isOjAdmin));
             $solutionList = array_reverse($solutionList, true);
         } else {
             $order = array('id' => 'DESC');
             $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => $order, 'limit' => $pageSize, 'include_contest' => $this->isOjAdmin));
         }
     }
     // 获取userHash
     $userIds = array_unique(array_column($solutionList, 'user_id'));
     $userHash = UserCommonInterface::getById(array('id' => $userIds));
     // 格式化solution
     foreach ($solutionList as &$solutionInfo) {
         // level
         $userInfo = $userHash[$solutionInfo['user_id']];
         list($solutionInfo['level'], $solutionInfo['permission']) = OjSolutionHelper::solutionPermission($solutionInfo, $userInfo['share'], Arr::get('id', $this->loginUserInfo, 0), $this->isOjAdmin);
         $solutionInfo['has_log'] = OjSolutionHelper::hasLog($solutionInfo);
     }
     // 计算minId, maxId
     $minId = $maxId = 0;
     if (!empty($solutionList)) {
         $n = count($solutionList);
         $maxId = $solutionList[0]['id'];
         $minId = $solutionList[$n - 1]['id'];
     }
     // 缓存html
     $html = array();
     $html['pager'] = $this->view->fetch(array('renderMaxId' => $maxId, 'renderMinId' => $minId), 'widget/pager_status.php');
     $tpl = $this->isOjAdmin ? 'status/list_admin.php' : 'status/list.php';
     // render
     $this->renderFramework(array('userHash' => $userHash, 'solutionList' => $solutionList, 'html' => $html), $tpl);
 }
 public function defaultAction()
 {
     $pageSize = 15;
     // 获取参数
     $username = Request::getGET('username');
     $language = (int) Request::getGET('language', -1);
     $result = (int) Request::getGET('result', -1);
     $problemHash = Request::getGET('problem-hash');
     $maxId = (int) Request::getGET('max-id', -1);
     $minId = (int) Request::getGET('min-id', -1);
     $globalId = array_search($problemHash, $this->contestInfo['problem_hash']);
     // 获取userInfo,username转换为userId
     $userInfo = array();
     if (!empty($username)) {
         $userInfo = UserCommonInterface::getByLoginName(array('login_name' => $username));
     }
     // 构建where
     $where = array();
     $where[] = array('contest_id', '=', $this->contestInfo['id']);
     $where[] = array('hidden', '=', 0);
     $where[] = array('problem_global_id', 'IN', $this->contestInfo['global_ids']);
     if (!empty($username)) {
         $where[] = array('user_id', '=', Arr::get('id', $userInfo, 0));
     }
     if (!empty($globalId)) {
         $where[] = array('problem_global_id', '=', $globalId);
     }
     if ($language != -1) {
         $where[] = array('language', '=', $language);
     }
     if ($result != -1) {
         $where[] = array('result', '=', $result);
     }
     // 获取solutionList
     if ($maxId != -1) {
         $where[] = array('solution_id', '<=', $maxId);
         $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => array('id' => 'DESC'), 'limit' => $pageSize, 'include_contest' => true));
     } else {
         if ($minId != -1) {
             $where[] = array('solution_id', '>=', $minId);
             $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => array('id' => 'ASC'), 'limit' => $pageSize, 'include_contest' => true));
             $solutionList = array_reverse($solutionList, true);
         } else {
             $solutionList = OjSolutionInterface::getList(array('where' => $where, 'order' => array('id' => 'DESC'), 'limit' => $pageSize, 'include_contest' => true));
         }
     }
     // 获取userHash
     $userIds = array_unique(array_column($solutionList, 'user_id'));
     $userHash = UserCommonInterface::getById(array('id' => $userIds));
     // 格式化solution
     foreach ($solutionList as &$solutionInfo) {
         $solutionInfo['permission'] = false;
         if ($this->isContestAdmin || $solutionInfo['user_id'] == $this->loginUserInfo['id']) {
             $solutionInfo['permission'] = true;
         }
         $solutionInfo['has_log'] = OjSolutionHelper::hasLog($solutionInfo);
     }
     // 如果是报名,获取报名列表
     $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);
     }
     // 计算minId, maxId
     $minId = $maxId = 0;
     if (!empty($solutionList)) {
         $solutionIds = array_keys($solutionList);
         $maxId = $solutionIds[0];
         $minId = end($solutionIds);
     }
     // 缓存html
     $html = array();
     $html['pager'] = $this->view->fetch(array('renderMaxId' => $maxId, 'renderMinId' => $minId), 'widget/pager_status.php');
     // render
     $this->renderFramework(array('userHash' => $userHash, 'solutionList' => $solutionList, 'html' => $html, 'applyHash' => $applyHash), 'status/list.php');
 }