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


class DBWorkAnwVerhalten extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, belegid, ktid, textid';
    $this->_dataModulFields = array('belegid', 'ktid', 'textid', 'id');
  }

  
  public function getNewObject()
  {
    return new AnwVerhalten();
  }

  public function getTableName(): string
  {
    return 'anw_verhalten';
  }

  
  public function getAnwVerhaltenByBelegungInfoAndKursTageInfoId($pBelegungInfoAry, $pKursTageInfoAry)
  {
    $belegIds = array();
    foreach ($pBelegungInfoAry as $belegungInfo) {
      $belegIds[] = $belegungInfo->belegid;
    }

    $kursTagIds = array();
    foreach ($pKursTageInfoAry as $kursTageInfo) {
      $kursTagIds[] = $kursTageInfo->id;
    }

    return $this->getAnwVerhaltenByBelegungIdsAndKursTageIds($belegIds, $kursTagIds);
  }

  
  public function getAnwVerhaltenByBelegungIdsAndKursTageIds($pBelegIds, $pKursTageIds)
  {
    if (empty($pBelegIds) || empty($pKursTageIds)) {
      return array();
    }

    $sql = 'SELECT ' . $this->select
           . ' FROM ' . $this->getTableName()
           . ' WHERE belegid IN (' . implode(',', $pBelegIds) . ')'
           . '   AND ktid IN (' . implode(',', $pKursTageIds) . ')';

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getAnwVerhaltenByTextId($pTextId)
  {
    $cleanTextId = $this->dboperation->getInt4Sql($pTextId);

    $sql = 'SELECT ' . $this->select
           . ' FROM ' . $this->getTableName()
           . ' WHERE textid = ' . $cleanTextId;

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function insertVerhalten($pAnwVerhalten, $pDontProtocol = false, $pDmzBetrieb = false)
  {
    $pAnwVerhalten->id = $this->insertRecord($this->getTableName(), 'id', array('belegid', 'ktid', 'textid'), false);
    if (!$pDontProtocol) {
      $this->saveChanging($this->getTableName(), '', '', $pAnwVerhalten->id, 'id', 'I', $pDmzBetrieb);
    }
    $orgAnwVerhalten = $this->getOneNoFilter($pAnwVerhalten->id);
    $this->updateObject($pAnwVerhalten, $orgAnwVerhalten, $this->getTableName(), 'id', 'A', $pDontProtocol, $pDmzBetrieb);

    $this->setActionIDs();

    return $orgAnwVerhalten->id;
  }

  
  public function deleteVerhalten($pId, $pDontProtocol = false, $pDmzBetrieb = false)
  {
    $cleanId = $this->dboperation->getInt4Sql($pId);
    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM ' . $this->getTableName() . ' WHERE id=' . $cleanId);
    if (!$pDontProtocol) {
      $this->saveChanging($this->getTableName(), '', '', "'" . $cleanId . "'", '', 'D', $pDmzBetrieb);
    }
    $this->setActionIDs();
  }

  
  public function getVerhaltensObjekteFromDB($pKnr, $pBelIdAry, $pVonDat, $pBisDat, $pAuswahlInVerhDialog, $pShowOnlyTnr = false)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $defaultDateInDbFormat = $this->datetimeop->getFormatDate4Server(DEFAULTDATE);
    $belids = array();
    foreach ($pBelIdAry as $belid) {
      $belids[] = $this->dboperation->getInt4Sql($belid);
    }
    
    
    
    if (empty($belids)) {
      Logger::logg('Es sind keine oder keine korrekten Belegungs Ids vorhanden', 301);

      return array();
    }

    $bisPart = '';
    
    if (!empty($pBisDat)) {
      $bisDatInDbFormat 
      = $this->datetimeop->getFormatDateTime4Server($this->dboperation->getString4Sql($pBisDat . ' 23:59:59'));
      $bisPart = ' AND ' . $this->dboperation->dbparser->isNull('kt.datum', $defaultDateInDbFormat) . '<='
      . $bisDatInDbFormat;
    }
    
    $vonPart = '';
    if (!empty($pVonDat)) {
      $vonDatInDbFormat 
        = $this->datetimeop->getFormatDateTime4Server($this->dboperation->getString4Sql($pVonDat) . ' ' . DEFAULTTIME_WITH_SECONDS);
      $vonPart = ' AND (' . $this->dboperation->dbparser->isNull('kt.datum', $vonDatInDbFormat) . '>='
      . $vonDatInDbFormat . ' OR kt.datum = ' . $this->datetimeop->getFormatDate4Server(DEFAULTDATE) . ')';
    }

    
    $fachPart = '';
    
    if (!empty($pAuswahlInVerhDialog->fachAuswahl) and is_array($pAuswahlInVerhDialog->fachAuswahl)) {
      
      if (!in_array('alle', $pAuswahlInVerhDialog->fachAuswahl)) {
        $fachAuswahlStr = StringOperations::convertArray2InList($pAuswahlInVerhDialog->fachAuswahl);

        
        $fachPart = ' AND ' . $this->dboperation->dbparser->isNull('kf.fach', "' '") . ' in (' . $fachAuswahlStr . ')';
      }
    }

    
    $dozPart = '';
    
    if (!empty($pAuswahlInVerhDialog->dozentenAuswahl) and is_array($pAuswahlInVerhDialog->dozentenAuswahl)) {
      
      if (!in_array('0', $pAuswahlInVerhDialog->dozentenAuswahl)) {
        $dozAuswahlStr = StringOperations::convertArray2InList($pAuswahlInVerhDialog->dozentenAuswahl);

        
        $dozPart = ' AND d.nr in (' . $dozAuswahlStr . ')';
      }
    }

    
    $verhaltenPart = '';
    
    
    if (!empty($pAuswahlInVerhDialog->verhaltenAuswahl) and is_array($pAuswahlInVerhDialog->verhaltenAuswahl)) {
      
      if (!in_array('alle', $pAuswahlInVerhDialog->verhaltenAuswahl)) {
        $verhaltenAuswahlStr = StringOperations::convertArray2InList($pAuswahlInVerhDialog->verhaltenAuswahl);

        
        $verhaltenPart = ' AND avt.text in (' . $verhaltenAuswahlStr . ')';
      }
    }

    
    

    
    $select = 'b.id AS belegid, b.knr, b.tnr, t.name as tnname,t.vorname as tnvorname, kt.id as ktid, kt.datum, kt.fachid, '
              . ' kf.fach, kf.bemerkung as fachbem, '
              . ' av.id, av.textid, avt.text as verhalten, d.name as dozname, d.vorname as dozvorname ' . "\n";

    
    
    

    

    $sql = 'SELECT ' . $select
           . ' FROM belegung b INNER JOIN teilnehm t on (b.tnr = t.nr) '
           . ' INNER JOIN kurstage kt ON (b.knr=kt.knr) ' . "\n"
           . ' LEFT JOIN kurs_fach kf ON (kt.fachid=kf.id) ' . "\n"
           . ' INNER JOIN anw_verhalten av on (av.ktid = kt.ID and av.belegid = b.id) ' . "\n"
           . ' LEFT JOIN anw_verhalten_text avt on  (av.textid = avt.id) ' . "\n"
           . ' INNER JOIN doz_kurs dk ON (kt.knr = dk.kurs and kt.dnr = dk.dozent) ' . "\n"
           . ' LEFT JOIN dozenten d ON (d.nr = dk.dozent) ' . "\n"
           . ' WHERE kt.knr = \'' . $knr . '\' AND b.id IN (' . StringOperations::convertArray2InList($belids) . ')'
           . $vonPart
           . $bisPart . "\n"
           . $fachPart . "\n"
           . $dozPart . "\n"
           . $verhaltenPart . "\n"
           . ' AND ' . $this->dboperation->dbparser->isStringEmptyOrNull('dk.art') . "\n" 
           . ' ORDER BY ' . $this->dboperation->dbparser->isNull('kt.datum', $defaultDateInDbFormat) . ','
           . '(CASE ' . $this->dboperation->dbparser->isNull('t.vorname', "' '") . " WHEN ' ' THEN "
            . $this->dboperation->dbparser->isNull('t.name', "' '") . ' ELSE '
            . $this->dboperation->dbparser->concat(array($this->dboperation->dbparser->isNull('t.name', "' '"), "', '",
            $this->dboperation->dbparser->isNull('t.vorname', "' '"))) . ' END)';


    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $result = array();
    $rowcount = $this->dboperation->rowcount();

    for ($i = 0; $i < $rowcount; $i++) {
      $this->dboperation->selectrow($i);
      $tmpVerhaltenObj = $this->_getVerhaltenInfoFromResultset($i);
      
      $result[] = $tmpVerhaltenObj;
    }

    return $result;
  }

  

  
  private function _getVerhaltenInfoFromResultset($pIndex)
  {
    $this->dboperation->selectrow($pIndex);
    $tmp = new VerhaltensInfo();

    
    
    $tmp->id = $this->getDatamodulValue('ktid');
    $tmp->belid = $this->getDatamodulValue('belegid');
    $tmp->tnr = $this->getDatamodulValue('tnr');
    $tnvorname = $this->getDatamodulValue('tnvorname');
    $tmp->tnname = $tnvorname != '' ? $tnvorname . ' ' . $this->getDatamodulValue('tnname') : $this->getDatamodulValue('tnname');
    $tmp->fach = $this->getDatamodulValue('fach');
    $tmp->fachbem = $this->getDatamodulValue('fachbem');
    $tmp->datum = $this->getDatamodulValue('datum');
    $dozvorname = $this->getDatamodulValue('dozvorname');
    $tmp->dozent = $dozvorname != '' ? $dozvorname . ' ' . $this->getDatamodulValue('dozname') : $this->getDatamodulValue('dozname');
    $tmp->verhalten = $this->getDatamodulValue('verhalten');

    return $tmp;
  }

  

  
  public function getVerhaltenInfoByKurs($pKnr)
  {
    $knr = $this->dboperation->getString4Sql($pKnr);
    $sql = 'SELECT avt.text AS verhalten FROM kurstage kt' . "\n"
            . 'INNER JOIN anw_verhalten av ON (av.ktid = kt.id)' . "\n"
            . 'LEFT JOIN anw_verhalten_text avt ON (av.textid = avt.id)' . "\n"
            . 'WHERE kt.knr = \'' . $knr . '\' ORDER BY av.id';

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    $verhalten = array_unique($this->getAllRowsSingleColumn('verhalten'));

    return array_map(static function($verhalten) {
      $result = new \VerhaltensInfo();
      $result->verhalten = $verhalten;

      return $result;
    }, $verhalten);
  }
}