<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\KuferTools\Dozent\Lehrplan\PrintGrafikListenLehrplan;


class PrintGrafikListenLehrplanUseCase extends \Kufer\KuferTools\Dozent\KursUebersicht\PdfListen\GrafikListen\PrintGrafikListen\PrintGrafikListenUseCase
{
  public function __construct($pRequest)
  {
    parent::__construct($pRequest);
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    }
    
    if (!$this->isRechtAllowed('23.0.4', 7)) { 
      return false;
    }

    $knrs = explode(':', $this->request->id);
    if (count($knrs) > 0) {
      $user = $this->getUser();
      $dbwKursTage = new \DBWorkKurseTageWT();
      $knrDoz = $dbwKursTage->getKursNrByDozentNr($user->dnr);

      foreach ($knrs as $knr) {
        if (!in_array($knr, $knrDoz)) {
          return false;
        }
      }
    }

    return true;
  }

  public function action()
  {
    if (!$this->_printAllowed4User) {
      $fehlerText = ' Keine Zugriffsberechtigung auf einen Kurs oder mehrere Kurse für Liste ' . $this->request->listName . '.';
      \Logger::logg($fehlerText, 120);
    } else {
      $this->listObj->loadSettings();
      $this->setKursTageUndZeit();
      $this->recomputeKursTage();
    }

    return $this->getResponse();
  }

  
  protected function _isPrintAllowed()
  {
    return $this->listObj->isAllowedToPrint(7);
  }

  private function recomputeKursTage()
  {
    $dbwKursTage = new \DBWorkKurseTageWT();
    $dbwKursTage->newSearch();
    $dbwKursTage->fromobject->datum = $this->listObj->settings->kurstage->von;
    $dbwKursTage->toobject->datum = $this->listObj->settings->kurstage->bis;
    $dbwKursTage->fromobject->zeit_ende = $this->listObj->settings->zeitbereich->zeitvon;
    $dbwKursTage->toobject->zeit_beg = $this->listObj->settings->zeitbereich->zeitbis;
    $this->listObj->selnr = implode(':', $dbwKursTage->getKursNrByDozentNr($this->dnr));
  }

  private function setKursTageUndZeit()
  {
    $this->listObj->settings->kurstage->von = $this->request->ktVon;
    $this->listObj->settings->kurstage->bis = $this->request->ktBis;
    $this->listObj->settings->zeitbereich->zeitvon = $this->request->ktZeitvon;
    $this->listObj->settings->zeitbereich->zeitbis = $this->request->ktZeitbis;
  }
}

class PrintDruckListenLehrplanRequest
{
  public $id;
  public $listName;
  public $dnr;
  public $ktVon;
  public $ktBis;
  public $ktZeitvon;
  public $ktZeitbis;
}