/**
  * Deletes a particular model.
  * If deletion is successful, the browser will be redirected to the 'admin' page.
  * @param integer $id the ID of the model to be deleted
  */
 public function actionDelete($id)
 {
     if ($_GET['group_id'] == 1) {
         //check exist course and test created by a trainer
         if (Course::model()->checkCourse($id) && Test::model()->checkTest($id)) {
             Yii::app()->user->setFlash('unsuccess', ' Can not delete a trainer.');
             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin', 'group_id' => $_GET['group_id']));
         }
         // delete trainer
         $this->loadModel($id)->delete();
         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
         if (!isset($_GET['ajax'])) {
             Yii::app()->user->setFlash('success', 'You did delete a trainer when: <br>- Not exist course created by trainer.<br>- Not exist test created by trainer.');
             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin', 'group_id' => $_GET['group_id']));
         }
     } elseif ($_GET['group_id'] == 2) {
         //Delete course trainee
         CourseTrainee::model()->deleteCourseTrainee($id);
         //Delete user test
         UserTest::model()->deleteUserTest($id);
         //Delete training log and training random question
         TrainingLog::model()->deleteTrainingLog($id);
         TrainingRandomquestion::model()->deleteTrainingRadomQuestion($id);
         //delete trainee
         $this->loadModel($id)->delete();
         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
         Yii::app()->user->setFlash('success', 'You did delete a trainee: <br>- Delete course trainee.<br>- Delete Training log and training random question.<br>- Delete user test and user answers.');
         if (!isset($_GET['ajax'])) {
             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin', 'group_id' => $_GET['group_id']));
         }
     } elseif ($_GET['group_id'] == 3) {
         // delete training manager
         $this->loadModel($id)->delete();
         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
         Yii::app()->user->setFlash('success', 'You did delete a training manager.');
         if (!isset($_GET['ajax'])) {
             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin', 'group_id' => $_GET['group_id']));
         }
     } elseif ($_GET['group_id'] == 4) {
         // delete team leader
         ATrainingTeam::deleteTeamleader($id);
         $this->loadModel($id)->delete();
         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
         Yii::app()->user->setFlash('success', 'You did delete a team leader.');
         if (!isset($_GET['ajax'])) {
             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin', 'group_id' => $_GET['group_id']));
         }
     }
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     if (empty(Yii::app()->user->id) || Yii::app()->session['group_id'] == 2) {
         $this->redirect(Yii::app()->createUrl(''));
     }
     $cs = Yii::app()->getClientScript();
     //$cs->registerScriptFile(Yii::app()->theme->baseUrl.'/js/ckeditor/ckeditor.js');
     $cs->registerCssFile(Yii::app()->theme->baseUrl . '/js/jqueryUpload/jquery.fileupload.css');
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/bootstrap-datepicker/bootstrap-datetimepicker.js', CClientScript::POS_END);
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jqueryUpload/jquery.ui.widget.js', CClientScript::POS_END);
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jqueryUpload/jquery.fileupload.js', CClientScript::POS_END);
     $model = WCourse::model()->find('id=' . $id . ' AND created_by=' . Yii::app()->user->id);
     $nameSession = "";
     $nameSessionTrainee = "";
     if (!empty($model->id)) {
         $nameSession = "_" . $model->id;
         $nameSessionTrainee = $model->id;
     }
     if (empty($model)) {
         $this->redirect(Yii::app()->createUrl(''));
     }
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $model->start_date = date('Y-m-d', strtotime($model->start_date));
     $model->end_date = date('Y-m-d', strtotime($model->end_date));
     if (isset($_POST['WCourse'])) {
         if (!empty($_POST['avatar'])) {
             $_POST['WCourse']['avatar'] = Common::createThumb('avatars/course', $_POST['avatar'], $_POST['WCourse']['course_name']);
             $name_avatar_old = $model->avatar;
             if (!empty($name_avatar_old) and file_exists(Yii::app()->params['upload_path'] . 'uploads/avatars/course/' . $name_avatar_old)) {
                 unlink(Yii::app()->params['upload_path'] . 'uploads/avatars/course/' . $name_avatar_old);
             }
         }
         $model->attributes = $_POST['WCourse'];
         $model->created_date = date('Y-m-d H:i:s');
         if (Yii::app()->session['group_id'] == 2) {
             $model->company_code = Yii::app()->session['company_code'];
         }
         $model->created_by = Yii::app()->user->id;
         $dataLecture = Yii::app()->session->get('dataLecture' . $nameSession) ? json_decode(Yii::app()->session->get('dataLecture' . $nameSession), true) : array();
         $listTrainee = Yii::app()->session->get('listTrainee_' . $nameSessionTrainee) ? json_decode(Yii::app()->session->get('listTrainee_' . $nameSessionTrainee), true) : array();
         $model->trainee_count = count($listTrainee);
         $model->lecture_count = count($dataLecture);
         if ($model->save()) {
             if (!empty($dataLecture)) {
                 foreach ($dataLecture as $item) {
                     $modelCourseLecture = CourseLecture::model()->find('course_id=' . $model->id . ' AND lecture_id=' . $item['id']);
                     if (empty($modelCourseLecture)) {
                         $modelCourseLecture = new CourseLecture();
                         $modelCourseLecture->course_id = $model->id;
                         $modelCourseLecture->lecture_id = $item['id'];
                         $modelCourseLecture->trainer_id = Yii::app()->user->id;
                     }
                     $modelCourseLecture->start_date = $item['start_date'];
                     $modelCourseLecture->end_date = $item['end_date'];
                     $modelCourseLecture->status = 1;
                     if ($modelCourseLecture->save()) {
                         unset($_SESSION['dataLecture' . $nameSession]);
                     }
                 }
             }
         }
         if (!empty($listTrainee)) {
             foreach ($listTrainee as $item) {
                 $modelCourseTrainee = WCourseTrainee::model()->find('course_id=' . $model->id . ' AND trainee_id=' . $item['id']);
                 if (empty($modelCourseTrainee)) {
                     $modelCourseTrainee = new WCourseTrainee();
                     $modelCourseTrainee->course_id = $model->id;
                     $modelCourseTrainee->trainee_id = $item['id'];
                     $modelCourseTrainee->status = 1;
                 }
                 if ($modelCourseTrainee->save()) {
                     unset($_SESSION['listTrainee_' . $nameSessionTrainee]);
                 }
             }
         }
         $this->redirect(array('update', 'id' => $model->id));
     }
     $dataCourseLecture = array();
     if (!empty($id)) {
         unset($_SESSION['dataLecture' . $nameSession]);
         unset($_SESSION['listTrainee_' . $nameSessionTrainee]);
         $data = CourseLecture::model()->findAll('course_id=' . $id . ' ORDER BY start_date ASC');
         $data = CJSON::decode(CJSON::encode($data));
         $dataCourseLecture = array();
         if (!empty($data)) {
             foreach ($data as $key => $item) {
                 if (!empty($item["lecture_id"])) {
                     $lectureDetails = Lecture::model()->find('id=' . $item['lecture_id']);
                     if (!empty($lectureDetails)) {
                         $lectureDetails = CJSON::decode(CJSON::encode($lectureDetails));
                         $dataCourseLecture[$item['lecture_id']]['id'] = $item['lecture_id'];
                         $dataCourseLecture[$item['lecture_id']]['start_date'] = date('Y-m-d H:i', strtotime($item['start_date']));
                         $dataCourseLecture[$item['lecture_id']]['end_date'] = date('Y-m-d H:i', strtotime($item['end_date']));
                         $dataCourseLecture[$item['lecture_id']]['lecture_name'] = $lectureDetails['lecture_name'];
                         $dataCourseLecture[$item['lecture_id']]['course_id'] = $item['course_id'];
                     }
                 }
             }
         }
         $dataTrainee = CourseTrainee::model()->findAll('course_id=' . $id . ' AND status=1');
         $dataTrainee = CJSON::decode(CJSON::encode($dataTrainee));
         $listTrainee = array();
         if (!empty($dataTrainee)) {
             foreach ($dataTrainee as $item) {
                 $traineeDetails = WUser::model()->find('id=' . $item['trainee_id']);
                 if (!empty($traineeDetails)) {
                     $traineeDetails = CJSON::decode(CJSON::encode($traineeDetails));
                     $listTrainee[$traineeDetails['id']]['id'] = $traineeDetails['id'];
                     $listTrainee[$traineeDetails['id']]['course_id'] = $id;
                     $listTrainee[$traineeDetails['id']]['fullname'] = $traineeDetails['fullname'];
                     $listTrainee[$traineeDetails['id']]['username'] = $traineeDetails['username'];
                 }
             }
         }
         Yii::app()->session->add('listTrainee_' . $nameSessionTrainee, json_encode($listTrainee));
         Yii::app()->session->add('dataLecture' . $nameSession, json_encode($dataCourseLecture));
     }
     $this->render('update', array('model' => $model, 'dataCourseLecture' => $dataCourseLecture));
 }
 /**
  * Returns the static model of the specified AR class.
  * Please note that you should have this exact method in all your CActiveRecord descendants!
  * @param string $className active record class name.
  * @return WCourseTrainee the static model class
  */
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }