| Current Path : /var/www/html/ekursus/backend/controllers/ |
| Current File : /var/www/html/ekursus/backend/controllers/SurveyController.php |
<?php
namespace backend\controllers;
use Yii;
use backend\models\survey\Survey;
use backend\models\survey\SurveySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\course\Course;
use backend\models\timetable\Timetable;
use backend\models\contractorstaff\ContractorStaff;
use backend\models\contractor\Contractor;
use backend\models\Application;
use backend\models\Trainee;
/**
* SurveyController implements the CRUD actions for Survey model.
*/
class SurveyController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Survey models.
* @return mixed
*/
public function actionIndex($kod_jadual)
{
$searchModel = new SurveySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Survey 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 Survey model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate($kod_permohonan)
{
$kod_permohonan = $_GET['kod_permohonan'];
$model = new Survey;
$model->kod_permohonan = $kod_permohonan;
// var_dump($model);exit;
if ($model->load(Yii::$app->request->post())) {
if ($model->save(false)) {
$trainee = new Trainee();
$trainee = Trainee::find()->where(['kod_permohonan' => $_GET['kod_permohonan']])->one();
$trainee->borang_penilaian = 'Y';
if ($trainee->save(false)) {
$application = new Application();
$no_siri = $application->no_siri_sijil;
$year = date('Y', strtotime($timetable->tkh_kursus));
$negeri = $timetable->kod_negeri;
$application->no_siri_sijil = $this->genSiri($negeri, $year);
$application->save(false);
yii::$app->session->setFlash('success','Borang penilaian berjaya dihantar. Anda boleh mencetak sijil penyertaan');
$this->redirect('index.php?r=trainee/index4');
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
public function actionSave($kod_permohonan)
{
$model = new Survey;
if ($model->load(Yii::$app->request->post())) {
$model->kod_permohonan = $_GET['kod_permohonan'];
// var_dump($model);exit;
if ($model->save(false)) {
$trainee = Trainee::find()->where(['kod_permohonan' => $model->kod_permohonan])->one();
$trainee->borang_penilaian = 'Y';
// var_dump($trainee);exit;
if ($trainee->save(false)) {
$application = Application::find()->where(['id' => $model->kod_permohonan])->one();
$application->no_siri_sijil = $this->genSiri($negeri, $year);
if ($application->save(false)) {
Yii::$app->session->setFlash('success', Yii::t('app', 'Terima kasih kerana menjawab borang penilaian. Anda boleh mencetak sijil penyertaan kursus'));
return $this->redirect('index.php?r=trainee/index4');
}
}
}
}
return $this->render('create', [
'kod_permohonan' => $model->kod_permohonan,
]);
}
private function genSiri($negeri2, $year) {
$sql = "select max(no_siri_sijil) as id from application a, timetable tt
where
a.kod_jadual = tt.id and
year(tt.tkh_kursus) = '$year' and
tt.kod_negeri = '$negeri2'";
$app = Application::findBySql($sql)->one();
if ($app->id == '') {
return 1;
} else {
return $app->id + 1;
}
}
/**
* Updates an existing Survey model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Survey 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 Survey model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Survey the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Survey::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}