BxCoursesModule.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $aData['node_id']]);
  195. if(!empty($aNode['counters'])) {
  196. $sUsage = $this->_oConfig->getUsageI2S($aData['usage']);
  197. $aCounters = json_decode($aNode['counters'], true);
  198. if(!empty($aCounters[$sUsage][$sContentType])) {
  199. if((int)$aCounters[$sUsage][$sContentType] > 1)
  200. $aCounters[$sUsage][$sContentType] -= 1;
  201. else
  202. unset($aCounters[$sUsage][$sContentType]);
  203. $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $aData['node_id']]);
  204. }
  205. }
  206. }
  207. public function serviceEntityContentStructureBlock($iProfileId = 0)
  208. {
  209. $CNF = &$this->_oConfig->CNF;
  210. if(!$this->_oConfig->isContent())
  211. return '';
  212. if(!$iProfileId)
  213. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  214. if(!$iProfileId)
  215. return '';
  216. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  217. $sGridKey = 'OBJECT_GRID_CNT_STRUCTURE_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
  218. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
  219. if(!$oGrid)
  220. return '';
  221. $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
  222. return $oGrid->getCode();
  223. }
  224. public function serviceEntityContentDataBlock($iProfileId = 0)
  225. {
  226. $CNF = &$this->_oConfig->CNF;
  227. if(!$this->_oConfig->isContent())
  228. return '';
  229. if(!$iProfileId)
  230. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  231. if(!$iProfileId)
  232. return '';
  233. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  234. $sGridKey = 'OBJECT_GRID_CNT_DATA_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
  235. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
  236. if(!$oGrid)
  237. return '';
  238. $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
  239. return $oGrid->getCode();
  240. }
  241. public function serviceEntityStructureL1Block($iContentId = 0)
  242. {
  243. if(!$this->_oConfig->isContent())
  244. return '';
  245. return $this->_serviceTemplateFuncEx ('entryStructureByLevel', $iContentId, [
  246. 'level' => 1,
  247. 'selected' => ($iNodeId = bx_get('parent_id')) !== false ? (int)$iNodeId : 0,
  248. 'start' => ($iStart = bx_get('start')) !== false ? (int)$iStart : 0,
  249. 'per_page' => ($iPerPage = bx_get('per_page')) !== false ? (int)$iPerPage : 0,
  250. ]);
  251. }
  252. public function serviceEntityStructureL2Block($iContentId = 0, $iParentId = 0)
  253. {
  254. if(!$this->_oConfig->isContent())
  255. return '';
  256. if(!$iParentId && ($_iParentId = bx_get('parent_id')) !== false)
  257. $iParentId = (int)$_iParentId;
  258. return $this->_serviceTemplateFuncEx ('entryStructureByParentMl3', $iContentId, [
  259. 'parent_id' => $iParentId
  260. ]);
  261. }
  262. public function serviceEntityNodeBlock($iContentId = 0, $iNodeId = 0, $iUsage = false)
  263. {
  264. if(!$this->_oConfig->isContent())
  265. return '';
  266. if(!$iNodeId && ($_iNodeId = bx_get('node_id')) !== false)
  267. $iNodeId = (int)$_iNodeId;
  268. if($iUsage === false && ($_iUsage = bx_get('usage')) !== false)
  269. $iUsage = (int)$_iUsage;
  270. return $this->_serviceTemplateFuncEx ('entryNode', $iContentId, [
  271. 'node_id' => $iNodeId,
  272. 'usage' => $iUsage
  273. ]);
  274. }
  275. public function getNodeLevelByParent($aParentInfo)
  276. {
  277. return (is_array($aParentInfo) && !empty($aParentInfo['level']) ? (int)$aParentInfo['level'] : 0) + 1;
  278. }
  279. public function getDataTotalByNode($mixedNode, $iUsage = 0)
  280. {
  281. if(!is_array($mixedNode))
  282. $mixedNode = $this->_oDb->getContentNodes([
  283. 'sample' => 'id',
  284. 'id' => (int)$mixedNode
  285. ]);
  286. $iTotal = 0;
  287. $sUsage = $this->_oConfig->getUsageI2S($iUsage);
  288. if(!empty($mixedNode['counters']) && ($aCountes = json_decode($mixedNode['counters'], true)) && !empty($aCountes[$sUsage]) && is_array($aCountes[$sUsage]))
  289. $iTotal = array_sum($aCountes[$sUsage]);
  290. return $iTotal;
  291. }
  292. public function isNodeStarted($iProfileId, $mixedNode)
  293. {
  294. $iNodeId = is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode;
  295. $aChildNodes = $this->_oDb->getContentStructure([
  296. 'sample' => 'parent_id',
  297. 'parent_id' => $iNodeId
  298. ]);
  299. if(!empty($aChildNodes) && is_array($aChildNodes)) {
  300. foreach($aChildNodes as $aChildNode)
  301. if($this->isNodePassed($iProfileId, $aChildNode['node_id']))
  302. return true;
  303. else
  304. return $this->isNodeStarted($iProfileId, $aChildNode['node_id']);
  305. }
  306. else {
  307. $aChildData = $this->_oDb->getContentData([
  308. 'sample' => 'node_id',
  309. 'node_id' => $iNodeId
  310. ]);
  311. if(!empty($aChildData) && is_array($aChildData))
  312. foreach($aChildData as $aChildData)
  313. if($this->isDataPassed($iProfileId, $aChildData['id']))
  314. return true;
  315. return false;
  316. }
  317. }
  318. public function isNodePassed($iProfileId, $mixedNode)
  319. {
  320. $aUserTrack = $this->_oDb->getContentStructure([
  321. 'sample' => 'user_passed',
  322. 'profile_id' => $iProfileId,
  323. 'node_id' => is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode
  324. ]);
  325. return !empty($aUserTrack) && is_array($aUserTrack);
  326. }
  327. public function isDataPassed($iProfileId, $mixedData)
  328. {
  329. $aUserTrack = $this->_oDb->getContentData([
  330. 'sample' => 'user_passed',
  331. 'profile_id' => $iProfileId,
  332. 'data_id' => is_array($mixedData) ? $mixedData['id'] : (int)$mixedData
  333. ]);
  334. return !empty($aUserTrack) && is_array($aUserTrack);
  335. }
  336. protected function _getOptionsContentModules()
  337. {
  338. $aResult = [];
  339. $aModules = bx_srv('system', 'get_modules_by_type', ['content']);
  340. foreach($aModules as $aModule)
  341. $aResult[] = ['key' => $aModule['name'], 'value' => $aModule['title']];
  342. return $aResult;
  343. }
  344. }
  345. /** @} */