db/class_effort.inc.php

00001 <?php if(!function_exists('startedIndexPhp')) { header("location:../index.php"); exit();}
00002 # streber - a php5 based project management system  (c) 2005-2007  / www.streber-pm.org
00003 # Distributed under the terms and conditions of the GPL as stated in lang/license.html
00004 
00024 class Effort extends DbProjectItem
00025 {
00026     public $level;              # level if child of parent-tasks
00027     public $type;
00028     public $effort_status;
00029 
00030     //=== constructor ================================================
00031     function __construct ($id_or_array=NULL)
00032     {
00033         global $effort_fields;
00034         $this->fields= &$effort_fields;
00035 
00036         parent::__construct($id_or_array);
00037         if(!$this->type) {
00038             $this->type= ITEM_EFFORT;
00039         }
00040     }
00041 
00045     static function initFields()
00046     {
00047         global $effort_fields;
00048         $effort_fields=array();
00049         addProjectItemFields(&$effort_fields);
00050     
00051                 foreach(array(
00052                     new FieldInternal(array(    'name'=>'id',
00053                         'default'=>0,
00054                         'in_db_object'=>1,
00055                         'in_db_item'=>1,
00056                     )),
00057                     new FieldInternal(array(    'name'=>'project',
00058                         'default'=>0,
00059                         'in_db_object'=>1,
00060                         'in_db_item'=>1,
00061                     )),
00062                     new FieldString(array(      'name'=>'name',
00063                         'title'=>__('Summary'),
00064                         'tooltip'=>__('optional if tasks linked to this effort'),
00065                     )),
00066     
00067                     new FieldInternal(array(      'name'=>'task',
00068                     )),
00069     
00070                     new FieldDatetime(array(    'name'=>'time_start',
00071                         'title'=> __('Time Start'),
00072                         'default'=>FINIT_NOW
00073                     )),
00074                     new FieldDatetime(array(    'name'=>'time_end',
00075                         'title'=> __('Time End'),
00076                         'default'=>FINIT_NOW
00077                     )),
00078                     new FieldInternal(array(    'name'=>'person',
00079                     )),
00080                     new FieldText(array(        'name'=>'description',
00081                         'title'=>__('Description'),
00082                     )),
00083                     new FieldInternal(array(    'name'=>'as_duration',
00084                         'default'=>0,
00085                     )),
00086                     new FieldOption   (array(    'name'=>'status',
00087                         'title'=>__('Status'),
00088                         'view_in_forms'=>true,
00089                         'default'=>1,
00090                     )),
00091     
00092                 ) as $f) {
00093                     $effort_fields[$f->name]=$f;
00094                 }
00095     }
00096 
00102     static function getById($id)
00103     {
00104         $e= new Effort($id);
00105         if($e->id) {
00106             return $e;
00107         }
00108         return NULL;
00109     }
00110 
00111 
00119     static function getVisibleById($id)
00120     {
00121         if($e= Effort::getById($id)) {
00122             if($p= Project::getById($e->project)) {
00123                 if($p->validateViewItem($e)) {
00124                     return $e;
00125                 }
00126             }
00127         }
00128         return NULL;
00129     }
00130 
00134     static function getEditableById($id)
00135     {
00136         if($e= Effort::getById($id)) {
00137             if($p= Project::getById($e->project)) {
00138                 if($p->validateEditItem($e)) {
00139                     return $e;
00140                 }
00141             }
00142         }
00143         return NULL;
00144     }
00145 
00146 
00147     public static function getDateCreatedLast()
00148     {
00149         global $auth;
00150         $prefix= confGet('DB_TABLE_PREFIX');
00151 
00152         require_once(confGet('DIR_STREBER') . 'db/class_effort.inc.php');
00153         $dbh = new DB_Mysql;
00154         $sth= $dbh->prepare(
00155             "SELECT MAX(e.time_end)
00156                  from {$prefix}item i,  {$prefix}effort e
00157                 WHERE   i.created_by={$auth->cur_user->id}
00158                     AND i.type = '".ITEM_EFFORT."'
00159                     AND e.id = i.id
00160                     AND i.state = 1
00161                 "
00162         )->execute();
00163         $tmp=$sth->fetchall_assoc();
00164         if($tmp) {
00165             $tmp_values=array_values($tmp[0]);
00166             return $tmp_values[0];
00167         }
00168         else {
00169             return false;
00170         }
00171     }
00172 
00173 
00174 
00175 
00176 
00177 
00184     static function &getAll( $args=NULL)
00185     {
00186         global $auth;
00187         $prefix = confGet('DB_TABLE_PREFIX');
00188 
00189         ### default params ###
00190         $project            = NULL;
00191         $person             = NULL;
00192         $order_by           = 'e.time_start DESC';
00193         $visible_only       = true;       # use project rights settings
00194         $alive_only         = true;       # ignore deleted
00195         $task               = NULL;       # for a parent task?
00196         $date_min           = NULL;
00197         $date_max           = NULL;
00198         $search             = NULL;       # search query
00199         $effort_status_min  = NULL;
00200         $effort_status_max  = NULL;
00201 
00202         ### filter params ###
00203         if($args) {
00204             foreach($args as $key=>$value) {
00205                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00206                     trigger_error("unknown parameter",E_USER_NOTICE);
00207                 }
00208                 else {
00209                     $$key= $value;
00210                 }
00211             }
00212         }
00213 
00214         $str_project= $project
00215             ? 'AND i.project=' . intval($project)
00216             : '';
00217 
00218 
00219         $str_project2= $project
00220             ? 'AND upp.project=' . intval($project)
00221             : '';
00222 
00223 
00224         $str_is_alive= $alive_only
00225             ? 'AND i.state=' . ITEM_STATE_OK
00226             : '';
00227 
00228 
00229         $str_date_min= $date_min
00230             ? "AND i.modified >= '" . asCleanString($date_min) . "'"
00231             : '';
00232 
00233         $str_date_max= $date_max
00234             ? "AND i.modified <= ' ". asCleanString($date_max) . "'"
00235             : '';
00236             
00237         $str_status_min = $effort_status_min
00238             ? "AND e.status >= '" . asCleanString($effort_status_min) . "'"
00239             : '';
00240 
00241         $str_status_max = $effort_status_max
00242             ? "AND e.status <= ' ". asCleanString($effort_status_max) . "'"
00243             : '';
00244 
00245 
00246         $str_task= !is_null($task)
00247             ? 'AND e.task=' . intval($task)
00248             : '';
00249 
00250         $str_person= $person
00251             ? 'AND e.person=' . intval($person)
00252             : '';
00253 
00254         if ($auth->cur_user->user_rights & RIGHT_VIEWALL)
00255         {
00256             $str_projectperson = "";
00257         }
00258         else
00259         {
00260             $str_projectperson = "AND upp.person = {$auth->cur_user->id}";
00261         }
00262 
00263         $str_match= $search
00264             ? "AND MATCH (e.description) AGAINST ('". asCleanString($search) ."*' IN BOOLEAN MODE)"
00265         : '';
00266                 
00270         if($visible_only) {
00271             $str_query=
00272             "SELECT DISTINCT i.*, e.* from {$prefix}item i, {$prefix}effort e, {$prefix}project p, {$prefix}projectperson upp
00273             WHERE
00274                     i.type = '".ITEM_EFFORT."'
00275                 $str_project
00276                 $str_projectperson
00277                 $str_project2
00278                 $str_person
00279                 $str_is_alive
00280                 AND ( i.pub_level >= upp.level_view
00281                       OR
00282                       i.created_by = {$auth->cur_user->id}
00283                 )
00284                 AND i.project = p.id
00285 
00286                 AND i.id = e.id
00287                  $str_task
00288                  $str_date_max
00289                  $str_date_min
00290                  $str_status_max
00291                  $str_status_min
00292                  $str_match
00293 
00294             ". getOrderByString($order_by)
00295             ;
00296         }
00297         ### show all ###
00298         else {
00299             $str_query=
00300             "SELECT i.*, e.* from {$prefix}item i, {$prefix}effort e, {$prefix}project p
00301             WHERE
00302                 i.type = '".ITEM_EFFORT."'
00303             $str_project
00304             $str_is_alive
00305 
00306             AND i.project = p.id
00307 
00308             AND i.id = e.id
00309              $str_task
00310              $str_date_max
00311              $str_date_min
00312              $str_match
00313 
00314             ". getOrderByString($order_by)
00315             ;
00316         }
00317 
00318         $dbh = new DB_Mysql;
00319         $sth= $dbh->prepare($str_query);
00320 
00321         $sth->execute("",1);
00322         $tmp=$sth->fetchall_assoc();
00323         
00324         $files=array();
00325         foreach($tmp as $t) {
00326             $effort=new Effort($t);
00327             $efforts[]=$effort;
00328         }
00329         return $efforts;
00330     }
00331     
00332     static function &getSumEfforts($args=NULL)
00333     {
00334         global $auth;
00335         $sum = 0.0;
00336 
00337         $prefix= confGet('DB_TABLE_PREFIX');
00338         require_once(confGet('DIR_STREBER') . 'db/class_effort.inc.php');
00339         $dbh = new DB_Mysql;
00340         
00341         $project = NULL;
00342         $person = NULL;
00343         $task = NULL;
00344         $status = false;
00345         
00346         ### filter params ###
00347         if($args) {
00348             foreach($args as $key=>$value) {
00349                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00350                     trigger_error("unknown parameter",E_USER_NOTICE);
00351                 }
00352                 else {
00353                     $$key= $value;
00354                 }
00355             }
00356         }
00357         
00358         $str_person = $person
00359                     ? "AND e.person = " . $person
00360                     : "";
00361                     
00362         if(!is_null($task)){
00363             $str_task = "AND e.task = " . $task;
00364         }
00365         else{
00366             $str_task = "";
00367         }
00368         
00369         $str_status = $status
00370                     ? "AND e.status = ' ". asCleanString($status) . "'"
00371                     : "";
00372                                                   
00373         if(!is_null($project))
00374          {
00375              $query_str = "SELECT SUM(unix_timestamp(e.time_end) - unix_timestamp(e.time_start)) as sum_efforts
00376                            FROM {$prefix}item i, {$prefix}effort e
00377                            WHERE e.project = $project
00378                            $str_person
00379                            $str_task
00380                            $str_status
00381                            AND i.type = '".ITEM_EFFORT."'
00382                            AND e.id = i.id
00383                            AND i.state = '". ITEM_STATE_OK ."'"; 
00384             $sth = $dbh->prepare($query_str);
00385             $sth->execute("",1);
00386             $tmp = $sth->fetch_row();
00387             if($tmp) {
00388                 $sum += $tmp[0];
00389             }
00390             return $sum;
00391         }
00392         
00393         return sum;
00394     }
00395     
00396     static function &getEffortPersons($args=NULL)
00397     {
00398         $prefix= confGet('DB_TABLE_PREFIX');
00399         require_once(confGet('DIR_STREBER') . 'db/class_effort.inc.php');
00400         require_once(confGet('DIR_STREBER') . 'db/class_projectperson.inc.php');
00401         $dbh = new DB_Mysql;
00402         
00403         $project = NULL;
00404         $effort_status_min = EFFORT_STATUS_NEW;
00405         $effort_status_max = EFFORT_STATUS_BALANCED;
00406         
00407         ### filter params ###
00408         if($args) {
00409             foreach($args as $key=>$value) {
00410                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00411                     trigger_error("unknown parameter",E_USER_NOTICE);
00412                 }
00413                 else {
00414                     $$key= $value;
00415                 }
00416             }
00417         }
00418         
00419         $str_status_min = $effort_status_min
00420             ? "AND e.status >= '" . asCleanString($effort_status_min) . "'"
00421             : '';
00422 
00423         $str_status_max = $effort_status_max
00424             ? "AND e.status <= ' ". asCleanString($effort_status_max) . "'"
00425             : '';
00426         
00427         if($effort_status_min != $effort_status_max){
00428             $str_st = "";
00429         }
00430         else{
00431             $str_st = ", e.status";
00432         }
00433         
00434          if(!is_null($project))
00435          {
00436              $query_str = "SELECT DISTINCT e.person, e.project {$str_st}
00437                            FROM {$prefix}item i, {$prefix}effort e
00438                            WHERE e.project = {$project}
00439                            AND i.type = '".ITEM_EFFORT."'
00440                            AND e.id = i.id
00441                            $str_status_min
00442                            $str_status_max
00443                            AND i.state = '". ITEM_STATE_OK ."';"; 
00444             $sth = $dbh->prepare($query_str);
00445             $sth->execute("",1);
00446             $tmp=$sth->fetchall_assoc();
00447             $efforts=array();
00448             foreach($tmp as $t) {
00449                 $effort=new Effort($t);
00450                 $efforts[]=$effort;
00451             }
00452             return $efforts;
00453         }
00454         
00455         return NULL;
00456     }
00457     
00458     static function &getEffortTasks($args=NULL)
00459     {
00460         $prefix= confGet('DB_TABLE_PREFIX');
00461         require_once(confGet('DIR_STREBER') . 'db/class_effort.inc.php');
00462         require_once(confGet('DIR_STREBER') . 'db/class_projectperson.inc.php');
00463         $dbh = new DB_Mysql;
00464         
00465         $project = NULL;
00466         $effort_status_min = EFFORT_STATUS_NEW;
00467         $effort_status_max = EFFORT_STATUS_BALANCED;
00468         
00469         ### filter params ###
00470         if($args) {
00471             foreach($args as $key=>$value) {
00472                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00473                     trigger_error("unknown parameter",E_USER_NOTICE);
00474                 }
00475                 else {
00476                     $$key= $value;
00477                 }
00478             }
00479         }
00480         
00481         $str_status_min = $effort_status_min
00482             ? "AND e.status >= '" . asCleanString($effort_status_min) . "'"
00483             : '';
00484 
00485         $str_status_max = $effort_status_max
00486             ? "AND e.status <= ' ". asCleanString($effort_status_max) . "'"
00487             : '';
00488             
00489         if($effort_status_min != $effort_status_max){
00490             $str_st = "";
00491         }
00492         else{
00493             $str_st = ", e.status";
00494         }
00495         
00496          if(!is_null($project))
00497          {
00498              $query_str = "SELECT DISTINCT e.task, e.project {$str_st}
00499                            FROM {$prefix}item i, {$prefix}effort e
00500                            WHERE e.project = {$project}
00501                            AND i.type = '".ITEM_EFFORT."'
00502                            AND e.id = i.id
00503                            $str_status_min
00504                            $str_status_max
00505                            AND i.state = '". ITEM_STATE_OK ."';"; 
00506             $sth = $dbh->prepare($query_str);
00507             $sth->execute("",1);
00508             $tmp=$sth->fetchall_assoc();
00509             $efforts=array();
00510             foreach($tmp as $t) {
00511                 $effort=new Effort($t);
00512                 $efforts[]=$effort;
00513             }
00514             
00515             return $efforts;
00516         }
00517         
00518         return NULL;
00519     }
00520         
00521     function getProject()
00522     {
00523         require_once(confGet('DIR_STREBER') . 'db/class_project.inc.php');
00524         if(!$this->project) {
00525             #trigger_error("Task:getProject. project-id not set",E_USER_WARNING);
00526             return NULL;
00527         }
00528         $project= Project::getById($this->project);
00529         return $project;
00530     }
00531 
00532 
00533     function getProjectLink()
00534     {
00535         if($project= $this->getProject()) {
00536             return "<nobr>".$project->getLink().'</nobr>';
00537         }
00538         else {
00539             return NULL;
00540         }
00541     }
00542 
00543 
00544     function getPerson()
00545     {
00546         require_once(confGet('DIR_STREBER') . 'db/class_person.inc.php');
00547         if($this->person) {
00548             $person= Person::getById($this->person);
00549         }
00550         else {
00551             $person= Person::getById($this->created_by);
00552         }
00553         return $person;
00554     }
00555 
00556 
00557     function getPersonLink()
00558     {
00559         if($person= $this->getPerson()) {
00560             return "<nobr>".$person->getLink().'</nobr>';
00561         }
00562         else {
00563             return NULL;
00564         }
00565     }
00566 
00567     public static function getMinMaxTime($args=NULL)
00568     {
00569         global $auth;
00570         $prefix = confGet('DB_TABLE_PREFIX');
00571         $dbh = new DB_Mysql;
00572 
00573         ### default params ###
00574         $e_ids            = NULL;
00575 
00576         ### filter params ###
00577         if($args) {
00578             foreach($args as $key=>$value) {
00579                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00580                     trigger_error("unknown parameter",E_USER_NOTICE);
00581                 }
00582                 else {
00583                     $$key= $value;
00584                 }
00585             }
00586         }
00587 
00588         $effort_ids = $e_ids;
00589 
00590         if($effort_ids)
00591         {
00592             $str = "SELECT MIN(e.time_start), MAX(e.time_end) FROM {$prefix}effort e
00593                     WHERE e.id = " . intval($effort_ids[0]);
00594 
00595             $num = count($effort_ids);
00596             if($num > 1)
00597             {
00598                 for($i = 1; $i < $num; $i++)
00599                 {
00600                     $str .= " OR e.id = " . intval($effort_ids[$i]);
00601                 }
00602             }
00603 
00604             $str .= ";";
00605 
00606             $sth= $dbh->prepare($str);
00607             $sth->execute("",1);
00608             $tmp=$sth->fetch_row();
00609 
00610             return $tmp;
00611         }
00612         else {
00613             return NULL;
00614         }
00615     }
00616     
00617     public function getLink($short_name= true)
00618     {
00619 
00620         $style_isdone= $this->status >= EFFORT_STATUS_BALANCED
00621                     ? 'isDone'
00622                     : '';
00623 
00624         global $PH;
00625         if($short_name) {
00626             return '<span  title="'.asHtml($this->name).'" class="item task">'.$PH->getLink('effortView',$this->getShort(),array('effort'=>$this->id),$style_isdone).'</span>';
00627         }
00628         else {
00629             return '<span  class="item task">'.$PH->getLink('effortView',$this->name,array('effort'=>$this->id),$style_isdone).'</span>';
00630         }
00631     }
00632     
00633     function setStatus($status=NULL)
00634     {
00635         $this->effort_status = $status;
00636     }
00637     
00638     function getStatus()
00639     {
00640         return $this->effort_status;
00641     }
00642         
00643 }
00644 Effort::initFields();
00645 
00646 ?>

Generated on Sun Mar 4 17:19:27 2007 for streber by  doxygen 1.5.1-p1