/**
  * 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]);
     }
 }
Exemplo n.º 2
0
 public function actionApply()
 {
     $trainId = Yii::$app->request->get('train_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('您已经参与了该级别下的培训课程,请耐心等待培训结果,谢谢。');
         }
         //报名成功,给出用户的序号
         $trainUsersOrder = TrainUsers::getTrainUsersOrder($trainId);
         if (empty($trainUsersOrder)) {
             $trainUsersOrder = 1;
         } else {
             $trainUsersOrder = $trainUsersOrder + 1;
         }
         $transaction = Yii::$app->db->beginTransaction();
         //如果用户未完成报名流程,更新状态为审核中
         $isSignExist = TrainUsers::getUserIsExistTrainStatusSign($userId, $trainInfo['level_id']);
         if (!empty($isSignExist)) {
             TrainUsers::updateAll(['status' => TrainUsers::APPROVED], ['id' => $isSignExist['id']]);
             $trainName = Train::getOneTrainNameById($isSignExist['train_id']);
             $data = ['orders' => $isSignExist['orders'], 'trainName' => $trainName];
             $transaction->commit();
             return $this->render('/user/apply-success', ['data' => $data]);
         } else {
             //如果没有记录,新增一条
             $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()) {
                 $trainName = Train::getOneTrainNameById($model->train_id);
                 $data = ['orders' => $model->orders, 'trainName' => $trainName];
                 $transaction->commit();
                 return $this->render('/user/apply-success', ['data' => $data]);
             } else {
                 throw new ServerErrorHttpException('系统错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
             }
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Finds the Train model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Train the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Train::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemplo n.º 4
0
 public function actionView()
 {
     $trainId = Yii::$app->request->get('id');
     $trainModel = Train::findOne(['id' => $trainId]);
     $trainTeachersModel = TrainTeachers::getAllTeachersByTrainId($trainModel['id']);
     $trainUsers = [];
     if ($trainModel['recruit_count'] > 0) {
         for ($i = 1; $i <= $trainModel['recruit_count']; $i++) {
             $trainUsersInfo = TrainUsers::findOne(['train_id' => $trainModel['id'], 'orders' => $i]);
             if (empty($trainUsersInfo)) {
                 $trainUsers[$i]['status'] = '未报名';
                 $trainUsers[$i]['class'] = 'red';
                 $trainUsers[$i]['userId'] = '';
             } else {
                 $trainUsers[$i]['status'] = TrainUsers::$statusList[$trainUsersInfo['status']];
                 $trainUsers[$i]['userId'] = $trainUsersInfo['user_id'];
                 if ($trainUsersInfo['status'] == TrainUsers::NO_APPROVED) {
                     $trainUsers[$i]['class'] = 'blue';
                 } else {
                     $trainUsers[$i]['class'] = '';
                 }
             }
         }
     }
     $data = ['trainModel' => $trainModel, 'trainTeachersModel' => $trainTeachersModel, 'trainUsers' => $trainUsers];
     return $this->render('/train/view', ['data' => $data]);
 }