コード例 #1
0
 /**
  * Displays a single Survey model.
  * @param integer $id
  * @param integer $Administrator_id
  * @return mixed
  */
 public function actionView($id)
 {
     $validate = false;
     $model = $this->findModel($id);
     if (Yii::$app->user->can('surveyDo') && !SurveyHasParticipant::checkParticipantHasDone($id) && $model->is_enable == 1) {
         $validate = true;
     }
     if (Yii::$app->user->can('surveyEdit')) {
         $validate = true;
     }
     if ($validate) {
         //The permission name
         $questionModels = [];
         $questions = Question::getAllQuestionBySurveyId($model->id);
         foreach ($questions as $row) {
             $question = Question::findOne(['id' => $row['id']]);
             if ($question->required == 1) {
                 $question->temp_input_required = null;
             }
             if (TextBox::checkIsTextBoxByQuestionId($row['id'])) {
                 $question->temp_type = 0;
                 array_push($questionModels, $question);
                 continue;
             }
             if (Checkbutton::checkIsCheckBoxByQuestionId($row['id'])) {
                 $question->temp_type = 1;
                 array_push($questionModels, $question);
                 continue;
             }
             if (RadioButton::checkIsRadioButtonByQuestionId($row['id'])) {
                 $question->temp_type = 2;
                 array_push($questionModels, $question);
                 continue;
             }
         }
         $post = Yii::$app->request->post();
         if (Model::loadMultiple($questionModels, $post)) {
             foreach ($questionModels as $row) {
                 if ($row['required'] == 1) {
                     $input = $row['temp_input_required'];
                 } else {
                     $input = $row['temp_input'];
                     if ($input == '') {
                         continue;
                     }
                 }
                 if ($row['temp_type'] == 0) {
                     $respond = new TextResponse();
                     $respond->content = $input;
                     $respond->TextBox_id = $row['id'];
                     $respond->save();
                 }
                 if ($row['temp_type'] == 1) {
                     foreach ($input as $checked) {
                         Checkbutton::increaseOptionById($checked);
                     }
                 }
                 if ($row['temp_type'] == 2) {
                     Radiobutton::increaseOptionById($input);
                 }
             }
             $participant = new SurveyHasParticipant();
             $participant->Participant_id = Yii::$app->user->id;
             $participant->Survey_id = $model->id;
             $participant->datetime = date("Y-m-d H:i:s");
             $participant->save();
             \app\models\User::addScore(5, Yii::$app->user->id);
             return $this->redirect(['index', 'done' => true]);
         } else {
             //die(var_dump($questionModels));
             return $this->render('view', ['model' => $model, 'questionModels' => $questionModels]);
         }
     } else {
         if (Yii::$app->user->isGuest) {
             Yii::$app->user->loginRequired();
         } else {
             throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
         }
     }
 }
コード例 #2
0
 /**
  * Finds the RadioButton model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @param integer $Question_id
  * @return RadioButton the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id, $Question_id)
 {
     if (($model = RadioButton::findOne(['id' => $id, 'Question_id' => $Question_id])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }