| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- <?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
- *
- * @{
- */
- /**
- * How to pass a lesson's steps:
- * 0 - all at once, the order is not important
- * 1 - alternate, the order is important
- */
- define('BX_COURSES_CND_PASSING_ALL', 0);
- define('BX_COURSES_CND_PASSING_ALTERNATE', 1);
- /**
- * 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]);
- }
- $sLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY_NODE'] . '&id=' . $iEntryId, [
- 'node_id' => $iNodeId
- ]);
- return [
- 'code' => 0,
- 'redirect' => $this->_bIsApi ? bx_api_get_relative_url($sLink) : $sLink
- ];
- }
- 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']]);
- if($this->_bIsApi)
- $aResult['redirect'] = bx_api_get_relative_url($aResult['redirect']);
- 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 ($aGrid = $oGrid->getCodeAPI()) ? [
- bx_api_get_block('grid', $aGrid)
- ] : [];
- 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 ($aGrid = $oGrid->getCodeAPI()) ? [
- bx_api_get_block('grid', $aGrid)
- ] : [];
- 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 : 2,
- ]);
- return $this->_bIsApi ? [bx_api_get_block('course_structure', $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('module_structure', $mixedResult)] : $mixedResult;
- }
-
- public function serviceEntityNodeBlock($iContentId = 0, $iNodeId = 0, $iUsage = false)
- {
- if(!$this->_oConfig->isContent())
- return $this->_bIsApi ? [] : '';
- if(!$iNodeId && ($_iNodeId = bx_get('node_id')) !== false)
- $iNodeId = (int)$_iNodeId;
- if($iUsage === false && ($_iUsage = bx_get('usage')) !== false)
- $iUsage = (int)$_iUsage;
- $mixedResult = $this->_serviceTemplateFuncEx('entryNode', $iContentId, [
- 'node_id' => $iNodeId,
- 'usage' => $iUsage
- ]);
- return $this->_bIsApi ? [bx_api_get_block('lesson_structure', $mixedResult)] : $mixedResult;
- }
- 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);
- }
- public function getEntryPass($iProfileId, $iContentId)
- {
- $aChildren = $this->_oDb->getContentStructure([
- 'sample' => 'entry_id',
- 'entry_id' => $iContentId,
- 'level' => 1
- ]);
- $aPassDetails = [];
- foreach($aChildren as $aChild)
- $this->getNodePass($iProfileId, $iContentId, $aChild, $aPassDetails);
- $iPassPercent = 0;
- $sPassStatus = $sPassTitle = '';
- $iLevelMax = $this->_oConfig->getContentLevelMax();
- if(isset($aPassDetails[$iLevelMax]) && ($aPassLevelMax = $aPassDetails[$iLevelMax])) {
- $iPassPercent = ($iTotal = (int)$aPassLevelMax['total']) != 0 ? (int)round(100 * $aPassLevelMax['passed']/$iTotal) : 0;
- if($aPassLevelMax['passed'] != 0) {
- if($aPassLevelMax['passed'] != $aPassLevelMax['total']) {
- $sPassStatus = '_bx_courses_txt_status_in_process';
- $sPassTitle = '_bx_courses_txt_pass_continue';
- }
- else {
- $sPassStatus = '_bx_courses_txt_status_completed';
- $sPassTitle = '_bx_courses_txt_pass_again';
- }
- }
- else {
- $sPassStatus = '_bx_courses_txt_status_not_started';
- $sPassTitle = '_bx_courses_txt_pass_start';
- }
- }
- return [
- $iPassPercent,
- $aPassDetails,
- _t($sPassStatus),
- _t($sPassTitle)
- ];
- }
- public function getNodePass($iProfileId, $iContentId, $aNode, &$aResults)
- {
- $iLevel = $aNode['level'];
- if(!isset($aResults[$iLevel]))
- $aResults[$iLevel] = [
- 'passed' => 0,
- 'total' => 0
- ];
- $aResults[$iLevel]['passed'] += $this->isNodePassed($iProfileId, $aNode['node_id']);
- $aResults[$iLevel]['total'] += 1;
- if($iLevel != $this->_oConfig->getContentLevelMax() && !empty($aNode['cn_l' . ($iLevel + 1)])) {
- $aChildren = $this->_oDb->getContentStructure([
- 'sample' => 'parent_id',
- 'parent_id' => $aNode['node_id']
- ]);
-
- foreach($aChildren as $aChild)
- $this->getNodePass($iProfileId, $iContentId, $aChild, $aResults);
- }
- }
- public function getNodePassByChildren($iProfileId, $iContentId, $aNode, &$aResults)
- {
- $aUserTrack = $this->_oDb->getContentStructure([
- 'sample' => 'user_track',
- 'entry_id' => $iContentId,
- 'node_id' => $aNode['node_id'],
- 'profile_id' => $iProfileId
- ]);
- $iLevelChildren = $aNode['level'] + 1;
- $iCountChildren = $aNode['cn_l' . $iLevelChildren];
- if(!isset($aResults[$iLevelChildren]))
- $aResults[$iLevelChildren] = [
- 'passed' => 0,
- 'total' => 0
- ];
- $aResults[$iLevelChildren]['passed'] += count($aUserTrack);
- $aResults[$iLevelChildren]['total'] += $iCountChildren;
- if($iCountChildren && $iLevelChildren != $this->_oConfig->getContentLevelMax()) {
- $aChildren = $this->_oDb->getContentStructure([
- 'sample' => 'parent_id',
- 'parent_id' => $aNode['node_id']
- ]);
-
- foreach($aChildren as $aChild)
- $this->getNodePassByChildren($iProfileId, $iContentId, $aChild, $aResults);
- }
- }
- public function getNodePassByData($iProfileId, $iContentId, $aNode)
- {
- $iTotal = 0;
- $iPassCount = $iPassPercent = 0;
- $sPassStatus = $sPassTitle = '';
- if(($iTotal = $this->getDataTotalByNode($aNode)) != 0) {
- $aUserTrack = $this->_oDb->getContentData([
- 'sample' => 'user_track',
- 'entry_id' => $iContentId,
- 'node_id' => $aNode['node_id'],
- 'profile_id' => $iProfileId
- ]);
- $iPassCount = count($aUserTrack);
- $iPassPercent = (int)round(100 * $iPassCount/$iTotal);
-
- if($iPassCount == 0) {
- $sPassStatus = '_bx_courses_txt_status_not_started';
- $sPassTitle = '_bx_courses_txt_pass_start';
- }
- else {
- if($iPassCount != $iTotal) {
- $sPassStatus = '_bx_courses_txt_status_in_process';
- $sPassTitle = '_bx_courses_txt_pass_continue';
- }
- else {
- $sPassStatus = '_bx_courses_txt_status_completed';
- $sPassTitle = '_bx_courses_txt_pass_again';
- }
- }
- }
- return [
- $iPassPercent,
- _t('_bx_courses_txt_n_m_steps', $iPassCount, $iTotal),
- _t($sPassStatus ? $sPassStatus : '_undefined'),
- _t($sPassTitle)
- ];
- }
- public function decodeDataAPI($aData, $aParams = [])
- {
- $CNF = $this->_oConfig->CNF;
- $aResult = parent::decodeDataAPI($aData, $aParams);
- $aResultAdd = [];
- $iEntryId = (int)$aData[$CNF['FIELD_ID']];
- $aLevelToNode = $this->_oConfig->getContentLevel2Node(false);
- if($this->isFan($iEntryId, $this->_iProfileId)) {
- $sTxtProgress = _t('_bx_courses_txt_n_m_progress');
- list($iPassPercent, $aPassDetails, $sPassStatus, $sPassTitle) = $this->getEntryPass($this->_iProfileId, $iEntryId);
- $aCounters = [];
- foreach($aPassDetails as $iLevel => $aDetails) {
- $aCounters[] = [
- 'level' => $iLevel,
- 'title' => $aLevelToNode[$iLevel],
- 'passed' => $aDetails['passed'],
- 'total' => $aDetails['total'],
- 'progress' => bx_replace_markers($sTxtProgress, $aDetails)
- ];
- }
- $aResultAdd = [
- 'percent' => $iPassPercent,
- 'counters' => $aCounters,
- 'status' => $sPassStatus,
- 'show_pass' => $iPassPercent > 0 && $iPassPercent < 100,
- ];
- if($aResultAdd['show_pass'])
- $aResultAdd = array_merge($aResultAdd, [
- 'pass_link' => $aResult['url'],
- 'pass_title' => $sPassTitle,
- ]);
-
- }
- else {
- $iLevelMax = $this->_oConfig->getContentLevelMax();
- $aEntryCounters = $this->_oDb->getContentStructure(['sample' => 'entry_id_counters', 'entry_id' => $iEntryId]);
- $aCounters = [];
- for($i = 1; $i <= $iLevelMax; $i++) {
- $aCounters[] = [
- 'level' => $i,
- 'title' => $aLevelToNode[$i],
- 'total' => $aEntryCounters['cn_l' . $i]
- ];
- }
- $aResultAdd = [
- 'counters' => $aCounters,
- ];
- }
- return array_merge($aResult, $aResultAdd);
- }
- 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;
- }
- }
- /** @} */
|