• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/countersprocessorhomeostasis.php
  • Класс: for
  • Вызов: for::onTaskGetList
static function onTaskGetList($arFilter, $tasksCountInList)
{


	//_dump_r('getlist');

	if ( ! CTaskCountersProcessorInstaller::isInstallComplete() )
	{
		//_dump_r('Not complete');
		return;
	}

	// Is there our marker?
	if ( ! (
		array_key_exists('::MARKERS', $arFilter)
		&& array_key_exists(self::MARKER_ID, $arFilter['::MARKERS'])
		&& ($tasksCountInList !== null)
	))
	{
		return;
	}

	$tasksCountInList = (int) $tasksCountInList;
	$counterOwnerUserId = $arFilter['::MARKERS'][self::MARKER_ID]['userId'];
	$counterId = $arFilter['::MARKERS'][self::MARKER_ID]['counterId'];
	$counterValue = (int) CUserCounter::GetValue($counterOwnerUserId, $counterId, $site_id = '**');

	if (in_array(
		$counterId,
		array(
			CTaskCountersProcessor::COUNTER_TASKS_MY_EXPIRED,
			CTaskCountersProcessor::COUNTER_TASKS_ACCOMPLICE_EXPIRED,
			CTaskCountersProcessor::COUNTER_TASKS_AUDITOR_EXPIRED,
			CTaskCountersProcessor::COUNTER_TASKS_ORIGINATOR_EXPIRED,

			CTaskCountersProcessor::COUNTER_TASKS_MY_EXPIRED_CANDIDATES,
			CTaskCountersProcessor::COUNTER_TASKS_ACCOMPLICE_EXPIRED_CANDIDATES
		),
		true
	))
	{
		$isExpirityCounter = true;
	}
	else
		$isExpirityCounter = false;

	$needRecalc = false;
	$realTasksCount         = null;

	// Is checksum correct?
	$filterCheksum = $arFilter['::MARKERS'][self::MARKER_ID]['filterCheksum'];
	$realCheksum   = self::calcFilterChecksum($arFilter);

	// break detection part
	if ($filterCheksum === $realCheksum) // this is exactly the same filter for that counter
	{
		$realTasksCount = $tasksCountInList;

		// if counter is not equal to task count, we certainly must recalc now
		if(($counterValue < 0) || ($tasksCountInList != $counterValue))
		{
			$needRecalc = true;
		}
	}
	else if (
		// this can be the same filter, but with only parent (root) tasks shown
		isset($arFilter['SAME_GROUP_PARENT'], $arFilter['ONLY_ROOT_TASKS'])
		&& ($arFilter['SAME_GROUP_PARENT'] === 'Y')
		&& ($arFilter['ONLY_ROOT_TASKS'] === 'Y')
	)
	{
		// unset the corresponding fields and try to compare checksums again
		unset($arFilter['SAME_GROUP_PARENT']);
		unset($arFilter['ONLY_ROOT_TASKS']);

		$realCheksum = self::calcFilterChecksum($arFilter);

		if ($filterCheksum === $realCheksum) // okay, we were right about filter
		{
			// tasks count in list shouldn't be more than registered in counter
			// and counter shouldn't be less than zero
			if (($counterValue < 0) || ($tasksCountInList > $counterValue))
			{
				$needRecalc = true;
			}
			else // or else we dont exactly know if counter is broken, so have to recalculate, but not every time
			{
				$needRecalc = static::getCountersRecheckForSubTasksNeed();
			}
		}
	}

	if ($needRecalc)
	{
		if($realTasksCount === null)
		{
			$rsTasksCount = CTasks::getCount(
				$arFilter,
				array(
					'bIgnoreDbErrors'  => true,
					'bSkipUserFields'  => true,
					'bSkipExtraTables' => true,
				)
			);

			if (
				$rsTasksCount
				&& ($arTasksCount = $rsTasksCount->fetch())
			)
			{
				$realTasksCount = intval($arTasksCount['CNT']);
			}
		}

		if($realTasksCount !== null && $realTasksCount != $counterValue) // sure, we were right about filter mistiming
		{
			if ($isExpirityCounter) // a special way for correction of 'deadline expired' counters
			{
				// pend counters reinstalling (agent is used)
				self::pendCountersRecalculation();
			}
			else	// for all other counters we can fix them by counter correction JUST RIGHT NOW
			{
				$delta = $realTasksCount - $counterValue;

				CTaskCountersQueue::push(
					$counterId,
					CTaskCountersQueue::OP_INCREMENT,
					array($counterOwnerUserId),
					$delta
				);
				CTaskCountersQueue::execute();
			}
		}
		// else sadly skip it, what else to do?
	}
}