#include <infodrom.style>
#include "lt.style"
#include "functions.inc"
#include "edit.style"
#include "jobs.inc"

<postgresconnect>
<build_timeframe>
<tab_avail_stubs>
<normal_time>
<fragment_ok>
<ltpage title="Supporter">

# This page intends to provide an overview about a given supporter and
# provide means to assign a job to the person.  Hence, the page contains:
#  . Availability through the entire week
#  . Availability splitted by day
#  . Including hooks to unassigned jobs for assignments

<?
  if (!is_supporter() && !is_also_supporter()) {
    die ("Only supporters may read these page.");
  }

  if (isset ($id)) {
    $query = sprintf ("SELECT name,email,comment FROM person WHERE id = %d", $id);
    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

    if (pg_NumRows ($sth) > 0) {
      $row = pg_fetch_array ($sth, 0);
      $name = $row['name'];
      $email = $row['email'];
      $comment = $row['comment'];
    }

    $query = sprintf ("SELECT * FROM availability WHERE person = %d ORDER BY starttime", $id);
    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

    for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
      $row = pg_fetch_array ($sth, $nr);

      $foo = explode (" ", $row['starttime']);
      $time = build_timeframe ($row['starttime'], $row['endtime']);
      if (strlen ($avail[$foo[0]]) > 0) {
	$avail[$foo[0]] .= "&nbsp;" . $time;
      } else {
	$avail[$foo[0]] = $time;
      }

      $foo = normal_time ($row['starttime'], $row['endtime']);
      $available[$foo[0]][] = array ('start' => $foo[1], 'end' => $foo[2]);
    }

    $query = sprintf ("SELECT assignment.starttime,assignment.endtime,job.title,job.id,assignment.oid "
		     ."FROM assignment,fragment,job WHERE person = %s "
		     ."AND fragment=fragment.id AND fragment.job = job.id "
		     ."ORDER BY starttime", $id);
    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

    for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
      $row = pg_fetch_array ($sth, $nr);

      $foo = normal_time ($row['starttime'], $row['endtime']);
      $assignment[$foo[0]][] = array ('start' => $foo[1],
				      'end'   => $foo[2],
				      'title' => $row['title'],
				      'oid'   => $row['oid'],
				      'job'   => $row['id']);
    }

    if (strlen ($email) > 0) {
      printf ("<h3>%s &lt;%s&gt;</h3>", $name, $email);
    } else {
      printf ("<h3>%s</h3>", $name);
    }
   if (((is_admin() && is_supporter()) || $LINUXTAG_AUTH['name'] == $name) && strlen ($comment) > 0) {
      printf ("<p>Comment: %s</p>", $comment);
    }
?>

# Basically, this table is a copy of a single line from availability
#
<p><table cellpadding=0 cellspacing=1>
<?
    printf ($f_header_head, "<colh>");
    reset ($ltdays);
    while (list($key,$day) = each ($ltdays)) {
      $foo = explode (",", $day);
      printf ($f_header_day, $foo[0]);
      $col[] = $key;
    }
    echo ($f_header_tail);

    if ((is_admin () && is_supporter())
     || ($id == $LINUXTAG_AUTH['id'])) {
      printf ($f_row_head, "<cold>", "edit_person", $id, $name);
    } else {
      printf ($f_row_head, "<cold>", "supporter", $id, $name);
    }

    reset ($ltdays);
    while (list($date,$day) = each ($ltdays)) {
      if (strlen ($avail[$date]) > 0) {
        printf ($f_row_day, $avail[$date]);
      } else {
        printf ($f_row_day, "&nbsp;");
      }
    }
    echo ($f_row_tail);
