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


class DBWorkFeedBackGedruckt extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);

    
    
    $this->select = 'id, nr, typ, knr, bogenid, druckdatum, bearbeiter, bearbdatum, erfasstvon, erfasstam, dnr';
    $this->_dataModulFields = array('id', 'nr', 'typ', 'knr', 'bogenid', 'druckdatum', 'bearbeiter', 'bearbdatum', 'erfasstvon', 'erfasstam', 'dnr');
  }

  
  public function getNewObject()
  {
    return new \FeedBackGedruckt();
  }

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

  
  public function insertFeedBackGedruckt($pFeedBackGedruckt)
  {
    $pFeedBackGedruckt->id = $this->insertRecord($this->getTableName(), 'id', 'nr', true);

    $originalEintrag = new FeedBackGedruckt();
    $originalEintrag->id = $pFeedBackGedruckt->id;

    $this->updateObject($pFeedBackGedruckt, $originalEintrag, $this->getTableName(), 'id', 'U', true);

    return $pFeedBackGedruckt;
  }

  
  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
        ->select($this->select)
        ->from($this->getTableName())
        ->paramRaw('table', $this->getTableName());
  }

  
  public function getAlldataByKnrAndTypAndTnrQuery($pKnrs, $pTyp, $pTnrs)
  {
    $builder = $this->getAlldataByKnrAndTypQuery($pKnrs, $pTyp);
    if (\DBWorkBASysSettings::isInlinguaMuenchen()) {
      $builder->and('(nr IN (:tnrs:) OR nr = -1)');
    } else {
      $builder->and('nr IN (:tnrs:)');
    }

    if ($pTnrs instanceof \SelectQueryBuilder) {
      $builder = $builder->paramSelectQuery('tnrs', $pTnrs);
    } else {
      $builder = $builder->paramIntArray('tnrs', $pTnrs);
    }

    return $builder;
  }

  
  public function getAlldataByKnrAndTypAndTnr($pKnrs, $pTyp, $pTnrs)
  {
    $builder = $this->getAlldataByKnrAndTypAndTnrQuery($pKnrs, $pTyp, $pTnrs);
    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function getAlldataByKnrAndTypQuery($pKnrs, $pTyp)
  {
    return $this->getAllQuery()
        ->and('knr IN (:knrs:)')
        ->and('typ = :typ:')
        ->paramStringArray('knrs', $pKnrs)
        ->paramString('typ', $pTyp);
  }

  
  public function getAlldataByKnrAndTyp($pKnrs, $pTyp)
  {
    $sqlstr = $this->getAlldataByKnrAndTypQuery($pKnrs, $pTyp)->getSQL();

    
    $this->dboperation->clear();

    
    $this->dboperation->query($sqlstr);

    return $this->getAllRows();
  }

  
  public function getAlldataByKnrAndTypAndFuerDozent($pKnrs, $pTyp, \Kufer\Web\Classes\Identifier\Identifier $dozId)
  {
    assert(!is_null($dozId->getId()));

    $builder = $this->getAlldataByKnrAndTypQuery($pKnrs, $pTyp)
      ->andExpr(static function(SelectQueryBuilder $builder) {
        return $builder
          ->orExpr(static function(SelectQueryBuilder $builder) {
            
            return $builder
              ->and(':ifnull(dnr, -1): != -1')
              ->and('dnr = :dozid:');
          })
          ->orExpr(static function(SelectQueryBuilder $builder) {
            
            
            return $builder
              ->and(':ifnull(dnr, -1): = -1')
              ->and('knr IN (SELECT kurs FROM doz_kurs GROUP BY kurs HAVING COUNT(*) = 1)');
          });
      })
      ->paramInt('dozid', $dozId->getId());

    
    $this->dboperation->clear();

    
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  public function getAlldataByKnrAndTypAndMaybeFuerDozent($pKnrs, $pTyp, \Kufer\Web\Classes\Identifier\Identifier $dozId)
  {
    assert(!is_null($dozId->getId()));

    $sqlstr = $this->getAlldataByKnrAndTypQuery($pKnrs, $pTyp)
      ->andExpr(static function(\SelectQueryBuilder $builder) use ($dozId): void {
        $builder->or('dnr = -1')
          ->or('dnr = :dozid:')
          ->paramInt('dozid', $dozId->getId());
      })
      ->getSQL();

    
    $this->dboperation->clear();

    
    $this->dboperation->query($sqlstr);

    return $this->getAllRows();
  }

  
  public function getCountFeedBackGedruckt($pKnr, $pTyp, $pUserNr)
  {
    $builder = $this->getAlldataByKnrAndTypQuery(array($pKnr), $pTyp)
      ->select('COUNT(*) AS anzahl')
      ->and('nr = :userId:')
      ->paramInt('userId', $pUserNr);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());
    $this->dboperation->selectrow(0);

    return $this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }
}