• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/internals/useroption/task.php
  • Класс: BitrixTasksInternalsUserOptionTask
  • Вызов: Task::onTaskUpdate
static function onTaskUpdate(array $oldFields, array $newFields): void
{
	$taskId = (int)$newFields['ID'];

	$oldCreator = $oldFields['CREATED_BY'];
	$oldResponsible = $oldFields['RESPONSIBLE_ID'];
	$oldAccomplices = (array)$oldFields['ACCOMPLICES'];
	$oldAuditors = (array)$oldFields['AUDITORS'];

	$oldParticipants = array_unique(
		array_merge(
			[$oldCreator, $oldResponsible],
			$oldAccomplices,
			$oldAuditors
		)
	);

	$newCreator = ($newFields['CREATED_BY'] ?? null);
	$newResponsible = ($newFields['RESPONSIBLE_ID'] ?? null);
	$newAccomplices = ($newFields['ACCOMPLICES'] ?? null);
	$newAuditors = ($newFields['AUDITORS'] ?? null);

	$newParticipants = array_unique(
		array_merge(
			[($newCreator ?? $oldCreator), ($newResponsible ?? $oldResponsible)],
			(isset($newAccomplices) ? (array)$newAccomplices : $oldAccomplices),
			(isset($newAuditors) ? (array)$newAuditors : $oldAuditors)
		)
	);
	$newAccomplices = (isset($newAccomplices) ? (array)$newAccomplices : []);
	$newAuditors = (isset($newAuditors) ? (array)$newAuditors : []);

	$addedParticipants = array_unique(array_diff($newParticipants, $oldParticipants));
	$removedParticipants = array_unique(array_diff($oldParticipants, $newParticipants));

	$newUsersExceptAuditors = $newAccomplices;
	if (isset($newCreator))
	{
		$newUsersExceptAuditors[] = $newCreator;
	}
	if (isset($newResponsible))
	{
		$newUsersExceptAuditors[] = $newResponsible;
	}
	$newUsersExceptAuditors = array_unique($newUsersExceptAuditors);

	// new user for task was added directly to auditors
	foreach ($addedParticipants as $userId)
	{
		if (
			in_array($userId, $newAuditors)
			&& !in_array($userId, $newUsersExceptAuditors)
		)
		{
			UserOption::add($taskId, $userId, Option::MUTED);
		}
	}

	// removed users from task
	foreach ($removedParticipants as $userId)
	{
		UserOption::deleteByTaskIdAndUserId($taskId, $userId);
	}

	// user was removed from auditors and added to another role
	$removedAuditors = array_unique(array_diff($oldAuditors, $newAuditors));
	foreach ($removedAuditors as $userId)
	{
		if (in_array($userId, $newUsersExceptAuditors))
		{
			UserOption::deleteByTaskIdAndUserId($taskId, $userId);
		}
	}

	// user was not removed from auditors but added to another role
	foreach ($newAuditors as $userId)
	{
		if (in_array($userId, $newUsersExceptAuditors))
		{
			UserOption::deleteByTaskIdAndUserId($taskId, $userId);
		}
	}
}