?>
</table>
<hr>
<?
  function display_jobs ($dbh, $date, $start, $end, $id)
  {
    global $LINUXTAG_AUTH;
 
    if ($start == "24:00") { $start = "23:59"; }
    if ($end == "24:00") { $end = "23:59"; }

    $query = sprintf ("SELECT fragment.id,job.title,starttime,endtime,job FROM job,fragment "
		     ."WHERE starttime >= '%s %s' AND endtime <= '%s %s' "
		     ."AND job.id=fragment.job ORDER BY starttime,endtime",
		     $date, $start, $date, $end);
    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

    printf ("<br>%s-%s: free", $start, $end);

    if ((is_admin() && is_supporter()) || $id == $LINUXTAG_AUTH['id']) {
      if ( pg_NumRows ($sth) > 0) {
	for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
	  $row = pg_fetch_array ($sth, $nr);

	  if (!fragment_ok ($dbh, $row['id'])) {
	    $foo = normal_time ($row['starttime'], $row['endtime']);

	    echo ("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&gt;");

	    printf (" %s-%s: <a href=\"job.php3?id=%d\">%s</a> "
		   ."(<a href=\"update.php3?type=assign&fragment=%d&person=%d\">assign</a>)",
		    $foo[1], $foo[2], $row['job'], $row['title'],
		    $row['id'], $id);
	  }
	}
      }
    }
  }

    reset ($ltdays);
    while (list($date,$day) = each ($ltdays)) {
      if (strlen ($avail[$date]) > 0) {
	printf ("<h3>%s</h3>", $day);
	# printf ("<p><b>Available:</b> %s (DEBUG)", $avail[$date]);

	if (is_array ($assignment[$date])) {
	  $assign = $assignment[$date];
	} else {
	  $assign = array ();
	}

	if (is_array ($available[$date])) {
	  $around = $available[$date];
	} else {
	  $around = array ();
	}

	$av_pivot = 0;   // availability
	$as_pivot = 0;   // assignment
	$slot = $around[0]['start'];

	while (!($av_pivot >= count ($around) && $as_pivot >= count ($assign))) {
	  if ($as_pivot >= count ($assign)) {
	    display_jobs ($dbh, $date, $slot, $around[$av_pivot++]['end'], $id);
	    if ($av_pivot < count ($around)) {
	      $slot = $around[$av_pivot]['start'];
	    }
	  } elseif (time_lt ($slot, $assign[$as_pivot]['start'])) {
	    if (time_lt ($around[$av_pivot]['end'], $assign[$as_pivot]['start'])) {
	      $end = $around[$av_pivot]['end'];
	    } else {
	      $end = $assign[$as_pivot]['start'];
	    }
	    display_jobs ($dbh, $date, $slot, $end, $id);
	    if ($av_pivot+1 < count ($around) &&
	        time_lt ($around[$av_pivot+1]['start'], $assign[$as_pivot]['start'])) {
	      $slot = $around[++$av_pivot]['start'];
	    } else {
	      $slot = $assign[$as_pivot]['start'];
	    }
	  } else {
	    if ((is_admin() && is_supporter()) || $id == $LINUXTAG_AUTH['id']) {
	      printf ("<br>%s-%s: <a href=\"job.php3?id=%d\">%s</a> "
		     ."(<a href=\"update.php3?type=loose&assignment=%d\">loose this</a>)",
		     $assign[$as_pivot]['start'], $assign[$as_pivot]['end'],
		     $assign[$as_pivot]['job'], $assign[$as_pivot]['title'],
		     $assign[$as_pivot]['oid']);
	    } else {
	      printf ("<br>%s-%s: <a href=\"job.php3?id=%d\">%s</a>",
		     $assign[$as_pivot]['start'], $assign[$as_pivot]['end'],
		     $assign[$as_pivot]['job'], $assign[$as_pivot]['title']);
	    }
	    $slot = $assign[$as_pivot++]['end'];
	    if (!time_lt ($slot, $around[$av_pivot]['end'])) {
	      $slot = $around[++$av_pivot]['start'];
	    }
	  }
	}
      }
    }

  } else {
    echo "<p><b>No such user found.</b></p>";
  }
?>

</ltpage>

# Local variables:
# mode: text
# mode: auto-fill
# end:
