<?php

namespace common\modules\testings\controllers;

use Yii;
use common\components\AdminController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

use common\modules\testings\models\Session;
use common\modules\testings\models\SearchSession;
use common\modules\testings\models\Passing;
use common\modules\testings\models\Test;
use common\modules\testings\models\User;
use common\modules\testings\models\UserGroup;
use common\modules\testings\models\UserGroupAssign;

class SessionAdminController extends AdminController
{
    public static function actionsTitles()
    {
        return array(
            'View'   => 'Просмотр сессии',
            'Create' => 'Создание сессии',
            'Update' => 'Редактирование сессии',
            'Delete' => 'Удаление сессии',
            'Manage' => 'Управление сессиями',
        );
    }

	public function actionView($id)
	{
		$model = $this->findModel($id);

		$test_ids = array_keys(\yii\helpers\ArrayHelper::map($model->tests, 'id', 'name'));

		$query = Passing::find()
			->joinWith('user')
			->where([
				'test_id' => $test_ids,
			]);

		$query->andWhere(['<>', 'end_date', ""]);

		$users = [];

		foreach ($query->all() as $passing) 
		{
			$users[$passing->end_date] = $passing->user;
		}

		return $this->render('view', [
			'model' => $model,
			'users' => $users
		]);
	}

	public function actionCreate()
	{
		$model = new Session;

        Yii::$app->controller->page_title = 'Добавить сессию';
        Yii::$app->controller->breadcrumbs = [
            ['Список сессий' => '/testings/session-admin/manage'],
            'Добавить сессию'
        ];

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['manage']);
        } else {
            $form = new \common\components\BaseForm('/common/modules/testings/forms/SessionForm', $model);
            return $this->render('create', [
                'model' => $model,
                'form' => $form->out
            ]);
        }
	}

	public function actionUpdate($id)
	{
		Yii::$app->controller->page_title = 'Редактировать сессию';
        Yii::$app->controller->breadcrumbs = [
            ['Список сессий' => '/testings/session-admin/manage'],
            'Редактировать сессию'
        ];

        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['manage']);
        } else {
            $form = new \common\components\BaseForm('/common/modules/testings/forms/SessionForm', $model);
            return $this->render('update', [
                'model' => $model,
                'form' => $form->out
            ]);
        }
	}

	public function actionDelete($id)
	{
		$this->findModel($id)->delete();

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

	public function actionManage()
	{
		$searchModel = new SearchSession();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        Yii::$app->controller->page_title = 'Список сессий';
        Yii::$app->controller->breadcrumbs = [
            'Список сессий',
        ];

        return $this->render('manage', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
	}

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