/**
  * Updates an existing TrainUsers 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);
     $trainInfo = Train::findOne(['id', $model->train_id]);
     $sourceStatus = $model->status;
     $userInfo = Users::findOne($model->user_id);
     $model->userName = $userInfo['username'];
     $model->trainName = $trainInfo->name;
     $statusList[TrainUsers::SIGN] = ['list' => [TrainUsers::APPROVED => TrainUsers::$statusList[TrainUsers::APPROVED]]];
     $statusList[TrainUsers::APPROVED] = ['list' => [TrainUsers::ENROLL => TrainUsers::$statusList[TrainUsers::ENROLL], TrainUsers::NO_APPROVED => TrainUsers::$statusList[TrainUsers::NO_APPROVED]]];
     $statusList[TrainUsers::ENROLL] = ['list' => [TrainUsers::DOING => TrainUsers::$statusList[TrainUsers::DOING]]];
     $statusList[TrainUsers::DOING] = ['list' => [TrainUsers::END => TrainUsers::$statusList[TrainUsers::END]]];
     $statusList[TrainUsers::END] = ['list' => [TrainUsers::END => TrainUsers::$statusList[TrainUsers::END]]];
     $statusList[TrainUsers::NO_APPROVED] = ['list' => [TrainUsers::APPROVED => TrainUsers::$statusList[TrainUsers::APPROVED]]];
     $statusList[TrainUsers::CANCEL] = ['list' => [TrainUsers::CANCEL => TrainUsers::$statusList[TrainUsers::CANCEL]]];
     $statusList[TrainUsers::PASS] = ['list' => [TrainUsers::PASS => TrainUsers::$statusList[TrainUsers::PASS]]];
     $statusList[TrainUsers::NO_PASS] = ['list' => [TrainUsers::NO_PASS => TrainUsers::$statusList[TrainUsers::NO_PASS]]];
     if (Yii::$app->request->isPost) {
         $updateParams = Yii::$app->request->post();
         $transaction = Yii::$app->db->beginTransaction();
         if ($model->load($updateParams) && $model->save()) {
             if ($updateParams['TrainUsers']['status'] == TrainUsers::END) {
                 //如果结束状态,必须评分
                 if ($sourceStatus == TrainUsers::END) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('请提交不同的状态!');
                 }
                 if ($updateParams['TrainUsers']['practice_score'] <= 0 || $updateParams['TrainUsers']['theory_score'] <= 0 || $updateParams['TrainUsers']['rule_score'] <= 0) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('每项评分都必须大于零!');
                 }
                 if ($updateParams['TrainUsers']['practice_score'] > 100 || $updateParams['TrainUsers']['theory_score'] > 100 || $updateParams['TrainUsers']['rule_score'] > 100) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('每项评分都不能超过100分!');
                 }
                 //根据3个单项评分计算全部评价内容包括考勤
                 $result = $this->getSource($model->train_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 == TrainUsers::PASS) {
                         $trainCode = $trainInfo['code'];
                         $trainPeriodNum = $trainInfo['period_num'];
                         $levelInfo = Level::findOne($model->level_id);
                         $certificateNumber = $this->getCertificateNumber($trainCode, $trainPeriodNum, sprintf("%04d", $model->orders), $levelInfo['code']);
                         $res = UsersLevel::updateAll(['status' => 1, 'certificate_number' => $certificateNumber, 'update_time' => date('Y-m-d H:i:s', time()), 'update_user' => Yii::$app->admin->identity->username], ['user_id' => $model->user_id, 'train_id' => $model->train_id]);
                         if ($res) {
                             //通过发短信
                             $content = "您好,您的培训课程考试已经通过,请进入教练员管理系统-我的课程查看。【教练系统】";
                             $result = $this->sendMessage($content, Messages::TRAIN_PASS, $model->user_id, $userInfo['mobile_phone'], '1');
                             if ($result != '0') {
                                 $transaction->rollBack();
                                 throw new ServerErrorHttpException('' . $result . '!');
                             } else {
                                 $transaction->commit();
                                 return $this->redirect(['view', 'id' => $model->id]);
                             }
                         } else {
                             $transaction->rollBack();
                             throw new ServerErrorHttpException(json_encode($res, JSON_UNESCAPED_UNICODE) . '!');
                         }
                     } else {
                         UsersLevel::updateAll(['status' => UsersLevel::TRAIN_NO_PASS], ['user_id' => $userInfo['id'], 'train_id' => $model->train_id]);
                         //未通过发短信
                         $content = "尊敬的学员,您没有通过培训课程,谢谢!【教练系统】";
                         $result = $this->sendMessage($content, Messages::TRAIN_NO_PASS, $model->user_id, $userInfo['mobile_phone'], '1');
                         if ($result != '0') {
                             $transaction->rollBack();
                             throw new ServerErrorHttpException('' . $result . '!');
                         }
                         $transaction->commit();
                         return $this->redirect(['view', 'id' => $model->id]);
                     }
                 } else {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException(json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
                 }
             } else {
                 if ($updateParams['TrainUsers']['status'] == TrainUsers::ENROLL) {
                     if ($sourceStatus == TrainUsers::ENROLL) {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('请提交不同的状态!');
                     }
                     //录取发短信
                     $content = "您好,很高兴的通知您,您报名的培训班已通过审核,请按指定日期前往培训地点缴费上课。【教练系统】";
                     $result = $this->sendMessage($content, Messages::TRAIN_SIGN_SUCCESS, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('' . $result . '!');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } elseif ($updateParams['TrainUsers']['status'] == TrainUsers::NO_APPROVED) {
                     if ($sourceStatus == TrainUsers::NO_APPROVED) {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('请提交不同的状态!');
                     }
                     //未通过审核发短信
                     $content = "尊敬的学员,您没有通过培训课程审核,谢谢!【教练系统】";
                     $result = $this->sendMessage($content, Messages::TRAIN_SIGN_ERROR, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('' . $result . '!');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } elseif ($updateParams['TrainUsers']['status'] == TrainUsers::DOING) {
                     if ($sourceStatus == TrainUsers::DOING) {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('请提交不同的状态!');
                     }
                     //培训开始发短信
                     $content = "尊敬的学员您好,您已缴费成功,请根据课培训班的课程安排按时上下课,谢谢!【教练系统】";
                     $result = $this->sendMessage($content, Messages::TRAIN_DOING, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('' . $result . '!');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } else {
                     if ($updateParams['TrainUsers']['practice_score'] != 0 || $updateParams['TrainUsers']['theory_score'] != 0 || $updateParams['TrainUsers']['rule_score'] != 0) {
                         throw new ServerErrorHttpException('只有更新结束状态需要评分!');
                     }
                     $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, 'statusList' => $statusList]);
     }
 }
 /**
  * Updates an existing UsersLevel 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);
     if (Yii::$app->request->isPost) {
         $infoParams = Yii::$app->request->post();
         if ($infoParams['UsersLevel']['status'] == UsersLevel::SEND_CARD) {
             $infoParams['UsersLevel']['end_date'] = date('Y-m-d', time() + 3600 * 24 * 365);
         }
         $userLevelInfo = $infoParams;
         if ($model->load($userLevelInfo) && $model->save()) {
             //如果状态更新为已经注册
             if ($model->status == UsersLevel::SEND_CARD) {
                 if ($model->level_id == 1) {
                     //如果是注册学员升级市级教练
                     UsersLevel::updateAll(['status' => UsersLevel::LEVEL_UP], ['user_id' => $model->user_id, 'level_id' => $model->level_id]);
                     $userInfo = UsersInfo::findOne(['user_id' => $model->user_id]);
                     //新增一条用户和级别对应的信息
                     $userLevelModel = new UsersLevel();
                     $userLevelModel->user_id = $model->user_id;
                     $userLevelModel->level_id = $model->level_id + 1;
                     $userLevelModel->credentials_number = $userInfo['credentials_number'];
                     $userLevelModel->district = $userInfo['account_location'];
                     $userLevelModel->receive_address = $userInfo['contact_address'];
                     $userLevelModel->postcode = $userInfo['contact_postcode'];
                     $userLevelModel->status = UsersLevel::NO_TRAIN;
                     if (!$userLevelModel->save()) {
                         throw new ServerErrorHttpException(json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
                     } else {
                         Users::updateAll(['level_id' => $model->level_id + 1, 'level_order' => $model->level_id + 1], ['id' => $model->user_id]);
                     }
                 }
                 $usersTrainInfo = UsersLevel::getUserTrainInfo($model->id);
                 $teacherInfo = Teachers::findOne($usersTrainInfo['teachers_id']);
                 $usersTrainModel = new UsersTrain();
                 $usersTrain['UsersTrain']['user_id'] = $usersTrainInfo['user_id'];
                 $usersTrain['UsersTrain']['credentials_number'] = $usersTrainInfo['certificate_number'];
                 $usersTrain['UsersTrain']['begin_time'] = $usersTrainInfo['begin_time'];
                 $usersTrain['UsersTrain']['end_time'] = $usersTrainInfo['end_time'];
                 $usersTrain['UsersTrain']['level'] = Level::getOneLevelNameById($usersTrainInfo['level_id']);
                 $usersTrain['UsersTrain']['address'] = $usersTrainInfo['address'];
                 $usersTrain['UsersTrain']['witness'] = $teacherInfo['name'];
                 $usersTrain['UsersTrain']['witness_phone'] = $teacherInfo['phone'];
                 $usersTrain['UsersTrain']['description'] = '通过培训课程' . $usersTrainInfo['content'];
                 if ($usersTrainModel->load($usersTrain) && $usersTrainModel->save()) {
                     $content = "很高兴的通知您,您已完成" . Level::getOneLevelNameById($model->level_id) . "级教练员的注册,注册时效为1年,您现在可以正式开展教练员工作。【教练系统】";
                     $userInfo = Users::findOne($model->user_id);
                     $result = $this->sendMessage($content, Messages::REGISTER_SUCCESS, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         throw new ServerErrorHttpException($result);
                     } else {
                         return $this->redirect(['view', 'id' => $model->id]);
                     }
                 } else {
                     throw new ServerErrorHttpException(json_encode($usersTrainModel->errors, JSON_UNESCAPED_UNICODE));
                 }
             }
         }
     } else {
         $photo = UsersInfo::getPhotoByUserId($model->user_id);
         return $this->render('update', ['model' => $model, 'photo' => $photo]);
     }
 }
Exemplo n.º 3
0
 public function actionApply()
 {
     $trainId = Yii::$app->request->get('id');
     $trainInfo = Train::findOne(['id' => $trainId]);
     if ($trainInfo['status'] != Train::BEGIN_SIGN_UP) {
         throw new ServerErrorHttpException('该课程的状态不是开始报名,谢谢。');
     }
     if (Yii::$app->user->isGuest) {
         return $this->redirect(['/user/register', 'train_id' => $trainId]);
     } else {
         $userId = Yii::$app->user->id;
         if (Yii::$app->user->identity->status != '1') {
             throw new ServerErrorHttpException('您目前的状态是未审核,不能报名课程,谢谢。');
         }
         $trainLevelInfo = Level::findOne($trainInfo['level_id']);
         if ($trainLevelInfo['order'] != Yii::$app->user->identity->level_order + 1) {
             throw new ServerErrorHttpException('您目前没有权限报名该级别下的课程,谢谢。');
         }
         //检查用户参与的课程,状态不是取消的都算是已经参与了报名
         $isExist = TrainUsers::getUserIsExistTrainStatus($userId, $trainInfo['level_id']);
         if (!empty($isExist)) {
             throw new ServerErrorHttpException('您已经参与了该级别下的培训课程,请耐心等待培训结果,谢谢。');
         }
         //            if (strtotime($trainInfo['sign_up_begin_time']) < time()) {
         //                throw new ServerErrorHttpException('此课程已过期。');
         //            }
         $noPassInfo = TrainUsers::findOne(['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id + 1, 'status' => TrainUsers::NO_PASS]);
         if (!empty($noPassInfo)) {
             $noPassTrainInfo = Train::findOne($noPassInfo['train_id']);
             if (!empty($noPassTrainInfo)) {
                 if ($noPassTrainInfo['end_time'] > $trainInfo['begin_time']) {
                     throw new ServerErrorHttpException('训课课程的开课时间一定要大于未通过考试培训课程结束时间。');
                 }
             }
         }
         //报名成功,给出用户的序号
         $trainUsersOrder = TrainUsers::getMaxSignUpOrder($trainId);
         if (empty($trainUsersOrder)) {
             $trainUsersOrder = 1;
         } else {
             $trainUsersOrder = $trainUsersOrder + 1;
         }
         $data = ['train_id' => $trainId, 'user_id' => $userId, 'status' => TrainUsers::APPROVED, 'practice_score' => 0, 'theory_score' => 0, 'rule_score' => 0, 'level_id' => $trainInfo['level_id'], 'orders' => $trainUsersOrder];
         $model = new TrainUsers();
         $model->setAttributes($data);
         if ($model->save()) {
             $trainUserId = $model->id;
             $trainUser = TrainUsers::findOne(['id' => $trainUserId]);
             $trainName = Train::getOneTrainNameById($trainUser['train_id']);
             $data = ['orders' => $trainUser['orders'], 'trainName' => $trainName];
             //更新级别信息的信息
             UsersLevel::updateAll(['train_id' => $trainId], ['user_id' => $userId, 'level_id' => Yii::$app->user->identity->level_id]);
             return $this->render('/train/apply-success', ['data' => $data]);
         } else {
             throw new ServerErrorHttpException('' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
         }
     }
 }
Exemplo n.º 4
0
 public function actionRegisterCoach()
 {
     if (Yii::$app->request->isPost) {
         $userId = Yii::$app->user->id;
         $credentialsNumber = Yii::$app->request->post('credentials_number');
         $certificateNumber = Yii::$app->request->post('certificate_number');
         $result = UsersLevel::findOne(['user_id' => $userId, 'certificate_number' => $certificateNumber, 'credentials_number' => $credentialsNumber]);
         if (empty($result)) {
             throw new ServerErrorHttpException('系统错误,原因:没有您的注册信息,谢谢');
         } else {
             $levelOrder = Level::getOrderById($result['level_id']);
             if ($levelOrder != Yii::$app->user->identity->level_order) {
                 throw new ServerErrorHttpException('您没有当前级别的注册信息,谢谢');
             } else {
                 if ($result['status'] == 0) {
                     throw new ServerErrorHttpException('您还没有参加培训,谢谢');
                 } else {
                     UsersLevel::updateAll(['status' => 2, 'update_time' => date('Y-m-d H:i:s', time()), 'update_user' => Yii::$app->user->identity->username], ['user_id' => $userId, 'level_id' => $result['level_id']]);
                     return $this->redirect(['/user/register-coach-success']);
                 }
             }
         }
     } else {
         $userLevelInfo = [];
         $isRegister = Yii::$app->request->get('register');
         if ($isRegister) {
             $userLevelInfo = UsersLevel::findOne(['level_id' => Yii::$app->user->identity->level_id, 'user_id' => Yii::$app->user->id]);
         }
         $newReg = UsersLevel::getAllByCount(5);
         if (!empty($newReg)) {
             foreach ($newReg as $key => $val) {
                 $newRegNews[$key]['title'] = Users::getOneUserNameById($val['user_id']) . "注册" . Level::getOneLevelNameById($val['level_id'] + 1) . "教练员";
                 $newRegNews[$key]['create_time'] = $val['create_time'];
                 $newRegNews[$key]['user_id'] = $val['user_id'];
             }
         }
         $regComment = Pages::getContentById(4);
         $data = ['newRegNews' => $newRegNews, 'regComment' => $regComment, 'userLevelInfo' => $userLevelInfo];
         return $this->render('register-coach', ['data' => $data]);
     }
 }
 public function actionLevelUp()
 {
     $levelInfo = Level::findOne(['id' => Yii::$app->user->identity->level_id + 1]);
     $usersLevel = UsersLevel::findOne(['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id]);
     $usersLevelStatusCurrent = $usersLevel['status'];
     if ($usersLevelStatusCurrent != UsersLevel::SEND_CARD) {
         throw new ServerErrorHttpException('当前注册状态不符合!');
     }
     $loginDuration = Users::getLoginDuration(Yii::$app->user->id);
     $loginDurationDiff = $levelInfo['login_duration'] - $loginDuration;
     if ($loginDurationDiff > 0) {
         throw new ServerErrorHttpException('当前注册时长不符合!');
     }
     $scoreDiff = $levelInfo['score'] - Yii::$app->user->identity->score;
     if ($scoreDiff > 0) {
         throw new ServerErrorHttpException('当前一般活动积分不符合!');
     }
     $creditDiff = $levelInfo['credit'] - Yii::$app->user->identity->credit;
     if ($creditDiff > 0) {
         throw new ServerErrorHttpException('当前公益活动积分不符合!');
     }
     $transaction = Yii::$app->db->beginTransaction();
     UsersLevel::updateAll(['status' => UsersLevel::LEVEL_UP], ['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id]);
     $userInfo = UsersInfo::findOne(['user_id' => Yii::$app->user->id]);
     //新增一条用户和级别对应的信息
     $userLevelModel = new UsersLevel();
     $userLevelModel->user_id = Yii::$app->user->id;
     $userLevelModel->level_id = Yii::$app->user->identity->level_id + 1;
     $userLevelModel->credentials_number = $userInfo['credentials_number'];
     $userLevelModel->district = $userInfo['account_location'];
     $userLevelModel->receive_address = $userInfo['contact_address'];
     $userLevelModel->postcode = $userInfo['contact_postcode'];
     $userLevelModel->status = UsersLevel::NO_TRAIN;
     if (!$userLevelModel->save()) {
         $transaction->rollBack();
         throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
     } else {
         Users::updateAll(['level_id' => Yii::$app->user->identity->level_id + 1, 'level_order' => Yii::$app->user->identity->level_order + 1], ['id' => Yii::$app->user->id]);
         $transaction->commit();
     }
     return $this->redirect('/user-center/index');
 }