home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / dotproject / modules / tasks / gantt.php < prev    next >
Encoding:
PHP Script  |  2004-02-02  |  6.3 KB  |  244 lines

  1. <?php /* TASKS $Id: gantt.php,v 1.24 2004/02/02 05:13:51 robertoibarra Exp $ */
  2.  
  3. /*
  4.  * Gantt.php - by J. Christopher Pereira
  5.  * TASKS $Id: gantt.php,v 1.24 2004/02/02 05:13:51 robertoibarra Exp $
  6.  */
  7.  
  8. error_reporting( E_ALL );    // this only for development testing
  9.  
  10. include ("{$AppUI->cfg['root_dir']}/lib/jpgraph/src/jpgraph.php");
  11. include ("{$AppUI->cfg['root_dir']}/lib/jpgraph/src/jpgraph_gantt.php");
  12.  
  13. $project_id = defVal( @$_REQUEST['project_id'], 0 );
  14. $f = defVal( @$_REQUEST['f'], 0 );
  15.  
  16. // pull valid projects and their percent complete information
  17. $psql = "
  18. SELECT project_id, project_color_identifier, project_name
  19. FROM permissions, projects
  20. LEFT JOIN tasks t1 ON projects.project_id = t1.task_project
  21. WHERE project_active <> 0
  22.     AND permission_user = $AppUI->user_id
  23.     AND permission_value <> 0
  24.     AND (
  25.         (permission_grant_on = 'all')
  26.         OR (permission_grant_on = 'projects' AND permission_item = -1)
  27.         OR (permission_grant_on = 'projects' AND permission_item = project_id)
  28.         )
  29. GROUP BY project_id
  30. ORDER BY project_name
  31. ";
  32. // echo "<pre>$psql</pre>";
  33. $prc = db_exec( $psql );
  34. echo db_error();
  35. $pnums = db_num_rows( $prc );
  36.  
  37. $projects = array();
  38. for ($x=0; $x < $pnums; $x++) {
  39.     $z = db_fetch_assoc( $prc );
  40.     $projects[$z["project_id"]] = $z;
  41. }
  42.  
  43. // get any specifically denied tasks
  44. $dsql = "
  45. SELECT task_id
  46. FROM tasks, permissions
  47. WHERE permission_user = $AppUI->user_id
  48.     AND permission_grant_on = 'tasks'
  49.     AND permission_item = task_id
  50.     AND permission_value = 0
  51. ";
  52. $drc = db_exec( $dsql );
  53. echo db_error();
  54. $deny = array();
  55. while ($row = db_fetch_row( $drc )) {
  56.         $deny[] = $row[0];
  57. }
  58.  
  59. // pull tasks
  60.  
  61. $select = "
  62. tasks.task_id, task_parent, task_name, task_start_date, task_end_date, task_duration, task_duration_type,
  63. task_priority, task_percent_complete, task_order, task_project, task_milestone,
  64. project_name
  65. ";
  66.  
  67. $from = "tasks";
  68. $join = "LEFT JOIN projects ON project_id = task_project";
  69. $where = "project_active <> 0".($project_id ? "\nAND task_project = $project_id" : '');
  70.  
  71. switch ($f) {
  72.     case 'all':
  73.         $where .= "\nAND task_status > -1";
  74.         break;
  75.     case 'myproj':
  76.         $where .= "\nAND task_status > -1\n    AND project_owner = $AppUI->user_id";
  77.         break;
  78.     case 'mycomp':
  79.         $where .= "\nAND task_status > -1\n    AND project_company = $AppUI->user_company";
  80.         break;
  81.     case 'myinact':
  82.         $from .= ", user_tasks";
  83.         $where .= "
  84.     AND task_project = projects.project_id
  85.     AND user_tasks.user_id = $AppUI->user_id
  86.     AND user_tasks.task_id = tasks.task_id
  87. ";
  88.         break;
  89.     default:
  90.         $from .= ", user_tasks";
  91.         $where .= "
  92.     AND task_status > -1
  93.     AND task_project = projects.project_id
  94.     AND user_tasks.user_id = $AppUI->user_id
  95.     AND user_tasks.task_id = tasks.task_id
  96. ";
  97.         break;
  98. }
  99.  
  100. $tsql = "SELECT $select FROM $from $join WHERE $where ORDER BY project_id, task_start_date";
  101. ##echo "<pre>$tsql</pre>".mysql_error();##
  102.  
  103. $ptrc = db_exec( $tsql );
  104. $nums = db_num_rows( $ptrc );
  105. echo db_error();
  106. $orrarr[] = array("task_id"=>0, "order_up"=>0, "order"=>"");
  107.  
  108. //pull the tasks into an array
  109. for ($x=0; $x < $nums; $x++) {
  110.     $row = db_fetch_assoc( $ptrc );
  111.     
  112.     // calculate or set blank task_end_date if unset
  113.     if($row["task_end_date"] == "0000-00-00 00:00:00") {
  114.         if($row["task_duration"]) {
  115.             $row["task_end_date"] = db_unix2dateTime ( db_dateTime2unix( $row["task_start_date"] ) + SECONDS_PER_DAY * convert2days( $row["task_duration"], $row["task_duration_type"] ) );
  116.         } else {
  117.             $row["task_end_date"] = "";
  118.         }
  119.     }
  120.         
  121.     $projects[$row['task_project']]['tasks'][] = $row;
  122. }
  123.  
  124. $width = dPgetParam( $_GET, 'width', 600 );
  125. $start_date = dPgetParam( $_GET, 'start_date', 0 );
  126. $end_date = dPgetParam( $_GET, 'end_date', 0 );
  127.  
  128. $count = 0;
  129. $graph = new GanttGraph($width);
  130. $graph->ShowHeaders(GANTT_HYEAR | GANTT_HMONTH | GANTT_HDAY | GANTT_HWEEK);
  131. //$graph->ShowHeaders(GANTT_HYEAR | GANTT_HMONTH | GANTT_HDAY);
  132. $graph->SetFrame(false);
  133. $graph->SetBox(true, array(0,0,0), 2);
  134. $graph->scale->week->SetStyle(WEEKSTYLE_FIRSTDAY);
  135.  
  136. $jpLocale = $AppUI->getConfig( 'jpLocale' );
  137. if ($jpLocale) {
  138.     $graph->scale->SetDateLocale( $jpLocale );
  139. }
  140.  
  141. if ($start_date && $end_date) {
  142.     $graph->SetDateRange( $start_date, $end_date );
  143. }
  144.  
  145. //This kludgy function echos children tasks as threads
  146.  
  147. function showtask( &$a, $level=0 ) {
  148.     /* Add tasks to gantt chart */
  149.  
  150.     global $gantt_arr;
  151.  
  152.     $gantt_arr[] = array($a, $level);
  153.  
  154. }
  155.  
  156. function findchild( &$tarr, $parent, $level=0 ){
  157.     GLOBAL $projects;
  158.     $level = $level+1;
  159.     $n = count( $tarr );
  160.     for ($x=0; $x < $n; $x++) {
  161.         if($tarr[$x]["task_parent"] == $parent && $tarr[$x]["task_parent"] != $tarr[$x]["task_id"]){
  162.             showtask( $tarr[$x], $level );
  163.             findchild( $tarr, $tarr[$x]["task_id"], $level);
  164.         }
  165.     }
  166. }
  167.  
  168. reset($projects);
  169. $p = &$projects[$project_id];
  170. $tnums = count( $p['tasks'] );
  171.  
  172. for ($i=0; $i < $tnums; $i++) {
  173.     $t = $p['tasks'][$i];
  174.     if ($t["task_parent"] == $t["task_id"]) {
  175.         showtask( $t );
  176.         findchild( $p['tasks'], $t["task_id"] );
  177.     }
  178. }
  179.  
  180. $hide_task_groups = false;
  181.  
  182. if($hide_task_groups) {
  183.     for($i = 0; $i < count($gantt_arr); $i ++ ) {
  184.         // remove task groups
  185.         if($i != count($gantt_arr)-1 && $gantt_arr[$i + 1][1] > $gantt_arr[$i][1]) {
  186.             // it's not a leaf => remove
  187.             array_splice($gantt_arr, $i, 1);
  188.             continue;
  189.         }
  190.     }
  191. }
  192.  
  193. $row = 0;
  194. for($i = 0; $i < count(@$gantt_arr); $i ++ ) {
  195.  
  196.     $a = $gantt_arr[$i][0];
  197.     $level = $gantt_arr[$i][1];
  198.  
  199.     if($hide_task_groups) $level = 0;
  200.  
  201.     $name = strlen( utf8_decode($a["task_name"]) ) > 25 ? substr( utf8_decode($a["task_name"]), 0, 22 ).'...' : utf8_decode($a["task_name"]) ;
  202.     $start = substr($a["task_start_date"], 0, 10);
  203.     $end = substr($a["task_end_date"], 0, 10);
  204.     $progress = $a["task_percent_complete"];
  205.     $flags = ($a["task_milestone"]?"m":"");
  206.  
  207.     $cap = "";
  208.     if(!$start || $start == "0000-00-00"){
  209.         $start = !$end ? date("Y-m-d") : $end;
  210.         $cap .= "(no start date)";
  211.     }
  212.     
  213.     if(!$end) {
  214.         $end = $start;
  215.         $cap .= " (no end date)";
  216.     } else {
  217.         $cap = "";
  218.     }
  219.     
  220.     if($flags == "m") {
  221.         $bar = new MileStone($row++, $name, $start, $start);
  222.     } else {
  223.         $bar = new GanttBar($row++, str_repeat("   ", $level) . $name, $start, $end, $cap);
  224.         $bar->progress->Set($progress/100);
  225.     }
  226.  
  227.     $sql = "SELECT dependencies_task_id FROM task_dependencies WHERE dependencies_req_task_id=" . $a["task_id"];
  228.     $query = db_exec($sql);
  229.  
  230.     while($dep = db_fetch_assoc($query)) {
  231.         // find row num of dependencies
  232.         for($d = 0; $d < count($gantt_arr); $d++ ) {
  233.             if($gantt_arr[$d][0]["task_id"] == $dep["dependencies_task_id"]) {
  234.                 $bar->SetConstrain($d, CONSTRAIN_ENDSTART);
  235.             }
  236.         }
  237.     }
  238.  
  239.     $graph->Add($bar);
  240. }
  241.  
  242. $graph->Stroke();
  243. ?>
  244.