• Модуль: learning
  • Путь к файлу: ~/bitrix/modules/learning/classes/general/ilearngraphnode.php
  • Класс: CLearnGraphNode
  • Вызов: CLearnGraphNode::_InsertOrUpdate
static function _InsertOrUpdate ($arInFields, $mode, $id = false)
{
	global $DB, $USER;

	$createdBy = 1;
	if (is_object($USER) && method_exists($USER, 'getId'))
		$createdBy = (int) $USER->getId();

	switch ($mode)
	{
		case 'update':
			$accessLevel  = self::SQL_UPDATE;
			$throwErrCode = LearnException::EXC_ERR_GN_UPDATE;
			$isInsert     = false;
			$isForUpdate  = true;

			if ( ! is_numeric ($id) )
				throw new LearnException ('EA_PARAMS: $id', $throwErrCode);
		break;

		case 'insert':
			$accessLevel  = self::SQL_INSERT;
			$throwErrCode = LearnException::EXC_ERR_GN_CREATE;
			$isInsert     = true;
			$isForUpdate  = false;
		break;

		default:
			throw new LearnException ('EA_LOGIC',
				LearnException::EXC_ERR_ALL_LOGIC);
		break;
	}

	// Mapping of fields' names in db and in function arguments
	$arFieldsMap = self::_GetFieldsMap();

	// Check params for access_level (throws LearnException on failure). After - canonize it
	$arFields = self::_CheckAndCanonizeFields ($arFieldsMap, $arInFields, $accessLevel, $isForUpdate);

	// Prepares array of fields with values for query to DB. Also, uploads/removes files, if there are.
	if ($isForUpdate)
		$arFieldsToDb = self::_PrepareDataForQuery ($arFieldsMap, $arFields, $id);
	else
		$arFieldsToDb = self::_PrepareDataForQuery ($arFieldsMap, $arFields, false);

	$newLessonId = null;

	if ($isInsert)
	{
		$arInsert = $DB->PrepareInsert('b_learn_lesson', $arFieldsToDb);

		$strSql =
			"INSERT INTO b_learn_lesson 
				(" . $arInsert[0] . ", 
				TIMESTAMP_X, DATE_CREATE, CREATED_BY) " .
			"VALUES 
				(" . $arInsert[1] . ", "
				. $DB->GetNowFunction() . ", " . $DB->GetNowFunction() . ", " . $createdBy . ")";

		$rc = $DB->Query($strSql, true);

		$newLessonId = intval($DB->LastID());
	}
	else	// update
	{
		$strUpdate = $DB->PrepareUpdate('b_learn_lesson', $arFieldsToDb);

		if ($strUpdate !== '')
			$strUpdate .= ', ';

		$strSql = "UPDATE b_learn_lesson SET $strUpdate 
				TIMESTAMP_X = " . $DB->GetNowFunction()
			. " WHERE ID = " . ($id + 0);

		$rc = $DB->Query($strSql, $bIgnoreErrors = true);

		// TIMESTAMP_X - date when data last changed, so update it
		$arFieldsToDb['TIMESTAMP_X'] = $DB->GetNowFunction();
	}

	if ($rc === false)
		throw new LearnException ('EA_SQLERROR', $throwErrCode);

	if ($isInsert)
		return ($newLessonId);	// id of created node
}