CBPAllTaskService :: Update: Bitrix method

      
<?php 
//  CBPAllTaskService :: Update()
//  /dist/bitrix/modules/bizproc/classes/general/taskservice.php:526

    
public static function Update($id$arFields)
    {
        global 
$DB;

        
$id intval($id);
        if (
$id <= 0)
        {
            throw new 
Exception("id");
        }

        
self::ParseFields($arFields$id);

        
$strUpdate $DB->PrepareUpdate("b_bp_task"$arFields);

        if (
$strUpdate)
        {
            
$strSql =
                
"UPDATE b_bp_task SET ".
                
"    ".$strUpdate.", ".
                
"    MODIFIED = ".$DB->CurrentTimeFunction()." ".
                
"WHERE ID = ".intval($id)." ";
            
$DB->Query($strSqlFalse"File: ".__FILE__."<br>Line: ".__LINE__);
        }

        
$removedUsers $addedUsers $decremented $incremented = [];

        if (
is_set($arFields'USERS'))
        {
            
$arFields['USERS'] = array_map('intval'array_unique($arFields['USERS']));
            
$previousUserIds = static::getTaskUserIds($id);

            foreach (
$arFields['USERS'] as $userId)
            {
                if (
in_array($userId$previousUserIdstrue))
                {
                    continue;
                }

                
$DB->Query(
                    
"INSERT INTO b_bp_task_user (USER_ID, TASK_ID, ORIGINAL_USER_ID) ".
                    
"VALUES ({$userId}{$id}{$userId})"
                
);

                
$incremented[] = $userId;
                
CUserCounter::Increment($userId'bp_tasks''**');
            }

            
$diff array_diff($previousUserIds$arFields['USERS']);
            foreach (
$diff as $removedUserId)
            {
                
$decremented[] = $removedUserId;
                
CUserCounter::Decrement($removedUserId'bp_tasks''**');
                
$removedUsers[] = $removedUserId;
                
$DB->Query("DELETE FROM b_bp_task_user WHERE TASK_ID = {$id} AND USER_ID = {$removedUserId}");
            }
        }

        
$userStatuses = array();
        if (isset(
$arFields['STATUS']) && $arFields['STATUS'] > CBPTaskStatus::Running)
        {
            
$dbResUser $DB->Query("SELECT USER_ID FROM b_bp_task_user WHERE TASK_ID = ".$id." AND STATUS = ".CBPTaskUserStatus::Waiting);
            while (
$userIterator $dbResUser->Fetch())
            {
                
$decremented[] = $userIterator["USER_ID"];
                
CUserCounter::Decrement($userIterator["USER_ID"], 'bp_tasks''**');

                if (
$arFields['STATUS'] == CBPTaskStatus::Timeout)
                    
$userStatuses[$userIterator["USER_ID"]] = CBPTaskUserStatus::No;
                else
                    
$removedUsers[] = $userIterator["USER_ID"];
            }
            if (
$arFields['STATUS'] == CBPTaskStatus::Timeout)
            {
                
$DB->Query("UPDATE b_bp_task_user SET STATUS = ".CBPTaskUserStatus::No.", DATE_UPDATE = ".$DB->CurrentTimeFunction()
                    .
" WHERE TASK_ID = ".$id." AND STATUS = ".CBPTaskUserStatus::Waiting);
            }
            else
                
$DB->Query("DELETE FROM b_bp_task_user WHERE TASK_ID = ".$id." AND STATUS = ".CBPTaskUserStatus::Waiting);
        }

        foreach (
GetModuleEvents("bizproc""OnTaskUpdate"true) as $arEvent)
            
ExecuteModuleEventEx($arEvent, array($id$arFields));

        if (
$removedUsers)
        {
            
$arFields['USERS_REMOVED'] = $removedUsers;
        }
        if (
$addedUsers)
        {
            
$arFields['USERS_ADDED'] = $addedUsers;
        }
        if (
$userStatuses)
        {
            
$arFields['USERS_STATUSES'] = $userStatuses;
        }

        
$arFields['COUNTERS_INCREMENTED'] = $incremented;
        
$arFields['COUNTERS_DECREMENTED'] = $decremented;

        
self::onTaskChange($id$arFieldsCBPTaskChangedStatus::Update);
        return 
$id;
    }