public function actionRank()
 {
     //$rank = GameEventReportDayModel::model()->getRank(10);
     $rank = array();
     $thread = GameEventThreadModel::model()->findAll();
     if ($thread) {
         foreach ($thread as $value) {
             $sql = "SELECT thread_id,point, user_phone,id,SEC_TO_TIME(TIME_TO_SEC(time(time_end))-TIME_TO_SEC(time(time_start))) as time_play\n\t\t\t\t\t\tFROM game_event_report_day\n\t\t\t\t\t\twhere thread_id=:thread_id\n\t\t\t\t\t\tORDER BY point DESC,time_play ASC\n\t\t\t\t\t\tlimit 1";
             $command = Yii::app()->db->createCommand($sql);
             $command->bindParam(':thread_id', $value->id, PDO::PARAM_INT);
             $rank[] = $command->queryRow();
         }
     }
     $rankSort = array();
     foreach ($rank as $value) {
         if ($value) {
             $rankSort[$value['point']] = $value;
         }
     }
     krsort($rankSort);
     $this->render('rank', array('rank' => $rankSort));
 }
 public function actionIndex()
 {
     //unset($_SESSION['qt_list']);
     //unset($_SESSION['thread']);
     $params = array();
     $completed = 0;
     $point = 0;
     $sharePoint = false;
     $rankThread = null;
     $msg = "";
     $userSub = $this->userSub;
     $currDate = date('Y-m-d');
     $userPhone = yii::app()->user->getState('msisdn');
     /* if($userSub){ */
     $countPlayByDate = GameEventThreadModel::countPlayByDate($userPhone, $currDate);
     if ($countPlayByDate <= 10) {
         //tạo session câu hỏi lần đầu tiên tham gia
         //if(!isset($_SESSION['qt_list']) && !isset($_SESSION['thread']) && $countPlayByDate==0){
         if ($countPlayByDate == 0 && !isset($_SESSION['thread'])) {
             $newThread = GameEventThreadModel::getNewRandomThread($userPhone);
             if ($newThread) {
                 $questionList = GameEventThreadModel::getQuestionsByThread($newThread);
                 $qtList = array();
                 foreach ($questionList as $key => $value) {
                     $qtList[] = $value['id'];
                 }
                 $_SESSION['qt_list'] = $qtList;
                 $_SESSION['thread'] = $newThread;
                 $_SESSION['count'] = 1;
             } else {
                 //het 10 bo cau hoi
                 $completed = 1;
                 $msg = 'Bạn đã hoàn thành trả lời 10 bộ câu hỏi event 8/3.';
             }
             //echo '<pre>';print_r($questionList);
         }
         //tra loi tiep theo
         if (isset($_SESSION['qt_list']) && isset($_SESSION['thread'])) {
             if (count($_SESSION['qt_list']) == 0) {
                 $completed = 2;
                 $msg = 'Bạn đã hoàn thành trả lời câu hỏi ngày hôm nay.';
                 $sharePoint = true;
                 $isShare = GameEventActivityModel::isShareOnDay($userPhone, date('Y-m-d'));
                 if ($isShare) {
                     $sharePoint = false;
                 }
                 $point = GameEventUserLogModel::model()->getPoint($userPhone, $_SESSION['thread']);
                 $rankThread = GameEventReportDayModel::model()->getRankByThread($_SESSION['thread']);
             } else {
                 $qtList = $_SESSION['qt_list'];
                 $maxQt = count($qtList) - 1;
                 $indexRand = rand(0, $maxQt);
                 $qtChosen = $qtList[$indexRand];
                 if (isset($_GET['notrep']) && $_GET['notrep'] == 1 && isset($_GET['askid']) && $_GET['askid'] > 0) {
                     if (in_array($_GET['askid'], $qtList)) {
                         $qtChosen = $_GET['askid'];
                     }
                 }
                 $question = GameEventQuestionModel::model()->findByPk($qtChosen);
                 $answer = GameEventAnswerModel::model()->findAllByAttributes(array('ask_id' => $qtChosen));
                 $_SESSION['startTime'] = date('Y-m-d H:i:s');
                 $params = array('question' => $question, 'answer' => $answer);
             }
         } else {
             $completed = 3;
             $msg = 'Bạn đã tham gia trả lời câu hỏi ngày hôm nay.';
         }
     } else {
         //da choi ngay hom nay
         $completed = 4;
         $msg = 'Bạn đã tham gia trả lời câu hỏi ngày hôm nay.';
     }
     /* }else{
     			$this->redirect('/event/register');
     			Yii::app()->end();
     		} */
     $params['completed'] = $completed;
     $params['sharePoint'] = $sharePoint;
     $params['point'] = $point;
     $params['userSub'] = $userSub;
     $params['msg'] = $msg;
     $params['rankThread'] = $rankThread;
     $this->render('index', $params);
 }
 public function actionDeleteQuestion()
 {
     $threadId = Yii::app()->request->getParam('thread_id', null);
     $questionId = Yii::app()->request->getParam('question_id', null);
     $questionId = (int) $questionId;
     if ($threadId && $questionId) {
         $model = GameEventThreadModel::model()->findByPk($threadId);
         $oldQuestionList = explode(',', $model['question_list']);
         $newQuestionList = '';
         foreach ($oldQuestionList as $question) {
             $newQuestionList .= $questionId == (int) $question ? '' : ',' . trim($question);
         }
         $newQuestionList = $newQuestionList != '' ? substr($newQuestionList, 1) : '';
         $model->question_list = $newQuestionList;
         $model->save();
         $this->redirect(Yii::app()->createUrl("event/gameEventQuestion/view", array("id" => $threadId)));
     }
     /*
     		if(Yii::app()->request->isPostRequest)
     		{
     // we only allow deletion via POST request
     $this->loadModel($id)->delete();
     	
     // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
     if(!isset($_GET['ajax']))
     	$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
     		}
     		else
     throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
     */
 }
 public function actionRegisterNow()
 {
     $phone = yii::app()->user->getState('msisdn');
     $register = $this->_DoRegister($phone);
     /* $register = new stdClass();
     		$register->errorCode=0;
     		$register->msg="Lỗi hệ thống, hãy thử lại sau ít phút."; */
     $currDate = date('Y-m-d');
     $countPlayByDate = GameEventThreadModel::countPlayByDate($phone, $currDate);
     $this->render('register_now', array('register' => $register, 'countPlayByDate' => $countPlayByDate));
 }
 public function actionAddQuestion()
 {
     $flag = true;
     $threadId = Yii::app()->request->getParam('thread_id', "");
     if (Yii::app()->getRequest()->ispostRequest) {
         $flag = false;
         $questionList = $_POST['cid'];
         $model = GameEventThreadModel::model()->findByPk($threadId);
         $oldQuestionList = explode(',', $model['question_list']);
         $newQuestionList = $model['question_list'] ? ',' . $model['question_list'] : '';
         foreach ($questionList as $question) {
             $newQuestionList .= in_array($question, $oldQuestionList) ? '' : ',' . $question;
         }
         $newQuestionList = $newQuestionList != '' ? substr($newQuestionList, 1) : '';
         $model->question_list = $newQuestionList;
         $model->save();
         //AdminCollectionItemModel::model()->addList($this->userId, $collect_id, $albumList, 'album');
     }
     if ($flag) {
         Yii::app()->user->setState('pageSize', 20);
         $questionModel = new GameEventQuestionModel();
         $questionModel->unsetAttributes();
         if (isset($_GET['GameEventQuestionModel'])) {
             $questionModel->attributes = $_GET['GameEventQuestionModel'];
         }
         $questionModel->setAttributes(array('status' => GameEventQuestionModel::PUBLISHED));
         Yii::app()->clientScript->scriptMap['jquery.js'] = false;
         $this->renderPartial('addQuestion', array('questionModel' => $questionModel, 'thread_id' => $threadId), false, true);
     }
 }