public function actionApply()
 {
     $activityId = Yii::$app->request->get('id');
     $activityInfo = Activity::findOne(['id' => $activityId]);
     if ($activityInfo['status'] != Activity::BEGIN_SIGN_UP) {
         throw new ServerErrorHttpException('该活动的状态不是开始报名,谢谢。');
     }
     if (Yii::$app->user->isGuest) {
         return $this->redirect(['/user/register', 'activity_id' => $activityId]);
     } else {
         $userId = Yii::$app->user->id;
         $CredentialsNumber = UsersInfo::getCredentialsNumberByUserId($userId);
         if (Yii::$app->user->identity->status != '1') {
             throw new ServerErrorHttpException('系统错误,原因:您目前的状态是未审核,不能报名课程,谢谢。');
         }
         $activityLevelInfo = Level::findOne(['id' => $activityInfo['level_id']]);
         if ($activityLevelInfo['order'] != Yii::$app->user->identity->level_order + 1) {
             throw new ServerErrorHttpException('系统错误,原因:您目前没有权限报名该级别下的课程,谢谢。');
         }
         //检查用户参与的课程,状态不是取消的都算是已经参与了报名
         $isExist = ActivityUsers::getUserIsExistActivityStatus($userId, $activityInfo['level_id']);
         if (!empty($isExist)) {
             throw new ServerErrorHttpException('系统错误,原因:您已经参与了该级别下的培训课程,请耐心等待培训结果,谢谢。');
         }
         //报名成功,给出用户的序号
         $activityUsersOrder = ActivityUsers::getActivityUsersOrder($userId, $activityId);
         if (empty($activityUsersOrder)) {
             $activityUsersOrder = 1;
         }
         $transaction = Yii::$app->db->beginTransaction();
         $data = ['activity_id' => $activityId, 'user_id' => $userId, 'status' => ActivityUsers::APPROVED, 'practice_score' => 0, 'theory_score' => 0, 'rule_score' => 0, 'level_id' => $activityInfo['level_id'], 'orders' => $activityUsersOrder];
         $model = new ActivityUsers();
         $model->setAttributes($data);
         if ($model->save()) {
             $activityUserId = $model->id;
             $activityUser = ActivityUsers::findOne(['id' => $activityUserId]);
             $activityName = Activity::getOneActivityNameById($activityUser['activity_id']);
             $data = ['orders' => $activityUser['orders'], 'activityName' => $activityName];
             //新增一条用户和级别对应的信息
             $userLevelModel = new UsersLevel();
             $userLevelModel->user_id = $userId;
             $userLevelModel->activity_id = $activityId;
             $userLevelModel->level_id = $activityInfo['level_id'];
             $userLevelModel->credentials_number = $CredentialsNumber;
             if (!$userLevelModel->save()) {
                 $transaction->rollBack();
                 throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
             } else {
                 $transaction->commit();
             }
             return $this->render('/activity/apply-success', ['data' => $data]);
         } else {
             throw new ServerErrorHttpException('系统错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
         }
     }
 }
 public function actionActivityApply()
 {
     $activityId = Yii::$app->request->get('id');
     $activityInfo = Activity::findOne(['id' => $activityId]);
     $levelId = $activityInfo['level_id'];
     if ($activityInfo['status'] != Activity::BEGIN_SIGN_UP) {
         throw new ServerErrorHttpException('该活动的状态不是开始报名,谢谢。');
     }
     if (Yii::$app->user->isGuest) {
         return $this->redirect('/login/login');
     } else {
         $userId = Yii::$app->user->id;
         if (Yii::$app->user->identity->status != '1') {
             throw new ServerErrorHttpException('系统错误,原因:您目前的状态是未审核,不能报名活动,谢谢。');
         }
         $activityLevelInfo = Level::findOne(['id' => $activityInfo['level_id']]);
         if ($activityLevelInfo['order'] != Yii::$app->user->identity->level_order) {
             throw new ServerErrorHttpException('系统错误,原因:您目前没有权限报名该级别下的活动,谢谢。');
         }
         //检查用户参与的活动,状态不是取消的都算是已经参与了报名
         $isExist = ActivityUsers::getUserIsExistActivityStatus($userId, $activityInfo['level_id']);
         if (!empty($isExist)) {
             throw new ServerErrorHttpException('系统错误,原因:您已经参与了该级别下的活动,请耐心等待活动结果,谢谢。');
         }
         //报名成功,给出用户的序号
         $activityUsersOrder = ActivityUsers::getActivityUsersOrder($userId, $activityId);
         if (empty($activityUsersOrder)) {
             $activityUsersOrder = 1;
         }
         $transaction = Yii::$app->db->beginTransaction();
         $data = ['activity_id' => $activityId, 'user_id' => $userId, 'status' => ActivityUsers::APPROVED, 'level_id' => $levelId, 'orders' => $activityUsersOrder];
         $model = new ActivityUsers();
         $model->setAttributes($data);
         if ($model->save()) {
             $activityUserId = $model->id;
             $activityUser = ActivityUsers::findOne(['id' => $activityUserId]);
             $activityName = Activity::getOneActivityNameById($activityUser['activity_id']);
             $data = ['orders' => $activityUser['orders'], 'activityName' => $activityName];
             //新增一条用户和级别对应的信息
             $result = UsersLevel::updateAll(['activity_id' => $activityId], ['user_id' => $userId, 'level_id' => $levelId]);
             if (!$result) {
                 $transaction->rollBack();
                 throw new ServerErrorHttpException('更新状态错误,原因:更新数据库错误!');
             } else {
                 $transaction->commit();
             }
             return $this->render('/activity/apply-success', ['data' => $data]);
         } else {
             throw new ServerErrorHttpException('系统错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
         }
     }
 }
 /**
  * Updates an existing ActivityUsers model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $activityInfo = Activity::findOne(['id', $model->activity_id]);
     $userInfo = Users::findOne($model->user_id);
     $model->userName = $userInfo['username'];
     $model->activityName = $activityInfo->name;
     if (Yii::$app->request->isPost) {
         $updateParams = Yii::$app->request->post();
         $transaction = Yii::$app->db->beginTransaction();
         if ($updateParams['ActivityUsers']['practice_score'] > 100 || $updateParams['ActivityUsers']['theory_score'] > 100 || $updateParams['ActivityUsers']['rule_score'] > 100) {
             throw new ServerErrorHttpException('评分更新错误,原因:每项评分都不能超过100分!');
         }
         if ($model->load($updateParams) && $model->save()) {
             if ($updateParams['ActivityUsers']['status'] == ActivityUsers::END) {
                 //如果结束状态,必须评分
                 if ($updateParams['ActivityUsers']['practice_score'] <= 0 || $updateParams['ActivityUsers']['theory_score'] <= 0 || $updateParams['ActivityUsers']['rule_score'] <= 0) {
                     throw new ServerErrorHttpException('评分更新错误,原因:每项评分都必须大于零!');
                 }
                 //根据3个单项评分计算全部评价内容包括考勤
                 $result = $this->getSource($model->activity_id, $model->user_id, $model->practice_score, $model->theory_score, $model->rule_score);
                 $model->score_appraise = $result['scoreAppraise'];
                 $model->appraise_result = $result['performance'];
                 $model->attendance_appraise = $result['attendanceComment'];
                 $model->practice_comment = $result['practiceComment'];
                 $model->theory_comment = $result['theoryComment'];
                 $model->rule_comment = $result['rulesComment'];
                 $model->total_comment = $result['commentAppraise'];
                 $model->result_comment = $result['scoreAppraise'];
                 $model->status = $result['status'];
                 if ($model->save()) {
                     //如果通过更新晋级信息
                     if ($model->status == ActivityUsers::PASS) {
                         //                            $messagesInfo = Messages::findOne(['id' => 29]);
                         //                            $message = $messagesInfo['content'];
                         //                            $data = [
                         //                                'messages_id' => 29,
                         //                                'users_id' => $model->user_id,
                         //                            ];
                         //                            MessagesUsers::addInfo($data);
                         $content = "尊敬的学员,您已经被成功完成活动,请到个人中心查看具体信息,谢谢。【教练系统】";
                         //                            $smsModel = Sms::getInstance(Yii::$app->params['smsUserName'],Yii::$app->params['smsPassword']);
                         //                            $result = $smsModel->pushMt($userInfo['phone'], time(), $content, 0);
                         //                            if ($result != '0') {
                         //                                throw new ServerErrorHttpException('更新状态错误,原因:' . $result . '!');
                         //                            }
                         $transaction->commit();
                         return $this->redirect(['view', 'id' => $model->id]);
                     }
                     $transaction->commit();
                     Yii::$app->getSession()->setFlash('success', '更新成功!');
                 } else {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('评分更新错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
                 }
             } else {
                 if ($updateParams['ActivityUsers']['status'] == ActivityUsers::ENROLL) {
                     //如果录取,给出用户的序号
                     $activityUsersOrder = ActivityUsers::getActivityUsersOrder($model->user_id, $model->activity_id);
                     if (empty($activityUsersOrder)) {
                         $activityUsersOrder = 1;
                     }
                     $model->orders = $activityUsersOrder;
                     $model->status = ActivityUsers::ENROLL;
                     if ($model->save()) {
                         $transaction->commit();
                         //                        $messagesInfo = Messages::findOne(['id' => 30]);
                         //                        $message = $messagesInfo['content'];
                         //                        $data = [
                         //                            'messages_id' => 30,
                         //                            'users_id' => $model->user_id,
                         //                        ];
                         //                        MessagesUsers::addInfo($data);
                         //                        $content = $message . "【教练系统】";
                         //录取发短信
                         //                        $content = "尊敬的学员,您已经被成功录取,序号为" . $activityUsersOrder . ",谢谢!【教练系统】";
                         //                        $smsModel = Sms::getInstance(Yii::$app->params['smsUserName'],Yii::$app->params['smsPassword']);
                         //                        $result = $smsModel->pushMt($userInfo['phone'], time(), $content, 0);
                         //                        if ($result != '0') {
                         //                            throw new ServerErrorHttpException('更新状态错误,原因:' . $result . '!');
                         //                        }
                         return $this->redirect(['view', 'id' => $model->id]);
                     } else {
                         $transaction->rollBack();
                         //                    Yii::$app->getSession()->setFlash('error', '更新状态错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
                         throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
                     }
                 } else {
                     $transaction->commit();
                     Yii::$app->getSession()->setFlash('success', '更新成功!');
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             }
         } else {
             $transaction->rollBack();
             throw new ServerErrorHttpException('评分更新错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
         }
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }