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

  1. <?php /* TASKS $Id: tasks.php,v 1.51 2004/01/31 21:58:49 gnome444 Exp $ */
  2. GLOBAL $m, $a, $project_id, $f, $min_view, $query_string, $durnTypes;
  3. GLOBAL $task_sort_item1, $task_sort_type1, $task_sort_order1;
  4. GLOBAL $task_sort_item2, $task_sort_type2, $task_sort_order2;
  5. /*
  6.     tasks.php
  7.  
  8.     This file contains common task list rendering code used by
  9.     modules/tasks/index.php and modules/projects/vw_tasks.php
  10.  
  11.     External used variables:
  12.  
  13.     * $min_view: hide some elements when active (used in the vw_tasks.php)
  14.     * $project_id
  15.     * $f
  16.     * $query_string
  17. */
  18.  
  19. if (empty($query_string)) {
  20.     $query_string = "?m=$m&a=$a";
  21. }
  22.  
  23. $durnTypes = dPgetSysVal( 'TaskDurationType' );
  24.  
  25. $task_project = intval( dPgetParam( $_GET, 'task_project', null ) );
  26. $task_id = intval( dPgetParam( $_GET, 'task_id', null ) );
  27.  
  28. $task_sort_item1 = dPgetParam( $_GET, 'task_sort_item1', '' );
  29. $task_sort_type1 = dPgetParam( $_GET, 'task_sort_type1', '' );
  30. $task_sort_item2 = dPgetParam( $_GET, 'task_sort_item2', '' );
  31. $task_sort_type2 = dPgetParam( $_GET, 'task_sort_type2', '' );
  32. $task_sort_order1 = intval( dPgetParam( $_GET, 'task_sort_order1', 0 ) );
  33. $task_sort_order2 = intval( dPgetParam( $_GET, 'task_sort_order2', 0 ) );
  34.  
  35. $where = '';
  36. $join = winnow( 'projects', 'project_id', $where );
  37.  
  38. // pull valid projects and their percent complete information
  39. $psql = "
  40. SELECT project_id, project_color_identifier, project_name,
  41.     COUNT(t1.task_id) as total_tasks,
  42.     SUM(t1.task_duration*t1.task_percent_complete)/SUM(t1.task_duration) as project_percent_complete
  43. FROM projects
  44. LEFT JOIN tasks t1 ON projects.project_id = t1.task_project" .
  45. $join .
  46. "WHERE $where GROUP BY project_id
  47. ORDER BY project_name
  48. ";
  49.  
  50. // echo "<pre>$psql</pre>";
  51.  
  52. $prc = db_exec( $psql );
  53. echo db_error();
  54.  
  55. $projects = array();
  56. while ($row = db_fetch_assoc( $prc )) {
  57.     $projects[$row["project_id"]] = $row;
  58. }
  59.  
  60. // pull tasks
  61. $select = "
  62. tasks.task_id, task_parent, task_name, task_start_date, task_end_date,
  63. task_priority, task_percent_complete, task_duration, task_duration_type, task_project,
  64. task_description, task_owner, user_username, task_milestone
  65. ";
  66.  
  67. $from = "tasks";
  68. $join = "LEFT JOIN projects ON project_id = task_project";
  69. $join .= " LEFT JOIN users as usernames ON task_owner = usernames.user_id";
  70. $where = $project_id ? "\ntask_project = $project_id" : 'project_active != 0';
  71.  
  72. switch ($f) {
  73.     case 'all':
  74.         break;
  75.     case 'children':
  76.         $where .= "\n    AND task_parent = $task_id AND task_id != $task_id";    
  77.         break;
  78.     case 'myproj':
  79.         $where .= "\n    AND project_owner = $AppUI->user_id";
  80.         break;
  81.     case 'mycomp':
  82.         $where .= "\n    AND project_company = $AppUI->user_company";
  83.         break;
  84.     case 'myunfinished':
  85.         $from .= ", user_tasks";
  86.         // This filter checks all tasks that are not already in 100% 
  87.         // and the project is not on hold nor completed
  88.         $where .= "
  89.                     AND task_project             = projects.project_id
  90.                     AND user_tasks.user_id       = $AppUI->user_id
  91.                     AND user_tasks.task_id       = tasks.task_id
  92.                     AND task_percent_complete    < '100'
  93.                     AND projects.project_active  = '1'
  94.                     AND projects.project_status != '4'
  95.                     AND projects.project_status != '5'";
  96.         break;
  97.     case 'allunfinished':
  98.         $from .= ", user_tasks";
  99.         $where .= "
  100.                     AND task_project             = projects.project_id
  101.                     AND user_tasks.task_id       = tasks.task_id
  102.                     AND task_percent_complete    < '100'
  103.                     AND projects.project_active  = '1'
  104.                     AND projects.project_status != '4'
  105.                     AND projects.project_status != '5'";
  106.         break;
  107.     case 'unassigned':
  108.         $join .= "\n LEFT JOIN user_tasks ON tasks.task_id = user_tasks.task_id";
  109.         $where .= "
  110.                     AND task_status > -1
  111.                     AND user_tasks.task_id IS NULL";
  112.         break;
  113.     default:
  114.         $from .= ", user_tasks";
  115.         $where .= "
  116.     AND task_project = projects.project_id
  117.     AND user_tasks.user_id = $AppUI->user_id
  118.     AND user_tasks.task_id = tasks.task_id";
  119.         break;
  120. }
  121.  
  122. if ( $min_view )
  123.     $task_status = intval( dPgetParam( $_GET, 'task_status', null ) );
  124. else
  125.     $task_status = intval( $AppUI->getState( 'inactive' ) );
  126.  
  127. $where .= "\n    AND task_status = '$task_status'";
  128.  
  129. // filter tasks considering task and project permissions
  130. $projects_filter = '';
  131. $tasks_filter = '';
  132.  
  133. // TODO: Enable tasks filtering
  134.  
  135. $join .= winnow( 'projects', 'tasks.task_project', $projects_filter, 'perm1' );
  136. $join .= winnow( 'tasks', 'tasks.task_id', $tasks_filter, 'perm2' );
  137. $where .= " AND ( ($projects_filter) )";
  138. // echo "<pre>$where</pre>";
  139.  
  140. // Filter by company
  141. if ( ! $min_view && $f2 != 'all' ) {
  142.      $join .= "\nLEFT JOIN companies ON company_id = projects.project_company";
  143.          $where .= "\nAND company_id = $f2  ";
  144. }
  145.  
  146. $tsql = "SELECT $select FROM $from $join WHERE $where" .
  147.   "\nORDER BY project_id, task_start_date";
  148.  
  149. //echo "<pre>$tsql</pre>";
  150.  
  151. $ptrc = db_exec( $tsql );
  152. $nums = db_num_rows( $ptrc );
  153. echo db_error();
  154.  
  155. //pull the tasks into an array
  156.  
  157. for ($x=0; $x < $nums; $x++) {
  158.     $row = db_fetch_assoc( $ptrc );
  159.     $projects[$row['task_project']]['tasks'][] = $row;
  160. }
  161.  
  162. //This kludgy function echos children tasks as threads
  163.  
  164. if (! function_exists('showtask') ) {
  165. function showtask( &$a, $level=0 ) {
  166.     global $AppUI, $done, $query_string, $durnTypes;
  167.     $df = $AppUI->getPref( 'SHDATEFORMAT' );
  168.     $done[] = $a['task_id'];
  169.  
  170.     $start_date = intval( $a["task_start_date"] ) ? new CDate( $a["task_start_date"] ) : null;
  171.     $end_date = intval( $a["task_end_date"] ) ? new CDate( $a["task_end_date"] ) : null;
  172.  
  173.     $s = "\n<tr>";
  174. // edit icon
  175.     $s .= "\n\t<td>";
  176.     $canEdit = !getDenyEdit( 'tasks', $a["task_id"] );
  177.     if ($canEdit) {
  178.         $s .= "\n\t\t<a href=\"?m=tasks&a=addedit&task_id={$a['task_id']}\">"
  179.             . "\n\t\t\t".'<img src="./images/icons/pencil.gif" alt="'.$AppUI->_( 'Edit Task' ).'" border="0" width="12" height="12">'
  180.             . "\n\t\t</a>";
  181.     }
  182.     $s .= "\n\t</td>";
  183. // percent complete
  184.     $s .= "\n\t<td align=\"right\">".intval( $a["task_percent_complete"] ).'%</td>';
  185. // priority
  186.     $s .= "\n\t<td>";
  187.     if ($a["task_priority"] < 0 ) {
  188.         $s .= "\n\t\t<img src=\"./images/icons/low.gif\" width=13 height=16>";
  189.     } else if ($a["task_priority"] > 0) {
  190.         $s .= "\n\t\t<img src=\"./images/icons/" . $a["task_priority"] .'.gif" width=13 height=16>';
  191.     }
  192.     $s .= "\n\t</td>";
  193. // dots
  194.     $s .= '<td width="90%">';
  195.     for ($y=0; $y < $level; $y++) {
  196.         if ($y+1 == $level) {
  197.             $s .= '<img src="./images/corner-dots.gif" width="16" height="12" border="0">';
  198.         } else {
  199.             $s .= '<img src="./images/shim.gif" width="16" height="12"  border="0">';
  200.         }
  201.     }
  202. // name link
  203.     $alt = htmlspecialchars( $a["task_description"] );
  204.  
  205.     if ($a["task_milestone"] > 0) {
  206.         $s .= ' <a href="./index.php?m=tasks&a=view&task_id=' . $a["task_id"] . '" title="' . $alt . '"><b>' . $a["task_name"] . '</b></a></td>';
  207.     } else {
  208.         $s .= ' <a href="./index.php?m=tasks&a=view&task_id=' . $a["task_id"] . '" title="' . $alt . '">' . $a["task_name"] . '</a></td>';
  209.     }
  210. // task owner
  211.     $s .= '<td nowrap="nowrap" align=center>'. $a["user_username"] .'</td>';
  212. // start date
  213.     $s .= '<td nowrap="nowrap">'.($start_date ? $start_date->format( $df ) : '-').'</td>';
  214. // duration or milestone
  215.     $s .= '<td align="right">';
  216.     if ( $a['task_milestone'] == '0' ) {
  217.         $s .= $a['task_duration'] . ' ' . $AppUI->_( $durnTypes[$a['task_duration_type']] );
  218.     } else {
  219.         $s .= $AppUI->_("Milestone");
  220.     }
  221.     $s .= '</td>';
  222. // end date
  223.     $s .= '<td nowrap="nowrap">'.($end_date ? $end_date->format( $df ) : '-').'</td>';
  224.  
  225.     $s .= '</tr>';
  226.  
  227.     echo $s;
  228. }
  229.  
  230. }
  231.  
  232. if (! function_exists('findchild') ) {
  233. function findchild( &$tarr, $parent, $level=0 ){
  234.     GLOBAL $projects;
  235.     $level = $level+1;
  236.     $n = count( $tarr );
  237.     for ($x=0; $x < $n; $x++) {
  238.         if($tarr[$x]["task_parent"] == $parent && $tarr[$x]["task_parent"] != $tarr[$x]["task_id"]){
  239.             showtask( $tarr[$x], $level );
  240.             findchild( $tarr, $tarr[$x]["task_id"], $level);
  241.         }
  242.     }
  243. }
  244. }
  245.  
  246. /* please throw this in an include file somewhere, its very useful */
  247.  
  248. function array_csort()   //coded by Ichier2003
  249. {
  250.     $args = func_get_args();
  251.     $marray = array_shift($args);
  252.     
  253.     if ( empty( $marray )) return array();
  254.     
  255.     $i = 0;
  256.     $msortline = "return(array_multisort(";
  257.     $sortarr = array();
  258.     foreach ($args as $arg) {
  259.         $i++;
  260.         if (is_string($arg)) {
  261.             foreach ($marray as $row) {
  262.                 $sortarr[$i][] = $row[$arg];
  263.             }
  264.         } else {
  265.             $sortarr[$i] = $arg;
  266.         }
  267.         $msortline .= "\$sortarr[".$i."],";
  268.     }
  269.     $msortline .= "\$marray));";
  270.  
  271.     eval($msortline);
  272.     return $marray;
  273. }
  274.  
  275. function sort_by_item_title( $title, $item_name, $item_type )
  276. {
  277.     global $AppUI,$project_id,$min_view,$m;
  278.     global $task_sort_item1,$task_sort_type1,$task_sort_order1;
  279.     global $task_sort_item2,$task_sort_type2,$task_sort_order2;
  280.  
  281.     if ( $task_sort_item2 == $item_name ) $item_order = $task_sort_order2;
  282.     if ( $task_sort_item1 == $item_name ) $item_order = $task_sort_order1;
  283.  
  284.     if ( isset( $item_order ) ) {
  285.         if ( $item_order == SORT_ASC )
  286.             echo '<img src="./images/icons/low.gif" width=13 height=16>';
  287.         else
  288.             echo '<img src="./images/icons/1.gif" width=13 height=16>';
  289.     } else
  290.         $item_order = SORT_DESC;
  291.  
  292.     /* flip the sort order for the link */
  293.     $item_order = ( $item_order == SORT_ASC ) ? SORT_DESC : SORT_ASC;
  294.     if ( $m == 'tasks' )
  295.         echo '<a href="./index.php?m=tasks';
  296.     else
  297.         echo '<a href="./index.php?m=projects&a=view&project_id='.$project_id;
  298.  
  299.     echo '&task_sort_item1='.$item_name;
  300.     echo '&task_sort_type1='.$item_type;
  301.     echo '&task_sort_order1='.$item_order;
  302.     if ( $task_sort_item1 == $item_name ) {
  303.         echo '&task_sort_item2='.$task_sort_item2;
  304.         echo '&task_sort_type2='.$task_sort_type2;
  305.         echo '&task_sort_order2='.$task_sort_order2;
  306.     } else {
  307.         echo '&task_sort_item2='.$task_sort_item1;
  308.         echo '&task_sort_type2='.$task_sort_type1;
  309.         echo '&task_sort_order2='.$task_sort_order1;
  310.     }
  311.     echo '" class="hdr">';
  312.     
  313.     echo $AppUI->_($title);
  314.     
  315.     echo '</a>';
  316. }
  317.  
  318. ?>
  319.  
  320. <table width="100%" border="0" cellpadding="2" cellspacing="1" class="tbl">
  321. <tr>
  322.     <th width="10"> </th>
  323.     <th width="20"><?php echo $AppUI->_('Work');?></th>
  324.     <th width="15" align="center"> </th>
  325.     <th width="200"><?php sort_by_item_title( 'Task Name', 'task_name', SORT_STRING );?></th>
  326.     <th nowrap="nowrap"><?php sort_by_item_title( 'Task Creator', 'user_username', SORT_STRING );?></th>
  327.     <th nowrap="nowrap"><?php sort_by_item_title( 'Start Date', 'task_start_date', SORT_NUMERIC );?></th>
  328.     <th nowrap="nowrap"><?php sort_by_item_title( 'Duration', 'task_duration', SORT_NUMERIC );?>  </th>
  329.     <th nowrap="nowrap"><?php sort_by_item_title( 'Finish Date', 'task_end_date', SORT_NUMERIC );?></th>
  330.  
  331. </tr>
  332. <?php
  333. //echo '<pre>'; print_r($projects); echo '</pre>';
  334. reset( $projects );
  335. foreach ($projects as $k => $p) {
  336.     $tnums = count( @$p['tasks'] );
  337. // don't show project if it has no tasks
  338.     if ($tnums) {
  339. //echo '<pre>'; print_r($p); echo '</pre>';
  340.         if (!$min_view) {
  341. ?>
  342. <tr>
  343.     <td>
  344.         <a href="index.php?m=tasks&f=<?php echo $f;?>&project_id=<?php echo $project_id ? 0 : $k;?>">
  345.             <img src="./images/icons/<?php echo $project_id ? 'expand.gif' : 'collapse.gif';?>" width="16" height="16" border="0" alt="<?php echo $project_id ? 'show other projects' : 'show only this project';?>">
  346.         </a>
  347.     </td>
  348.     <td colspan="8">
  349.         <table width="100%" border="0">
  350.         <tr>
  351.             <td nowrap style="border: outset #eeeeee 2px;background-color:#<?php echo @$p["project_color_identifier"];?>">
  352.                 <a href="./index.php?m=projects&a=view&project_id=<?php echo $k;?>">
  353.                 <span style='color:<?php echo bestColor( @$p["project_color_identifier"] ); ?>;text-decoration:none;'><strong><?php echo @$p["project_name"];?></strong></span></a>
  354.             </td>
  355.             <td width="<?php echo (101 - intval(@$p["project_percent_complete"]));?>%">
  356.                 <?php echo (intval(@$p["project_percent_complete"]));?>%
  357.             </td>
  358.         </tr>
  359.         </table>
  360. </tr>
  361. <?php
  362.         }
  363.         global $done;
  364.         $done = array();
  365.         if ( $task_sort_item1 != "" )
  366.         {
  367.             if ( $task_sort_item2 != "" && $task_sort_item1 != $task_sort_item2 )
  368.                 $p['tasks'] = array_csort($p['tasks'], $task_sort_item1, $task_sort_order1, $task_sort_type1
  369.                                           , $task_sort_item2, $task_sort_order2, $task_sort_type2 );
  370.             else $p['tasks'] = array_csort($p['tasks'], $task_sort_item1, $task_sort_order1, $task_sort_type1 );
  371.         }
  372.         
  373.         for ($i=0; $i < $tnums; $i++) {
  374.             $t = $p['tasks'][$i];
  375.             if ($t["task_parent"] == $t["task_id"]) {
  376.                 showtask( $t );
  377.                 findchild( $p['tasks'], $t["task_id"] );
  378.             }
  379.         }
  380. // check that any 'orphaned' user tasks are also display
  381.         for ($i=0; $i < $tnums; $i++) {
  382.             if ( !in_array( $p['tasks'][$i]["task_id"], $done )) {
  383.                 showtask( $p['tasks'][$i], 1 );
  384.             }
  385.         }
  386.  
  387.         if($tnums && $AppUI->cfg['enable_gantt_charts'] && !$min_view) { ?>
  388.         <tr>
  389.             <td colspan="8" align="right">
  390.                 <input type="button" class="button" value="<?php echo $AppUI->_('see gantt chart');?>" onclick="javascript:window.location='index.php?m=tasks&a=viewgantt&project_id=<?php echo $k;?>';" />
  391.             </td>
  392.         </tr>
  393.         <?php }
  394.     }
  395. }
  396. ?>
  397. </table>
  398.