/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $answers = GameEventAnswerModel::model()->findAllByAttributes(array('ask_id' => $model->id), array('order' => 'position DESC, id'));
     if (isset($_POST['GameEventQuestionModel'])) {
         $model->attributes = $_POST['GameEventQuestionModel'];
         if ($model->save()) {
             //Update answers
             $answers_name = $_POST['answer'];
             $answers_isTrue = $_POST['answer_chk'];
             $position = array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D');
             if ($model->type == 'text') {
                 for ($i = 0; $i < 4; $i++) {
                     if ($answers_name[$i]) {
                         $answer = $answers[$i];
                         $answer->ask_id = $model->id;
                         $answer->name = $answers_name[$i];
                         $answer->is_true = $answers_isTrue == $i ? 1 : 0;
                         $answer->position = $position[$i];
                         $answer->save();
                     }
                 }
             } else {
                 $ask_image = array();
                 $ask_image[] = $_POST['ask_image_0'];
                 $ask_image[] = $_POST['ask_image_1'];
                 $ask_image[] = $_POST['ask_image_2'];
                 $ask_image[] = $_POST['ask_image_3'];
                 for ($i = 0; $i < 4; $i++) {
                     if ($ask_image[$i]) {
                         $answer = $answers[$i];
                         $answer->ask_id = $model->id;
                         $answer->name = $ask_image[$i];
                         $answer->is_true = $answers_isTrue == $i ? 1 : 0;
                         $answer->position = $position[$i];
                         $answer->save();
                     }
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('update', array('model' => $model, 'answers' => $answers));
 }
Ejemplo n.º 2
0
 public function actionAnswer()
 {
     if (Yii::app()->request->isPostRequest) {
         $userPhone = yii::app()->user->getState('msisdn');
         $ansId = $_POST['answer'];
         if (empty($ansId)) {
             //khong chon cau tra loi
             $redirect = Yii::app()->createUrl('/event/play', array('notrep' => 1, 'askid' => $_POST['askid']));
             $this->redirect($redirect);
             Yii::app()->end();
         }
         $resAnswer = GameEventAnswerModel::model()->findByPk($ansId);
         if ($resAnswer->is_true) {
             $point = GameEventQuestionModel::model()->findByPk($_POST['askid'])->point;
         } else {
             $point = 0;
         }
         $completedTime = date('Y-m-d H:i:s');
         $gameEventLog = new GameEventUserLogModel();
         $gameEventLog->setAttribute('user_phone', $userPhone);
         $gameEventLog->setAttribute('ask_id', $_POST['askid']);
         $gameEventLog->setAttribute('answer_id', $ansId);
         $gameEventLog->setAttribute('thread_id', $_SESSION['thread']);
         $gameEventLog->setAttribute('point', $point);
         $gameEventLog->setAttribute('started_datetime', $_SESSION['startTime']);
         $gameEventLog->setAttribute('completed_datetime', $completedTime);
         $gameEventLog->save();
         $this->_removeQuestion($_POST['askid']);
         $_SESSION['count'] += 1;
     }
     $this->redirect('/event/play');
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = GameEventAnswerModel::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }