<?php #lnk/xx_termine_teilnahmen/class.php

class Termine_DienstpläneLink extends BaseLink
{
  public function __construct($database, $keyvaluestore, $manager, $info, $page) {
    parent::__construct($database, $keyvaluestore, $manager, $info, $page);
  }
  
  protected function insert($ids) {
    if (!$this->man->user->HasRight($this->adminRight)) {
        return 403; // You shall not pass!
    }

    $qry = "UPDATE Termine SET Dienstplan = ? WHERE ID = ?";
    if ($stmt = $this->db->prepare($qry)) {
      $stmt->bind_param("ii", $ids["Dienstplan"], $ids["Termine"]);
      $stmt->execute();
      if (1==$stmt->affected_rows) {
        $this->man->AddMessage("Termin hinzugefügt!");
        return 201;
      } else {
        return 200;
      }
    } else {
      $this->man->AddMessage("Mysql error: ".$this->db->error);
    }
    return 500; // Should not reach this stage
  }
  
  protected function remove($ids) {
    if (!$this->man->user->HasRight($this->adminRight)) {
      return 403; // You shall not pass!
    }

    $qry = "UPDATE Termine SET Dienstplan = NULL WHERE ID = ?";
    if ($stmt = $this->db->prepare($qry)) {
      $stmt->bind_param("i", $ids["Termine"]);
      $stmt->execute();
      if (1==$stmt->affected_rows) {
        $this->man->AddMessage("Termin entfernt!");
        return 200;
      } else {
        return 404;
      }
    } else {
      $this->man->AddMessage("Mysql error: ".$this->db->error);
    }
    return 500;
  }
  
  private function canJoin($ids) {
    $ret = [];
    $qry = "SELECT t.Ausgelost, IF(t.Beginn<NOW(), 1, 0) vorbei, tp.MaxTeilnehmer, td.Abteilung, t.Gruppe, r.Name rName, tt.Status "
          .", (SELECT COUNT(*) FROM Termine_Teilnahmen tt2 WHERE tt2.Termine=t.ID AND tt2.Status>=1) num "
          .", (SELECT COUNT(*) FROM Termine_Teilnahmen tt3 LEFT JOIN Termine t2 ON t2.ID=tt3.Termine "
          ."   WHERE tt3.Personal=? AND t2.ID!=t.ID AND t2.Kategorie=t.Kategorie "
          ."   AND t2.Beginn BETWEEN t.Beginn - INTERVAL 7 DAY AND t.Beginn + INTERVAL 7 DAY) naheDienste "
          ."FROM Termine t "
          ."LEFT JOIN Termine_Dienstpläne td ON td.ID=t.Dienstplan "
          ."LEFT JOIN Termine_Platzvergaben tp ON tp.ID=t.Platzvergabe "
          ."LEFT JOIN Termine_Kategorien tk ON tk.ID = t.Kategorie "
          ."LEFT JOIN Rechte r ON tk.Recht = r.ID "
          ."LEFT JOIN Termine_Teilnahmen tt ON tt.Termine=t.ID AND tt.Personal = ? "
          ."WHERE t.ID = ?";
    if ($stmt = $this->db->prepare($qry)) {
      $stmt->bind_param("iii", $ids["Personal"], $ids["Personal"], $ids["Termine"]);
      $stmt->execute();
      $row = $stmt->get_result()->fetch_assoc();
      $stmt->close();
      
      // Mögliche Rechte zum Bearbeiten
      $possiblePrivileges = array($this->adminRight);
      if (null!==$row["rName"]) {
        $possiblePrivileges[] = $row["rName"];
      }
      
      if ($this->man->user->HasRight($possiblePrivileges) || "2"==$row["Status"]) {
        $ret = [0, 1, 2, null]; // This user is privileged enough to do what he wants in this appointment
      } else if ("0"==$row["vorbei"]) {
        if ($this->man->user->InAbteilung($row["Abteilung"]) 
        && $this->man->user->InGruppe($row["Gruppe"])
        && intVal($row["naheDienste"])==0) {
        // The appointment is not over yet and the user is allowed to join
          if ("0"==$row["MaxTeilnehmer"]) {
            // There is no restriction 
            $ret = [1];
          } else if ($row["Ausgelost"]=="1" && intVal($row["num"])<intVal($row["MaxTeilnehmer"])) {
            $ret = [1];
          } else {
            $ret = [0];
          }
          if (null!==$row["Status"]) {
            $ret[] = null;
          }
        }
      }
    } else {
      $this->man->AddMessage("Mysql error: ".$this->db->error);
    }
    return $ret;
  }
}
