<?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)
    {
        $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);
        }
   }
}
