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


class VerhaltensListe extends DruckListe
{
  
  private $_belIds;

  
  private $_showOnlyTnr;

  
  private $_auswahlInVerhDialog;


  
  private $_gesamtbreite;
  
  private $_kurs;
  
  private $_tableheaderSize;
  
  private $_tablebodySize;
  
  private $_headerRightBeg;
  
  private $_verhaltenObjAry;

  
  private $_title1 = ' ';

  
  public function __construct($pSelnr, $pToolId, $pVerhaltensListeObj = null)
  {
    
    
    
    
    
    
    $this->settings = new stdClass();
    $this->setUseSettings(false);
    $this->selnr = $pSelnr;
    $this->toolId = $pToolId;

    

    
    $this->dtop = new DateTimeOperations();
    $this->mop = new MathOperations();
    $this->htmlop = new HTMLOperations();
    $this->sprachSettings = SettingsFactory::getSettings('SprachSettingsObject');
    $this->kurstage = new webtools\drucklisten\Kurstage();

    
    
    
    $this->loadDialogSettingsFromPOST($pVerhaltensListeObj);
  }

  
  
  
  public function getListID()
  {
    return 27;
  }

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

  
  public function loadSettings()
  {
    
    $this->loadFactoryDefaults();
  }

  
  public function loadFactoryDefaults()
  {
    
    
    
    if (!isset($this->settings->druckbereich)) {
      $this->settings->druckbereich = new webtools\drucklisten\Druckbereich();
    }
    $this->settings->druckbereich->hochformat = 1; 
    
    $this->setTitle1();
  }

  public function setTitle1()
  {
    
    $this->_title1 = 'Verhaltenseinträge';
  }

  
  public function _prepare($pKnr = '')
  {
    DruckSheet::_prepare($pKnr);

    

    
    $this->setVerhaltenObjAry();

    $dbwKurse = new DBWorkKurse(1, true);
    $this->_kurs = $dbwKurse->getKurs($this->selnr);

    
    
    $this->spaltenbeginnary = array(8,   12, 34.5,   61, 76.5);
    $this->spaltenbreiteary = array(53, 22.5, 26.5, 15.5, 16.5);
    $endindex = count($this->spaltenbeginnary) - 1; 
    $this->_gesamtbreite = $this->spaltenbeginnary[$endindex] + $this->spaltenbreiteary[$endindex] - $this->spaltenbeginnary[0]; 
    $this->_tableheaderSize = 12.5;
    $this->_tablebodySize = 10.5;
    $this->_headerRightBeg = 76;
  }

  
  public function getDruckbereichEnabled($optionName)
  {
    switch ($optionName) {
      case 'hochformat':
        return true;
    }

    return false;
  }

  
  protected function loadDialogSettingsFromPOST($pVerhaltensListeObj = null)
  {
    if (isset($pVerhaltensListeObj)) {
      $verhaltensListeObj = $pVerhaltensListeObj;
    }
    
    
    
    elseif (\PostOperations::isDefined('verhaltenslisteobj')) {
      
      $verhaltensListeObj = JsonOperations::getFromStringBase64(\PostOperations::getVarValue('verhaltenslisteobj'));
      
      SessionOperations::setVarValueWTIntern('verhaltenslisteobj_' . $this->toolId, $verhaltensListeObj);
    } else {  
      $verhaltensListeObj = SessionOperations::getVarValueWTIntern('verhaltenslisteobj_' . $this->toolId);
    }

    
    $this->kurstage->von = trim($verhaltensListeObj->ktVon);
    $this->kurstage->bis = trim($verhaltensListeObj->ktBis);

    
    $this->_showOnlyTnr = $verhaltensListeObj->showOnlyTnr == 1;
    
    
    $this->_belIds = explode(':', $verhaltensListeObj->belIds);

    
    


    
    
    $this->_auswahlInVerhDialog = new stdClass();
    
    $this->_auswahlInVerhDialog->fachAuswahl = $verhaltensListeObj->fachAuswahl;
    $this->_auswahlInVerhDialog->dozentenAuswahl = $verhaltensListeObj->dozentenAuswahl;
    $this->_auswahlInVerhDialog->verhaltenAuswahl = $verhaltensListeObj->verhaltenAuswahl;
  }

  
  public function setVerhaltenObjAry()
  {
    $this->_verhaltenObjAry = array();
    $vonDat = '';
    $bisDat = '';
    
    
    
    if (isset($this->kurstage->von, $this->kurstage->bis)) {
      $vonDat = $this->kurstage->von;
      $bisDat = $this->kurstage->bis;
    }
    
    $verhaltApi = new VerhaltenAPI(SessionOperations::getUserIdentifier());
    $this->_verhaltenObjAry = $verhaltApi->getVerhaltenObjektAry(
      $this->selnr,
      $this->_belIds,
      $vonDat,
      $bisDat,
      $this->_auswahlInVerhDialog,
      $this->_showOnlyTnr
    );
  }

  
  protected function _getZeitraumAnzeige()
  {
    if (empty($this->kurstage->von) && empty($this->kurstage->bis)) {
      return '';
    }
    if (empty($this->kurstage->von)) {
      $zeitraum = ' bis ' . $this->kurstage->bis;
    } elseif (empty($this->kurstage->bis)) {
      $zeitraum = ' ab ' . $this->kurstage->von;
    } else {
      $zeitraum = ' vom: ' . $this->kurstage->von . ' bis ' . $this->kurstage->bis;
    }

    return $zeitraum;
  }

  
  public function _createHeader($pKurs = '')
  {
    $this->_createHeaderTop('Stand: ');

    
    $this->pdf->CellAtX('7%', $this->_title1 . $this->_getZeitraumAnzeige(), 'B', '', 11.3);
    $this->pdf->CellAtX($this->_headerRightBeg . '%', 'Seite:         ' . $this->pdf->PageNo());
    $maxwidth = $this->getWidth4Title();
    if ($this->pdf->GetStringWidth($this->_title1) > $maxwidth) {
      $this->pdf->ln(10);
    }
    $this->pdf->ln(11);

    
    $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', '', '', '', '', $this->_gesamtbreite . '%', 'B', 2);
    
    $this->pdf->ln(4);
    $this->pdf->CellAtX($this->spaltenbeginnary[0] . '%', 'Kurs:  ' . $this->_kurs->kurzbez, 'B', '', $this->_tableheaderSize, $this->spaltenbreiteary[0] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[3] . '%', 'Kursnummer: ', 'B', '', $this->_tableheaderSize, $this->spaltenbreiteary[3] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary[4] . '%', $this->_kurs->id, 'B', '', $this->_tableheaderSize, $this->spaltenbreiteary[4] . '%', 0, $this->height);
    $this->pdf->ln($this->height + 2);
  }

  
  protected function _createHeaderTop($pDatumsText)
  {
    $allgsettings = AllgSettingsAPI::getAllgSettings();
    
    $this->pdf->ln();
    
    $this->pdf->CellAtX('7%', StringOperations::html_entity_decode($allgsettings->vhsname), 'B', '', 14.3);
    $this->pdf->CellAtX($this->_headerRightBeg . '%', $pDatumsText . $this->dtop->getDateNow());
    $this->pdf->ln($this->height);
  }

  
  protected function _printDruckListe($pKurs = '')
  {
    
    if (count($this->_verhaltenObjAry) > 0) {
      
      foreach ($this->_verhaltenObjAry as $verhaltensObj) {
        
        
        $teilnehmer = 'Teilnehmer:';
        $teilnehmerwert = $verhaltensObj->tnname;
        if ($this->_showOnlyTnr) {
          $teilnehmer = 'Teilnehmernr.:';
          $teilnehmerwert = $verhaltensObj->tnr;
        }
        $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $teilnehmer, 'B', '', $this->_tablebodySize, $this->spaltenbreiteary[1] . '%', 0, $this->height);
        $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $teilnehmerwert, '', '', $this->_tablebodySize, $this->spaltenbreiteary[2] . '%', 0, $this->height);
        $this->pdf->ln($this->height + 0.5);
        
        $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', 'Fach:', 'B', '', $this->_tablebodySize, $this->spaltenbreiteary[1] . '%', 0, $this->height);
        $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $verhaltensObj->fach, '', '', $this->_tablebodySize, $this->spaltenbreiteary[2] . '%', 0, $this->height);
        $this->pdf->ln($this->height + 0.5);
        
