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




class DBWorkQualifikationenAD extends DBWork
{
  
  public $considerAnzeigeWeb;

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = ' qa.id, qa.nr, qa.typ, qa.quali, qa.qualiid, qa.text, '
        . $this->clob('qa.bemerkung') . ', qa.gueltig, qa.art, qa.gemachtam, qa.anbieter ';
    $this->considerAnzeigeWeb = false;
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->nr = $this->getDatamodulValue('nr');
    $tmp->typ = $this->getDatamodulValue('typ');
    $tmp->quali = $this->getDatamodulValue('quali');
    $tmp->text = $this->getDatamodulValue('text');
    $tmp->bemerkung = $this->getDatamodulValue('bemerkung');
    $tmp->qualiid = $this->getDatamodulValue('qualiid');
    $tmp->gueltig = $this->getDatamodulValue('gueltig');
    $tmp->art = $this->getDatamodulValue('art');
    $tmp->gemachtam = $this->getDatamodulValue('gemachtam');
    $tmp->anbieter = $this->getDatamodulValue('anbieter');
    if ($tmp->qualiid > 0 && ($tmp->text == '' || $tmp->quali == 0)) {
      $dbwq = new DBWorkQualifikationen(1, true);
      $qual = $dbwq->getQualifikation($tmp->qualiid);
      if ($tmp->text == '') {
        $tmp->text = $qual->text;
      }
      if ($tmp->quali == 0) {
        $tmp->quali = $qual->quali;
      }
      $tmp->quali_nr = $qual->quali;
    }

    return $tmp;
  }








  
  public function getChildQualifikationenAD($pParentId)
  {
    $parentId = $this->dboperation->getInt4Sql($pParentId);
    $this->dboperation->clear();
    $this->dboperation->query('SELECT ' . $this->select
              . " FROM quali_ad qa WHERE typ = 'Q' AND nr = " . $parentId);

    return $this->getAllRows();
  }

  
  public function getQualifikationen4Dozent($pDnr)
  {
    $dnr = $this->dboperation->getInt4Sql($pDnr);
    $sql = 'SELECT ' . $this->select
              . " FROM quali_ad qa WHERE typ='D' AND nr=" . $dnr;
    if ($this->considerAnzeigeWeb) {
      $sql .= ' AND ' . $this->dboperation->dbparser->isNull('qa.anzeigeweb', '\'F\'') . ' = \'W\' order by sort';
    }
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getQualifikationenByBASysUserID($pBASysUserId)
  {
    $userid = $this->dboperation->getInt4Sql($pBASysUserId);
    $queryBuilder = $this->getSelectQueryBuilder();
    $queryBuilder->select('benutzer.name, quali.text')
            ->from('benutzer')
            ->innerJoin('quali_ad', 'quali_ad.nr = benutzer.id')
            ->innerJoin('quali', 'quali.id = quali_ad.qualiid')
            ->where('benutzer.id = :id:')
            ->paramInt('id', $userid);
    $this->dboperation->clear();
    $this->dboperation->query($queryBuilder->getSQL());

    return $this->getAllRows();
  }

  public function getAllQuery(): SelectQueryBuilder
  {
    $query = $this->getSelectQueryBuilder();

    return $query->select($this->select)
                   ->from($this->getTableName(), 'qa');
  }

  public function getQualifikationAdByIdQuery($pID): SelectQueryBuilder
  {
    $query = $this->getAllQuery();

    return $query->and('qa.id = :id:')
                   ->paramInt('id', $pID);
  }

  public function getQualifikationAdById($pID): \Qualifikation_Ad
  {
    $query = $this->getQualifikationAdByIdQuery($pID);

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

    return $this->getFirstRow();
  }
}