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


class DBWorkKursInfo extends DBWorkStrict
{
  function __construct($pDBTyp = 1, $pNewInstance = false)
  { 
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'k.nr, k.teil_min, k.teil_max, k.beginn_dat, k.ende_dat, k.anm_ende, k.abschluss, k.ausfall, k.webflags, k2.tnki_min, k2.tnki_max, k.kinder_min, k.kinder_max, k.kinder_ev, k.typ';
  }
  
  
  function getNewObject()
  {
    return new classes\datamodul\KursInfo();
  }
  
  
  function getActualRow()
  {
    $result = $this->getNewObject();
    $result->id                   = $this->getDatamodulValue('nr');
    $result->teil_min             = $this->getDatamodulValue('teil_min');
    $result->teil_max             = $this->getDatamodulValue('teil_max');
    $result->beginn_dat           = $this->getDatamodulValue('beginn_dat');
    $result->ende_dat             = $this->getDatamodulValue('ende_dat');
    $result->anm_ende             = $this->getDatamodulValue('anm_ende');
    $result->abschluss            = $this->getDatamodulValue('abschluss');
    $result->ausfall              = $this->getDatamodulValue('ausfall');
    $result->webflags             = $this->getDatamodulValue('webflags');
    $result->tnki_min             = $this->getDatamodulValue('tnki_min');
    $result->tnki_max             = $this->getDatamodulValue('tnki_max');
    $result->kinder_min           = $this->getDatamodulValue('kinder_min');
    $result->kinder_max           = $this->getDatamodulValue('kinder_max');
    $result->teinzel              = $this->getDatamodulValue('teinzel');
    $result->kinder_ev            = $this->getDatamodulValue('kinder_ev');
    $result->typ                  = $this->getDatamodulValue('typ');
    
    
    $dbwBelegung = DBWorkBelegung::getDefaultInstance($this->dbtyp, true, true); 
    
    if (System::isKuferSql5()) {
      $result->kinderanzahl       = $dbwBelegung->getKinderAnzahlByKnr($result->id);
    }
    else {
      $dbwKinder = DBWorkKinder::getDefaultInstance($this->dbtyp, true);
      $result->kinderanzahl       = $dbwKinder->getKinderAnzahlByKnr($result->id);
    }
    $result->kinderanzahl         += $result->kinder_ev;
    
    $result->anmeldungen          = $dbwBelegung->getBelegungCountByKnr($result->id);
    $result->anmeldungen          += $result->teinzel;
    
    $result->teil_akt             = $result->anmeldungen;
    $result->teil_frei            = $result->getTeilMax() - $result->teil_akt;
    
    
    $result->anmeldungeng = BelegungOperations::getAnmStatusForAngemeldeteTn($result, ToolSettingsAPI::getToolSettings(0), false);
    $result->anmeldungeng_web = BelegungOperations::getAnmStatusForAngemeldeteTn($result, ToolSettingsAPI::getToolSettings(4));
    
    return $result;
  }
  
  
  function getKursInfosById($pKnrAry)
  {
    if (is_string($pKnrAry)) {
      $pKnrAry = array($pKnrAry);
    }
    if (count($pKnrAry) == 0) {
      return array();
    }
    $knrString = '(';
    foreach ($pKnrAry as $knr) {
      $knrString .= '\''.$knr.'\',';
    }
    
    $knrString = trim(StringOperations::substr($knrString,0,-1));
    
    $knrString .= ')';  
    $sql =  'SELECT '. $this->select .
            ' FROM kurse k LEFT JOIN kurse2 k2 ON k.nr = k2.nr' .
            ' WHERE k.nr IN ' . $knrString;
    $sql .= ' ORDER BY k.nr ASC';
    
    $this->dboperation->query($sql);
    return $this->getAllRows();
  }
}