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

namespace Kufer\KuferTools\Utils\WssField;

class WssFieldBelegungElearningLink extends WssField
{
  public const LEERER_WERT = '- kein eLearning-Kurs -';

  protected function getValue($datamodulWert, $isFormular = false)
  {
    $objsettings = \ToolSettingsAPI::getToolSettings(0);
    $eLearningPlattformURL = $objsettings->eLearningUrl;
    $dbwKurse = new \DBWorkKurseWT(1, true);
    $knr = $datamodulWert->getVarValueEx('id', 'kurs');
    $kurs = $dbwKurse->getKurs($knr);
    $elearningKursID = $kurs->elearningKursID;

    $user = \SessionOperations::getCurrentWebToolsUser();
    if (!($user->tnr > 0)) {
      return static::LEERER_WERT;
    }

    if ($elearningKursID > 0) {
      if (!(\SessionOperations::getCurrentWebToolsTeilnehmer()->elearninguserid > 0)) {
        return
          '<p style="color: red;">Sie sind nicht bei Moodle registiert.</p>'
          . '<p>Bitte wenden Sie sich an unsere Einrichtung.</p>';
      }

      
      $username = \StringOperations::strtolower($user->login);
      if (\DBWorkBASysSettings::isBlak()) {
        if ($username == 'ckuchinke') {
          $username = 'cschernich';
        } elseif ($username == 'mbischoff') {
          $username = 'dbischoff';
        } elseif ($username == 'idoenges') {
          $username = 'idoenged';
        } elseif ($username == 'jbraun') {
          $username = 'jfeichtinger';
        } elseif ($username == 'ssebold') {
          $username = 'shopperdietzel';
        } elseif ($username == 'kseubert') {
          $username = 'kback';
        } elseif ($username == 'cgleissner') {
          $username = 'cheinold';
        } elseif ($username == 'mklemme') {
          $username = 'mniessner';
        } elseif ($username == 'vkern1') {
          $username = 'vvonaltenblaskowitzkern';
        } elseif ($username == 'hboerschlein') {
          $username = 'hvoll';
        } elseif ($username == 'kkurz1') {
          $username = 'kkuntz';
        } elseif ($username == 'kfilipp') {
          $username = 'kzwanziger';
        } elseif ($username == 'bhaslauer') {
          $username = 'bhaslauer1';
        } elseif ($username == 'kott2') {
          $username = 'kbrendler1';
        } elseif ($username == 'kspengler') {
          $username = 'kkelm';
        } elseif ($username == 'ldrummer') {
          $username = 'lismeier';
        } elseif ($username == 'kbenz') {
          $username = 'krehm1';
        } elseif ($username == 'criedl1') {
          $username = 'cweiss2';
        } elseif ($username == 'truth') {
          $username = 'truth1';
        } elseif ($username == 'rspringer1') {
          $username = 'rsack';
        } elseif ($username == 'pknopp') {
          $username = 'phadravsk';
        } elseif ($username == 'dwelsch1') {
          $username = 'dcavalji';
        } elseif ($username == 'bexner') {
          $username = 'bginkel';
        } elseif ($username == 'sschurig') {
          $username = 'sgimpl';
        } elseif ($username == 'jmader2') {
          $username = 'jwitt';
        } elseif ($username == 'cleonhardt') {
          $username = 'cmerfort43';
        } elseif ($username == 'twill') {
          $username = 'thoffmann1';
        } elseif ($username == 'hreimann') {
          $username = 'hkranich';
        } elseif ($username == 'sng') {
          $username = 'sdannler';
        } elseif ($username == 'vhohenberger1') {
          $username = 'vseuss';
        } elseif ($username == 'dmotzke') {
          $username = 'dheissig';
        } elseif ($username == 'tzweiboehmer') {
          $username = 'tngo';
        } elseif ($username == 'astieglitz') {
          $username = 'ameinl';
        } elseif ($username == 'kbitzer') {
          $username = 'kott1';
        } elseif ($username == 'ccuiper') {
          $username = 'cfalk';
        } elseif ($username == 'anaber') {
          $username = 'ameier1';
        } elseif ($username == 'crothfelder') {
          $username = 'cspengler1';
        } elseif ($username == 'nschiessl') {
          $username = 'npoellmann';
        } elseif ($username == 'sbauer8') {
          $username = 'sthoma';
        } elseif ($username == 'nstock1') {
          $username = 'nwalth';
        } elseif ($username == 'sdrexel') {
          $username = 'skrieglstein';
        } elseif ($username == 'kboon') {
          $username = 'kgerbersdorf';
        } elseif ($username == 'rhaemmer') {
          $username = 'rtinz';
        } elseif ($username == 'kgraf2') {
          $username = 'krosniatowski';
        } elseif ($username == 'eschunk1') {
          $username = 'eschunk';
        } elseif ($username == 'mholzmann') {
          $username = 'mhochstein';
        } elseif ($username == 'ppopp1') {
          $username = 'pschmid54';
        } elseif ($username == 'szimmermann11') {
          $username = 'sholzinger';
        } elseif ($username == 'uschreff') {
          $username = 'umohler';
        } elseif ($username == 'ceckert1') {
          $username = 'credlingshoefer';
        } elseif ($username == 'aantropov') {
          $username = 'agieck';
        } elseif ($username == 'vschranner') {
          $username = 'vwohlschlaeger';
        } elseif ($username == 'cstaeudel') {
          $username = 'cschmiga';
        } elseif ($username == 'aknoll1') {
          $username = 'aguggemos1';
        } elseif ($username == 'mwagner7') {
          $username = 'mendress1';
        } elseif ($username == 'aschleicher1') {
          $username = 'aschleicher2';
        } elseif ($username == 'aschleicher2') {
          return 'Es ist ein Fehler aufgetreten. Bitte kontaktieren Sie die Einrichtung';
        } elseif ($username == 'tgolfier') {
          $username = 'tbayer1';
        }
      }
      $courseid = $elearningKursID;
      
      $key1 = '62ceEWf_UK$Z-';
      $key2 = 'kN8WdMt!ORjAp';
      $key3 = 'fwUMZNyr2@kf7';
      $key4 = 'gUYw7H+CFmnmJ';
      $date = time() + 30;
      $token1 = md5($key1 . $courseid . $username . $key2);
      $token2 = md5($key3 . $date . $key4);

      $urlparams = array(
          'username' => $username,
          'courseid' => $courseid,
          'date' => $date,
          'token1' => $token1,
          'token2' => $token2
      );

      return '<a href="' . $eLearningPlattformURL . '/login/index.php?' . http_build_query($urlparams) . '" class="eLearningLink" target="_blank">Hier geht es zum Online-Kurs</a>';
    } else {
      return self::LEERER_WERT;
    }
  }

  public function isValueEmpty($value): bool
  {
    return parent::isValueEmpty($value) || $value == self::LEERER_WERT;
  }

  protected function encodeValue($value)
  {
    return $this->purifyHtml($value);
  }
}