Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Instance.php

00001 <?php
00002 include_once (GALAXIA_LIBRARY.'/src/common/Base.php');
00005 
00011 class Instance extends Base {
00012   var $properties = Array();
00013   var $owner = '';
00014   var $status = '';
00015   var $started;
00016   var $nextActivity;
00017   var $nextUser;
00018   var $ended;
00020   var $activities = Array();
00021   var $pId;
00022   var $instanceId = 0;
00024   var $workitems = Array(); 
00025   
00026   function Instance($db) {
00027     $this->db = $db;
00028   }
00029   
00033   function getInstance($instanceId) {
00034     // Get the instance data
00035     $query = "select * from `".GALAXIA_TABLE_PREFIX."instances` where `instanceId`=?";
00036     $result = $this->query($query,array((int)$instanceId));
00037     if(!$result->numRows()) return false;
00038     $res = $result->fetchRow();
00039 
00040     //Populate 
00041     $this->properties = unserialize($res['properties']);
00042     $this->status = $res['status'];
00043     $this->pId = $res['pId'];
00044     $this->instanceId = $res['instanceId'];
00045     $this->owner = $res['owner'];
00046     $this->started = $res['started'];
00047     $this->ended = $res['ended'];
00048     $this->nextActivity = $res['nextActivity'];
00049     $this->nextUser = $res['nextUser'];
00050     // Get the activities where the instance is (ids only is ok)
00051     $query = "select * from `".GALAXIA_TABLE_PREFIX."instance_activities` where  `instanceId`=?";
00052     $result = $this->query($query,array((int)$instanceId));    
00053     while($res = $result->fetchRow()) {
00054       $this->activities[]=$res;
00055     }    
00056   }
00057   
00064   function setNextActivity($actname) {
00065     $pId = $this->pId;
00066     $actname=trim($actname);
00067     $aid = $this->getOne("select `activityId` from `".GALAXIA_TABLE_PREFIX."activities` where `pId`=? and `name`=?",array($pId,$actname));
00068     if(!$this->getOne("select count(*) from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=? and `pId`=?",array($aid,$pId))) {
00069       trigger_error(tra('Fatal error: setting next activity to an unexisting activity'),E_USER_WARNING);
00070     }
00071     $this->nextActivity=$aid;
00072     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `nextActivity`=? where `instanceId`=?";
00073     $this->query($query,array((int)$aid,(int)$this->instanceId));
00074   }
00075 
00081   function setNextUser($user) {
00082     $pId = $this->pId;
00083     $this->nextUser = $user;
00084     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `nextUser`=? where `instanceId`=?";
00085     $this->query($query,array($user,(int)$this->instanceId));
00086   }
00087  
00094   function _createNewInstance($activityId,$user) {
00095     // Creates a new instance setting up started,ended,user
00096     // and status
00097     $pid = $this->getOne("select `pId` from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=?",array((int)$activityId));
00098     $this->status = 'active';
00099     $this->nextActivity = 0;
00100     $this->setNextUser('');
00101     $this->pId = $pid;
00102     $now = date("U");
00103     $this->started=$now;
00104     $this->owner = $user;
00105     $props=serialize($this->properties);
00106     $query = "insert into `".GALAXIA_TABLE_PREFIX."instances`(`started`,`ended`,`status`,`pId`,`owner`,`properties`) values(?,?,?,?,?,?)";
00107     $this->query($query,array($now,0,'active',$pid,$user,$props));
00108     $this->instanceId = $this->getOne("select max(`instanceId`) from `".GALAXIA_TABLE_PREFIX."instances` where `started`=? and `owner`=?",array((int)$now,$user));
00109     $iid=$this->instanceId;
00110     
00111     // Now update the properties!
00112     $props = serialize($this->properties);
00113     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `properties`=? where `instanceId`=?";
00114     $this->query($query,array($props,(int)$iid));
00115 
00116     // Then add in ".GALAXIA_TABLE_PREFIX."instance_activities an entry for the
00117     // activity the user and status running and started now
00118     $query = "insert into `".GALAXIA_TABLE_PREFIX."instance_activities`(`instanceId`,`activityId`,`user`,`started`,`status`) values(?,?,?,?,?)";
00119     $this->query($query,array((int)$iid,(int)$activityId,$user,(int)$now,'running'));
00120   }
00121   
00126   function set($name,$value) {
00127     $this->properties[$name] = $value;
00128     $props = serialize($this->properties);
00129     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `properties`=? where `instanceId`=?";
00130     $this->query($query,array($props,$this->instanceId));
00131   }
00132   
00136   function get($name) {
00137     if(isset($this->properties[$name])) {
00138       return $this->properties[$name];
00139     } else {
00140       return false;
00141     }
00142   }
00143   
00148   function getActivities() {
00149     return $this->activities;
00150   }
00151   
00156   function getStatus() {
00157     return $this->status;
00158   }
00159   
00164   function setStatus($status) {
00165     $this->status = $status; 
00166     // and update the database
00167     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `status`=? where `instanceId`=?";
00168     $this->query($query,array($status,(int)$this->instanceId));  
00169   }
00170   
00174   function getInstanceId() {
00175     return $this->instanceId;
00176   }
00177   
00181   function getProcessId() {
00182     return $this->pId;
00183   }
00184   
00188   function getOwner() {
00189     return $this->owner;
00190   }
00191   
00195   function setOwner($user) {
00196     $this->owner = $user;
00197     // save database
00198     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `owner`=? where `instanceId`=?";
00199     $this->query($query,array($owner,(int)$this->instanceId));  
00200   }
00201   
00207   function setActivityUser($activityId,$theuser) {
00208     if(empty($theuser)) $theuser='*';
00209     for($i=0;$i<count($this->activities);$i++) {
00210       if($this->activities[$i]['activityId']==$activityId) {
00211         $this->activities[$i]['user']=$theuser;
00212         $query = "update `".GALAXIA_TABLE_PREFIX."instance_activities` set `user`=? where `activityId`=? and `instanceId`=?";
00213 
00214         $this->query($query,array($theuser,(int)$activityId,(int)$this->instanceId));
00215       }
00216     }  
00217   }
00218   
00223   function getActivityUser($activityId) {
00224     for($i=0;$i<count($this->activities);$i++) {
00225       if($this->activities[$i]['activityId']==$activityId) {
00226         return $this->activities[$i]['user'];
00227       }
00228     }  
00229     return false;
00230   }
00231 
00236   function setActivityStatus($activityId,$status) {
00237     for($i=0;$i<count($this->activities);$i++) {
00238       if($this->activities[$i]['activityId']==$activityId) {
00239         $this->activities[$i]['status']=$status;
00240         $query = "update `".GALAXIA_TABLE_PREFIX."instance_activities` set `status`=? where `activityId`=? and `instanceId`=?";
00241         $this->query($query,array($status,(int)$activityId,(int)$this->instanceId));
00242       }
00243     }  
00244   }
00245   
00246   
00251   function getActivityStatus($activityId) {
00252     for($i=0;$i<count($this->activities);$i++) {
00253       if($this->activities[$i]['activityId']==$activityId) {
00254         return $this->activities[$i]['status'];
00255       }
00256     }  
00257     return false;
00258   }
00259   
00263   function setActivityStarted($activityId) {
00264     $now = date("U");
00265     for($i=0;$i<count($this->activities);$i++) {
00266       if($this->activities[$i]['activityId']==$activityId) {
00267         $this->activities[$i]['started']=$now;
00268         $query = "update `".GALAXIA_TABLE_PREFIX."instance_activities` set `started`=? where `activityId`=? and `instanceId`=?";
00269         $this->query($query,array($now,(int)$activityId,(int)$this->instanceId));
00270       }
00271     }  
00272   }
00273   
00277   function getActivityStarted($activityId) {
00278     for($i=0;$i<count($this->activities);$i++) {
00279       if($this->activities[$i]['activityId']==$activityId) {
00280         return $this->activities[$i]['started'];
00281       }
00282     }  
00283     return false;
00284   }
00285   
00290   function _get_instance_activity($activityId) {
00291     for($i=0;$i<count($this->activities);$i++) {
00292       if($this->activities[$i]['activityId']==$activityId) {
00293         return $this->activities[$i];
00294       }
00295     }  
00296     return false;
00297   }
00298 
00302   function setStarted($time) {
00303     $this->started=$time;
00304     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `started`=? where `instanceId`=?";
00305     $this->query($query,array((int)$time,(int)$this->instanceId));    
00306   }
00307   
00311   function getStarted() {
00312     return $this->started;
00313   }
00314   
00318   function setEnded($time) {
00319     $this->ended=$time;
00320     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `ended`=? where `instanceId`=?";
00321     $this->query($query,array((int)$time,(int)$this->instanceId));    
00322   }
00323   
00327   function getEnded() {
00328     return $this->ended;
00329   }
00330   
00347   function complete($activityId=0,$force=false,$addworkitem=true) {
00348     global $user;
00349     global $__activity_completed;
00350     
00351     $__activity_completed = true;
00352   
00353     if(empty($user)) {$theuser='*';} else {$theuser=$user;}
00354     
00355     if($activityId==0) {
00356       $activityId=$_REQUEST['activityId'];
00357     }  
00358     
00359     // If we are completing a start activity then the instance must 
00360     // be created first!
00361     $type = $this->getOne("select `type` from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=?",array((int)$activityId));    
00362     if($type=='start') {
00363       $this->_createNewInstance((int)$activityId,$theuser);
00364     }
00365       
00366     // Now set ended
00367     $now = date("U");
00368     $query = "update `".GALAXIA_TABLE_PREFIX."instance_activities` set `ended`=? where `activityId`=? and `instanceId`=?";
00369     $this->query($query,array((int)$now,(int)$activityId,(int)$this->instanceId));
00370     
00371     //Add a workitem to the instance 
00372     $iid = $this->instanceId;
00373     if($addworkitem) {
00374       $max = $this->getOne("select max(`orderId`) from `".GALAXIA_TABLE_PREFIX."workitems` where `instanceId`=?",array((int)$iid));
00375       if(!$max) {
00376         $max=1;
00377       } else {
00378         $max++;
00379       }
00380       $act = $this->_get_instance_activity($activityId);
00381       if(!$act) {
00382         //Then this is a start activity ending
00383         $started = $this->getStarted();
00384         $putuser = $this->getOwner();
00385       } else {
00386         $started=$act['started'];
00387         $putuser = $act['user'];
00388       }
00389       $ended = date("U");
00390       $properties = serialize($this->properties);
00391       $query="insert into `".GALAXIA_TABLE_PREFIX."workitems`(`instanceId`,`orderId`,`activityId`,`started`,`ended`,`properties`,`user`) values(?,?,?,?,?,?,?)";    
00392       $this->query($query,array((int)$iid,(int)$max,(int)$activityId,(int)$started,(int)$ended,$properties,$putuser));
00393     }
00394     
00395     //Set the status for the instance-activity to completed
00396     $this->setActivityStatus($activityId,'completed');
00397     
00398     //If this and end actt then terminate the instance
00399     if($type=='end') {
00400       $this->terminate();
00401       return;
00402     }
00403     
00404     //If the activity ending is autorouted then send to the
00405     //activity
00406     if ($type != 'end') {
00407       if (($force) || ($this->getOne("select `isAutoRouted` from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=?",array($activityId)) == 'y'))   {
00408         // Now determine where to send the instance
00409         $query = "select `actToId` from `".GALAXIA_TABLE_PREFIX."transitions` where `actFromId`=?";
00410         $result = $this->query($query,array((int)$activityId));
00411         $candidates = Array();
00412         while ($res = $result->fetchRow()) {
00413           $candidates[] = $res['actToId'];
00414         }  
00415         if($type == 'split') {
00416           $first = true;
00417           foreach ($candidates as $cand) {
00418             $this->sendTo($activityId,$cand,$first);
00419             $first = false;
00420           }
00421         } elseif($type == 'switch') {
00422           if (in_array($this->nextActivity,$candidates)) {
00423             $this->sendTo((int)$activityId,(int)$this->nextActivity);
00424           } else {
00425             trigger_error(tra('Fatal error: nextActivity does not match any candidate in autorouting switch activity'),E_USER_WARNING);
00426           }
00427         } else {
00428           if (count($candidates)>1) {
00429             trigger_error(tra('Fatal error: non-deterministic decision for autorouting activity'),E_USER_WARNING);
00430           } else {
00431             $this->sendTo((int)$activityId,(int)$candidates[0]);
00432           }
00433         }
00434       }
00435     }
00436   }
00437   
00442   function abort($activityId=0,$theuser = '',$addworkitem=true) {
00443     if(empty($theuser)) {
00444       global $user;
00445       if (empty($user)) {$theuser='*';} else {$theuser=$user;}
00446     }
00447     
00448     if($activityId==0) {
00449       $activityId=$_REQUEST['activityId'];
00450     }  
00451     
00452     // If we are completing a start activity then the instance must 
00453     // be created first!
00454     $type = $this->getOne("select `type` from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=?",array((int)$activityId));    
00455     if($type=='start') {
00456       $this->_createNewInstance((int)$activityId,$theuser);
00457     }
00458       
00459     // Now set ended
00460     $now = date("U");
00461     $query = "update `".GALAXIA_TABLE_PREFIX."instance_activities` set `ended`=? where `activityId`=? and `instanceId`=?";
00462     $this->query($query,array((int)$now,(int)$activityId,(int)$this->instanceId));
00463     
00464     //Add a workitem to the instance 
00465     $iid = $this->instanceId;
00466     if($addworkitem) {
00467       $max = $this->getOne("select max(`orderId`) from `".GALAXIA_TABLE_PREFIX."workitems` where `instanceId`=?",array((int)$iid));
00468       if(!$max) {
00469         $max=1;
00470       } else {
00471         $max++;
00472       }
00473       $act = $this->_get_instance_activity($activityId);
00474       if(!$act) {
00475         //Then this is a start activity ending
00476         $started = $this->getStarted();
00477         $putuser = $this->getOwner();
00478       } else {
00479         $started=$act['started'];
00480         $putuser = $act['user'];
00481       }
00482       $ended = date("U");
00483       $properties = serialize($this->properties);
00484       $query="insert into `".GALAXIA_TABLE_PREFIX."workitems`(`instanceId`,`orderId`,`activityId`,`started`,`ended`,`properties`,`user`) values(?,?,?,?,?,?,?)";    
00485       $this->query($query,array((int)$iid,(int)$max,(int)$activityId,(int)$started,(int)$ended,$properties,$putuser));
00486     }
00487     
00488     //Set the status for the instance-activity to aborted
00489 // TODO: support 'aborted' if we keep activities after termination some day
00490     //$this->setActivityStatus($activityId,'aborted');
00491 
00492     // terminate the instance with status 'aborted'
00493     $this->terminate('aborted');
00494   }
00495   
00502   function terminate($status = 'completed') {
00503     //Set the status of the instance to completed
00504     $now = date("U");
00505     $query = "update `".GALAXIA_TABLE_PREFIX."instances` set `status`=?, `ended`=? where `instanceId`=?";
00506     $this->query($query,array($status,(int)$now,(int)$this->instanceId));
00507     $query = "delete from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=?";
00508     $this->query($query,array((int)$this->instanceId));
00509     $this->status = $status;
00510     $this->activities = Array();
00511   }
00512   
00513   
00519   function sendTo($from,$activityId,$split=false) {
00520     //1: if we are in a join check
00521     //if this instance is also in
00522     //other activity if so do
00523     //nothing
00524     $type = $this->getOne("select `type` from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=?",array((int)$activityId));
00525     
00526     // Verify the existance of a transition
00527     if(!$this->getOne("select count(*) from `".GALAXIA_TABLE_PREFIX."transitions` where `actFromId`=? and `actToId`=?",array($from,(int)$activityId))) {
00528       trigger_error(tra('Fatal error: trying to send an instance to an activity but no transition found'),E_USER_WARNING);
00529     }
00530     
00531     //try to determine the user or *
00532     //Use the nextUser
00533     if($this->nextUser) {
00534       $putuser = $this->nextUser;
00535     } else {
00536       $candidates = Array();
00537       $query = "select `roleId` from `".GALAXIA_TABLE_PREFIX."activity_roles` where `activityId`=?";
00538       $result = $this->query($query,array((int)$activityId)); 
00539       while ($res = $result->fetchRow()) {
00540         $roleId = $res['roleId'];
00541         $query2 = "select `user` from `".GALAXIA_TABLE_PREFIX."user_roles` where `roleId`=?";
00542         $result2 = $this->query($query2,array((int)$roleId)); 
00543         while ($res2 = $result2->fetchRow()) {
00544           $candidates[] = $res2['user'];
00545         }
00546       }
00547       if(count($candidates) == 1) {
00548         $putuser = $candidates[0];
00549       } else {
00550         $putuser = '*';
00551       }
00552     }        
00553     //update the instance_activities table
00554     //if not splitting delete first
00555     //please update started,status,user
00556     if(!$split) {
00557       $query = "delete from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=? and `activityId`=?";
00558       $this->query($query,array((int)$this->instanceId,$from));
00559     }
00560     $now = date("U");
00561     $iid = $this->instanceId;
00562     $query="delete from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=? and `activityId`=?";
00563     $this->query($query,array((int)$iid,(int)$activityId));
00564     $query="insert into `".GALAXIA_TABLE_PREFIX."instance_activities`(`instanceId`,`activityId`,`user`,`status`,`started`) values(?,?,?,?,?)";
00565     $this->query($query,array((int)$iid,(int)$activityId,$putuser,'running',(int)$now));
00566     
00567     //we are now in a new activity
00568     $this->activities=Array();
00569     $query = "select * from `".GALAXIA_TABLE_PREFIX."instance_activities` where `instanceId`=?";
00570     $result = $this->query($query,array((int)$iid));
00571     while ($res = $result->fetchRow()) {
00572       $this->activities[]=$res;
00573     }    
00574   
00575     if ($type == 'join') {
00576       if (count($this->activities)>1) {
00577         // This instance will have to wait!
00578         return;
00579       }
00580     }    
00581 
00582      
00583     //if the activity is not interactive then
00584     //execute the code for the activity and
00585     //complete the activity
00586     $isInteractive = $this->getOne("select `isInteractive` from `".GALAXIA_TABLE_PREFIX."activities` where `activityId`=?",array((int)$activityId));
00587     if ($isInteractive=='n') {
00588 
00589       // Now execute the code for the activity (function defined in lib/Galaxia/config.php)
00590       galaxia_execute_activity($activityId, $iid , 1);
00591 
00592       // Reload in case the activity did some change
00593       $this->getInstance($this->instanceId);
00594       $this->complete($activityId);
00595     }
00596   }
00597   
00601   function get_instance_comment($cId) {
00602     $iid = $this->instanceId;
00603     $query = "select * from `".GALAXIA_TABLE_PREFIX."instance_comments` where `instanceId`=? and `cId`=?";
00604     $result = $this->query($query,array((int)$iid,(int)$cId));
00605     $res = $result->fetchRow();
00606     return $res;
00607   }
00608   
00612   function replace_instance_comment($cId, $activityId, $activity, $user, $title, $comment) {
00613     if (!$user) {
00614       $user = 'Anonymous';
00615     }
00616     $iid = $this->instanceId;
00617     if ($cId) {
00618       $query = "update `".GALAXIA_TABLE_PREFIX."instance_comments` set `title`=?,`comment`=? where `instanceId`=? and `cId`=?";
00619       $this->query($query,array($title,$comment,(int)$iid,(int)$cId));
00620     } else {
00621       $hash = md5($title.$comment);
00622       if ($this->getOne("select count(*) from `".GALAXIA_TABLE_PREFIX."instance_comments` where `instanceId`=? and `hash`=?",array($iid,$hash))) {
00623         return false;
00624       }
00625       $now = date("U");
00626       $query ="insert into `".GALAXIA_TABLE_PREFIX."instance_comments`(`instanceId`,`user`,`activityId`,`activity`,`title`,`comment`,`timestamp`,`hash`) values(?,?,?,?,?,?,?,?)";
00627       $this->query($query,array((int)$iid,$user,(int)$activityId,$activity,$title,$comment,(int)$now,$hash));
00628     }  
00629   }
00630   
00634   function remove_instance_comment($cId) {
00635     $iid = $this->instanceId;
00636     $query = "delete from `".GALAXIA_TABLE_PREFIX."instance_comments` where `cId`=? and `instanceId`=?";
00637     $this->query($query,array((int)$cId,(int)$iid));
00638   }
00639  
00643   function get_instance_comments() {
00644     $iid = $this->instanceId;
00645     $query = "select * from `".GALAXIA_TABLE_PREFIX."instance_comments` where `instanceId`=? order by ".$this->convert_sortmode("timestamp_desc");
00646     $result = $this->query($query,array((int)$iid));    
00647     $ret = Array();
00648     while($res = $result->fetchRow()) {    
00649       $ret[] = $res;
00650     }
00651     return $ret;
00652   }
00653 
00654 }
00655 ?>

Generated on Mon Jun 7 16:37:37 2004 for Galaxia by doxygen 1.3.4