/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ActivityProcess::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'activity_id' => $this->activity_id, 'user_id' => $this->user_id, 'day' => $this->day, 'status' => $this->status]);
     return $dataProvider;
 }
 /**
  * Updates an existing Activity 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);
     $transaction = Yii::$app->db->beginTransaction();
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             //课程开始
             if ($model->status == Activity::DOING) {
                 //获取该课程下已录取的学员
                 $ActivityUsers = ActivityUsers::getApprovedActivityUsersByActivityId($model->id);
                 if (!empty($ActivityUsers)) {
                     //根据课程id,用户id更新用户状态为正在进行
                     ActivityUsers::updateActivityUsersStatusByActivityId(ActivityUsers::DOING, $model->id);
                     foreach ($ActivityUsers as $key => $val) {
                         //创建考勤信息
                         for ($i = strtotime($model->begin_time); $i < strtotime($model->end_time); $i += 86400) {
                             $day = date('Y-m-d H:i:s', $i);
                             $isExist = ActivityProcess::findOne(['activity_id' => $model->id, 'user_id' => $val['user_id'], 'day' => $day]);
                             if (empty($isExist)) {
                                 $attendance = ['activity_id' => $model->id, 'user_id' => $val['user_id'], 'day' => $day];
                                 ActivityProcess::add($attendance);
                             }
                         }
                     }
                 } else {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('更新状态失败,原因:该培训课程下没有学员!');
                 }
             }
             $transaction->commit();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }
 public function actionMyActivityView()
 {
     $activityUsersId = \Yii::$app->request->get('activityUserId');
     if (empty($activityUsersId)) {
         throw new ServerErrorHttpException('查看活动信息失败,原因:参数错误!');
     }
     $levelName = Level::getOneLevelNameById(\Yii::$app->user->identity->level_id);
     $photo = UsersInfo::getPhotoByUserId(\Yii::$app->user->id);
     $messageCount = MessagesUsers::getCountByUserIdAndType(\Yii::$app->user->id);
     $currentTrain = TrainUsers::getTrainByUserId(\Yii::$app->user->id);
     //培训信息
     $activityModel = ActivityUsers::getActivityInfoById($activityUsersId);
     if (empty($activityModel)) {
         throw new ServerErrorHttpException('查看课程信息失败,原因:不存在的培训课程!');
     }
     //考勤信息
     $activityProcessModel = ActivityProcess::getAllByActivityIdAndUserId($activityModel['activity_id'], $activityModel['user_id']);
     //迟到数量
     $finishCount = ActivityProcess::getCount($activityModel['activity_id'], $activityModel['user_id'], ActivityProcess::FINISH);
     $noFinishCount = ActivityProcess::getCount($activityModel['activity_id'], $activityModel['user_id'], ActivityProcess::NO_FINISH);
     $data = ['levelName' => $levelName, 'currentTrain' => $currentTrain, 'activityModel' => $activityModel, 'activityProcessModel' => $activityProcessModel, 'finishCount' => $finishCount, 'noFinishCount' => $noFinishCount, 'attendanceAppraise' => $activityModel['attendance_appraise'], 'photo' => $photo, 'messageCount' => $messageCount];
     return $this->render('my-activity-view', ['data' => $data]);
 }
 /**
  * Finds the ActivityProcess model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ActivityProcess the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ActivityProcess::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * 计算评分。
  *
  * 评分:实践(满分100分)理论(满分100分)规则(满分100分) 总评((实践分数+理论分数+规则分数)/3)
  *
  * 考勤:迟到20,早退20,旷课40,请假10
  *
  * 评估:优,90以上,良,60-89,差0-59
  *
  * 评价结果:
  *
  * 1:推荐晋级(80以上)  这种不需要获得活动中的晋级积分 优
  * 
2:实践1年推荐晋级(60-79)需要获得80分晋级积分,满12个月晋级 良
  * 
3:仅限当前级(60-79)无法晋级
  * 
4:通过(60以上)满80分可晋级
  * 
5:未通过(0-59)差
  *
  * @param $practice_score
  * @param $theory_score
  * @param $rule_score
  * @param $train_id
  * @param $user_id
  *
  */
 public function getSource($activityId, $userId, $practiceScore, $theoryScore, $ruleScore)
 {
     if (!empty($practiceScore) && !empty($theoryScore) && !empty($ruleScore)) {
         //成绩总评
         $scoreAppraise = intval(($practiceScore + $theoryScore + $ruleScore) / 3);
         //考勤
         $attendanceList = ActivityProcess::getAllByActivityIdAndUserId($activityId, $userId);
         $attendanceScore = 100;
         foreach ($attendanceList as $key => $val) {
             if ($val['status'] == ActivityProcess::NO_FINISH) {
                 $attendanceScore -= ActivityProcess::NO_FINISH_SOURCE;
             } else {
                 $attendanceScore == $attendanceScore;
             }
         }
         if ($attendanceScore < 0) {
             $attendanceScore = 1;
         }
         //获取评估
         foreach (ActivityUsers::$assessRules as $k => $v) {
             if ($attendanceScore > $v['small'] && $attendanceScore <= $v['big']) {
                 //考勤结果
                 $attendanceComment = ActivityUsers::$assessList[$k];
             }
             if ($scoreAppraise > $v['small'] && $scoreAppraise <= $v['big']) {
                 //评估总评
                 $commentAppraise = ActivityUsers::$assessList[$k];
                 //成绩评价结果
                 $performance = ActivityUsers::$performanceList[$k];
             }
             if ($practiceScore > $v['small'] && $practiceScore <= $v['big']) {
                 //实践评估
                 $practiceComment = ActivityUsers::$assessList[$k];
             }
             if ($theoryScore > $v['small'] && $theoryScore <= $v['big']) {
                 //理论评估
                 $theoryComment = ActivityUsers::$assessList[$k];
             }
             if ($ruleScore > $v['small'] && $ruleScore <= $v['big']) {
                 //规则评估
                 $rulesComment = ActivityUsers::$assessList[$k];
             }
         }
         foreach (ActivityUsers::$statusRules as $kk => $vv) {
             if ($scoreAppraise > $vv['small'] && $scoreAppraise <= $vv['big']) {
                 $status = $kk;
             }
         }
         $result = ['scoreAppraise' => $scoreAppraise, 'performance' => $performance, 'attendanceComment' => $attendanceComment, 'practiceComment' => $practiceComment, 'theoryComment' => $theoryComment, 'rulesComment' => $rulesComment, 'commentAppraise' => $commentAppraise, 'status' => $status];
     }
     return $result;
 }