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 static function getLevel($id = 0, $username_phone = null, $user_code = null) { $info = self::getInfo($id, $username_phone, $user_code); $level = isset($info['user_level']) ? (int) $info['user_level'] : 1; $start_time = isset($info['level_start_time']) ? $info['level_start_time'] : 0; $userlevel = UsersLevel::getData($level); if (1 == $level) { return $userlevel; } else { $exp_days = isset($userlevel['exp_days']) ? (int) $userlevel['exp_days'] : 0; $exp_time = $exp_days * 24 * 3600; if (time() - $start_time > $exp_time) { return UsersLevel::getData(1); } //过期 return $userlevel; } }