        $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', 'Bemerkung:', 'B', '', $this->_tablebodySize, $this->spaltenbreiteary[1] . '%', 0, $this->height);
        $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $verhaltensObj->fachbem, '', '', $this->_tablebodySize, $this->spaltenbreiteary[2] . '%', 0, $this->height);
        $this->pdf->ln($this->height + 0.5);
        
        $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', 'Datum des Kurstages:', 'B', '', $this->_tablebodySize, $this->spaltenbreiteary[1] . '%', 0, $this->height);
        $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $verhaltensObj->datum, '', '', $this->_tablebodySize, $this->spaltenbreiteary[2] . '%', 0, $this->height);
        $this->pdf->ln($this->height + 0.5);
        
        $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', 'Referent:', 'B', '', $this->_tablebodySize, $this->spaltenbreiteary[1] . '%', 0, $this->height);
        $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $verhaltensObj->dozent, '', '', $this->_tablebodySize, $this->spaltenbreiteary[2] . '%', 0, $this->height);
        $this->pdf->ln($this->height + 0.5);
        
        $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', 'Verhaltenseintrag:', 'B', '', $this->_tablebodySize, $this->spaltenbreiteary[1] . '%', 0, $this->height);
        $this->pdf->CellAtX($this->spaltenbeginnary[2] . '%', $verhaltensObj->verhalten, '', '', $this->_tablebodySize, $this->spaltenbreiteary[2] . '%', 0, $this->height);
        $this->pdf->ln(2 * $this->height + 1);
      }
      $this->pdf->ln(3);
    } else {
      $noEntryText = 'Für diese Einstellungen wurden keine Verhaltenseinträge gefunden.';
      
      $this->pdf->CellAtX($this->spaltenbeginnary[1] . '%', $noEntryText, '', '', '', $this->_gesamtbreite . '%', 0, $this->height);
    }
  }
}