PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/controllers/SiteController.php

https://bitbucket.org/shel3over/scoreboard
PHP | 182 lines | 115 code | 32 blank | 35 comment | 8 complexity | 15e6e32d000dd0d0b9a4d1f6ed8eba97 MD5 | raw file
  1. <?php
  2. class SiteController extends Controller {
  3. /**
  4. * Declares class-based actions.
  5. */
  6. public function actions() {
  7. return array(
  8. // captcha action renders the CAPTCHA image displayed on the contact page
  9. 'captcha' => array(
  10. 'class' => 'CCaptchaAction',
  11. 'backColor' => 0xFFFFFF,
  12. ),
  13. );
  14. }
  15. /**
  16. * @return array action filters
  17. */
  18. public function filters() {
  19. return array(
  20. 'accessControl', // perform access control for CRUD operations
  21. );
  22. }
  23. /**
  24. * Specifies the access control rules.
  25. * This method is used by the 'accessControl' filter.
  26. * @return array access control rules
  27. */
  28. public function accessRules() {
  29. return array(
  30. array('allow', // allow all users to perform 'index' and 'view' actions
  31. 'actions' => array('index', 'login', 'log', 'rank'),
  32. 'users' => array('*'),
  33. ),
  34. /* array('deny',
  35. 'actions'=>array('login'),
  36. 'users'=>array('@'),
  37. ), */
  38. array('allow', // allow authenticated user to perform 'create' and 'update' actions
  39. 'actions' => array('index', 'showlog', 'logout'),
  40. 'users' => array('@'),
  41. ),
  42. array('deny', // deny all users
  43. 'users' => array('*'),
  44. ),
  45. );
  46. }
  47. /**
  48. * This is the default 'index' action that is invoked
  49. * when an action is not explicitly requested by users.
  50. */
  51. public function actionIndex() {
  52. $criteria = new CDbCriteria;
  53. $criteria->condition = 'cid=:cid';
  54. $criteria->params = array(':cid' => 1);
  55. $dataProvider1 = new CActiveDataProvider('Challenge');
  56. $dataProvider1->setCriteria($criteria);
  57. $criteria = new CDbCriteria;
  58. $criteria->condition = 'cid=:cid';
  59. $criteria->params = array(':cid' => 2);
  60. $dataProvider2 = new CActiveDataProvider('Challenge');
  61. $dataProvider2->setCriteria($criteria);
  62. $criteria = new CDbCriteria;
  63. $criteria->condition = 'cid=:cid';
  64. $criteria->params = array(':cid' => 3);
  65. $dataProvider3 = new CActiveDataProvider('Challenge');
  66. $dataProvider3->setCriteria($criteria);
  67. $this->render('index', array(
  68. 'dataProvider1' => $dataProvider1,
  69. 'dataProvider2' => $dataProvider2,
  70. 'dataProvider3' => $dataProvider3,
  71. ));
  72. }
  73. /**
  74. * This is the action to handle external exceptions.
  75. */
  76. public function actionError() {
  77. if ($error = Yii::app()->errorHandler->error) {
  78. if (Yii::app()->request->isAjaxRequest)
  79. echo $error['message'];
  80. else
  81. $this->render('error', $error);
  82. }
  83. }
  84. /** * Displays the login page
  85. */
  86. public function actionLogin() {
  87. $model = new LoginForm;
  88. // if it is ajax validation request
  89. if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
  90. echo CActiveForm::validate($model);
  91. Yii::app()->end();
  92. }
  93. // collect user input data
  94. if (isset($_POST['LoginForm'])) {
  95. $model->attributes = $_POST['LoginForm'];
  96. // validate user input and redirect to the previous page if valid
  97. if ($model->validate() && $model->login())
  98. $this->redirect(Yii::app()->user->returnUrl);
  99. }
  100. // display the login form
  101. $this->render('login', array('model' => $model));
  102. }
  103. /**
  104. * Logs out the current user and redirect to homepage.
  105. */
  106. public function actionLogout() {
  107. Yii::app()->user->logout();
  108. $this->redirect(Yii::app()->homeUrl);
  109. }
  110. public function actionlog() {
  111. $sql = 'select DATE_FORMAT(time,\'%d%H%i\') as date , s.tid ,t.name as team_name,c.score as data ,c.name as challenge_name from score s,challenge c,team t where s.tid=t.id and c.id=s.challnges order by s.id';
  112. $sql_data = Yii::app()->db->createCommand($sql)->queryAll();
  113. //order data by team :)
  114. $data = array();
  115. $date = array();
  116. foreach($sql_data as $item ){
  117. @$data[$item['team_name']][$item['date']] = end($data[$item['team_name']]) + intval($item['data']);
  118. @$date[$item['date']] = 0;
  119. }
  120. $date['999999']=0;
  121. //the structer of the chartjs
  122. foreach ($data as $key => $value) {
  123. $value = $value+$date;
  124. ksort($value);
  125. //full the gaps
  126. $last=0;
  127. foreach ($value as $date_key => $v) {
  128. if($v==0){
  129. $value[$date_key]=$last;
  130. }else{
  131. $last=$v;
  132. }
  133. }
  134. $chart[]=array('name'=>$key,'data'=>array_values($value));
  135. }
  136. $this->render('log', array(
  137. 'data' => $chart,
  138. ));
  139. }
  140. public function actionRank() {
  141. $sql = ' SELECT `name`, `score` as `data` FROM `team` `t` LEFT JOIN `score` `s` ON s.tid=t.id GROUP BY `t`.`name` ORDER BY `t`.`score` DESC, `s`.`time` ASC ';
  142. $chart = Yii::app()->db->createCommand($sql)->queryAll();
  143. foreach ($chart as $key => $value) {
  144. $chart[$key]['data']=array(intval($value['data']));
  145. }
  146. $this->render('rank', array(
  147. 'data' => $chart,
  148. ));
  149. }
  150. }