<?php

namespace common\modules\school\controllers;

use Yii;

use common\components\BaseController;

use common\modules\school\models\Lessons;
use common\modules\school\models\LessonImage;
use common\modules\testings\models\Passing;

class LessonController extends BaseController
{    
    public static function actionsTitles()
    {
        return [
            'View'	 => 'Урок',
            'Download-documents' => 'Скачивание документа',
        ];
    }
    
    public function actionView($id)
    {
        if($_GET["source"] == 'email' && !Yii::$app->response->cookies->get('source')){
            $options['name'] = 'source';
            $options['value'] = 'email'; 
            $options['expire'] = time()+86400*365; 
            $cookie = new \yii\web\Cookie($options);
            \Yii::$app->response->cookies->add($cookie); 
        }
        
       $model = Lessons::findOne($id);

        \Yii::$app->view->registerMetaTag([
            'property' => 'og:title',
            'content' => $model->title
        ]);

        // Ищим прохождение теста
        $passing = null;
        if($model->test)
        {
            $passing = Passing::find()->where(['test_id' => $model->test_id, 'user_id' => Yii::$app->user->id])->one();
        }

        return $this->render('view', compact('model', 'passing'));
    }
    
   
   public function actionDownloadDocuments($id)
   {
       $modelLesson = new Lessons();
       $docId=intval($id);
       $doc = LessonImage::findOne($docId);
      /* $fileContent = file_get_contents($modelLesson->getPath());
       \Yii::app()->request->sendFile($doc->filename, $fileContent);*/
       $file = $modelLesson->getPath().$doc->filename;
       if (file_exists($file)) {
           \Yii::$app->response->sendFile($file);
        }
   }
   
    function cookie($name,$value=null,$expire=null){  

     $options['name'] = $name;
     $options['value'] = $value; 
     $options['expire'] = time()+86400*365; 
     $cookie = new \yii\web\Cookie($options);
     \Yii::$app->response->cookies->add($cookie); 
 }
}
