Your IP : 216.73.216.79


Current Path : /var/www/html/learn/backend/modules/learning/controllers/
Upload File :
Current File : /var/www/html/learn/backend/modules/learning/controllers/LearningChapterController.php

<?php

namespace backend\modules\learning\controllers;

use Yii;
use backend\modules\learning\models\LearningChapter;
use backend\modules\learning\models\LearningChapterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use backend\modules\learning\models\LearningQuiz;
use backend\modules\learning\models\LearningQuizAnswer;

/**
 * LearningChapterController implements the CRUD actions for LearningChapter model.
 */
class LearningChapterController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all LearningChapter models.
     * @return mixed
     */
    public function actionIndex($main_id) {
        $searchModel = new LearningChapterSearch();
        $searchModel->main_id = $main_id;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single LearningChapter model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new LearningChapter model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreatetext($main_id) {
        $model = new LearningChapter();
        $model->scenario = 'create';
        $model->chapter_type = 'teks';
        $model->main_id = $main_id;

        $chapter_sort = (int) LearningChapter::find()->where(['=', 'main_id', $model->main_id])->count();
        $model->chapter_sort = $chapter_sort + 1;

        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('createtext', ['model' => $model,]);
    }

    public function actionUpdatetext($chapter_id) {
        $model = $this->findModel($chapter_id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('updatetext', ['model' => $model,]);
    }

    public function actionCreateimage($main_id) {
        $model = new LearningChapter();
        $model->chapter_type = 'imej';
        $model->main_id = $main_id;

        $chapter_sort = (int) LearningChapter::find()->where(['=', 'main_id', $model->main_id])->count();
        $model->chapter_sort = $chapter_sort + 1;

        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {
            if (!empty($_POST['image_no'])) {
                $image_arr = [];
                foreach ($_POST['image_no'] as $image_no) {
                    $image_filename = '';
                    $image_upload = UploadedFile::getInstanceByName('image_new_filename_' . $image_no);
                    if (!empty($image_upload)) {
                        $image_upload2 = $image_no . '_' . date("YmdHis") . '.' . $image_upload->extension;
                        if ($image_upload->saveAs('../uploads/learning/' . $image_upload2))
                            $image_filename = $image_upload2;
                    }

                    $image_description = $_POST['image_description_' . $image_no];
                    $image_arr[] = ['image_filename' => $image_filename, 'image_description' => $image_description];
                }
                $model->chapter_content = json_encode($image_arr);
            }
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('createimage', ['model' => $model,]);
    }

    public function actionUpdateimage($chapter_id) {
        $model = $this->findModel($chapter_id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {
            if (!empty($_POST['image_no'])) {
                $image_arr = [];
                foreach ($_POST['image_no'] as $image_no) {
                    $image_filename = '';
                    $image_upload = UploadedFile::getInstanceByName('image_new_filename_' . $image_no);
                    if (!empty($image_upload)) {
                        $image_upload2 = $image_no . '_' . date("YmdHis") . '.' . $image_upload->extension;
                        if ($image_upload->saveAs('../uploads/learning/' . $image_upload2))
                            $image_filename = $image_upload2;
                    } else {
                        $image_filename = $_POST['image_old_filename_' . $image_no];
                    }

                    $image_description = $_POST['image_description_' . $image_no];
                    $image_arr[] = ['image_filename' => $image_filename, 'image_description' => $image_description];
                }
                $model->chapter_content = json_encode($image_arr);
            }
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('updateimage', ['model' => $model,]);
    }

    public function actionCreatevideo($main_id) {
        $model = new LearningChapter();
        $model->scenario = 'createvideo';
        $model->chapter_video_type = 'internal';
        $model->chapter_type = 'video';
        $model->main_id = $main_id;

        $chapter_sort = (int) LearningChapter::find()->where(['=', 'main_id', $model->main_id])->count();
        $model->chapter_sort = $chapter_sort + 1;

        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {
            $model->chapter_content = UploadedFile::getInstance($model, 'chapter_content');
            if (!empty($model->chapter_content)) {
                $chapter_content = date("YmdHis") . '.' . $model->chapter_content->extension;
                if ($model->chapter_content->saveAs('../uploads/learning/' . $chapter_content))
                    $model->chapter_content = $chapter_content;
            }
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('createvideo', ['model' => $model,]);
    }

    public function actionUpdatevideo($chapter_id) {
        $model = $this->findModel($chapter_id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        $original_chapter_content = $model->chapter_content;
        if ($model->load(Yii::$app->request->post())) {
            $model->chapter_content = UploadedFile::getInstance($model, 'chapter_content');
            if (!empty($model->chapter_content)) {
                $chapter_content = date("YmdHis") . '.' . $model->chapter_content->extension;
                if ($model->chapter_content->saveAs('../uploads/learning/' . $chapter_content))
                    $model->chapter_content = $chapter_content;
            } else {
                $model->chapter_content = $original_chapter_content;
            }
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('updatevideo', ['model' => $model,]);
    }

    public function actionCreatepowerpoint($main_id) {
        $model = new LearningChapter();
        $model->scenario = 'create';
        $model->chapter_type = 'powerpoint';
        $model->main_id = $main_id;

        $chapter_sort = (int) LearningChapter::find()->where(['=', 'main_id', $model->main_id])->count();
        $model->chapter_sort = $chapter_sort + 1;

        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {
            /* $model->chapter_content = UploadedFile::getInstance($model, 'chapter_content');
              if (!empty($model->chapter_content)) {
              $chapter_content = 'uploads/learning/chapter_ppt_' . date("YmdHis") . '.' . $model->chapter_content->extension;
              if ($model->chapter_content->saveAs($chapter_content))
              $model->chapter_content = $chapter_content;
              } */
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('createpowerpoint', ['model' => $model,]);
    }

    public function actionUpdatepowerpoint($chapter_id) {
        $model = $this->findModel($chapter_id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        //$original_chapter_content = $model->chapter_content;
        if ($model->load(Yii::$app->request->post())) {
            /* $model->chapter_content = UploadedFile::getInstance($model, 'chapter_content');
              if (!empty($model->chapter_content)) {
              $chapter_content = 'uploads/learning/chapter_ppt_' . date("YmdHis") . '.' . $model->chapter_content->extension;
              if ($model->chapter_content->saveAs($chapter_content))
              $model->chapter_content = $chapter_content;
              } else {
              $model->chapter_content = $original_chapter_content;
              } */
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('updatepowerpoint', ['model' => $model,]);
    }

    public function actionCreatepdf($main_id) {
        $model = new LearningChapter();
        $model->scenario = 'create';
        $model->chapter_type = 'pdf';
        $model->main_id = $main_id;

        $chapter_sort = (int) LearningChapter::find()->where(['=', 'main_id', $model->main_id])->count();
        $model->chapter_sort = $chapter_sort + 1;

        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {
            $model->chapter_content = UploadedFile::getInstance($model, 'chapter_content');
            if (!empty($model->chapter_content)) {
                $chapter_content = date("YmdHis") . '.' . $model->chapter_content->extension;
                if ($model->chapter_content->saveAs('../uploads/learning/' . $chapter_content))
                    $model->chapter_content = $chapter_content;
            }
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('createpdf', ['model' => $model,]);
    }

    public function actionUpdatepdf($chapter_id) {
        $model = $this->findModel($chapter_id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        $original_chapter_content = $model->chapter_content;
        if ($model->load(Yii::$app->request->post())) {
            $model->chapter_content = UploadedFile::getInstance($model, 'chapter_content');
            if (!empty($model->chapter_content)) {
                $chapter_content = date("YmdHis") . '.' . $model->chapter_content->extension;
                if ($model->chapter_content->saveAs('../uploads/learning/' . $chapter_content))
                    $model->chapter_content = $chapter_content;
            } else {
                $model->chapter_content = $original_chapter_content;
            }
            if ($model->save()) {
                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('updatepdf', ['model' => $model,]);
    }

    public function actionCreatequiz($main_id) {
        $model = new LearningChapter();
        $model->chapter_type = 'kuiz';
        $model->main_id = $main_id;

        $chapter_sort = (int) LearningChapter::find()->where(['=', 'main_id', $model->main_id])->count();
        $model->chapter_sort = $chapter_sort + 1;

        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->created_user_type = 'backend_user';
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {

            if ($model->save()) {

                $count_question = 0;
                $question_arr = $_POST['question_no'];
                if (!empty($question_arr)) {
                    foreach ($question_arr as $question_no) {
                        $count_question++;
                        $modelQuestion = new LearningQuiz();
                        $modelQuestion->chapter_id = $model->chapter_id;
                        $modelQuestion->quiz_question = $_POST['question_txt_' . $question_no];
                        $modelQuestion->quiz_sort = $count_question;
                        $modelQuestion->created_at = date("Y-m-d H:i:s");
                        $modelQuestion->created_by = Yii::$app->user->identity->id;
                        $modelQuestion->created_user_type = 'frontend_user';
                        $modelQuestion->updated_at = date("Y-m-d H:i:s");
                        $modelQuestion->updated_by = Yii::$app->user->identity->id;
                        $modelQuestion->updated_user_type = 'frontend_user';
                        if ($modelQuestion->save(false)) {

                            $answer_arr = $_POST['answer_no_' . $question_no];
                            if (!empty($answer_arr)) {
                                foreach ($answer_arr as $answer_no) {
                                    $modelAnswer = new LearningQuizAnswer();
                                    $modelAnswer->quiz_id = $modelQuestion->quiz_id;
                                    $modelAnswer->answer = $_POST['answer_txt_' . $answer_no];
                                    if (!empty($_POST['answer_correct_' . $answer_no]))
                                        $modelAnswer->answer_correct = $_POST['answer_correct_' . $answer_no];
                                    $modelAnswer->created_at = date("Y-m-d H:i:s");
                                    $modelAnswer->created_by = Yii::$app->user->identity->id;
                                    $modelAnswer->created_user_type = 'frontend_user';
                                    $modelAnswer->updated_at = date("Y-m-d H:i:s");
                                    $modelAnswer->updated_by = Yii::$app->user->identity->id;
                                    $modelAnswer->updated_user_type = 'frontend_user';
                                    $modelAnswer->save(false);
                                }
                            }
                        }
                    }
                }


                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('createquiz', ['model' => $model,]);
    }

    public function actionUpdatequiz($chapter_id) {
        $model = $this->findModel($chapter_id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->updated_user_type = 'backend_user';
        if ($model->load(Yii::$app->request->post())) {

            if ($model->save()) {

                $modelQuestionList = LearningQuiz::find()->where(['chapter_id' => $model->chapter_id])->orderBy(['quiz_sort' => SORT_ASC])->all();
                if (!empty($modelQuestionList)) {
                    foreach ($modelQuestionList as $rowQuestionList) {
                        LearningQuizAnswer::deleteAll(['quiz_id' => $rowQuestionList->quiz_id]);
                        $rowQuestionList->delete();
                    }
                }

                $count_question = 0;
                $question_arr = $_POST['question_no'];
                if (!empty($question_arr)) {
                    foreach ($question_arr as $question_no) {
                        $count_question++;
                        $modelQuestion = new LearningQuiz();
                        $modelQuestion->chapter_id = $model->chapter_id;
                        $modelQuestion->quiz_question = $_POST['question_txt_' . $question_no];
                        $modelQuestion->quiz_sort = $count_question;
                        $modelQuestion->created_at = date("Y-m-d H:i:s");
                        $modelQuestion->created_by = Yii::$app->user->identity->id;
                        $modelQuestion->created_user_type = 'frontend_user';
                        $modelQuestion->updated_at = date("Y-m-d H:i:s");
                        $modelQuestion->updated_by = Yii::$app->user->identity->id;
                        $modelQuestion->updated_user_type = 'frontend_user';
                        if ($modelQuestion->save(false)) {

                            $answer_arr = $_POST['answer_no_' . $question_no];
                            if (!empty($answer_arr)) {
                                foreach ($answer_arr as $answer_no) {
                                    $modelAnswer = new LearningQuizAnswer();
                                    $modelAnswer->quiz_id = $modelQuestion->quiz_id;
                                    $modelAnswer->answer = $_POST['answer_txt_' . $answer_no];
                                    if (!empty($_POST['answer_correct_' . $answer_no]))
                                        $modelAnswer->answer_correct = $_POST['answer_correct_' . $answer_no];
                                    $modelAnswer->created_at = date("Y-m-d H:i:s");
                                    $modelAnswer->created_by = Yii::$app->user->identity->id;
                                    $modelAnswer->created_user_type = 'frontend_user';
                                    $modelAnswer->updated_at = date("Y-m-d H:i:s");
                                    $modelAnswer->updated_by = Yii::$app->user->identity->id;
                                    $modelAnswer->updated_user_type = 'frontend_user';
                                    $modelAnswer->save(false);
                                }
                            }
                        }
                    }
                }


                return $this->redirect(['index', 'main_id' => $model->main_id]);
            }
        }
        return $this->render('updatequiz', ['model' => $model,]);
    }

    /**
     * Deletes an existing LearningChapter model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the LearningChapter model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return LearningChapter the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = LearningChapter::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }

    public function actionSortingchapter($data = '') {
        $count = 0;
        $data_arr = explode(',', $data);
        if (!empty($data_arr)) {
            foreach ($data_arr as $chapter_id) {
                $model = LearningChapter::findOne($chapter_id);
                if (!empty($model)) {
                    $count++;
                    $model->chapter_sort = $count;
                    $model->save(false);
                }
            }
        }
    }

}