<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Dozent\KursUebersicht\KursDetails;

class DozKursDetailsUseCase extends \Kufer\Web\KuferTools\Basis\Kursdetails\KursDetailsUseCase
{
  public function __construct($request)
  {
    parent::__construct($request);
  }

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

  public static function getToolSectionId()
  {
    return 334;
  }

  protected function getResponse($kurs, $user, $datumLetzterKurstag)
  {
    $this->response->referer = urlencode($this->getAktuelleURLRelative());
    $this->response->refererLabel = $this->getButtonLabel($kurs->nr_anzeige);
    parent::getResponse($kurs, $user, $datumLetzterKurstag);

    return $this->response;
  }

  private function getButtonLabel($pKursNr)
  {
    return \StringOperations::htmlentities('Zurück zum Kurs ' . $pKursNr);
  }

  private function getAktuelleURLRelative()
  {
    $url = \URL::loadCurrent();

    return $url->getUrlRelative();
  }

  protected function getButtons()
  {
    $buttonsObj = new \Kufer\Web\KuferTools\Basis\Kursdetails\Detailsreiter\DozReiterButtons($this, $this->request, $this->response);

    return $buttonsObj->getButtons();
  }

  
  protected function getDatumVomLetztenKurstag()
  {
    $toolDozAnwSettings = \ToolSettingsAPI::getToolSettings(\ToolsAPI::TOOL_ANWESENHEIT_DOZ);
    $dbwKurseTage = new \DBWorkKurseTageWT(1, true);
    $dbwKurseTage->newSearch();
    $dbwKurseTage->toobject->datum = \DateTimeOperations::getDateNow();
    $dbwKurseTage->toobject->nodoztool = true;
    $onlyCurrentLoggedInDozent = $toolDozAnwSettings->showKurstage !== 'alle4kurs';

    $kursTage = $dbwKurseTage->getKurstagebyKursNR($this->request->knr, -1, $onlyCurrentLoggedInDozent);
    $result = \DateTimeOperations::getDateNow();
    if (count($kursTage) > 0) {
      $result = $kursTage[count($kursTage) - 1]->datum;
    }

    return $result;
  }

  protected function getBackLink()
  {
    return 'index.php?tool_id=1&action=kursuebersicht';
  }

  protected function getBackButtonLabel()
  {
    return 'Zurück zur Übersicht';
  }
}