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


class Schuelerstundplangrafik extends DruckGrafikKW
{
  
  public $teilnid;

  
  public $dbwbelegung;
  
  public $tag;

  public function getListID()
  {
    return 20;
  }

  

  public function getListTyp()
  {
    return '18';
  }

  

  public function loadFactoryDefaults()
  {
    parent::loadFactoryDefaults();
    $this->setTitle1();
  }

  

  public function setTitle1()
  {
    $this->settings->title1 = 'Stundenplan';
  }

  

  public function getDruckbereichEnabled($optionName)
  {
    
    switch ($optionName) {
      case 'querformat':
        return true;
    }

    return false;
  }

  

  public function create($pDozId = -1, $pPrintAllowed4User = false)
  {
    $this->setDozId($pDozId);
    if (!empty($this->selnr)) {
      
      

      $dbwkurse = new DBWorkKurseWT(1, true);
      $this->dbwbelegung = new DBWorkBelegung(1, true);
      $selnrary = explode(':', $this->selnr);
      
      
      if (!$this->checkDozTable($selnrary) || !$pPrintAllowed4User) {
        $this->ausgabeFault($this->getWarntext($pPrintAllowed4User));
      } else {
        $this->tag = '';
        
        $this->pdf = $this->__initPDF();
        foreach ($selnrary as $knr) {
          
          $this->kursary[] = $dbwkurse->getKurs($knr);  
        }
        $belegungsary = array();
        $kursDontShowAry = array();

        
        foreach ($this->kursary as $kurs) {
          
          
          
          $isAllowed = BelegungAPI::isUserAllowedToSeeTn($kurs, $this->toolId, \ToolSettingsAPI::getToolSettings(0)->showKinderInAnwesenheiten5);
          if (! $isAllowed) {
            $kursDontShowAry[] = $kurs;
          } else {
            $belegungsary = array_merge($belegungsary, $this->dbwbelegung->getBelegungenbyKursNR($kurs->id));
          }
        }

        $teilnehmeridary = array();
        
        foreach ($belegungsary as $belegung) {
          $teilnehmeridary[] = $belegung->teilnehmer->id;
        }

        
        $teilnehmeridary = array_unique($teilnehmeridary);
        
        
        sort($teilnehmeridary);
        if (!empty($teilnehmeridary)) {
          if ($teilnehmeridary[0] == null) {
            array_shift($teilnehmeridary);
          }
          foreach ($teilnehmeridary as $tid) {
            $this->teilnid = $tid;
            $this->_prepare();
            $this->pdf->AddPage();
            $this->_createContent();
          }
        } 
        elseif (empty($kursDontShowAry)) {
          $this->_prepare();
          $this->pdf->AddPage();
          $this->pdf->CellAtX('3%', 'Für Ihre ausgewählten Kurse wurden keine Teilnehmer gefunden.', '', 'C', 15, '100%', 0, 15, '', '');
        }
        foreach ($kursDontShowAry as $kurs) {
          $this->_prepare();
          $this->pdf->AddPage();
          $this->pdf->ln();
          
          $strAry = $this->stringLineBreak('Diese Liste kann für den Kurs ' . $kurs->kurzbez . ' (' . $kurs->id . ') nicht erzeugt werden, ' . "\n"
                          . 'da die minimale Teilnehmeranzahl noch nicht erreicht wurde.', '93%');
          foreach ($strAry as $str) {
            $this->pdf->CellAtX('3%', $str, '', '', 10, '100%', 0, 15, '', '');
            $this->pdf->ln();
          }
        } 
        $this->pdf->Output('', $this->getOutputName());
      } 
    }
  }

  

  
  public function _getTimeary()
  {
    $dbwstdplan = new DBWorkHLDozStdplanUhr();
    
    $stdplanuhr = $dbwstdplan->getAll();

    $stdcount = count($stdplanuhr);
    for ($i = 0;$i < $stdcount;$i++) {
      $this->timeary[$i]['von'] = $stdplanuhr[$i]->uhrvon;
      $this->timeary[$i]['bis'] = $stdplanuhr[$i]->uhrbis;
    }
    
    $this->timeary[$stdcount]['von'] = '24:01';
    $this->timeary[$stdcount]['bis'] = '24:01';
  }

  

  
  protected function _printDruckListe($pKurs = '')
  {
    
    $belegungary = array();
    
    foreach ($this->kursary as $kurs) {
      $belegungary = array_merge($belegungary, $this->dbwbelegung->getBelegungenbyKursTeilnehmerNR($kurs->id, $this->teilnid));
    }

    $dbwkurstage = new DBWorkKurseTage(1, true);
    $dbwkurstage->newSearch();

    if ($this->toolId == \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ) {
      $dbwkurstage->toobject->nodoztool = true;
    }

    $this->kursetage = array();
    foreach ($belegungary as $bel) {
      $this->kursetage = array_merge(
        $this->kursetage,
        $dbwkurstage->getKurstagebyKursNR($bel->kurs->id, -1, array('A', 'X'))
      );
    }
    usort($this->kursetage, array('DruckGrafik', 'kTageCmp'));

    
    

    
    $this->pdf->ln(-0.5 * $this->height);
    $this->pdf->CellAtX('10.5%', 'Mo., ' . $this->kwary['Mo'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('22.9%', 'Di., ' . $this->kwary['Di'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('35.3%', 'Mi., ' . $this->kwary['Mi'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('47.7%', 'Do., ' . $this->kwary['Do'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('60.1%', 'Fr., ' . $this->kwary['Fr'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('72.5%', 'Sa., ' . $this->kwary['Sa'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->CellAtX('84.9%', 'So., ' . $this->kwary['So'], 'B', 'C', '', '12.4%', 1, $this->height, 0);
    $this->pdf->ln($this->height);
    $this->pdf->SetFillColor(200, 200, 200);	
    $this->pdf->CellAtX('3%', $belegungary[0]->teilnehmer->vorname . ' ' . $belegungary[0]->teilnehmer->name, 'B', '', '', '94.3%', 1, $this->height, 1);
    $this->pdf->ln($this->height);

    
    $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
    while ($this->dtop->isDateBefore($datum, $this->kwary['Mo']) && $this->kursetage) {
      array_shift($this->kursetage);
      $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
    }
    
    for ($itag = 0;$itag < 8;$itag++) {
      $this->_printKursCell($itag);
    }
  }

  

  
  public function _printKursCell($pTag)
  {
    $wotagary = array_keys($this->kwary);
    

    $izeit = 0;
    foreach ($this->timeary as $zeit) {
      
      if (strcmp($zeit['von'], '24:01') != 0 && strcmp($zeit['bis'], '24:01') != 0) {
        
        if ($pTag == 0) {
          $this->setFillColor4Border();	
          $this->pdf->CellAtX('3%', '', 'B', '', '', '7.5%', 1, 2 * $this->height, 1);
          $this->pdf->ln($this->height * 3 / 5);
          $this->pdf->CellAtX('3%', $zeit['von'] . '-' . $zeit['bis'], 'B', '', '', '7.5%', 0, 2 * $this->height, 0);
          $this->pdf->ln($this->height * -3 / 5);
        }
        else {
          $this->setFillColor4Cell();	
          $datum = isset($this->kursetage[0]->datum) ? $this->kursetage[0]->datum : '';
          if (strcmp($datum, $this->kwary[$wotagary[$pTag - 1]]) == 0) {
            $zeit_beg = isset($this->kursetage[0]->zeit_beg) ? $this->kursetage[0]->zeit_beg : '';
            $zeit_ende = isset($this->kursetage[0]->zeit_ende) ? $this->kursetage[0]->zeit_ende : '';
            if (strcmp($zeit['von'], $zeit_ende) <= 0 && strcmp($zeit['bis'], $zeit_beg) > 0) {
              $this->pdf->CellAtX(12.4 * ($pTag - 1) + 10.5 . '%', '', 'B', '', '', '12.4%', 1, 2 * $this->height, 1);
              $this->pdf->CellAtX(12.4 * ($pTag - 1) + 10.5 . '%', $this->kursetage[0]->knr, 'B', '', 8, '12.4%', 0, $this->height, 0);
              $this->pdf->CellAtX(12.4 * ($pTag - 1) + 10.5 . '%', $this->kursetage[0]->zeit_beg . '-' . $this->kursetage[0]->zeit_ende, '', 'R', 8, '12.4%', 0, $this->height, 0);
              $this->pdf->ln($this->height);
              $this->pdf->CellAtX(12.4 * ($pTag - 1) + 10.5 . '%', $this->kursetage[0]->kurs->kurzbez, '', '', 8, '12.4%', 0, 5, 0);
              $this->pdf->ln(-$this->height);
              if (strcmp($this->timeary[$izeit]['bis'], $this->kursetage[0]->zeit_ende) >= 0) {
                array_shift($this->kursetage);
              }
            }
            else {
              $this->pdf->CellAtX(12.4 * ($pTag - 1) + 10.5 . '%', '', 'B', '', '', '12.4%', 1, 2 * $this->height, 0);
            }
          }
          else {
            $this->pdf->CellAtX(12.4 * ($pTag - 1) + 10.5 . '%', '', 'B', '', '', '12.4%', 1, 2 * $this->height, 0);
          }
        }
        $izeit++;
        $this->pdf->ln(2 * $this->height);
      }
    }
    $this->pdf->ln(-2 * $this->height * ($izeit));
  }

  
}