| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup Courses Courses
- * @ingroup UnaModules
- *
- * @{
- */
- /**
- * Content data usage:
- * st - step,
- * at - attachment
- */
- define('BX_COURSES_CND_USAGE_ST', 0);
- define('BX_COURSES_CND_USAGE_AT', 1);
- /**
- * Courses profiles module.
- */
- class BxCoursesModule extends BxBaseModGroupsModule
- {
- function __construct(&$aModule)
- {
- parent::__construct($aModule);
- $this->_aSearchableNamesExcept[] = $this->_oConfig->CNF['FIELD_JOIN_CONFIRMATION'];
- }
- public function actionPassNode()
- {
- $iNodeId = bx_get('node_id');
- if($iNodeId === false)
- return echoJson([]);
- return echoJson($this->servicePassNode((int)$iNodeId));
- }
- public function actionPassData()
- {
- $iDataId = bx_get('data_id');
- if($iDataId === false)
- return echoJson([]);
- return echoJson($this->servicePassData((int)$iDataId));
- }
- public function serviceGetSafeServices()
- {
- return array_merge(parent::serviceGetSafeServices(), [
- 'PassNode' => '',
- 'PassData' => '',
- ]);
- }
- public function servicePassNode($iNodeId)
- {
- $CNF = &$this->_oConfig->CNF;
- $aNode = $this->_oDb->getContentNodes([
- 'sample' => 'id',
- 'id' => $iNodeId
- ]);
- if(empty($aNode) || !is_array($aNode))
- return ['code' => 1];
- $iEntryId = (int)$aNode['entry_id'];
- $iProfileId = bx_get_logged_profile_id();
- if($this->isNodePassed($iProfileId, $iNodeId)) {
- $aDataItems = $this->_oDb->getContentData([
- 'sample' => 'entry_node_ids',
- 'entry_id' => $iEntryId,
- 'node_id' => $iNodeId,
- ]);
- if(!empty($aDataItems) && is_array($aDataItems))
- foreach($aDataItems as $aDataItem)
- $this->_oDb->deleteContentData2Users(['data_id' => $aDataItem['id'], 'profile_id' => $iProfileId]);
- $this->_oDb->deleteContentNodes2Users(['node_id' => $iNodeId, 'profile_id' => $iProfileId]);
- }
- return [
- 'code' => 0,
- 'redirect' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY_NODE'] . '&id=' . $iEntryId, [
- 'node_id' => $iNodeId
- ])
- ];
- }
- public function servicePassData($iDataId)
- {
- $iNow = time();
- $iProfileId = bx_get_logged_profile_id();
-
- $aData = $this->_oDb->getContentData([
- 'sample' => 'id',
- 'id' => $iDataId
- ]);
-
- if(empty($aData) || !is_array($aData))
- return ['code' => 1];
- $this->_oDb->insertContentData2Users([
- 'data_id' => $iDataId,
- 'profile_id' => $iProfileId,
- 'date' => $iNow
- ]);
- $aNode = $this->_oDb->getContentNodes([
- 'sample' => 'id_full',
- 'id' => $aData['node_id']
- ]);
- $iTotal = $this->getDataTotalByNode($aNode, BX_COURSES_CND_USAGE_ST);
- $aUserTrack = $this->_oDb->getContentData([
- 'sample' => 'user_track',
- 'entry_id' => $aNode['entry_id'],
- 'node_id' => $aNode['id'],
- 'profile_id' => $iProfileId
- ]);
- if(count($aUserTrack) == $iTotal) {
- $this->_oDb->insertContentNodes2Users([
- 'node_id' => $aNode['id'],
- 'profile_id' => $iProfileId,
- 'date' => $iNow
- ]);
-
- $this->_autoPassNodes($iProfileId, $aNode['parent_id']);
- }
- $aResult = ['code' => 0];
- if(($sMethod = 'get_link') && bx_is_srv($aData['content_type'], $sMethod))
- $aResult['redirect'] = bx_srv($aData['content_type'], $sMethod, [$aData['content_id']]);
- return $aResult;
- }
-
- protected function _autoPassNodes($iProfileId, $iNodeId)
- {
- $aNode = $this->_oDb->getContentStructure([
- 'sample' => 'node_id',
- 'node_id' => $iNodeId,
- ]);
- if(empty($aNode) || !is_array($aNode))
- return;
- $iTotal = (int)$aNode['cn_l' . ($aNode['level'] + 1)];
- if(!$iTotal)
- return;
- $aUserTrack = $this->_oDb->getContentStructure([
- 'sample' => 'user_track',
- 'profile_id' => $iProfileId,
- 'entry_id' => $aNode['entry_id'],
- 'node_id' => $iNodeId
- ]);
- if(count($aUserTrack) == $iTotal) {
- $this->_oDb->insertContentNodes2Users([
- 'node_id' => $iNodeId,
- 'profile_id' => $iProfileId,
- 'date' => time()
- ]);
- if((int)$aNode['parent_id'] != 0)
- $this->_autoPassNodes($iProfileId, $aNode['parent_id']);
- }
- }
- public function serviceGetOptionsContentModulesSt()
- {
- return $this->_getOptionsContentModules();
- }
- public function serviceGetOptionsContentModulesAt()
- {
- return $this->_getOptionsContentModules();
- }
- public function serviceIsContentAvaliable($iProfileId)
- {
- if(!$this->_oConfig->isContent())
- return false;
- $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
- if($this->checkAllowedEdit($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
- return false;
- return true;
- }
- public function serviceOnContentAdded($sContentType, $iContentId, $iContextId, $iContextNodeId, $iContextUsage)
- {
- $this->_oDb->insertContentData([
- 'entry_id' => $iContextId,
- 'node_id' => $iContextNodeId,
- 'content_type' => $sContentType,
- 'content_id' => $iContentId,
- 'usage' => $iContextUsage,
- 'added' => time(),
- 'order' => $this->_oDb->getContentDataOrderMax($iContextId, $iContextNodeId)
- ]);
- $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $iContextNodeId]);
- $aCounters = [];
- if(!empty($aNode['counters']))
- $aCounters = json_decode($aNode['counters'], true);
- $sUsage = $this->_oConfig->getUsageI2S($iContextUsage);
- if(!isset($aCounters[$sUsage]))
- $aCounters[$sUsage] = [];
- if(!isset($aCounters[$sUsage][$sContentType]))
- $aCounters[$sUsage][$sContentType] = 0;
- $aCounters[$sUsage][$sContentType] += 1;
- $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $iContextNodeId]);
- }
- public function serviceOnContentDeleted($sContentType, $iContentId, $iContextId)
- {
- $aData = $this->_oDb->getContentData(['sample' => 'content', 'content_type' => $sContentType, 'content_id' => $iContentId]);
- if(empty($aData) || !is_array($aData))
- return;
- $this->_oDb->deleteContentData([
- 'entry_id' => $iContextId,
- 'content_type' => $sContentType,
- 'content_id' => $iContentId,
- ]);
- $this->_oDb->deleteContentData2Users([
- 'data_id' => $aData['id']
- ]);
- $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $aData['node_id']]);
- if(!empty($aNode['counters'])) {
- $sUsage = $this->_oConfig->getUsageI2S($aData['usage']);
- $aCounters = json_decode($aNode['counters'], true);
- if(!empty($aCounters[$sUsage][$sContentType])) {
- if((int)$aCounters[$sUsage][$sContentType] > 1)
- $aCounters[$sUsage][$sContentType] -= 1;
- else
- unset($aCounters[$sUsage][$sContentType]);
- $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $aData['node_id']]);
- }
- }
- }
- public function serviceEntityContentStructureBlock($iProfileId = 0)
- {
- $CNF = &$this->_oConfig->CNF;
- if(!$this->_oConfig->isContent())
- return '';
- if(!$iProfileId)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- if(!$iProfileId)
- return '';
- $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
- $sGridKey = 'OBJECT_GRID_CNT_STRUCTURE_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
- $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
- if(!$oGrid)
- return '';
- $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
- if($this->_bIsApi)
- return [
- bx_api_get_block('grid', $oGrid->getCodeAPI())
- ];
- return $oGrid->getCode();
- }
-
- public function serviceEntityContentDataBlock($iProfileId = 0)
- {
- $CNF = &$this->_oConfig->CNF;
- if(!$this->_oConfig->isContent())
- return '';
- if(!$iProfileId)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- if(!$iProfileId)
- return '';
- $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
- $sGridKey = 'OBJECT_GRID_CNT_DATA_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
- $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
- if(!$oGrid)
- return '';
- $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
- if($this->_bIsApi)
- return [
- bx_api_get_block('grid', $oGrid->getCodeAPI())
- ];
- return $oGrid->getCode();
- }
-
- public function serviceEntityStructureL1Block($iContentId = 0)
- {
- if(!$this->_oConfig->isContent() || $this->_oConfig->getContentLevelMax() == 1)
- return $this->_bIsApi ? [] : '';
- $mixedResult = $this->_serviceTemplateFuncEx ('entryStructureByLevel', $iContentId, [
- 'level' => 1,
- 'selected' => ($iNodeId = bx_get('parent_id')) !== false ? (int)$iNodeId : 0,
- 'start' => ($iStart = bx_get('start')) !== false ? (int)$iStart : 0,
- 'per_page' => ($iPerPage = bx_get('per_page')) !== false ? (int)$iPerPage : 0,
- ]);
- return $this->_bIsApi ? [bx_api_get_block('entity_structure_l1', $mixedResult)] : $mixedResult;
- }
- public function serviceEntityStructureL2Block($iContentId = 0, $iParentId = 0)
- {
- if(!$this->_oConfig->isContent())
- return $this->_bIsApi ? [] : '';
- if(!$iParentId && ($_iParentId = bx_get('parent_id')) !== false)
- $iParentId = (int)$_iParentId;
- $mixedResult = $this->_serviceTemplateFuncEx ('entryStructureByParentMl' . $this->_oConfig->getContentLevelMax(), $iContentId, [
- 'parent_id' => $iParentId
- ]);
- return $this->_bIsApi ? [bx_api_get_block('entity_structure_l2', $mixedResult)] : $mixedResult;
- }
-
- public function serviceEntityNodeBlock($iContentId = 0, $iNodeId = 0, $iUsage = false)
- {
- if(!$this->_oConfig->isContent())
- return '';
- if(!$iNodeId && ($_iNodeId = bx_get('node_id')) !== false)
- $iNodeId = (int)$_iNodeId;
- if($iUsage === false && ($_iUsage = bx_get('usage')) !== false)
- $iUsage = (int)$_iUsage;
- return $this->_serviceTemplateFuncEx ('entryNode', $iContentId, [
- 'node_id' => $iNodeId,
- 'usage' => $iUsage
- ]);
- }
-
- public function getNodeLevelByParent($aParentInfo)
- {
- return (is_array($aParentInfo) && !empty($aParentInfo['level']) ? (int)$aParentInfo['level'] : 0) + 1;
- }
-
- public function getDataTotalByNode($mixedNode, $iUsage = 0)
- {
- if(!is_array($mixedNode))
- $mixedNode = $this->_oDb->getContentNodes([
- 'sample' => 'id',
- 'id' => (int)$mixedNode
- ]);
-
- $iTotal = 0;
- $sUsage = $this->_oConfig->getUsageI2S($iUsage);
- if(!empty($mixedNode['counters']) && ($aCountes = json_decode($mixedNode['counters'], true)) && !empty($aCountes[$sUsage]) && is_array($aCountes[$sUsage]))
- $iTotal = array_sum($aCountes[$sUsage]);
- return $iTotal;
- }
- public function isNodeStarted($iProfileId, $mixedNode)
- {
- $iNodeId = is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode;
- $aChildNodes = $this->_oDb->getContentStructure([
- 'sample' => 'parent_id',
- 'parent_id' => $iNodeId
- ]);
- if(!empty($aChildNodes) && is_array($aChildNodes)) {
- foreach($aChildNodes as $aChildNode)
- if($this->isNodePassed($iProfileId, $aChildNode['node_id']))
- return true;
- else
- return $this->isNodeStarted($iProfileId, $aChildNode['node_id']);
- }
- else {
- $aChildData = $this->_oDb->getContentData([
- 'sample' => 'node_id',
- 'node_id' => $iNodeId
- ]);
- if(!empty($aChildData) && is_array($aChildData))
- foreach($aChildData as $aChildData)
- if($this->isDataPassed($iProfileId, $aChildData['id']))
- return true;
- return false;
- }
- }
- public function isNodePassed($iProfileId, $mixedNode)
- {
- $aUserTrack = $this->_oDb->getContentStructure([
- 'sample' => 'user_passed',
- 'profile_id' => $iProfileId,
- 'node_id' => is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode
- ]);
- return !empty($aUserTrack) && is_array($aUserTrack);
- }
-
- public function isDataPassed($iProfileId, $mixedData)
- {
- $aUserTrack = $this->_oDb->getContentData([
- 'sample' => 'user_passed',
- 'profile_id' => $iProfileId,
- 'data_id' => is_array($mixedData) ? $mixedData['id'] : (int)$mixedData
- ]);
- return !empty($aUserTrack) && is_array($aUserTrack);
- }
- protected function _getOptionsContentModules()
- {
- $aResult = [];
- $aModules = bx_srv('system', 'get_modules_by_type', ['content']);
- foreach($aModules as $aModule)
- $aResult[] = ['key' => $aModule['name'], 'value' => $aModule['title']];
- return $aResult;
- }
- }
- /** @} */
|