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

namespace Kufer\KuferTools\Dozent\Lehrplan\FilterKurstageUebersicht;

class FilterKurstageUebersichtUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  private $dbwKurstage;

  public function __construct($request)
  {
    parent::__construct($request, new FilterKurstageUebersichtResponse());
    $this->dbwKurstage = new \DBWorkKurseTage();
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_LEHRPLAN_DOZ;
  }

  public static function getToolSectionId()
  {
    return 23;
  }

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

    return true;
  }

  public function action()
  {
    $this->setzeFilterFuerKurstage();

    return $this->getResponse();
  }

  protected function getResponse()
  {
    $this->response->kurstagFilterVon = $this->dbwKurstage->fromobject;
    $this->response->kurstagFilterBis = $this->dbwKurstage->toobject;

    return $this->response;
  }

  
  private function setzeFilterFuerKurstage()
  {
    $dtop = new \DateTimeOperations();

    $this->dbwKurstage->newSearch();

    $this->handleKurstagFilter();

    if ($this->dbwKurstage->fromobject->datum == '') {
      $this->dbwKurstage->fromobject->datum = $dtop->getDateByParam(6); 
    }

    if ($this->dbwKurstage->toobject->datum == '') {
      $this->dbwKurstage->toobject->datum = $dtop->getDateByParam(7); 
    }


    
    
    if ($this->dbwKurstage->fromobject->zeit_ende == '') {
      $this->dbwKurstage->fromobject->zeit_ende = $dtop->getFloatasTimeString(STARTZEIT, true);
    }

    if ($this->dbwKurstage->toobject->zeit_beg == '') {
      $this->dbwKurstage->toobject->zeit_beg = $dtop->getFloatasTimeString(ENDEZEIT, true);
    }

    $this->dbwKurstage->fromobject->kurs->semester = $this->toolSettings->semlist;
    $this->dbwKurstage->toobject->kurs->semester = '';

    if (isset($this->toolSettings->keineAusgefallenenKurse) && $this->toolSettings->keineAusgefallenenKurse) {
      $this->dbwKurstage->dbwkurse->justNoAusfall = true;
    }
    if (isset($this->toolSettings->keineAbgeschlossenenKurse) && $this->toolSettings->keineAbgeschlossenenKurse) {
      $this->dbwKurstage->dbwkurse->justNoAbschluss = true;
    }
    if (isset($this->toolSettings->keineHauptkurse) && $this->toolSettings->keineHauptkurse) {
      $this->dbwKurstage->dbwkurse->keineHauptkurse = true;
    }

    if (($this->toolSettings->nurMitKursstatus ?? '') !== '') {
      $kursstatusliste = explode(',', $this->toolSettings->nurMitKursstatus);
      $this->dbwKurstage->fromobject->kurs->realkursstatus = $kursstatusliste;
    }

    $this->dbwKurstage->orderby = $this->toolSettings->sortkt;
    $this->dbwKurstage->orderbyasc = $this->toolSettings->sortktasc;
    $this->dbwKurstage->fromobject->ausfall = $this->toolSettings->ktstatus;
    $this->dbwKurstage->toobject->ausfall = $this->toolSettings->ktstatus;
    $this->dbwKurstage->setOrderAktuellesSemester($this->toolSettings);
  }

  protected function handleKurstagFilter()
  {
    $searchAPI = new \LehrplanSucheAPI();
    $this->dbwKurstage->fromobject = $searchAPI->getFromObjectBySession($this->dbwKurstage->fromobject, 'tool' . $this->getToolId());
    $this->dbwKurstage->toobject = $searchAPI->getToObjectBySession($this->dbwKurstage->toobject, 'tool' . $this->getToolId());
  }
}

class FilterKurstageUebersichtRequest
{
}

class FilterKurstageUebersichtResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurstagFilterVon;
  public $kurstagFilterBis;
}