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

namespace Kufer\Web\KuferTools\Teilnehmer\KursUebersicht\Detailsreiter\InfoAnDoz;

class InfoAnDozUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  private $belegungshelper;

  public function __construct($request)
  {
    parent::__construct($request, new InfoAnDozResponse());
    $user = $this->getUser();
    $this->belegungshelper = new \Kufer\Web\KuferTools\Utils\DatamodulHelper\Belegungen\TeilnehmerBelegungen($this->toolSettings, $user->tnr);
  }

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

  public static function getToolSectionId()
  {
    return 335;
  }

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

    return true;
  }

  protected function getAllowedIds()
  {
    return $this->belegungshelper->getIdsOhneFilter();
  }

  protected function getRequestedId()
  {
    return $this->request->belegId;
  }

  public function action()
  {
    $belegung = $this->belegungshelper->getDatamodulById($this->request->belegId);
    $knr = $belegung->knr;
    $kurs = $this->getKurs($knr);
    
    $dbwKurseBase = new \DBWorkKurseBase(1, true);
    $dozentenTmp = $dbwKurseBase->getDozentenForKurse(array($knr));

    $dozenten = array();
    
    array_walk_recursive($dozentenTmp, function($dozentObj) use (&$dozenten) { $dozenten[] = $dozentObj; });

    return $this->getResponse($kurs, $dozenten);
  }

  protected function getResponse($kurs, $dozenten)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->dozenten = $dozenten;
    $this->response->countDozenten = count($dozenten);
    $this->response->belegId = $this->request->belegId;

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this)
    );
  }
}

class InfoAnDozRequest
{
  public $knr;
  public $belegId;
}

class InfoAnDozResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $menuPunkte;
  public $kurs;
  public $belegId;
  public $dozenten;
  public $countDozenten;
  public $user;
  public $menuBar;
}