• Модуль: learning
  • Путь к файлу: ~/bitrix/modules/learning/classes/general/clearnaccess.php
  • Класс: CLearnAccess
  • Вызов: CLearnAccess::SQLClauseForAccessibleLessons
public function SQLClauseForAccessibleLessons ($in_bitmaskOperations, $isUseCache = false, $lessonId = 0, $in_prfx = 'DEFPRFX')
{
	global $DB;
	if ( ! (is_int($in_bitmaskOperations) && ($in_bitmaskOperations > 0)) )
	{
		throw new LearnException ('bitmask must be an integer > 0', 
			LearnException::EXC_ERR_ALL_ACCESS_DENIED 
			| LearnException::EXC_ERR_ALL_PARAMS);
	}

	$prfx   = $DB->ForSQL ($in_prfx);
	$userId = (int) $this->userId;

	// access codes for user $this->userId
	$arUserAccessSymbols = $this->GetAccessCodes ($isUseCache);

	$userAccessSymbols = 'NULL';
	// convert array to comma-separeted list for sql query (items will be escaped)
	if (count($arUserAccessSymbols) > 0)
		$userAccessSymbols = $this->Array2CommaSeparatedListForSQL ($arUserAccessSymbols);

	/**
	 * There are some operations, granted on all lessons in context of some user.
	 * So, we must adjust $in_bitmaskOperations on operations, which are already 
	 * accessible by user (in both roles: as author(CR) and as just any user(Any)).
	 * User role is unknown now, it will be known on SQL query only.
	 */
	// Get bitmask of operations granted on all lessons (any user mode)
	$bitmaskAvailOperationsForAny = $this->GetBitmaskOperationsForAllLessons($arUserAccessSymbols);
	// Get bitmask of operations granted on all lessons (user-author mode)
	$bitmaskAvailOperationsForCR  = $this->GetBitmaskOperationsForAllLessons(array_merge($arUserAccessSymbols, array('CR')));

	/**
	 * Now, switch off bits for operations, 
	 * that are available for current user 
	 * on all lessons (or all own lessons for author).
	 * Because, we must check only rights, that are not
	 * available on all lessons yet.
	 */
	$bitmaskOperationsForAny = $in_bitmaskOperations & ( ~ $bitmaskAvailOperationsForAny );
	$bitmaskOperationsForCR  = $in_bitmaskOperations & ( ~ $bitmaskAvailOperationsForCR );

	// Convert bitmasks to sql comma-separated list of operations' names
	$sqlOperationsForAny = false;
	$sqlOperationsForCR  = false;
	if ($bitmaskOperationsForAny !== 0)
		$sqlOperationsForAny = $this->ParseOperationsForSQL ($bitmaskOperationsForAny);
	if ($bitmaskOperationsForCR !== 0)
		$sqlOperationsForCR  = $this->ParseOperationsForSQL ($bitmaskOperationsForCR);

	$arSqlWhere = array();

	// Is some operations must be checked for author?
	if ($sqlOperationsForCR !== false)
		$arSqlWhere[] = "(${prfx}TLR.SUBJECT_ID = 'CR' AND ${prfx}TLL.CREATED_BY = $userId AND ${prfx}XTO.NAME IN ($sqlOperationsForCR))";
	else
		$arSqlWhere[] = "(${prfx}TLL.CREATED_BY = $userId)";	// All requested operations are permitted for author

	if ($sqlOperationsForAny !== false)
		$arSqlWhere[] = "(${prfx}TLR.SUBJECT_ID IN ($userAccessSymbols) AND ${prfx}XTO.NAME IN ($sqlOperationsForAny))";
	else
		$arSqlWhere[] = "(1=1)";	// All requested operations permitted for user $this->userId

	$sqlWhere = implode("n OR n", $arSqlWhere);

	$lessonId = intval($lessonId);
	if ($lessonId > 0)
	{
		$sqlWhere = "${prfx}TLL.ID={$lessonId} AND (".$sqlWhere.")";
	}

	$sql = "SELECT ${prfx}TLL.ID AS LESSON_ID
	FROM b_learn_lesson ${prfx}TLL
	LEFT OUTER JOIN b_learn_rights ${prfx}TLR
		ON ${prfx}TLL.ID = ${prfx}TLR.LESSON_ID
	LEFT OUTER JOIN b_task_operation ${prfx}TTO
		ON ${prfx}TLR.TASK_ID = ${prfx}TTO.TASK_ID
	LEFT OUTER JOIN b_operation ${prfx}XTO
		ON ${prfx}TTO.OPERATION_ID = ${prfx}XTO.ID
	WHERE
		$sqlWhere";

	return ($sql);
	
	/*
	prev version of code:

	$userAccessSymbols = $this->GetAccessCodesForSQL ($isUseCache);
	$sqlOperations     = $this->ParseOperationsForSQL ($in_bitmaskOperations);
	$prfx   = $DB->ForSQL ($in_prfx);
	$userId = $this->userId;

	$sql = "
	SELECT ${prfx}TLR.LESSON_ID
	FROM b_learn_rights ${prfx}TLR
	INNER JOIN b_task_operation ${prfx}TTO
		ON ${prfx}TLR.TASK_ID = ${prfx}TTO.TASK_ID
	INNER JOIN b_operation ${prfx}TO
		ON ${prfx}TTO.OPERATION_ID = ${prfx}TO.ID
	INNER JOIN b_learn_lesson ${prfx}TLL
		ON ${prfx}TLL.ID = ${prfx}TLR.LESSON_ID
	WHERE 
		TO.NAME IN ($sqlOperations)
		AND
		(
		(${prfx}TLR.SUBJECT_ID = 'CR' AND ${prfx}TLL.CREATED_BY = $userId)
		OR (TLR.SUBJECT_ID IN ($userAccessSymbols))
		)";

	return ($sql);
	*/
}