/**
  * Luu ket qua test vao db 
  **/
 public function actionResult()
 {
     $this->layout = '//layouts/main_trainer';
     $cs = Yii::app()->getClientScript();
     $cs->registerCssFile(Yii::app()->theme->baseUrl . '/css/jquery-ui.css?ver=' . time());
     $cs->registerCssFile(Yii::app()->theme->baseUrl . '/css/dialog.css?ver=' . time());
     //$cs->registerCssFile(Yii::app()->theme->baseUrl.'/css/jquery.countdown.css?ver='.time());
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jquery.simplemodal-1.4.4.js', CClientScript::POS_END);
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jquery-ui.js');
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jquery.slimscroll.min.js');
     $courseid = isset($_REQUEST['course_id']) ? $_REQUEST['course_id'] : '';
     $lecture_id = isset($_REQUEST['lecture_id']) ? $_REQUEST['lecture_id'] : '';
     $test_id = isset($_REQUEST['test_id']) ? $_REQUEST['test_id'] : '';
     $total = isset($_REQUEST['total']) ? $_REQUEST['total'] : '';
     $kind = isset($_REQUEST['kind']) ? $_REQUEST['kind'] : '';
     //Thong tin bai test
     $testDetails = WTestParent::model()->find("id=" . $test_id);
     //Get tong so cau hoi
     $db = Yii::app()->db;
     $comm = $db->createCommand()->select('t.*')->from('pls_test_user t')->where('t.id_test_parent=' . $test_id . ' AND t.user_id=' . Yii::app()->user->id . ' AND kind=' . $kind . ' AND t.course_id=' . $courseid . ' AND t.lecture_id=' . $lecture_id);
     $dataTestUser = $comm->queryRow();
     if (empty($dataTestUser)) {
         $this->redirect(Yii::app()->createUrl("/"));
     }
     $totalquestion = 0;
     $totalTrue = 0;
     $totalQuestionText = 0;
     $totalfails = 0;
     $totalnotdoing = 0;
     $totalnoanswer = 0;
     if (!empty($dataTestUser) and !empty($dataTestUser["dataquestion"])) {
         if (!empty($dataTestUser["dataquestion"])) {
             $dataquestion = json_decode($dataTestUser["dataquestion"], true);
             $totalquestion = count($dataquestion);
         }
         //lay so cau hoi dung
         if (!empty($dataTestUser["datafinish"])) {
             $datafinish = json_decode($dataTestUser["datafinish"], true);
             foreach ($datafinish as $itemdatafinish) {
                 if ($itemdatafinish["question_type"] != 2 and isset($itemdatafinish["check"]) and $itemdatafinish["check"] == "OK") {
                     ++$totalTrue;
                 } elseif ($itemdatafinish["question_type"] != 2 and isset($itemdatafinish["check"]) and $itemdatafinish["check"] == "NOK" and isset($itemdatafinish["doing"]) and $itemdatafinish["doing"] == 1) {
                     $totalfails++;
                 } elseif ($itemdatafinish["question_type"] != 2 and isset($itemdatafinish["check"]) and $itemdatafinish["check"] == "NOK" and isset($itemdatafinish["doing"]) and $itemdatafinish["doing"] == 0) {
                     $totalnotdoing++;
                 } elseif ($itemdatafinish["question_type"] == 2) {
                     ++$totalQuestionText;
                 } elseif (!isset($itemdatafinish["check"])) {
                     ++$totalnoanswer;
                 }
             }
         }
     }
     // Tong diem
     $comm->reset();
     $comm = $db->createCommand()->select('t.*')->from('pls_user_test t')->where('t.test_id=' . $test_id . ' AND test_kind=' . $kind . ' AND t.course_id=' . $courseid . ' AND t.lecture_id=' . $lecture_id . ' AND user_id=' . Yii::app()->user->id);
     $dataUserTest = $comm->queryRow();
     $courseDetails = WCourse::model()->find("id=" . $courseid);
     $username = WUser::getFullNameUser(Yii::app()->user->id);
     $trainer = WUser::getFullNameUser($courseDetails->created_by);
     $title = "Học viên " . $username . " Làm xong bài Test :" . $testDetails->test_name;
     $content = "Dear <strong>" . $trainer . "</strong>";
     $content .= "<br/>Có học viên <strong>" . $username . "</strong> làm xong bài test : " . $testDetails->test_name . " (" . date('H:i d/m/Y') . ")";
     $content .= "<br/><a href='" . Yii::app()->createUrl("wUserTest/viewdetails", array("id" => $dataUserTest["id"])) . "'>Click để xem chi tiết</a>";
     WMessage::SendMessage($title, $content, "notifi", WUser::getIDMinUser(), $courseDetails->created_by);
     $modeltest = WTest::model()->find('id=:id', array(':id' => $test_id));
     $this->render('/wTest/trainee/_result', array('testDetails' => $testDetails, 'totalquestion' => $totalquestion, 'totalQuestionText' => $totalQuestionText, 'totalTrue' => $totalTrue, 'totalfails' => $totalfails, 'totalnotdoing' => $totalnotdoing, 'totalnoanswer' => $totalnoanswer, 'dataUserTest' => $dataUserTest, 'dataTestUser' => $dataTestUser));
 }
 public static function SendMessage($title, $content, $typeSend = "notifi", $iduserFrom = NULL, $iduserTo)
 {
     $titlemessage = $title;
     $contentmessage = $content;
     if (empty($iduserFrom)) {
         $iduserFrom = WUser::getIDMinUser();
     }
     $date = date('Y-m-d H:i:s');
     if (!empty($iduserTo)) {
         $message = new WMessage();
         $message->status = 1;
         $message->title = $titlemessage;
         $message->created_by = $iduserFrom;
         $message->created_date = $date;
         $message->type_message = $typeSend;
         if ($message->save()) {
             $idmessage = $message->id;
             $messgetTrash = new WMessageTrash();
             $messgetTrash->id_message = $idmessage;
             $messgetTrash->trash = 0;
             $messgetTrash->save();
             $messageContent = new WMessageContent();
             $messageContent->created_date = $date;
             $messageContent->content = $contentmessage;
             $messageContent->id_message = $idmessage;
             $messageContent->created_by = $iduserFrom;
             $messageContent->status = 1;
             if ($messageContent->save()) {
                 $idmessagecontent = $messageContent->id;
                 if (!empty($iduserTo)) {
                     $dataUser = WUser::getUserByUsername($iduserTo);
                     $messageSendto = new WMessageSendto();
                     $messageSendto->id_message = $idmessage;
                     $messageSendto->id_message_content = $idmessagecontent;
                     $messageSendto->id_user_to = $iduserTo;
                     $messageSendto->type_message = $typeSend;
                     $messageSendto->status = 0;
                     $messageSendto->trash = 0;
                     if ($messageSendto->save()) {
                     } else {
                         echo json_encode(array('result' => 'false', 'html' => 'Lỗi thao tác 03. Mời bạn thử lại'));
                         die;
                     }
                 }
             } else {
                 echo json_encode(array('result' => 'false', 'html' => 'Lỗi thao tác 02. Mời bạn thử lại'));
                 die;
             }
         } else {
             echo json_encode(array('result' => 'false', 'html' => 'Lỗi thao tác 01. Mời bạn thử lại'));
             die;
         }
     } else {
         echo json_encode(array('result' => 'false', 'html' => 'Không có User nào cần gửi'));
     }
 }
 public function actionSendComment()
 {
     $dataType = $_REQUEST["dataType"];
     $dataId = $_REQUEST["dataId"];
     $action_url = $_REQUEST["action_url"];
     $contentComment = $_REQUEST["contentComment"];
     $parent_id = $_REQUEST["parent_id"];
     $_db = Yii::app()->db;
     $comm = $_db->createCommand();
     $result = $comm->insert('pls_comment', array('parent_id' => $parent_id, 'user_id' => Yii::app()->user->id, 'content' => $contentComment, 'data_id' => $dataId, 'data_type' => $dataType, 'status' => 1, 'created_date' => date('Y-m-d H:i:s'), 'action_url' => strtolower($action_url)));
     if ($result) {
         $idnewrecord = Yii::app()->db->getLastInsertID($result);
         if ($dataType == "course") {
             $courseDetails = WCourse::model()->find("id=" . $dataId);
             if (Yii::app()->user->id != $courseDetails->created_by) {
                 $title = "Bình Luận Khóa học: " . $courseDetails->course_name;
                 $content = "";
                 $user = WUser::getUserById(Yii::app()->user->id);
                 if (Yii::app()->session["group_id"] == 1) {
                     $content .= "Có giáo viên <strong>" . $user->username . "</strong> bình luận tại Khóa học: <a href='" . Yii::app()->createUrl('wCourse/view', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . "'>\"" . $courseDetails->course_name . "\"</a>";
                 } else {
                     if (Yii::app()->session["group_id"] == 2) {
                         $content .= "Có học viên <strong>" . $user->username . "</strong> bình luận tại Khóa học: <a href='" . Yii::app()->createUrl('wCourse/view', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . "'>\"" . $courseDetails->course_name . "\"</a>";
                     } else {
                         if (Yii::app()->session["group_id"] == 3) {
                             $content .= "Có Quản trị viên <strong>" . $user->username . "</strong> bình luận tại Khóa học: <a href='" . Yii::app()->createUrl('wCourse/view', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . "'>\"" . $courseDetails->course_name . "\"</a>";
                         }
                     }
                 }
                 $content .= '<br/>Click xem chi tiết: <a href="' . Yii::app()->createUrl('wCourse/view', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . '">' . $courseDetails->course_name . '</a>';
                 WMessage::SendMessage($title, $content, "notifi", WUser::getIDMinUser(), $courseDetails->created_by);
             }
         } elseif ($dataType == "courselecture") {
             $courseLecture = WLecture::model()->find("id=" . $dataId);
             if (Yii::app()->user->id != $courseLecture->created_by) {
                 $title = "Bình Luận Bài Giảng: " . $courseLecture->lecture_name;
                 $content = "";
                 $user = WUser::getUserById(Yii::app()->user->id);
                 if (Yii::app()->session["group_id"] == 1) {
                     $content .= "Có giáo viên <strong>" . $user->username . "</strong> bình luận tại Bài Giảng: <a href='" . Yii::app()->createUrl('wLecture/View', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . "'>\"" . $courseLecture->lecture_name . "\"</a>";
                 } else {
                     if (Yii::app()->session["group_id"] == 2) {
                         $content .= "Có học viên <strong>" . $user->username . "</strong> bình luận tại Bài Giảng: <a href='" . Yii::app()->createUrl('wLecture/View', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . "'>\"" . $courseLecture->lecture_name . "\"</a>";
                     } else {
                         if (Yii::app()->session["group_id"] == 3) {
                             $content .= "Có Quản trị viên <strong>" . $user->username . "</strong> bình luận tại Bài Giảng: <a href='" . Yii::app()->createUrl('wLecture/View', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . "'>\"" . $courseLecture->lecture_name . "\"</a>";
                         }
                     }
                 }
                 $content .= '<br/>Click xem chi tiết: <a href="' . Yii::app()->createUrl('wLecture/View', array("id" => $dataId, "row" => "comment_row_" . $idnewrecord)) . '">' . $courseLecture->lecture_name . '</a>';
                 WMessage::SendMessage($title, $content, "notifi", WUser::getIDMinUser(), $courseLecture->created_by);
             }
         }
         echo json_encode(array("result" => "true"));
     } else {
         echo json_encode(array("result" => "false"));
     }
 }
 public function actionEndLectureText()
 {
     $course_lecture_id = $_POST['course_lecture_id'];
     $idtraininglog = $_POST['idtraininglog'];
     $totalText = $_POST['totalText'];
     $totalViewText = $_POST['totalViewText'];
     $viewListText = $_POST['viewListText'];
     $user_id = Yii::app()->user->id;
     $timeOK = $totalViewText / $totalText * 100;
     $viewText = $totalViewText . ' / ' . $totalText;
     $check = 'NOK';
     if ($timeOK >= 90) {
         $check = 'OK';
     }
     //$des = ' View : ('. number_format($timeOK,2,',','.') .'% View pages '.$viewText.')';
     $des = ' Hoàn thành : (' . number_format($timeOK, 2, ',', '.') . '% Xem ' . $viewText . ')';
     $desNotifi = 'Học viên hoàn thành : (' . number_format($timeOK, 2, ',', '.') . '% Xem ' . $viewText . ')';
     $modelTrainingLog = WTrainingLog::model()->findByPk($idtraininglog);
     $modelTrainingLog->user_id = $user_id;
     $modelTrainingLog->description = $des;
     $modelTrainingLog->result = $check;
     $modelTrainingLog->data = $viewListText;
     if ($modelTrainingLog->save()) {
         if ($modelTrainingLog->result == 'OK') {
             $userinfo = WUser::getFullNameUser(Yii::app()->user->id);
             $couseDetails = WCourseLecture::getInfoCourse($course_lecture_id);
             $trainer = WUser::getFullNameUser($couseDetails["created_by"]);
             $title = "Học viên " . $userinfo . " hoàn thành bài giảng:" . $couseDetails["course_name"];
             $content = "Dear <strong>" . $trainer . "</strong>";
             $content .= "<br/>Học viên <strong>" . $userinfo . '</strong> hoàn thành Bài giảng: <a href="' . Yii::app()->createUrl("wCourse/view", array("id" => $couseDetails["id"])) . '"><strong>' . $couseDetails["course_name"] . " (" . date('H:i d/m/Y') . ")</strong></a>";
             //$content .="<br/>==> <a href='".Yii::app()->createUrl("wCourse/view",array("id" => $couseDetails["id"]))."'>".$couseDetails["course_name"]."</a>";
             //$content .="<br />Congratulations, you have completed the lesson. ";
             //$content .="<br />Xin chúc mừng, Bạn đã hoàn thành bài học. ";
             $content .= "<br />" . $modelTrainingLog->description;
             WMessage::SendMessage($title, $content, "notifi", WUser::getIDMinUser(), $couseDetails["created_by"]);
             $mess = 'Xin chúc mừng, Bạn đã hoàn thành bài học. <br />' . $modelTrainingLog->description;
             // Chúc mừng bạn đã hoàn thành bài học
         } else {
             //$mess = 'You need to learn the lesson. <br />'.$modelTrainingLog->description;//Bạn cần phải học lại bài học.
             $mess = 'Bạn chưa hoàn thành bài học. <br />' . $modelTrainingLog->description;
             //Bạn cần phải học lại bài học.
         }
         echo json_encode(array('status' => 'true', 'result' => $modelTrainingLog->result, 'mess' => $mess));
     } else {
         echo json_encode(array('status' => 'false'));
     }
 }