<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

if (isset($_REQUEST["id"])) {
  $pw_id = $_REQUEST["id"];
}
  
$dal = new DAL();

$sql = "SELECT PTR.id AS id, PTR.teamrole_id AS role_id, PE.id AS pe_id, " .
       "CASE WHEN " .
          "(LENGTH(surname) < 10 && LENGTH(first_name) < 10) " . 
            "THEN CONCAT(surname,', ',first_name) " . 
            "ELSE CONCAT(surname,', ',SUBSTRING(first_name,1,1)) " . 
       "END AS text " .
       "FROM projectworkdaypersonnel PWP, personnel PE, projectteamrole PTR " .
       "WHERE PWP.projectworkday_id = $pw_id " .
       "AND PE.id = PWP.personnel_id AND PTR.projectworkdaypersonnel_id = PWP.id " .
       "ORDER BY text, PTR.id ";
$result = $dal->query($sql);
$currid = 0;
foreach ($result as $role) {
  if ($currid == $role->pe_id)
    $role->addedFlag = 1;
  else {
    $role->addedFlag = 0;
    $currid = $role->pe_id;
  }    
}

echo json_encode($result);

?>

