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


class ToolDozLehrplan extends ToolBase
{
  

  private $_user;

  public function __construct()
  {
    parent::__construct();
    $this->toolTitle = 'Meine Kurse';

    $this->_user = SessionOperations::getCurrentWebToolsUser();
  }

  
  public function init($pPageId)
  {
    
    switch ($pPageId) {
      default:
      case 23: 
        $page = $this->_handleTermine4Lehrplan();
      break;
    }

    if ($page == null) {
      throw new Exception('ungültige PageId');
    }
    $this->currentPage = $page;
  }

  
  private function _handleTermine4Lehrplan($pPageId = 23)
  {
    $dbwKursTage = new DBWorkKurseTageWT();
    $dtop = new DateTimeOperations();

    
    $vonDatum = $dtop->getDateByParam(6);  
    $bisDatum = $dtop->getDateByParam(7);  

    
    

    
    
    if (GetOperations::isDefined('datVon') && GetOperations::getVarValue('datVon') != '') {
      $vonDatum = $dtop->getValidDateString(GetOperations::getVarValue('datVon', ''));
    }
    if (GetOperations::isDefined('datBis') && GetOperations::getVarValue('datBis') != '') {
      $bisDatum = $dtop->getValidDateString(GetOperations::getVarValue('datBis', $bisDatum));
    }

    
    $vonZeit = $dtop->getFloatasTimeString(STARTZEIT, true);
    $bisZeit = $dtop->getFloatasTimeString(ENDEZEIT, true);

    
    
    if (GetOperations::isDefined('zeitVon')) {
      $vonZeit = GetOperations::getVarValue('zeitVon', '');
      $vonZeit = $dtop->getValidTimeString($vonZeit);
    }
    if (GetOperations::isDefined('zeitBis')) {
      $bisZeit = GetOperations::getVarValue('zeitBis', '');
      if ($bisZeit == '') {
        $bisZeit = '23:59';
      }
      $bisZeit = $dtop->getValidTimeString($bisZeit);
    }


    $dbwKursTage->newSearch();
    $dbwKursTage->fromobject->datum = $vonDatum;
    $dbwKursTage->toobject->datum = $bisDatum;

    $dbwKursTage->fromobject->kurs->semester = $this->toolSettings->semlist;
    $dbwKursTage->toobject->kurs->semester = '';
    $dbwKursTage->dbwkurse->justNoAusfall = $this->toolSettings->keineAusgefallenenKurse;

    $dbwKursTage->orderby = $this->toolSettings->sortkt;
    $dbwKursTage->orderbyasc = $this->toolSettings->sortktasc;
    $dbwKursTage->fromobject->ausfall = $this->toolSettings->ktstatus;
    $dbwKursTage->toobject->ausfall = $this->toolSettings->ktstatus;
    $dbwKursTage->setOrderAktuellesSemester($this->toolSettings);

    
    $dbwKursTage->fromobject->zeit_ende = $vonZeit;
    $dbwKursTage->toobject->zeit_beg = $bisZeit;

    
    $kurstageproseite = ($this->toolSettings->einsaetzeproseite > 0) ? $this->toolSettings->einsaetzeproseite : 10;

    $alleIds = $this->getAlleIdsKursTage($dbwKursTage);
    $data = $this->getData($dbwKursTage, $alleIds, $kurstageproseite);
    $anzahlKurstageDesDozentenZuDiesemKurs = count($alleIds);

    
    $selectedknrs = $dbwKursTage->getKursNrByWebUserId($this->_user->id);
    
    $dbwKursTage->newSearch();
    $alleKnrs = $dbwKursTage->getKursNrByWebUserId($this->_user->id);
    $this->setTotalData($alleKnrs);

    $pageLinks = array();
    
    
    if (\SecurityOperations::privilegAllowedCurrentUser($this->getToolId(), $pPageId . '.0.2')) {
      $pageLinks['filter'] = true;
    }

    
    $pageLinks['grafik'] = \SecurityOperations::privilegAllowedCurrentUser($this->getToolId(), $pPageId . '.0.4');

    
    
    $pageLinks['self'] = 'usertools.php?tool_id=' . $this->getToolId() . '&amp;toolsection_id=23';

    
    
    
    


    if (\SecurityOperations::privilegAllowedCurrentUser($this->getToolId(), $pPageId . '.0.1') == 2
      
      && ToolsAPI::rechteOR(array('334.1.1', '334.1.2', '334.1.3', '334.1.4', '334.1.5', '334.1.6', '334.1.7'), 1)) {

      
      $pageLinks['kursdetails'] = 'usertools.php?tool_id=1&amp;toolsection_id=334&amp;fromOtherTool=1';
    }

    
    $dbwTools = new DBWorkTools();
    if (\SecurityOperations::privilegAllowedCurrentUser($this->getToolId(), $pPageId . '.0.5') == 2
        && ($dbwTools->isToolAllowed(SessionOperations::getUserIdentifier(), 18))) {
      $pageLinks['anwesenheitLink'] = 'usertools.php?tool_id=18&toolsection_id=274';
    }

    $page = new PageDozTermine4Lehrplan($pPageId, $this, $data, $vonDatum, $bisDatum, $vonZeit, $bisZeit, $selectedknrs, $anzahlKurstageDesDozentenZuDiesemKurs, $kurstageproseite);

    $page->setLinks($pageLinks);

    return $page;
  }

  

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



class PageDozTermine4Lehrplan extends BrowseableObjectsPageBaseWT
{
  private $_data;
  private $_vonDate;
  private $_bisDate;
  private $_vonTime;
  private $_bisTime;

