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

class AnwesenheitslisteMitAntwortboxNRW extends Anwesenheitsliste
{
  public const FUSSZEILE_ADDITIONAL_TEXT_GROESSE = 9;

  public function getListID()
  {
    return 7;
  }

  public function getListTyp()
  {
    return '~';
  }

  public function showFusszeilen()
  {
    return false;
  }

  public function getSpaltenanordnungEnabled($optionName)
  {
    switch ($optionName) {
      case 'option6':
      case 'option7':
        return false;
    }

    return parent::getSpaltenanordnungEnabled($optionName);
  }

  public function _prepare($pKnr = '')
  {
    $dbwkurstage = new \DBWorkKurseTage(1, true);

    $dbwkurstage->newSearch();
    $dbwkurstage->fromobject->datum = $this->settings->kurstage->von;
    $dbwkurstage->toobject->datum = $this->settings->kurstage->bis;

    if ($this->toolId == \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ) {
      $dbwkurstage->toobject->nodoztool = true;
    }

    $allKtStatus = \ConstantOperations::getKurstagStatusKuerzel();
    $allowedKtStatus = array_diff($allKtStatus, array('A'));

    $tool0Settings = \ToolSettingsAPI::getToolSettings(0);
    $wtUser = \SessionOperations::getCurrentWebToolsUser();
    if ($tool0Settings->kurstageNurVonEingeloggtemDozent && $wtUser->dnr > 0) {
      $dbwkurstage->fromobject->dnr = $wtUser->dnr;
      $dbwkurstage->toobject->dnr = $wtUser->dnr;
    }

    
    $this->kursetage = $dbwkurstage->getKurstagebyKursNROnlyDifferent($this->knr, 'X', $allowedKtStatus, array('A'));

    $maxKurstageAufSeite = $this->getMaxKurstageAufSeite();
    $this->uebrigeKurstage = array_slice($this->kursetage, $maxKurstageAufSeite);
    $this->kursetage = array_slice($this->kursetage, 0, $maxKurstageAufSeite);
  }

  protected function printExtraSpalten()
  {
    if (count($this->kursetage) <= $this->_getBemerkungVisibleThreshold()) {
      $height = $this->_getTNCellHeight();
      for ($i = 0; $i < 2; $i++) {
        $this->pdf->CellExt($this->_getKtWidth(), '', 1, $height, '', '', '');
      }
    }
  }

  protected function printExtraSpaltenHeader()
  {
    $height = 5;
    $width = 2 * $this->_getKtWidth();
    $x = $this->pdf->GetX();
    $headerFontSize = $this->_getHeaderFontSize();

    $this->printExtraSpaltenHeaderUmrandung($x, $width, $height);
    $this->printExtraSpaltenHeaderBeschriftung($x, $width, $height, $headerFontSize);
    $this->printExtraSpaltenHeaderBeschriftungJaNein($x, $width, $height, $headerFontSize);
  }

  private function printExtraSpaltenHeaderUmrandung($pX, $pWidth, $pHeight)
  {
    $heightUmrandung = 2 * $pHeight;
    $y = $this->pdf->GetY();

    $this->pdf->SetY($y - 2 * $pHeight);
    $this->pdf->CellAtX($pX, '', '', '', 0, $pWidth, 1, $heightUmrandung);
    $this->pdf->SetY($y);
  }

  private function printExtraSpaltenHeaderBeschriftung($pX, $pWidth, $pHeight, $pHeaderFontSize)
  {
    $width = $pWidth + 1;
    $y = $this->pdf->GetY();

    $this->pdf->SetY($y - 2 * $pHeight);
    $this->pdf->CellAtX($pX, 'Ich wohne', 'B', '', $pHeaderFontSize, $width, 0, $pHeight);
    $this->pdf->SetY($y - $pHeight - 2);
    $this->pdf->CellAtX($pX, 'oder arbeite', 'B', '', $pHeaderFontSize, $width, 0, $pHeight);
    $this->pdf->SetY($y - 4);
    $this->pdf->CellAtX($pX, 'in NRW', 'B', '', $pHeaderFontSize, $width, 0, $pHeight);
    $this->pdf->SetY($y);
  }

  private function printExtraSpaltenHeaderBeschriftungJaNein($pX, $pWidth, $pHeight, $pHeaderFontSize)
  {
    $widthProSpalte = $pWidth / 2;
    $this->pdf->SetX($pX);

    $this->pdf->CellExt($widthProSpalte, 'ja', 1, $pHeight, 'B', $pHeaderFontSize);
    $this->pdf->CellExt($widthProSpalte, 'nein', 1, $pHeight, 'B', $pHeaderFontSize);
  }

  protected function getMaxKurstageAufSeite()
  {
    return parent::getMaxKurstageAufSeite() - 2; 
  }

  
  public function _getBemerkungVisibleThreshold()
  {
    return parent::_getBemerkungVisibleThreshold() - 2; 
  }

  public function getListBezeichnungForHTML()
  {
    return 'AnwesenheitslisteMitAntwortboxNRW';
  }

  
  public function __createFusszeilen()
  {
    $this->pdf->SetY(-($this->footerHeight + $this->pdf->getBMargin()));
    $zeilenHoehe = self::FUSSZEILE_ADDITIONAL_TEXT_GROESSE;

    $this->printDatumUnterschriftFelder($zeilenHoehe);
    $this->printUnterschriftText($zeilenHoehe);
  }

  public function __computeFooterHeight()
  {
    $height = 10;

    
    for ($i = 0; $i < 6; $i++) {
      $height += 3 + self::FUSSZEILE_ADDITIONAL_TEXT_GROESSE / 10;
    }

    $this->footerHeight = $height;
  }

  private function printDatumUnterschriftFelder($pZeilenHoehe)
  {
    $height = $this->pdf->_lnHeight;

    $x = 100;
    if ($this->_isHochformat()) {
      $x = 40;
    }

    $this->pdf->Ln(4);
    $this->pdf->SetX($x);
    $this->pdf->CellExt(0, '_________________________                        ________________________________________________*', 0, 0, '', $pZeilenHoehe);
    $this->pdf->Ln($height - 1);
    $this->pdf->SetX($x);
    $this->pdf->CellExt(0, '                  (Datum)                                                                                (Unterschrift)', 0, 0, '', $pZeilenHoehe);
    $this->pdf->Ln($height + 2);
  }

  private function printUnterschriftText($pZeilenHoehe)
  {
    $text1 = '*mit der Unterschrift bestätigt der Dozent die Richtigkeit der Angaben und dass alle Teilnehmenden zum Zeitpunkt des Kurses ';
    $text2 = 'mindestens 16 Jahre alt gewesen sind.';

    if ($this->_isHochformat()) {
      $this->pdf->CellExt(0, $text1, 0, 0, '', $pZeilenHoehe);
      $this->pdf->Ln();
      $this->pdf->CellExt(0, $text2, 0, 0, '', $pZeilenHoehe);
    } else {
      $this->pdf->CellExt(0, $text1 . $text2, 0, 0, '', $pZeilenHoehe);
    }
    $this->pdf->Ln();
  }
}