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

namespace Kufer\KuferTools\Dozent\KursUebersicht\Detailsreiter\Teilnehmer\TerminKalenderTn;

class TerminKalenderTnUseCase extends \Kufer\KuferTools\Dozent\KursUebersicht\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new TerminKalenderTnResponse());
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ;
  }

  public static function getToolSectionId()
  {
    return 3;
  }

  public function hasAccess($pToolSectionId = null)
  {
    
    
    
    if (!parent::hasAccess()) {
      return false;
    }
    if (!$this->isRechtAllowed('3.0.5')) {
      return false;
    }

    return true;
  }

  public function action()
  {
    $terminKalenderTn = new \TerminKalenderTN($this->request->knr, $this->getToolId(), $this->getTerminKalenderObj());
    $terminKalenderTn->loadSettings();

    return $this->getResponse($terminKalenderTn);
  }

  protected function getResponse($pTerminKalenderTn)
  {
    $this->response->terminKalenderTn = $pTerminKalenderTn;

    return $this->response;
  }

  
  private function getTerminKalenderObj()
  {
    $dbwBelegungen = new \DBWorkBelegung(1, true);
    $dbwBelegungen->newSearch();
    $dbwBelegungen->fromobject->status = $this->toolSettings->tnstatus;
    $belegungen = $dbwBelegungen->getBelegungenbyKursNR($this->request->knr);

    
    $belIdsFromRequest = explode(':', $this->request->terminKalenderObj->belIds);
    $result = array();
    foreach ($belegungen as $belegung) {
      if (in_array($belegung->id, $belIdsFromRequest)) {
        $result[] = $belegung->id;
      }
    }

    
    $this->request->terminKalenderObj->belIds = implode(':', $result);

    return $this->request->terminKalenderObj;
  }
}

class TerminKalenderTnRequest
{
  public $knr;
  public $terminKalenderObj;
}

class TerminKalenderTnResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $terminKalenderTn;
}