  public function __construct($pPageId, $pTool, $pData, $pVonDate, $pBisDate, $pVonTime, $pBisTime, $pSelectedKnrs, $pAnzahlAllerTermine, $pKurstageProSeite)
  {

   
    parent::__construct('', $pPageId, $pTool, $pAnzahlAllerTermine, $pKurstageProSeite);
    $this->_data = $pData;
    $this->_vonDate = $pVonDate;
    $this->_bisDate = $pBisDate;
    $this->_vonTime = $pVonTime;
    $this->_bisTime = $pBisTime;
    $this->selectedKnrs = $pSelectedKnrs;

    $user = SessionOperations::getCurrentWebToolsUser();
    if ($user->vorname != '') {
      $this->loginName = $user->name . ', ' . $user->vorname;
    } else {
      $this->loginName = $user->name;
    }
  }

  
  public function getContentHeader()
  {
    $inhalt = '';
    if (count($this->_data) > 0) {
      $inhalt .= 'Lehrplanübersicht ';
      if (!empty($this->_vonDate) && !empty($this->_bisDate)) {
        $inhalt .= 'vom ' . $this->_vonDate . ' bis ' . $this->_bisDate;
      }
    }

    return $inhalt;
  }

  public function getContent()
  {
    $dtop = new DateTimeOperations();

    $result = '';

    $result .= '<div id="main_content">' . "\n";

    if ((count($this->_data) != 0)
       && ($this->allgSettings->buttonleite_position == BUTTONBAR_TOP || $this->allgSettings->buttonleite_position == BUTTONBAR_BOTH)) {
      $result .= $this->_getButtonLeiste();
    }

    
    $aktDate = '';
    $maxDaysBack = '';
    if (isset($this->links['kursdetails']) && $this->links['kursdetails'] != '') {
      $aktDate = $dtop->getDateNow();
      $toolSettingsTool18 = ToolSettingsAPI::getToolSettings(\ToolsAPI::TOOL_ANWESENHEIT_DOZ);
      $maxDaysBack = $toolSettingsTool18->aktKurstageBackScroll;
    }


    $counter = 0;
    if (!empty($this->_data)) {
      foreach ($this->_data as $kurstag) {
        $builder = new SightBuilderUebersicht($this->tool->getToolId(), $this->id, $kurstag, true);
        $builder->weitereKlasseFuerSightDiv = 'termine';
        $evenClass = ($counter % 2 == 0) ? ' farbe_sicht_erstezeile' : '';
        $result .= '<div class="content_zeile' . $evenClass . '">' . "\n";
        $result .= '<div class="steuerelemente">' . "\n";



        
        if (isset($this->links['kursdetails']) && $this->links['kursdetails'] != '') {
          $sprachSettings = SettingsFactory::getSettings('SprachSettingsObject');
          $class = (DBWorkBASysSettings::isBlak()) ? 'class="kursdetails_lupe_blak"' : '';

          

          
          $result .= Menu::getUserLink(
            '<img src="images/details.gif" ' . $class . ' alt="Zu den Details" '
                                     . 'title="Zu den ' . CleanOperations::cleanStringForHtml($sprachSettings->Kurs['Singular']['Nominativ']) . 'details">',
            $this->links['kursdetails'] . '&nr=' . $kurstag->kurs->id,
            2,
            false
          );
          $result .= BR_WT;
        }
        if (isset($this->links['anwesenheitLink']) && $this->links['anwesenheitLink'] != '') {
          $daysBack = $dtop->getDaysFromTo($kurstag->datum, $aktDate);

          if ($daysBack <= ($maxDaysBack + 1) && $daysBack > 0) {
            $result .= Menu::getUserLink(
              '<img src="images/anwesenheit.gif" alt="Anwesenheiten Eintragen" '
                                       . 'title="Anwesenheiten Eintragen">',
              $this->links['anwesenheitLink'] . '&nr=' . $kurstag->kurs->id . '&ktid=' . CleanOperations::cleanInt($kurstag->id),
              2,
              false
            );
          }
        }

        $result .= '</div>' . "\n";
        $result .= $builder->buildSight();
        $result .= '</div>' . "\n";
        $counter++;
      } 
    }

    $result .= '</div>' . "\n";

    if ($counter == 0) { 
      $result .= $this->_getNotice();
    } elseif ($this->allgSettings->buttonleite_position != BUTTONBAR_TOP) {
      $result .= $this->_getButtonLeiste();
    }

    return $result;
  }

  
  protected function _getButtonLeiste()
  {
    $result = '<div class="main_buttons alletermine">';

    if (isset($this->links['grafik'])) {
      
      $result .= '<a class="grafik_button" href="' . CleanOperations::cleanStringForHtml($this->links['grafik']) . '" target="_blank">';
      $result .= '<img src="layout/button_90_druck_grafiken.gif" title="Lehrplangrafik" alt="Lehrplangrafik">';
      $result .= '</a>';
    }

    if (isset($this->links['filter'])) {
      $result .= '<a class="ktuebersicht_termineLinkFilter" href="#">';
      $result .= '<img src="layout/button_main_search_filter.gif" title="Filter öffnen" alt="Terminfilter" class="ktuebersicht_termineImgFilter">';
      $result .= '</a>';
    }

    $result .= $this->getNavigaton();

    $result .= "\n" . '</div>' . "\n";

    return $result;
  }

