CBPDocument::delegateTasks: Bitrix method

      
<?php 
//  CBPDocument :: delegateTasks()
//  /dist/bitrix/modules/bizproc/classes/general/document.php:590

    
public static function delegateTasks($fromUserId$toUserId$ids = array(), &$errors = array(), $allowedDelegationType null)
    {
        
$filter = array(
            
'USER_ID' => $fromUserId,
            
'STATUS' => CBPTaskStatus::Running,
            
'USER_STATUS' => CBPTaskUserStatus::Waiting
        
);

        if (
$ids)
        {
            
$ids array_filter(array_map('intval', (array)$ids));
            if (
$ids)
                
$filter['ID'] = $ids;
        }

        
$iterator CBPTaskService::GetList(
                array(
'ID'=>'ASC'),
                
$filter,
                
false,
                
false,
                array(
'ID''NAME''WORKFLOW_ID''ACTIVITY_NAME''DELEGATION_TYPE')
        );
        
$found false;
        
$trackingService null;
        
$sendImNotify = (CModule::IncludeModule("im"));

        while (
$task $iterator->fetch())
        {
            if (
$allowedDelegationType && !in_array((int)$task['DELEGATION_TYPE'], $allowedDelegationTypetrue))
            {
                
$errors[] = GetMessage('BPCGDOC_ERROR_DELEGATE_'.$task['DELEGATION_TYPE'], array('#NAME#' => $task['NAME']));
            }
            elseif (!
CBPTaskService::delegateTask($task['ID'], $fromUserId$toUserId))
            {
                
$errors[] = GetMessage('BPCGDOC_ERROR_DELEGATE', array('#NAME#' => $task['NAME']));
            }
            else
            {
                if (!
$found)
                {
                    
$runtime CBPRuntime::GetRuntime();
                    
$runtime->StartRuntime();
                    
/** @var CBPTrackingService $trackingService */
                    
$trackingService $runtime->GetService('TrackingService');
                }
                
$found true;

                
$trackingService->Write(
                    
$task['WORKFLOW_ID'],
                    
CBPTrackingType::Custom,
                    
$task['ACTIVITY_NAME'],
                    
CBPActivityExecutionStatus::Executing,
                    
CBPActivityExecutionResult::None,
                    
GetMessage('BPCGDOC_DELEGATE_LOG_TITLE'),
                    
GetMessage('BPCGDOC_DELEGATE_LOG', array(
                        
'#NAME#' => $task['NAME'],
                        
'#FROM#' => '{=user:user_'.$fromUserId.'}',
                        
'#TO#' => '{=user:user_'.$toUserId.'}'
                    
))
                );

                if (
$sendImNotify)
                {
                    
CIMNotify::Add(array(
                        
"MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
                        
'FROM_USER_ID' => $fromUserId,
                        
'TO_USER_ID' => $toUserId,
                        
"NOTIFY_TYPE" => IM_NOTIFY_FROM,
                        
"NOTIFY_MODULE" => "bizproc",
                        
"NOTIFY_EVENT" => "delegate_task",
                        
"NOTIFY_TAG" => "BIZPROC|TASK|".$task['ID'],
                        
'MESSAGE' => GetMessage('BPCGDOC_DELEGATE_NOTIFY_TEXT', array(
                            
'#TASK_URL#' => '/company/personal/bizproc/'.(int)$task['ID'].'/',
                            
'#TASK_NAME#' => $task['NAME']
                        ))
                    ));
                }
            }
        }
        return 
$found;
    }