/**
     * Fill the users
     *
     * @param bool $sendToAllReceiverFlag
     * @param id $workflowslotUser
     * @param String $decission, skipped or waiting
     */
    public function checkSendToAllReceiverAtOnce($sendToAllReceiverFlag, $workflowslotUser, $decission) {
        if($sendToAllReceiverFlag == 1) {
            $station = WorkflowSlotUserTable::instance()->getUserBySlotId($workflowslotUser->getWorkflowslotId())->toArray();
            foreach($station as $item) {
                WorkflowProcessUserTable::instance()->deleteWorkflowProcessUserByWorkfloSlotUserId($item['id']);
            }
            WorkflowProcessTable::instance()->deleteWorkflowProcessByWorkflowSlotId($workflowslotUser->getWorkflowslotId());
            foreach($station as $item) {
                 $wfp = new WorkflowProcess();
                 $wfp->setWorkflowtemplateId($this->station->workflowtemplate_id);
                 $wfp->setWorkflowversionId($this->station->version_id);
                 $wfp->setWorkflowslotId($workflowslotUser->getWorkflowslotId());
                 $wfp->save();
                 $wfoId = $wfp->getId();

                 $wfpu = new WorkflowProcessUser();
                 $wfpu->setWorkflowprocessId($wfoId);
                 $wfpu->setWorkflowslotuserId($item['id']);
                 $wfpu->setUserId($item['user_id']);
                 $wfpu->setInprogresssince(time());
                 $wfpu->setDecissionstate($decission);
                 $wfpu->setResendet(0);
                 $wfpu->save();
                 if($decission == 'WAITING') {
                     $mail = new PrepareStationEmail($this->station->version_id, $this->station->workflowtemplate_id, $item['user_id'], $this->station->context, $this->station->serverUrl);
                 }
            }
        }
        
    }
 /**
  * If slot is sent to all at once, remove all users
  */
 public function checkCurrentSlot() {
     if($this->station->currentSlotSendToAllReceiver == 1) {
         $station = WorkflowSlotUserTable::instance()->getUserBySlotId($this->station->currentWorkflowSlotUser->getWorkflowslotId())->toArray();
         foreach($station as $item) {
             WorkflowProcessUserTable::instance()->deleteWorkflowProcessUserByWorkfloSlotUserId($item['id']);
         }
         WorkflowProcessTable::instance()->deleteWorkflowProcessByWorkflowSlotId($this->station->currentWorkflowSlotUser->getWorkflowslotId());
     }
     
 }
    /**
     *  Function checks if the slot is sendet to all receivers at once
     *
     * @param int $wfProcessId, id of the WorkflowProcess entry
     */
    public function checkSendToAllReceiver($wfProcessId) {
        $settings = $this->slot->slotSettings;
        $workflowUsers = WorkflowSlotUserTable::instance()->getUserBySlotId($settings['workflowslot_id']);
        if($settings['sendtoallreceivers'] == 1) {
            foreach($workflowUsers as $user) {
                $singleUser = $user->toArray();
                $this->addProcessUser($singleUser['id'], $singleUser['user_id'], $wfProcessId);
                $mailObj = new PrepareStationEmail($slot->version_id, $slot->workflow_id, $user['user_id'], $slot->workflowObj->context, $slot->workflowObj->serverUrl);
            }

        }
        else {
            $user = $workflowUsers[0]->toArray();
            $this->addProcessUser($user['id'], $user['user_id'], $wfProcessId);
            $mailObj = new PrepareStationEmail($slot->version_id, $slot->workflow_id, $user['user_id'], $slot->workflowObj->context, $slot->workflowObj->serverUrl);
        }
    }
    /**
     *
     * Create next station if a workflow is been saved
     *
     * @param array $slots, all SLots
     * @param int $userId, userid
     * @param int $versionId, version of workflow
     * @return true
     */
    public function getNextStation(array $slots, $userId, $versionId) {
        foreach($slots as $slot) {
            $wfProcessData = WorkflowProcessUserTable::instance()->getActiveProcessUserForWorkflowSlot($slot['workflowslot_id'],$userId)->toArray();
            $toChange = WorkflowProcessUserTable::instance()->getWaitingStation($slot['workflowslot_id'],$userId);
            foreach($toChange as $itemToChange) {
                $pdoObj = Doctrine::getTable('WorkflowProcessUser')->find($itemToChange->getId());
                $pdoObj->setDecissionstate('DONE');
                $pdoObj->setDateofdecission(time());
                $pdoObj->save();
            }
            $versionId = $versionId;
            $wfSlotId = $slot['workflowslot_id'];
            $wsUserId = $wfProcessData[0]['workflowslotuser_id'];
            $checkWorkflow = new CreateNextStation($versionId,$wfSlotId,$wsUserId, $this);
        }
        $workflowVersion = WorkflowTemplateTable::instance()->getWorkflowTemplateByVersionId($versionId);
        $workflowData = MailinglistVersionTable::instance()->getSingleVersionById($workflowVersion[0]->getMailinglisttemplateversionId())->toArray();


        if($workflowData[0]['sendtoallslotsatonce'] == 1) {
            $slots = WorkflowSlotTable::instance()->getSlotByVersionId($versionId);
            $isCompleted = true;
            foreach($slots as $slot) {
                $users = WorkflowSlotUserTable::instance()->getUserBySlotId($slot->getId());
                foreach($users as $user) {
                    $processUsers = WorkflowProcessUserTable::instance()->getProcessUserByWorkflowSlotUserId($user->getId());
                    foreach($processUsers as $singleUser) {
                        $userArray = $singleUser->toArray();
                        if($userArray['decissionstate'] == 'WAITING') {
                            $isCompleted = false;
                        }
                    }
                }
            }
            if($isCompleted == true) {
                WorkflowTemplateTable::instance()->setWorkflowFinished($workflowVersion[0]['id']);
                $checkWorkflow->checkEndAction();
            }
        }
        return true;
    }
 /**
  *
  * Function checks if the next slot, needs to be sendet to all receivers at once,
  * or not. Then it loads the Users and adds them
  *
  * @param Doctrine_Collection $slot, WorkflowSlot Object that contains the next slot which is needed to be added.
  */
 public function addNewSlot(Doctrine_Collection $slot) {
     $mail = new SendSlotReachedEmail($this->nextStation->workflowslot_id, $slot[0]->getId(), $this->nextStation->workflowtemplate_id, $this->nextStation->version_id);
     $documenttemplateSlot = $slot[0]->getDocumenttemplateSlot()->toArray();
     $slotUser = WorkflowSlotUserTable::instance()->getUserBySlotId($slot[0]->getId())->toArray();
     if($documenttemplateSlot[0]['sendtoallreceivers'] == 1) {
         $processId = $this->nextStation->addProcess($this->nextStation->workflowtemplate_id, $this->nextStation->version_id, $slot[0]->getId());
         foreach($slotUser as $item) {
             $this->nextStation->addProcessUser($item['id'], $item['user_id'], $processId);
             $this->nextStation->checkSendToAllAtOnce();
         }
     }
     else {            
         $processId = $this->nextStation->addProcess($this->nextStation->workflowtemplate_id, $this->nextStation->version_id, $slot[0]->getId());
         $this->nextStation->addProcessUser($slotUser[0]['id'], $slotUser[0]['user_id'], $processId);
         $this->nextStation->checkSendToAllAtOnce();
     }
 }
 public function setCurrentWorkflowSlotUser() {
     $user = WorkflowSlotUserTable::instance()->getUserById($this->currentWorkflowSlotUser_id);
     $this->currentWorkflowSlotUser = $user[0];
 }
    /**
     * Function loads all slots, fields and users for a slot and returns all slots for a workflow
     * @param Doctrine_Collection $data
     * @return array $result
     */
    public function buildSaveData(Doctrine_Collection $data) {
        $a = 0;

        foreach($data as $slot) {
            $result[$a]['id'] = $slot->getId();
            $result[$a]['workflowversion_id'] = $slot->getWorkflowversionId();
            $result[$a]['slot_id'] = $slot->getSlotId();
            $result[$a]['position'] = $slot->getPosition();
            $result[$a]['fields'] = $this->getFields($slot->getId());
            $result[$a]['users'] = WorkflowSlotUserTable::instance()->getUserBySlotId($slot->getId())->toArray();
            $a++;
        }
        return $result;
    }
 public function getUser($slot_id, $workflowtemplate_id, $slotname, $slotcounter, $templateversion_id) {
     $users = WorkflowSlotUserTable::instance()->getUserBySlotId($slot_id);
     $result = array();
     $a = 0;
     foreach($users as $user) {
         $userlogin = array();
         $userlogin = $user->getUserLogin()->toArray();
         $userData = UserDataTable::instance()->getUserDataByUserId($userlogin[0]['id'])->toArray();
         $result[$a]['user_id'] = $user->getUserId();
         $result[$a]['id'] = $user->getId();
         $result[$a]['plainusername'] = $userData[0]['firstname'] . ' '  . $userData[0]['lastname'] . ' <i>('.$userlogin[0]['username'].')</i>';
         $result[$a]['username'] = '******' . $userlogin[0]['username'] . '</td></tr></table>';
         $result[$a]['slotgroup'] = '#' . $slotcounter . ' : ' . $slotname;
         $result[$a]['templateversion_id'] = $templateversion_id;
         $result[$a]['user'] = $this->getDecission($user, $templateversion_id);
         $a++;
     }
     return $result;
 }
    public function getProcess($version_id) {
        $slots = WorkflowSlotTable::instance()->getSlotByVersionId($version_id);
        $alreadyCompleted = 0;
        $toComplete = 0;
        foreach($slots as $slot) {
            $users = WorkflowSlotUserTable::instance()->getUserBySlotId($slot->getId());
            $toComplete += count($users);
            foreach($users as $user) {
                $processUser = WorkflowProcessUserTable::instance()->getProcessUserByWorkflowSlotUserId($user->getId())->toArray();
                if(!empty($processUser)) {
                    $waiting = true;
                    foreach($processUser as $process) {
                        if(count($processUser) == 1 AND $process['decissionstate'] != 'WAITING') {
                            $alreadyCompleted++;
                        }
                        else {
                            if($process['decissionstate'] != 'WAITING') {
                                $waiting = false;
                            }
                            else {
                                $waiting = true;
                            }
                        }
                    }
                    if(count($processUser) > 1 AND $waiting == false) {
                        $alreadyCompleted++;
                    }
                }
            }
        }
        $percentDone = ($toComplete/$toComplete);
        $fullPercentDone = 100 / $toComplete;
        $percentDone = $fullPercentDone * $alreadyCompleted;
        if($percentDone > 100) {
            $percentDone = 100;
        }
        $color = '';
        if($percentDone < 15) {

        }
        else if ($percentDone >= 15 AND $percentDone < 30) {
            $color = '#FF0000';
        }
        else if ($percentDone >= 30 AND $percentDone < 45) {
            $color = '#FF9933';
        }
        else if ($percentDone >= 45 AND $percentDone < 60) {
            $color = '#FFCC33';
        }
        else if ($percentDone >= 60 AND $percentDone < 75) {
            $color = '#FFFF33';
        }
        else if ($percentDone >= 75 AND $percentDone < 90) {
            $color = '#99FF99';
        }
        else {
            $color = '#00FF00';
        }
        return '<div style="background-color:'.$color.'; width:'.$percentDone.'px;">'.floor($percentDone) . ' %'.'</div>';
    }
 public function getNextUser($workflowslot_id, $position) {
     $user = WorkflowSlotUserTable::instance()->getUserBySlotIdAndPosition($workflowslot_id, $position)->toArray();
     return $user;
 }
 /**
  * Function is needed, when a worklow is sendet to all slots at once.
  * This function checks if each Entry from WorkflowProcessUser is != WAITING
  *
  * If no user needs to fill out the workflow, the workflow is completed and flag will be set
  */
 public function checkSendToAllAtOnce() {
     if($this->sendToAllAtOnce == 1) {
         $slots = WorkflowSlotTable::instance()->getSlotByVersionId($this->version_id);
         $isCompleted = true;
         foreach($slots as $slot) {
             $users = WorkflowSlotUserTable::instance()->getUserBySlotId($slot->getId());
             foreach($users as $user) {
                 $processUsers = WorkflowProcessUserTable::instance()->getProcessUserByWorkflowSlotUserId($user->getId());
                 foreach($processUsers as $singleUser) {
                     $userArray = $singleUser->toArray();
                     if($userArray['decissionstate'] == 'WAITING') {
                         $isCompleted = false;
                     }
                 }
             }
         }
         if($isCompleted == true) {
             WorkflowTemplateTable::instance()->setWorkflowFinished($this->workflowtemplate_id);
             $this->checkEndAction();
         }
     }
 }
 /**
  *
  * Check if the user is in the receiverslist of the workflow
  *
  * @param int $workflowversionId, id of the workflow
  * @param int $mailinglistVersionId, id of the mailinglist
  * @return boolean
  */
 public function checkReceiver($workflowversionId, $mailinglistVersionId) {
     $receiver = WorkflowSlotUserTable::instance()->getUserByWorkflowVersionId($this->userId, $workflowversionId)->toArray();
     if(empty($receiver) == true) {
         $result['allowedtosend'] = 0;
         return $result;
     }
     else {
         $sender = MailinglistAuthorizationSettingTable::instance()->getSettingsByType('receiver', $mailinglistVersionId)->toArray();
         $sender[0]['allowedtosend'] = 1;
         return $sender[0];
     }
     
 }