  public function getSpecialJS()
  {
    $jsAry = parent::getSpecialJS();
    $jsAry[] = './js/kurstageuebersicht.js';

    return $jsAry;
  }

  public function getSpecialCSS()
  {
    $result = parent::getSpecialCss();
    $result[] = './css/kursuebersicht.css';
    $result[] = './css/doztool.css';

    return $result;
  }

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();

    
    $result .= $this->getSelektorKursFelderToDetails();

    
    $result .= JsOperations::getDeclarationString('KuferTools.vonDatum', $this->_vonDate);
    $result .= JsOperations::getDeclarationString('KuferTools.bisDatum', $this->_bisDate);
    $result .= JsOperations::getDeclarationString('KuferTools.vonZeit', $this->_vonTime);
    $result .= JsOperations::getDeclarationString('KuferTools.bisZeit', $this->_bisTime);

    $result .= ToolsAPI::getJsFilterVorbereitung4Kurstage();

    $result .= JsOperations::getDeclarationJson('KuferTools.SelectedKnrs', $this->selectedKnrs);

    $result .= JsOperations::getDeclarationInt('KuferTools.PageId', $this->id);
    
    $result .= JsOperations::getDeclarationString('KuferTools.LoginName', $this->loginName);

    return $result;
  }

  
  private function _getNotice()
  {
    $result = '';

    
    if ($this->allgSettings->buttonleite_position == BUTTONBAR_TOP) {
      $result .= $this->_getNoticeFilter();
    }

    $result .= '<div class="seitentitel"><h2>Achtung</h2></div>'
    . '<p class="default_message">Im Zeitraum vom ' . CleanOperations::cleanDateString($this->_vonDate) . ' bis zum ' . CleanOperations::cleanDateString($this->_bisDate) . ' wurden keine Termine gefunden.' . BR_WT
    . 'Hier gelangen Sie <a href="javascript:history.back();">zurück</a>.'
    . '</p>';

    
    if ($this->allgSettings->buttonleite_position != BUTTONBAR_TOP) {
      $result .= $this->_getNoticeFilter();
    }

    return $result;
  }

  private function _getNoticeFilter()
  {
    $result = '';
    if (isset($this->links['filter'])) {
      $result .= '<p class="filterbutton">';
      $result .= '<a class="ktuebersicht_termineLinkFilter" href="#">';
      $result .= '<img src="layout/button_main_search_filter.gif" title="Filter öffnen" alt="Terminfilter" class="ktuebersicht_termineImgFilter">';
      $result .= '</a>' . '</p>';
    }

    return $result;
  }
}