BxCoursesModule.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Courses Courses
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Content data usage:
  13. * st - step,
  14. * at - attachment
  15. */
  16. define('BX_COURSES_CND_USAGE_ST', 0);
  17. define('BX_COURSES_CND_USAGE_AT', 1);
  18. /**
  19. * Courses profiles module.
  20. */
  21. class BxCoursesModule extends BxBaseModGroupsModule
  22. {
  23. function __construct(&$aModule)
  24. {
  25. parent::__construct($aModule);
  26. $this->_aSearchableNamesExcept[] = $this->_oConfig->CNF['FIELD_JOIN_CONFIRMATION'];
  27. }
  28. public function actionPassNode()
  29. {
  30. $iNodeId = bx_get('node_id');
  31. if($iNodeId === false)
  32. return echoJson([]);
  33. return echoJson($this->servicePassNode((int)$iNodeId));
  34. }
  35. public function actionPassData()
  36. {
  37. $iDataId = bx_get('data_id');
  38. if($iDataId === false)
  39. return echoJson([]);
  40. return echoJson($this->servicePassData((int)$iDataId));
  41. }
  42. public function serviceGetSafeServices()
  43. {
  44. return array_merge(parent::serviceGetSafeServices(), [
  45. 'PassNode' => '',
  46. 'PassData' => '',
  47. ]);
  48. }
  49. public function servicePassNode($iNodeId)
  50. {
  51. $CNF = &$this->_oConfig->CNF;
  52. $aNode = $this->_oDb->getContentNodes([
  53. 'sample' => 'id',
  54. 'id' => $iNodeId
  55. ]);
  56. if(empty($aNode) || !is_array($aNode))
  57. return ['code' => 1];
  58. $iEntryId = (int)$aNode['entry_id'];
  59. $iProfileId = bx_get_logged_profile_id();
  60. if($this->isNodePassed($iProfileId, $iNodeId)) {
  61. $aDataItems = $this->_oDb->getContentData([
  62. 'sample' => 'entry_node_ids',
  63. 'entry_id' => $iEntryId,
  64. 'node_id' => $iNodeId,
  65. ]);
  66. if(!empty($aDataItems) && is_array($aDataItems))
  67. foreach($aDataItems as $aDataItem)
  68. $this->_oDb->deleteContentData2Users(['data_id' => $aDataItem['id'], 'profile_id' => $iProfileId]);
  69. $this->_oDb->deleteContentNodes2Users(['node_id' => $iNodeId, 'profile_id' => $iProfileId]);
  70. }
  71. return [
  72. 'code' => 0,
  73. 'redirect' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY_NODE'] . '&id=' . $iEntryId, [
  74. 'node_id' => $iNodeId
  75. ])
  76. ];
  77. }
  78. public function servicePassData($iDataId)
  79. {
  80. $iNow = time();
  81. $iProfileId = bx_get_logged_profile_id();
  82. $aData = $this->_oDb->getContentData([
  83. 'sample' => 'id',
  84. 'id' => $iDataId
  85. ]);
  86. if(empty($aData) || !is_array($aData))
  87. return ['code' => 1];
  88. $this->_oDb->insertContentData2Users([
  89. 'data_id' => $iDataId,
  90. 'profile_id' => $iProfileId,
  91. 'date' => $iNow
  92. ]);
  93. $aNode = $this->_oDb->getContentNodes([
  94. 'sample' => 'id_full',
  95. 'id' => $aData['node_id']
  96. ]);
  97. $iTotal = $this->getDataTotalByNode($aNode, BX_COURSES_CND_USAGE_ST);
  98. $aUserTrack = $this->_oDb->getContentData([
  99. 'sample' => 'user_track',
  100. 'entry_id' => $aNode['entry_id'],
  101. 'node_id' => $aNode['id'],
  102. 'profile_id' => $iProfileId
  103. ]);
  104. if(count($aUserTrack) == $iTotal) {
  105. $this->_oDb->insertContentNodes2Users([
  106. 'node_id' => $aNode['id'],
  107. 'profile_id' => $iProfileId,
  108. 'date' => $iNow
  109. ]);
  110. $this->_autoPassNodes($iProfileId, $aNode['parent_id']);
  111. }
  112. $aResult = ['code' => 0];
  113. if(($sMethod = 'get_link') && bx_is_srv($aData['content_type'], $sMethod))
  114. $aResult['redirect'] = bx_srv($aData['content_type'], $sMethod, [$aData['content_id']]);
  115. return $aResult;
  116. }
  117. protected function _autoPassNodes($iProfileId, $iNodeId)
  118. {
  119. $aNode = $this->_oDb->getContentStructure([
  120. 'sample' => 'node_id',
  121. 'node_id' => $iNodeId,
  122. ]);
  123. if(empty($aNode) || !is_array($aNode))
  124. return;
  125. $iTotal = (int)$aNode['cn_l' . ($aNode['level'] + 1)];
  126. if(!$iTotal)
  127. return;
  128. $aUserTrack = $this->_oDb->getContentStructure([
  129. 'sample' => 'user_track',
  130. 'profile_id' => $iProfileId,
  131. 'entry_id' => $aNode['entry_id'],
  132. 'node_id' => $iNodeId
  133. ]);
  134. if(count($aUserTrack) == $iTotal) {
  135. $this->_oDb->insertContentNodes2Users([
  136. 'node_id' => $iNodeId,
  137. 'profile_id' => $iProfileId,
  138. 'date' => time()
  139. ]);
  140. if((int)$aNode['parent_id'] != 0)
  141. $this->_autoPassNodes($iProfileId, $aNode['parent_id']);
  142. }
  143. }
  144. public function serviceGetOptionsContentModulesSt()
  145. {
  146. return $this->_getOptionsContentModules();
  147. }
  148. public function serviceGetOptionsContentModulesAt()
  149. {
  150. return $this->_getOptionsContentModules();
  151. }
  152. public function serviceIsContentAvaliable($iProfileId)
  153. {
  154. if(!$this->_oConfig->isContent())
  155. return false;
  156. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  157. if($this->checkAllowedEdit($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  158. return false;
  159. return true;
  160. }
  161. public function serviceOnContentAdded($sContentType, $iContentId, $iContextId, $iContextNodeId, $iContextUsage)
  162. {
  163. $this->_oDb->insertContentData([
  164. 'entry_id' => $iContextId,
  165. 'node_id' => $iContextNodeId,
  166. 'content_type' => $sContentType,
  167. 'content_id' => $iContentId,
  168. 'usage' => $iContextUsage,
  169. 'added' => time(),
  170. 'order' => $this->_oDb->getContentDataOrderMax($iContextId, $iContextNodeId)
  171. ]);
  172. $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $iContextNodeId]);
  173. $aCounters = [];
  174. if(!empty($aNode['counters']))
  175. $aCounters = json_decode($aNode['counters'], true);
  176. $sUsage = $this->_oConfig->getUsageI2S($iContextUsage);
  177. if(!isset($aCounters[$sUsage]))
  178. $aCounters[$sUsage] = [];
  179. if(!isset($aCounters[$sUsage][$sContentType]))
  180. $aCounters[$sUsage][$sContentType] = 0;
  181. $aCounters[$sUsage][$sContentType] += 1;
  182. $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $iContextNodeId]);
  183. }
  184. public function serviceOnContentDeleted($sContentType, $iContentId, $iContextId)
  185. {
  186. $aData = $this->_oDb->getContentData(['sample' => 'content', 'content_type' => $sContentType, 'content_id' => $iContentId]);
  187. if(empty($aData) || !is_array($aData))
  188. return;
  189. $this->_oDb->deleteContentData([
  190. 'entry_id' => $iContextId,
  191. 'content_type' => $sContentType,
  192. 'content_id' => $iContentId,
  193. ]);
  194. $this->_oDb->deleteContentData2Users([
  195. 'data_id' => $aData['id']
  196. ]);
  197. $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $aData['node_id']]);
  198. if(!empty($aNode['counters'])) {
  199. $sUsage = $this->_oConfig->getUsageI2S($aData['usage']);
  200. $aCounters = json_decode($aNode['counters'], true);
  201. if(!empty($aCounters[$sUsage][$sContentType])) {
  202. if((int)$aCounters[$sUsage][$sContentType] > 1)
  203. $aCounters[$sUsage][$sContentType] -= 1;
  204. else
  205. unset($aCounters[$sUsage][$sContentType]);
  206. $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $aData['node_id']]);
  207. }
  208. }
  209. }
  210. public function serviceEntityContentStructureBlock($iProfileId = 0)
  211. {
  212. $CNF = &$this->_oConfig->CNF;
  213. if(!$this->_oConfig->isContent())
  214. return '';
  215. if(!$iProfileId)
  216. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  217. if(!$iProfileId)
  218. return '';
  219. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  220. $sGridKey = 'OBJECT_GRID_CNT_STRUCTURE_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
  221. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
  222. if(!$oGrid)
  223. return '';
  224. $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
  225. if($this->_bIsApi)
  226. return [
  227. bx_api_get_block('grid', $oGrid->getCodeAPI())
  228. ];
  229. return $oGrid->getCode();
  230. }
  231. public function serviceEntityContentDataBlock($iProfileId = 0)
  232. {
  233. $CNF = &$this->_oConfig->CNF;
  234. if(!$this->_oConfig->isContent())
  235. return '';
  236. if(!$iProfileId)
  237. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  238. if(!$iProfileId)
  239. return '';
  240. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  241. $sGridKey = 'OBJECT_GRID_CNT_DATA_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
  242. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
  243. if(!$oGrid)
  244. return '';
  245. $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
  246. if($this->_bIsApi)
  247. return [
  248. bx_api_get_block('grid', $oGrid->getCodeAPI())
  249. ];
  250. return $oGrid->getCode();
  251. }
  252. public function serviceEntityStructureL1Block($iContentId = 0)
  253. {
  254. if(!$this->_oConfig->isContent() || $this->_oConfig->getContentLevelMax() == 1)
  255. return $this->_bIsApi ? [] : '';
  256. $mixedResult = $this->_serviceTemplateFuncEx ('entryStructureByLevel', $iContentId, [
  257. 'level' => 1,
  258. 'selected' => ($iNodeId = bx_get('parent_id')) !== false ? (int)$iNodeId : 0,
  259. 'start' => ($iStart = bx_get('start')) !== false ? (int)$iStart : 0,
  260. 'per_page' => ($iPerPage = bx_get('per_page')) !== false ? (int)$iPerPage : 0,
  261. ]);
  262. return $this->_bIsApi ? [bx_api_get_block('entity_structure_l1', $mixedResult)] : $mixedResult;
  263. }
  264. public function serviceEntityStructureL2Block($iContentId = 0, $iParentId = 0)
  265. {
  266. if(!$this->_oConfig->isContent())
  267. return $this->_bIsApi ? [] : '';
  268. if(!$iParentId && ($_iParentId = bx_get('parent_id')) !== false)
  269. $iParentId = (int)$_iParentId;
  270. $mixedResult = $this->_serviceTemplateFuncEx ('entryStructureByParentMl' . $this->_oConfig->getContentLevelMax(), $iContentId, [
  271. 'parent_id' => $iParentId
  272. ]);
  273. return $this->_bIsApi ? [bx_api_get_block('entity_structure_l2', $mixedResult)] : $mixedResult;
  274. }
  275. public function serviceEntityNodeBlock($iContentId = 0, $iNodeId = 0, $iUsage = false)
  276. {
  277. if(!$this->_oConfig->isContent())
  278. return '';
  279. if(!$iNodeId && ($_iNodeId = bx_get('node_id')) !== false)
  280. $iNodeId = (int)$_iNodeId;
  281. if($iUsage === false && ($_iUsage = bx_get('usage')) !== false)
  282. $iUsage = (int)$_iUsage;
  283. return $this->_serviceTemplateFuncEx ('entryNode', $iContentId, [
  284. 'node_id' => $iNodeId,
  285. 'usage' => $iUsage
  286. ]);
  287. }
  288. public function getNodeLevelByParent($aParentInfo)
  289. {
  290. return (is_array($aParentInfo) && !empty($aParentInfo['level']) ? (int)$aParentInfo['level'] : 0) + 1;
  291. }
  292. public function getDataTotalByNode($mixedNode, $iUsage = 0)
  293. {
  294. if(!is_array($mixedNode))
  295. $mixedNode = $this->_oDb->getContentNodes([
  296. 'sample' => 'id',
  297. 'id' => (int)$mixedNode
  298. ]);
  299. $iTotal = 0;
  300. $sUsage = $this->_oConfig->getUsageI2S($iUsage);
  301. if(!empty($mixedNode['counters']) && ($aCountes = json_decode($mixedNode['counters'], true)) && !empty($aCountes[$sUsage]) && is_array($aCountes[$sUsage]))
  302. $iTotal = array_sum($aCountes[$sUsage]);
  303. return $iTotal;
  304. }
  305. public function isNodeStarted($iProfileId, $mixedNode)
  306. {
  307. $iNodeId = is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode;
  308. $aChildNodes = $this->_oDb->getContentStructure([
  309. 'sample' => 'parent_id',
  310. 'parent_id' => $iNodeId
  311. ]);
  312. if(!empty($aChildNodes) && is_array($aChildNodes)) {
  313. foreach($aChildNodes as $aChildNode)
  314. if($this->isNodePassed($iProfileId, $aChildNode['node_id']))
  315. return true;
  316. else
  317. return $this->isNodeStarted($iProfileId, $aChildNode['node_id']);
  318. }
  319. else {
  320. $aChildData = $this->_oDb->getContentData([
  321. 'sample' => 'node_id',
  322. 'node_id' => $iNodeId
  323. ]);
  324. if(!empty($aChildData) && is_array($aChildData))
  325. foreach($aChildData as $aChildData)
  326. if($this->isDataPassed($iProfileId, $aChildData['id']))
  327. return true;
  328. return false;
  329. }
  330. }
  331. public function isNodePassed($iProfileId, $mixedNode)
  332. {
  333. $aUserTrack = $this->_oDb->getContentStructure([
  334. 'sample' => 'user_passed',
  335. 'profile_id' => $iProfileId,
  336. 'node_id' => is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode
  337. ]);
  338. return !empty($aUserTrack) && is_array($aUserTrack);
  339. }
  340. public function isDataPassed($iProfileId, $mixedData)
  341. {
  342. $aUserTrack = $this->_oDb->getContentData([
  343. 'sample' => 'user_passed',
  344. 'profile_id' => $iProfileId,
  345. 'data_id' => is_array($mixedData) ? $mixedData['id'] : (int)$mixedData
  346. ]);
  347. return !empty($aUserTrack) && is_array($aUserTrack);
  348. }
  349. protected function _getOptionsContentModules()
  350. {
  351. $aResult = [];
  352. $aModules = bx_srv('system', 'get_modules_by_type', ['content']);
  353. foreach($aModules as $aModule)
  354. $aResult[] = ['key' => $aModule['name'], 'value' => $aModule['title']];
  355. return $aResult;
  356. }
  357. }
  358. /** @} */