Location: PHPKode > scripts > Prioritexter > Tasks.php
<?php
class Tasks {

  var $tasks, $show_active;

  function Tasks() {
    global $db, $task_categories;

    $this->show_active=TRUE;

    // init urgency in case no values assigned
    $urgency = Array();

    // seed random number generator with the current hour
    $this->seedRand();

    // NOTE: wait, only where type is task!? what about project,
    // at least!?
    $result = $db->simpleSelect("task", "type='task'");
    while($myrow = mysql_fetch_array($result)) {
      $adjusted_due = Date::logicalTime($myrow["due"]+$myrow["adjustment"]);
      $time_left = $adjusted_due - Date::logicalTime();
      $days_left = $time_left/86400;
      $urgency[$myrow["task_id"]] = 
	 $this->urgency($myrow["priority"],$days_left);
      $task_by_id[$myrow["task_id"]] = new Task(
	$myrow["task_id"], $urgency[$myrow["task_id"]], 
	Html::safe_html($myrow["name"]),
	$myrow["priority"], 
	$myrow["created"], $myrow["due"], $myrow["adjustment"], 
	Html::safe_html($myrow["notes"]), $myrow["type"], 
	$myrow["active"], $myrow["finished"],
	$task_categories->task_categories[$myrow["task_id"]]);
    }
    arsort($urgency);
    reset($urgency);
    // make sure it's an array, even if empty
    $this->tasks = array();
    foreach ($urgency as $key=>$value) {
      $this->tasks[] = $task_by_id[$key];
    }
  }

  function toStr($types=array(),$cats=array()) {
    $str = "";
    $table_content = "";

    // about continue: remember that it means break!!!
    foreach($this->tasks as $task) {
      //echo $task->type;
      // only show ACTIVE tasks (or, if flag is set, only show 
      // INACTIVE tasks
      if ($task->active != $this->show_active) continue;
      // if the array of acceptable types isn't blank,
      //  and the types in it do NOT include THIS task's type, 
      //  then go to the next task...
      if ((sizeof($types)!=0) && 
	  (array_search($task->type,$types) === FALSE)) continue;
      // ... ditto for cats...
      if ((sizeof($cats)!=0) && 
	  (sizeof(array_intersect($task->categories,
				  $cats))==0))
	continue;

      //      echo "adding content";
      //     echo $task->type;
      
      // if we got here, the type and category check out OK...
      // so add the task row to the output string.
      $adjusted_due = $task->due + $task->adjustment;
      $table_content .= Indenter::indent("<tr><td>$task->name</td>","++")
	 . Indenter::indent("<td>$task->priority</td>")
	 . Indenter::indent("<td><font color=\"")
	 . Date::diffToColor(Date::logicalTime(),$adjusted_due)
	 . "\">"
	 . Date::diffToEnglish(Date::logicalTime(),$adjusted_due)
	 . "</font></td>";
      $params = Array();
      $params["action"]="doneTask";
      $params["task_id"]=$task->task_id;
      $table_content .= Indenter::indent("<td>")
	 . Http::imageLink("checkIcon", $params, "Mark as done")
	 . "</td>";
      $params["action"]="postponeTask";
      $table_content .= Indenter::indent("<td>")
	 . Http::imageLink("alarmIcon", $params, "Give me more time")
	 . "</td>";
      unset($params["action"]);
      $params["page"]="confirmdelete";
      $table_content .= Indenter::indent("<td>")
	 . Http::imageLink("deleteIcon", $params, "Delete task")
	 . "</td>";
      unset($params["action"]);
      $params["page"]="edittask";
      $table_content .= Indenter::indent("<td>")
	 . Http::imageLink("editIcon", $params, "Edit task")
	 . "</td>";
      $table_content .= Indenter::indent("</tr>","--");
    }
    
    if (strlen($table_content)>0) {
      $str .= Indenter::indent("<table border=0 cellspacing=0 ","++")
	 . "cellpadding=3>"
	 . Indenter::indent("<tr>","++")
	 . Indenter::indent("<td class=emphasized width=200>","++");
      $str .= "Tasks: ";
      if (sizeof($types)>0) {
	foreach ($cats as $category)
	  $str .= Text::capitalizeFirstChar($category).", "; 
	$str = substr($str,0,strlen($str)-2);
      } 
      $str .= Indenter::indent("</td>","--")
	 . Indenter::indent("<td class=emphasized>Priority</td>")
	 . Indenter::indent("<td class=emphasized>Due date</td>")
	 . Indenter::indent("<td class=emphasized></td>")
	 . Indenter::indent("<td class=emphasized></td>")
	 . Indenter::indent("<td class=emphasized></td>")
	 . Indenter::indent("<td class=emphasized></td>")
	 . Indenter::indent("</tr>","--");
      $str .= $table_content;
      $str .= Indenter::indent("</table>","--");  
    }
    else { // if there was NO content, return false.
      //      echo "here";
      return FALSE;
    }

    return $str;
  }
  
  function eachCatToStr($types=array()) {
    global $categories;
    $hr = "\n<br><hr><p>\n";

    foreach($categories->categories as $category) {
      $this_cat = $this->toStr(array("task","project"),array($category));
      if ($this_cat !== FALSE)
	$str .= $this_cat . $hr;
    }

    if (strlen($str)>0) $str=substr($str,0,strlen($str)-strlen($hr));
    return $str;
  }


  function display($types=array(),$categories=array()) {
    echo $this->toStr($types,$categories);
  }

  function displayType($type) {
    echo $this->toStr(array($type));
  }

  function urgency($priority,$days_left) {
    $days_left = floor($days_left);
    // .015 be 2???
    $priority_urgency = pow(1+PRIORITY_IMPORTANCE*.02,$priority-10)*10;
    // NOTE: this may be WRONG approach to due date importance ...
    // why do i want to make 5 days so little more potent thatn 6 months?
    $due_date_urgency = pow(1+DUE_DATE_IMPORTANCE*.04,-1*($days_left+2))
       + 2 - DUE_DATE_IMPORTANCE*.2;
    $rand = rand(1,10);
    $log = log(.505-(.05*$rand),10);
      $random_urgency = -1*$log*pow(RANDOMNESS,2)/6;
    //    echo "$rand $log $random_urgency <p>";
    $urgency = ($priority_urgency+5) * $due_date_urgency + $random_urgency;

    return (round($urgency*100)/100);
 }

  // seed rand depending on hour of day
  function seedRand() {
    srand(floor(time()/3600));
  }
  
}
?>
Return current item: Prioritexter