\n";
	foreach($users->users as $user) {
		if($user->enabled && $user->uid != 0) {
			$str .= "\n";
		}
	}
	$str .= "";
	return $str;
}
class Task {
	public $id;
  public $title;
  public $deadline;
  public $description;
  public $lastreminder;
	public $reassignable;
  public $userid;
	public $completed;
	public function write($fp)
	{
		fwrite($fp, "  id) . "\"\n");
		fwrite($fp, "        title=\"" .xmlenc($this->title) . "\"\n");
		fwrite($fp, "        deadline=\"" . xmlenc($this->deadline) . "\"\n");
		fwrite($fp, "        userid=\"" . xmlenc($this->userid) . "\"\n");
		if($this->reassignable) fwrite($fp, "        reassignable=\"true\"\n");
		else fwrite($fp, "        reassignable=\"false\"\n");
		if($this->completed) fwrite($fp, "        completed=\"true\"\n");
		else fwrite($fp, "        completed=\"false\"\n");
		fwrite($fp, "        lastreminder=\"" . xmlenc($this->lastreminder) . "\">\n");
		fwrite($fp, "    ".xmlenc($this->description)."\n");
    if($this->refs) $this->refs->write($fp, "    ");
		fwrite($fp, "  \n");
	}
	public function show()
	{
		global $users, $current_user;
		$str .= "  
\n";
		$str .= "    \n";
		$str .= "  
\n";
    return $str;
  }
	
	public function mailTextSubject()
	{
		return $this->title . " - deadline: ".date("j/n-Y", $this->deadline);
	}
	public function mailTextBody()
	{
		return $this->title . "\n\n" . $this->description . "\n\nDeadline: ".date("j/n-Y", $this->deadline);
	}
  public function Task($id, $title, $deadline, $description, $lastreminder,
											 $reassignable, $userid, $completed)
  {
		$this->id = $id;
    $this->title = $title;
		$this->deadline = $deadline;
		$this->description = $description;
		$this->lastreminder = $lastreminder;
		$this->reassignable = $reassignable;
		$this->userid = $userid;
		$this->completed = $completed;
  }
}
class Tasks {
  private $file;
  public $tasks = array();
  public function show()
  {
    $str = "\n";
    foreach($this->tasks as $task) {
      if($task->completed == false) {
				$str .= $task->show();
      }
    }
    $str .= "
\n";
		$str .= "
\n";
    $str .= "\n";
    foreach($this->tasks as $task) {
      if($task->completed == true) {
				$str .= $task->show();
      }
    }
    $str .= "
\n";
    return $str;
  }
	
  public function add($task) {
		$this->tasks[$task->id] = $task;
  }
	
	public function getNextID()
	{
		$id = 0;
    foreach($this->tasks as $task) {
			if($task->id > $id) $id = $task->id;
    }
		return $id + 1;
	}
  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "\n");
    fwrite($fp, "\n");
    foreach($this->tasks as $task) {
			$task->write($fp);
    }
    fwrite($fp, "\n");
    fclose($fp);
  }
	
  private function read()
  {
		$dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $params = $dom->getElementsByTagName('task');
    foreach($params as $param) {
      $description = "";
      $dess = $param->getElementsByTagName('description');
      foreach($dess as $des) {
				$description = $des->textContent;
      }
      $task = new Task($param->getAttribute('id'),
											 $param->getAttribute('title'),
											 $param->getAttribute('deadline'),
											 $description,
											 $param->getAttribute('lastreminder'),
											 $param->getAttribute('reassignable') == "true",
											 $param->getAttribute('userid'),
											 $param->getAttribute('completed') == "true");
      $this->add($task);
    }
  }
  public function Tasks($file)
  {
    $this->file = $file;
    if(file_exists($file)) $this->read();
  }
}
function tasks_init()
{
  global $DATA_DIR;
  return new Tasks($DATA_DIR . "/tasks.xml");
}
function sendMail($id, $new, $reassigned)
{
	global $tasks, $users;
	$task = $tasks->tasks[$id];
	$task->lastreminder = time();
	$tasks->write();
	$user = $users->getUser($task->userid);
	$prefix = "Task Reminder - ";
	if($new) $prefix = "New Task - ";
	if($reassigned) $prefix = "Reassigned Task - ";
	$email = $user->email;
	$subject = $prefix . $task->mailTextSubject();
	$body = $task->mailTextBody();
	send($email, $subject, $body);
}
function tasks()
{
  global $action, $GLOBALS;
  $tasks = tasks_init();
  if($action == "tick") {
    $now = time();
    $h = (60 * 60);
    $d = ($h * 24);
    $mininterval = $d - $h * 4;
    $maxinterval = $d * 7 - $h * 4;
    $overdueinterval = $h * 7;
    foreach($tasks->tasks as $task) {
      
      if($task->completed) continue; // no need for reminding
      
      if(($now > $task->deadline) && (($now - $task->lastreminder) > $overdueinterval)) {
        // Deadline is overdue, and it has been more than $overdueinterval since last reminder.
        sendMail($task->id, false, false);
      } elseif((($task->deadline - $now) > $maxinterval) && (($now - $task->lastreminder) > $maxinterval)) {
        // Deadline is a long way off, but it has been $maxinterval since last reminder.
        sendMail($task->id, false, false);
      } elseif((($task->deadline - $now) > $mininterval) && (($now - $task->lastreminder) > $mininterval)) {
        // Deadline is near, and it has been $mininterval since last reminder.
        sendMail($task->id, false, false);
      }
	}
  }
  if($action == "update") {
    $reassigned = false;
    $id = $GLOBALS['id'];
    $task = $tasks->tasks[$id];
    if(isset($GLOBALS['completed'])) $task->completed = $GLOBALS['completed'] == "on";
    if(isset($GLOBALS['userid'])) {
      $reassigned = $task->userid != $GLOBALS['userid'];
      $task->userid = $GLOBALS['userid'];
    }
    $tasks->write();
    
    if($reassigned) sendMail($id, false, true);
  }
  
  if($action == "add") {
    
    $deadline = strtotime($GLOBALS['deadline_year']."-".
                          $GLOBALS['deadline_month']."-".
                          $GLOBALS['deadline_day']);
    
    $task = new Task($tasks->getNextID(),
                     stripslashes($GLOBALS['title']),
                     $deadline,
                     stripslashes($GLOBALS['description']),
                     0,
                     $GLOBALS['reassignable'] == "on",
                     $GLOBALS['userid'],
                     false);
    $tasks->add($task);
    $tasks->write();
    
    sendMail($task->id, true, false);
  }
  $str .= "\n";
  $str .= "
\n";
  $str .= $tasks->show();
  return $str;
}
?>