| Current Path : /var/www/html/learn/frontend/controllers/ |
| Current File : /var/www/html/learn/frontend/controllers/SiteController.php |
<?php
namespace frontend\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use kartik\mpdf\Pdf;
use yii\helpers\Url;
/**
* Site controller
*/
class SiteController extends Controller {
/**
* {@inheritDoc}
* @see \yii\web\Controller::beforeAction()
*/
public function beforeAction($action) {
// $this->layout = 'main_default';
// TODO Auto-generated method stub
return parent::beforeAction($action);
}
/**
* @inheritdoc
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup', 'error'],
'rules' => [
[
'actions' => ['signup', 'error'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout', 'error'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
],
];
}
/**
* @inheritdoc
*/
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex() {
return $this->render('index');
}
public function stub() {
return '1231';
}
/**
* Logs out the current user.
*
* @return mixed
*/
public function actionLogout() {
Yii::$app->user->logout();
return $this->goHome();
}
public function actionError() {
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $exception->getMessage();
}
}
public function actionViewpdf($doc_id) {
if (($model = \common\models\CourseDocument::findOne($doc_id)) !== null) {
$bgImage = '';
if ($model->doc_code == 'sijil-penyertaan-kursus')
$bgImage = 'body{background-image:url(/www/assets/img/bgcert_kursusjppm.jpg);background-image-resize:6;}';
if ($model->doc_code == 'sijil-tamat-pembelajaran-interaktif')
$bgImage = 'body{background-image:url(/www/assets/img/bgcert_pembelajaraninteraktif.jpg);background-image-resize:6;}';
$pdf = new Pdf([
'mode' => Pdf::MODE_BLANK,
'filename' => $model->doc_title . '.pdf',
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_DOWNLOAD,
'content' => $model->doc_content,
'cssInline' => $bgImage,
]);
return $pdf->render();
}
}
public function actionViewlearningcert($enroll_id) {
if (($model = \backend\modules\learning\models\LearningEnroll::findOne($enroll_id)) !== null) {
$pdf = new Pdf([
'mode' => Pdf::MODE_BLANK,
'filename' => 'Certificate.pdf',
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_LANDSCAPE,
'destination' => Pdf::DEST_DOWNLOAD,
'content' => $model->enroll_certificate,
]);
return $pdf->render();
}
}
public function actionSortingchapter($data = '') {
$count = 0;
$data_arr = explode(',', $data);
if (!empty($data_arr)) {
foreach ($data_arr as $chapter_id) {
$model = \backend\modules\learning\models\LearningChapter::findOne($chapter_id);
if (!empty($model)) {
$count++;
$model->chapter_sort = $count;
$model->save(false);
}
}
}
}
public function actionUpdatelearningprogress($id = '') {
$model = \backend\modules\learning\models\LearningEnrollChapter::findOne($id);
if (!empty($model)) {
$enroll_id = $model->learning_enroll_id;
if (empty($model->enroll_end))
$model->enroll_end = date("Y-m-d H:i:s");
$model->enroll_progress = '100';
$model->save(false);
//update progress main
$totalChapter = 0;
$totalProgress = 0;
$modelAllChapter = \backend\modules\learning\models\LearningEnrollChapter::find()->where(['=', 'learning_enroll_id', $enroll_id])->all();
if (!empty($modelAllChapter)) {
foreach ($modelAllChapter as $rowAllChapter) {
$totalChapter++;
$totalProgress += $rowAllChapter->enroll_progress;
}
}
$percentageProgress = ($totalProgress / ($totalChapter * 100)) * 100;
$modelEnroll = \backend\modules\learning\models\LearningEnroll::findOne($enroll_id);
if (!empty($modelEnroll)) {
$modelEnroll->enroll_progress = round($percentageProgress);
if ($modelEnroll->enroll_progress == 100 && empty($modelEnroll->enroll_end)) {
$modelEnroll->enroll_end = date("Y-m-d H:i:s");
}
$modelEnroll->save(false);
}
}
}
public function actionSubmitquiz($enroll_chapter_id = '', $quiz_form = '') {
$update_status = 'failed';
$output_arr = [];
$quiz_form_arr = explode('<--->', $quiz_form);
$total_question = 0;
$correct_answer = 0;
if (!empty($quiz_form_arr)) {
foreach ($quiz_form_arr as $quiz_data) {
if (!empty($quiz_data)) {
$quiz_data_arr = explode('<->', $quiz_data);
if (!empty($quiz_data_arr)) {
$question_id = $quiz_data_arr[0];
$answer_id = $quiz_data_arr[1];
$question_txt = '';
$modelQuestion = \backend\modules\learning\models\LearningQuiz::findOne($question_id);
if (!empty($modelQuestion))
$question_txt = $modelQuestion->quiz_question;
$user_answer = '';
$answer_status = 0;
$modelAnswer = \backend\modules\learning\models\LearningQuizAnswer::findOne($answer_id);
if (!empty($modelAnswer)) {
$user_answer = $modelAnswer->answer;
if ($modelAnswer->answer_correct == 1) {
$answer_status = 1;
$correct_answer++;
}
}
$answer_list = [];
$modelAnswerList = \backend\modules\learning\models\LearningQuizAnswer::find()->where(['=', 'quiz_id', $question_id])->orderBy(['answer_id' => SORT_ASC])->all();
if (!empty($modelAnswerList)) {
foreach ($modelAnswerList as $rowAnswerList) {
$answer_list[] = $rowAnswerList->answer;
}
}
$total_question++;
$output_arr[] = [
'question' => $question_txt,
'answer_list' => $answer_list,
'user_answer' => $user_answer,
'answer_status' => $answer_status,
];
}
}
}
}
$quiz_data = json_encode($output_arr);
$quiz_marks = $correct_answer . '/' . $total_question;
$quiz_marks_percent = $correct_answer / $total_question * 100;
$modelUpdate = \backend\modules\learning\models\LearningEnrollChapter::findOne($enroll_chapter_id);
if (!empty($modelUpdate)) {
$modelUpdate->quiz_data = $quiz_data;
$modelUpdate->quiz_marks = $quiz_marks;
$modelUpdate->quiz_marks_percent = $quiz_marks_percent;
if ($modelUpdate->save(false)) {
$update_status = 'success';
//generate certificate here
//if ($quiz_marks_percent >= 80) {
$enroll_id = $modelUpdate->learning_enroll_id;
$modelEnroll = \backend\modules\learning\models\LearningEnroll::findOne($enroll_id);
if (!empty($modelEnroll)) {
if (empty($modelEnroll->enroll_certificate)) {
$data_id = $enroll_id;
//generate certificate here
$date_generate = date("j") . ' ' . Yii::t('app', date("F")) . ' ' . date("Y");
$user_name = '';
$course_title = '';
if (isset($data_id) && !empty($data_id)) {
$sql = "SELECT l.learning_title AS course_title, u.fullname AS user_name
FROM learning_enroll e
INNER JOIN frontend_user u ON u.id=e.frontend_user_id
INNER JOIN learning_main l ON l.learning_id=e.learning_main
WHERE e.enroll_id=" . $data_id;
$model = Yii::$app->db->createCommand($sql)->queryAll();
if (!empty($model)) {
foreach ($model as $row) {
$user_name = $row['user_name'];
$course_title = $row['course_title'];
}
}
}
$output_doc = '
<div style="text-align:center;width:100%;margin:0 auto;">
<img src="'.Url::base(true).'/assets/img/Dynalearn.png" style="height:150px;margin-bottom:2em;">
<div style="padding-bottom:0.4em;">
<h1 style="color:#8b0000;"><i><b>Certificate of Completion</b></i></h1>
</div>
<div style="padding-bottom:0.4em;">
<h4>This is to certify</h4>
<h4 style="line-height:150%"><b>' . $user_name . '</b></h4>
</div>
<div style="padding-bottom:0.4em;">
<h4>has completed the course</h4>
<h4 style="line-height:150%"><b>' . $course_title . '</b></h4>
</div>
<div style="padding-bottom:0.4em;">
<h4><i>on</i></h4>
<h4 style="line-height:150%"><b>' . $date_generate . '</b></h4>
</div>
</div>';
$modelEnroll->enroll_certificate = $output_doc;
$modelEnroll->save(false);
}
}
//}
}
}
return $update_status;
}
}