BxCoursesModule.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. * How to pass a lesson's steps:
  13. * 0 - all at once, the order is not important
  14. * 1 - alternate, the order is important
  15. */
  16. define('BX_COURSES_CND_PASSING_ALL', 0);
  17. define('BX_COURSES_CND_PASSING_ALTERNATE', 1);
  18. /**
  19. * Content data usage:
  20. * st - step,
  21. * at - attachment
  22. */
  23. define('BX_COURSES_CND_USAGE_ST', 0);
  24. define('BX_COURSES_CND_USAGE_AT', 1);
  25. /**
  26. * Courses profiles module.
  27. */
  28. class BxCoursesModule extends BxBaseModGroupsModule
  29. {
  30. function __construct(&$aModule)
  31. {
  32. parent::__construct($aModule);
  33. $this->_aSearchableNamesExcept[] = $this->_oConfig->CNF['FIELD_JOIN_CONFIRMATION'];
  34. }
  35. public function actionPerform()
  36. {
  37. $CNF = &$this->_oConfig->CNF;
  38. $mixedContent = $this->_getContent();
  39. if($mixedContent === false)
  40. return echoJson(['code' => 1]);
  41. list($iContentId, $aContentInfo) = $mixedContent;
  42. $sAction = bx_process_input(bx_get('action'));
  43. if(empty($CNF['MENU_ITEM_TO_METHOD'][$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']][$sAction]))
  44. return echoJson(['code' => 2]);
  45. $sMethodCheck = $CNF['MENU_ITEM_TO_METHOD'][$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']][$sAction];
  46. $sMethodPerform = '_perform' . bx_gen_method_name($sAction, ['-', '_']);
  47. if(!method_exists($this, $sMethodCheck) || $this->$sMethodCheck($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED || !method_exists($this, $sMethodPerform))
  48. return echoJson(['code' => 3, 'msg' => _t('_sys_txt_access_denied')]);
  49. return echoJson($this->$sMethodPerform($aContentInfo) ? ['reload' => 1] : []);
  50. }
  51. public function actionPassNode()
  52. {
  53. $iNodeId = bx_get('node_id');
  54. if($iNodeId === false)
  55. return echoJson([]);
  56. return echoJson($this->servicePassNode((int)$iNodeId));
  57. }
  58. public function actionPassData()
  59. {
  60. $iDataId = bx_get('data_id');
  61. if($iDataId === false)
  62. return echoJson([]);
  63. return echoJson($this->servicePassData((int)$iDataId));
  64. }
  65. public function serviceGetSafeServices()
  66. {
  67. return array_merge(parent::serviceGetSafeServices(), [
  68. 'Hide' => '',
  69. 'Publish' => '',
  70. 'PassNode' => '',
  71. 'PassData' => '',
  72. ]);
  73. }
  74. public function serviceHide($iContentId)
  75. {
  76. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  77. if($this->checkAllowedHide($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  78. return false;
  79. return $this->_performHideCourseProfile($aContentInfo);
  80. }
  81. public function servicePublish($iContentId)
  82. {
  83. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  84. if($this->checkAllowedUnhide($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  85. return false;
  86. return $this->_performUnhideCourseProfile($aContentInfo);
  87. }
  88. public function servicePassNode($iNodeId)
  89. {
  90. $CNF = &$this->_oConfig->CNF;
  91. $aNode = $this->_oDb->getContentNodes([
  92. 'sample' => 'id',
  93. 'id' => $iNodeId
  94. ]);
  95. if(empty($aNode) || !is_array($aNode))
  96. return ['code' => 1];
  97. $iEntryId = (int)$aNode['entry_id'];
  98. $iProfileId = bx_get_logged_profile_id();
  99. if($this->isNodePassed($iProfileId, $iNodeId)) {
  100. $aDataItems = $this->_oDb->getContentData([
  101. 'sample' => 'entry_node_ids',
  102. 'entry_id' => $iEntryId,
  103. 'node_id' => $iNodeId,
  104. ]);
  105. if(!empty($aDataItems) && is_array($aDataItems))
  106. foreach($aDataItems as $aDataItem)
  107. $this->_oDb->deleteContentData2Users(['data_id' => $aDataItem['id'], 'profile_id' => $iProfileId]);
  108. $this->_oDb->deleteContentNodes2Users(['node_id' => $iNodeId, 'profile_id' => $iProfileId]);
  109. }
  110. $sLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY_NODE'] . '&id=' . $iEntryId, [
  111. 'node_id' => $iNodeId
  112. ]);
  113. return [
  114. 'code' => 0,
  115. 'redirect' => $this->_bIsApi ? bx_api_get_relative_url($sLink) : $sLink
  116. ];
  117. }
  118. public function servicePassData($iDataId)
  119. {
  120. $iNow = time();
  121. $iProfileId = bx_get_logged_profile_id();
  122. $aData = $this->_oDb->getContentData([
  123. 'sample' => 'id',
  124. 'id' => $iDataId
  125. ]);
  126. if(empty($aData) || !is_array($aData))
  127. return ['code' => 1];
  128. $this->_oDb->insertContentData2Users([
  129. 'data_id' => $iDataId,
  130. 'profile_id' => $iProfileId,
  131. 'date' => $iNow
  132. ]);
  133. $aNode = $this->_oDb->getContentNodes([
  134. 'sample' => 'id_full',
  135. 'id' => $aData['node_id']
  136. ]);
  137. $iTotal = $this->getDataTotalByNode($aNode, BX_COURSES_CND_USAGE_ST);
  138. $aUserTrack = $this->_oDb->getContentData([
  139. 'sample' => 'user_track',
  140. 'entry_id' => $aNode['entry_id'],
  141. 'node_id' => $aNode['id'],
  142. 'profile_id' => $iProfileId
  143. ]);
  144. if(count($aUserTrack) == $iTotal) {
  145. $this->_oDb->insertContentNodes2Users([
  146. 'node_id' => $aNode['id'],
  147. 'profile_id' => $iProfileId,
  148. 'date' => $iNow
  149. ]);
  150. $this->_autoPassNodes($iProfileId, $aNode['parent_id']);
  151. }
  152. $aResult = ['code' => 0];
  153. if(($sMethod = 'get_view') && bx_is_srv($aData['content_type'], $sMethod)) {
  154. $sView = bx_srv($aData['content_type'], $sMethod, [$aData['content_id']]);
  155. $aResult = $this->_oTemplate->entryData($aData, $sView);
  156. }
  157. else if(($sMethod = 'get_link') && bx_is_srv($aData['content_type'], $sMethod))
  158. $aResult['redirect'] = bx_srv($aData['content_type'], $sMethod, [$aData['content_id']]);
  159. if($this->_bIsApi)
  160. $aResult['redirect'] = bx_api_get_relative_url($aResult['redirect']);
  161. return $aResult;
  162. }
  163. protected function _autoPassNodes($iProfileId, $iNodeId)
  164. {
  165. $aNode = $this->_oDb->getContentStructure([
  166. 'sample' => 'node_id',
  167. 'node_id' => $iNodeId,
  168. ]);
  169. if(empty($aNode) || !is_array($aNode))
  170. return;
  171. $iTotal = (int)$aNode['cn_l' . ($aNode['level'] + 1)];
  172. if(!$iTotal)
  173. return;
  174. $aUserTrack = $this->_oDb->getContentStructure([
  175. 'sample' => 'user_track',
  176. 'profile_id' => $iProfileId,
  177. 'entry_id' => $aNode['entry_id'],
  178. 'node_id' => $iNodeId
  179. ]);
  180. if(count($aUserTrack) == $iTotal) {
  181. $this->_oDb->insertContentNodes2Users([
  182. 'node_id' => $iNodeId,
  183. 'profile_id' => $iProfileId,
  184. 'date' => time()
  185. ]);
  186. if((int)$aNode['parent_id'] != 0)
  187. $this->_autoPassNodes($iProfileId, $aNode['parent_id']);
  188. }
  189. }
  190. public function serviceGetOptionsContentModulesSt()
  191. {
  192. return $this->_getOptionsContentModules();
  193. }
  194. public function serviceGetOptionsContentModulesAt()
  195. {
  196. return $this->_getOptionsContentModules();
  197. }
  198. public function serviceIsContentAvaliable($iProfileId)
  199. {
  200. if(!$this->_oConfig->isContent())
  201. return false;
  202. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  203. if($this->checkAllowedEdit($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  204. return false;
  205. return true;
  206. }
  207. public function serviceOnContentAdded($sContentType, $iContentId, $iContextId, $iContextNodeId, $iContextUsage)
  208. {
  209. $this->_oDb->insertContentData([
  210. 'entry_id' => $iContextId,
  211. 'node_id' => $iContextNodeId,
  212. 'content_type' => $sContentType,
  213. 'content_id' => $iContentId,
  214. 'usage' => $iContextUsage,
  215. 'added' => time(),
  216. 'order' => $this->_oDb->getContentDataOrderMax($iContextId, $iContextNodeId) + 1
  217. ]);
  218. $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $iContextNodeId]);
  219. $aCounters = [];
  220. if(!empty($aNode['counters']))
  221. $aCounters = json_decode($aNode['counters'], true);
  222. $sUsage = $this->_oConfig->getUsageI2S($iContextUsage);
  223. if(!isset($aCounters[$sUsage]))
  224. $aCounters[$sUsage] = [];
  225. if(!isset($aCounters[$sUsage][$sContentType]))
  226. $aCounters[$sUsage][$sContentType] = 0;
  227. $aCounters[$sUsage][$sContentType] += 1;
  228. $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $iContextNodeId]);
  229. }
  230. public function serviceOnContentAddedRedirect($sContentType, $iContentId)
  231. {
  232. return $this->serviceOnContentActionRedirect('add', $sContentType, $iContentId);
  233. }
  234. public function serviceOnContentEditedRedirect($sContentType, $iContentId)
  235. {
  236. return $this->serviceOnContentActionRedirect('edit', $sContentType, $iContentId);
  237. }
  238. public function serviceOnContentActionRedirect($sAction, $sContentType, $iContentId)
  239. {
  240. $CNF = &$this->_oConfig->CNF;
  241. $aData = $this->_oDb->getContentData(['sample' => 'content', 'content_type' => $sContentType, 'content_id' => $iContentId]);
  242. if(empty($aData) || !is_array($aData))
  243. return false;
  244. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_EDIT_ENTRY_CONTENT'] . '&id=' . $aData['entry_id'], [
  245. 'parent_id' => $aData['node_id']
  246. ]);
  247. return $this->_bIsApi ? '' : $sUrl;
  248. }
  249. public function serviceOnContentDeleted($sContentType, $iContentId, $iContextId)
  250. {
  251. $aData = $this->_oDb->getContentData(['sample' => 'content', 'content_type' => $sContentType, 'content_id' => $iContentId]);
  252. if(empty($aData) || !is_array($aData))
  253. return;
  254. $this->_oDb->deleteContentData([
  255. 'entry_id' => $iContextId,
  256. 'content_type' => $sContentType,
  257. 'content_id' => $iContentId,
  258. ]);
  259. $this->_oDb->deleteContentData2Users([
  260. 'data_id' => $aData['id']
  261. ]);
  262. $aNode = $this->_oDb->getContentNodes(['sample' => 'id', 'id' => $aData['node_id']]);
  263. if(!empty($aNode['counters'])) {
  264. $sUsage = $this->_oConfig->getUsageI2S($aData['usage']);
  265. $aCounters = json_decode($aNode['counters'], true);
  266. if(!empty($aCounters[$sUsage][$sContentType])) {
  267. if((int)$aCounters[$sUsage][$sContentType] > 1)
  268. $aCounters[$sUsage][$sContentType] -= 1;
  269. else
  270. unset($aCounters[$sUsage][$sContentType]);
  271. $this->_oDb->updateContentNodes(['counters' => json_encode($aCounters)], ['id' => $aData['node_id']]);
  272. }
  273. }
  274. }
  275. public function serviceJoinedEntriesSummary ($iProfileId = 0)
  276. {
  277. if (!$iProfileId)
  278. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  279. if (!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  280. return false;
  281. if ($iProfileId != $this->_iProfileId)
  282. return false;
  283. return $this->_oTemplate->getJoinedEntriesSummary($iProfileId);
  284. }
  285. public function serviceEntityContentStructureBlock($iProfileId = 0)
  286. {
  287. $CNF = &$this->_oConfig->CNF;
  288. if(!$this->_oConfig->isContent())
  289. return '';
  290. if(!$iProfileId)
  291. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  292. if(!$iProfileId)
  293. return '';
  294. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  295. $sGridKey = 'OBJECT_GRID_CNT_STRUCTURE_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
  296. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
  297. if(!$oGrid)
  298. return '';
  299. $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
  300. if($this->_bIsApi)
  301. return ($aGrid = $oGrid->getCodeAPI()) ? [
  302. bx_api_get_block('grid', $aGrid)
  303. ] : [];
  304. return $oGrid->getCode();
  305. }
  306. public function serviceEntityContentDataBlock($iProfileId = 0)
  307. {
  308. $CNF = &$this->_oConfig->CNF;
  309. if(!$this->_oConfig->isContent())
  310. return '';
  311. if(!$iProfileId)
  312. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  313. if(!$iProfileId)
  314. return '';
  315. $aContentInfo = $this->_oDb->getContentInfoByProfileId($iProfileId);
  316. $sGridKey = 'OBJECT_GRID_CNT_DATA_' . ($this->checkAllowedEdit($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ? 'MANAGE' : 'VIEW');
  317. $oGrid = BxDolGrid::getObjectInstance($CNF[$sGridKey]);
  318. if(!$oGrid)
  319. return '';
  320. $oGrid->setEntryId($aContentInfo[$CNF['FIELD_ID']]);
  321. if($this->_bIsApi)
  322. return ($aGrid = $oGrid->getCodeAPI()) ? [
  323. bx_api_get_block('grid', $aGrid)
  324. ] : [];
  325. return $oGrid->getCode();
  326. }
  327. public function serviceEntityStructureL1Block($iContentId = 0)
  328. {
  329. if(!$this->_oConfig->isContent() || $this->_oConfig->getContentLevelMax() == 1)
  330. return $this->_bIsApi ? [] : '';
  331. $mixedResult = $this->_serviceTemplateFuncEx ('entryStructureByLevel', $iContentId, [
  332. 'level' => 1,
  333. 'selected' => ($iNodeId = bx_get('parent_id')) !== false ? (int)$iNodeId : 0,
  334. 'start' => ($iStart = bx_get('start')) !== false ? (int)$iStart : 0,
  335. 'per_page' => ($iPerPage = bx_get('per_page')) !== false ? (int)$iPerPage : $this->_oConfig->getPerPageDefault('structure_l1'),
  336. ]);
  337. return $this->_bIsApi ? [bx_api_get_block('course_structure', $mixedResult)] : $mixedResult;
  338. }
  339. public function serviceEntityStructureL2Block($iContentId = 0, $iParentId = 0)
  340. {
  341. if(!$this->_oConfig->isContent())
  342. return $this->_bIsApi ? [] : '';
  343. if(!$iParentId && ($_iParentId = bx_get('parent_id')) !== false)
  344. $iParentId = (int)$_iParentId;
  345. $mixedResult = $this->_serviceTemplateFuncEx ('entryStructureByParentMl' . $this->_oConfig->getContentLevelMax(), $iContentId, [
  346. 'parent_id' => $iParentId
  347. ]);
  348. return $this->_bIsApi ? [bx_api_get_block('module_structure', $mixedResult)] : $mixedResult;
  349. }
  350. public function serviceEntityNodeBlock($iContentId = 0, $iNodeId = 0, $iUsage = false)
  351. {
  352. if(!$this->_oConfig->isContent())
  353. return $this->_bIsApi ? [] : '';
  354. if(!$iNodeId && ($_iNodeId = bx_get('node_id')) !== false)
  355. $iNodeId = (int)$_iNodeId;
  356. if($iUsage === false && ($_iUsage = bx_get('usage')) !== false)
  357. $iUsage = (int)$_iUsage;
  358. $mixedResult = $this->_serviceTemplateFuncEx('entryNode', $iContentId, [
  359. 'node_id' => $iNodeId,
  360. 'usage' => $iUsage
  361. ]);
  362. return $this->_bIsApi ? [bx_api_get_block('lesson_structure', $mixedResult)] : $mixedResult;
  363. }
  364. public function checkAllowedHide($aDataEntry, $isPerformAction = false)
  365. {
  366. $CNF = &$this->_oConfig->CNF;
  367. if($aDataEntry[$CNF['FIELD_STATUS']] != 'active')
  368. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  369. // moderator and owner always have access
  370. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  371. return CHECK_ACTION_RESULT_ALLOWED;
  372. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  373. }
  374. public function checkAllowedUnhide($aDataEntry, $isPerformAction = false)
  375. {
  376. $CNF = &$this->_oConfig->CNF;
  377. if($aDataEntry[$CNF['FIELD_STATUS']] == 'active')
  378. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  379. // moderator and owner always have access
  380. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  381. return CHECK_ACTION_RESULT_ALLOWED;
  382. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  383. }
  384. public function getNodeLevelByParent($aParentInfo)
  385. {
  386. return (is_array($aParentInfo) && !empty($aParentInfo['level']) ? (int)$aParentInfo['level'] : 0) + 1;
  387. }
  388. public function getDataTotalByNode($mixedNode, $iUsage = 0)
  389. {
  390. if(!is_array($mixedNode))
  391. $mixedNode = $this->_oDb->getContentNodes([
  392. 'sample' => 'id',
  393. 'id' => (int)$mixedNode
  394. ]);
  395. $iTotal = 0;
  396. $sUsage = $this->_oConfig->getUsageI2S($iUsage);
  397. if(!empty($mixedNode['counters']) && ($aCountes = json_decode($mixedNode['counters'], true)) && !empty($aCountes[$sUsage]) && is_array($aCountes[$sUsage]))
  398. $iTotal = array_sum($aCountes[$sUsage]);
  399. return $iTotal;
  400. }
  401. public function isNodeStarted($iProfileId, $mixedNode)
  402. {
  403. $iNodeId = is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode;
  404. $aChildNodes = $this->_oDb->getContentStructure([
  405. 'sample' => 'parent_id',
  406. 'parent_id' => $iNodeId
  407. ]);
  408. if(!empty($aChildNodes) && is_array($aChildNodes)) {
  409. foreach($aChildNodes as $aChildNode)
  410. if($this->isNodePassed($iProfileId, $aChildNode['node_id']))
  411. return true;
  412. else
  413. return $this->isNodeStarted($iProfileId, $aChildNode['node_id']);
  414. }
  415. else {
  416. $aChildData = $this->_oDb->getContentData([
  417. 'sample' => 'node_id',
  418. 'node_id' => $iNodeId
  419. ]);
  420. if(!empty($aChildData) && is_array($aChildData))
  421. foreach($aChildData as $aChildData)
  422. if($this->isDataPassed($iProfileId, $aChildData['id']))
  423. return true;
  424. return false;
  425. }
  426. }
  427. public function isNodePassed($iProfileId, $mixedNode)
  428. {
  429. $aUserTrack = $this->_oDb->getContentStructure([
  430. 'sample' => 'user_passed',
  431. 'profile_id' => $iProfileId,
  432. 'node_id' => is_array($mixedNode) ? $mixedNode['id'] : (int)$mixedNode
  433. ]);
  434. return !empty($aUserTrack) && is_array($aUserTrack);
  435. }
  436. public function isDataPassed($iProfileId, $mixedData)
  437. {
  438. $aUserTrack = $this->_oDb->getContentData([
  439. 'sample' => 'user_passed',
  440. 'profile_id' => $iProfileId,
  441. 'data_id' => is_array($mixedData) ? $mixedData['id'] : (int)$mixedData
  442. ]);
  443. return !empty($aUserTrack) && is_array($aUserTrack);
  444. }
  445. public function getEntryPass($iProfileId, $iContentId)
  446. {
  447. $aChildren = $this->_oDb->getContentStructure([
  448. 'sample' => 'entry_id',
  449. 'entry_id' => $iContentId,
  450. 'level' => 1
  451. ]);
  452. $aPassDetails = [];
  453. foreach($aChildren as $aChild)
  454. $this->getNodePass($iProfileId, $iContentId, $aChild, $aPassDetails);
  455. $iPassPercent = 0;
  456. $sPassStatus = '_bx_courses_txt_status_not_started';
  457. $sPassTitle = '_bx_courses_txt_pass_start';
  458. $iLevelMax = $this->_oConfig->getContentLevelMax();
  459. if(isset($aPassDetails[$iLevelMax]) && ($aPassLevelMax = $aPassDetails[$iLevelMax])) {
  460. $iPassPercent = ($iTotal = (int)$aPassLevelMax['total']) != 0 ? (int)round(100 * $aPassLevelMax['passed']/$iTotal) : 0;
  461. if($aPassLevelMax['passed'] != 0) {
  462. if($aPassLevelMax['passed'] != $aPassLevelMax['total']) {
  463. $sPassStatus = '_bx_courses_txt_status_in_process';
  464. $sPassTitle = '_bx_courses_txt_pass_continue';
  465. }
  466. else {
  467. $sPassStatus = '_bx_courses_txt_status_completed';
  468. $sPassTitle = '_bx_courses_txt_pass_again';
  469. }
  470. }
  471. }
  472. return [
  473. $iPassPercent,
  474. $aPassDetails,
  475. _t($sPassStatus),
  476. _t($sPassTitle)
  477. ];
  478. }
  479. public function getNodePass($iProfileId, $iContentId, $aNode, &$aResults)
  480. {
  481. $iLevel = $aNode['level'];
  482. if(!isset($aResults[$iLevel]))
  483. $aResults[$iLevel] = [
  484. 'passed' => 0,
  485. 'total' => 0
  486. ];
  487. $aResults[$iLevel]['passed'] += $this->isNodePassed($iProfileId, $aNode['node_id']);
  488. $aResults[$iLevel]['total'] += 1;
  489. if($iLevel != $this->_oConfig->getContentLevelMax() && !empty($aNode['cn_l' . ($iLevel + 1)])) {
  490. $aChildren = $this->_oDb->getContentStructure([
  491. 'sample' => 'parent_id',
  492. 'parent_id' => $aNode['node_id']
  493. ]);
  494. foreach($aChildren as $aChild)
  495. $this->getNodePass($iProfileId, $iContentId, $aChild, $aResults);
  496. }
  497. }
  498. public function getNodePassByChildren($iProfileId, $iContentId, $aNode, &$aResults)
  499. {
  500. $aUserTrack = $this->_oDb->getContentStructure([
  501. 'sample' => 'user_track',
  502. 'entry_id' => $iContentId,
  503. 'node_id' => $aNode['node_id'],
  504. 'profile_id' => $iProfileId
  505. ]);
  506. $iLevelChildren = $aNode['level'] + 1;
  507. $iCountChildren = $aNode['cn_l' . $iLevelChildren];
  508. if(!isset($aResults[$iLevelChildren]))
  509. $aResults[$iLevelChildren] = [
  510. 'passed' => 0,
  511. 'total' => 0
  512. ];
  513. $aResults[$iLevelChildren]['passed'] += count($aUserTrack);
  514. $aResults[$iLevelChildren]['total'] += $iCountChildren;
  515. if($iCountChildren && $iLevelChildren != $this->_oConfig->getContentLevelMax()) {
  516. $aChildren = $this->_oDb->getContentStructure([
  517. 'sample' => 'parent_id',
  518. 'parent_id' => $aNode['node_id']
  519. ]);
  520. foreach($aChildren as $aChild)
  521. $this->getNodePassByChildren($iProfileId, $iContentId, $aChild, $aResults);
  522. }
  523. }
  524. public function getNodePassByData($iProfileId, $iContentId, $aNode)
  525. {
  526. $iTotal = 0;
  527. $iPassCount = $iPassPercent = 0;
  528. $sPassStatus = $sPassTitle = '';
  529. if(($iTotal = $this->getDataTotalByNode($aNode)) != 0) {
  530. $aUserTrack = $this->_oDb->getContentData([
  531. 'sample' => 'user_track',
  532. 'entry_id' => $iContentId,
  533. 'node_id' => $aNode['node_id'],
  534. 'profile_id' => $iProfileId
  535. ]);
  536. $iPassCount = count($aUserTrack);
  537. $iPassPercent = (int)round(100 * $iPassCount/$iTotal);
  538. if($iPassCount == 0) {
  539. $sPassStatus = '_bx_courses_txt_status_not_started';
  540. $sPassTitle = '_bx_courses_txt_pass_start';
  541. }
  542. else {
  543. if($iPassCount != $iTotal) {
  544. $sPassStatus = '_bx_courses_txt_status_in_process';
  545. $sPassTitle = '_bx_courses_txt_pass_continue';
  546. }
  547. else {
  548. $sPassStatus = '_bx_courses_txt_status_completed';
  549. $sPassTitle = '_bx_courses_txt_pass_again';
  550. }
  551. }
  552. }
  553. return [
  554. $iPassPercent,
  555. _t('_bx_courses_txt_n_m_steps', $iPassCount, $iTotal),
  556. _t($sPassStatus ? $sPassStatus : '_undefined'),
  557. _t($sPassTitle)
  558. ];
  559. }
  560. public function decodeDataAPI($aData, $aParams = [])
  561. {
  562. $CNF = $this->_oConfig->CNF;
  563. $aResult = parent::decodeDataAPI($aData, $aParams);
  564. $aResultAdd = [
  565. $CNF['FIELD_STATUS'] => $aData[$CNF['FIELD_STATUS']]
  566. ];
  567. $iEntryId = (int)$aData[$CNF['FIELD_ID']];
  568. $aLevelToNode = $this->_oConfig->getContentLevel2Node(false);
  569. if($this->isFan($iEntryId, $this->_iProfileId)) {
  570. $sTxtProgress = _t('_bx_courses_txt_n_m_progress');
  571. list($iPassPercent, $aPassDetails, $sPassStatus, $sPassTitle) = $this->getEntryPass($this->_iProfileId, $iEntryId);
  572. $aCounters = [];
  573. foreach($aPassDetails as $iLevel => $aDetails) {
  574. $aCounters[] = [
  575. 'level' => $iLevel,
  576. 'title' => $aLevelToNode[$iLevel],
  577. 'passed' => $aDetails['passed'],
  578. 'total' => $aDetails['total'],
  579. 'progress' => bx_replace_markers($sTxtProgress, $aDetails)
  580. ];
  581. }
  582. $aResultAdd = [
  583. 'percent' => $iPassPercent,
  584. 'counters' => $aCounters,
  585. 'pass_status' => $sPassStatus,
  586. 'show_pass' => $iPassPercent > 0 && $iPassPercent < 100,
  587. ];
  588. if($aResultAdd['show_pass'])
  589. $aResultAdd = array_merge($aResultAdd, [
  590. 'pass_link' => $aResult['url'],
  591. 'pass_title' => $sPassTitle,
  592. ]);
  593. }
  594. else {
  595. $iLevelMax = $this->_oConfig->getContentLevelMax();
  596. $aEntryCounters = $this->_oDb->getContentStructure(['sample' => 'entry_id_counters', 'entry_id' => $iEntryId]);
  597. $aCounters = [];
  598. if(!empty($aEntryCounters) && is_array($aEntryCounters))
  599. for($i = 1; $i <= $iLevelMax; $i++) {
  600. $aCounters[] = [
  601. 'level' => $i,
  602. 'title' => $aLevelToNode[$i],
  603. 'total' => $aEntryCounters['cn_l' . $i]
  604. ];
  605. }
  606. $aResultAdd = [
  607. 'counters' => $aCounters,
  608. ];
  609. }
  610. return array_merge($aResult, $aResultAdd);
  611. }
  612. protected function _getOptionsContentModules()
  613. {
  614. $aResult = [];
  615. $aModules = bx_srv('system', 'get_modules_by_type', ['content']);
  616. foreach($aModules as $aModule)
  617. $aResult[] = ['key' => $aModule['name'], 'value' => $aModule['title']];
  618. return $aResult;
  619. }
  620. protected function _performHideCourseProfile($aDataEntry)
  621. {
  622. $CNF = &$this->_oConfig->CNF;
  623. $iContentId = $aDataEntry[$CNF['FIELD_ID']];
  624. if(!$this->_oDb->updateEntriesBy([$CNF['FIELD_STATUS'] => 'hidden'], [$CNF['FIELD_ID'] => $iContentId]))
  625. return false;
  626. $this->checkAllowedHide($aDataEntry, true);
  627. return $this->_bIsApi ? [
  628. 'title' => _t($CNF['T']['menu_item_title_unhide']),
  629. 'request_url' => $this->_oConfig->getName() . '/publish/&params[]=' . $iContentId,
  630. ] : true;
  631. }
  632. protected function _performUnhideCourseProfile($aDataEntry)
  633. {
  634. $CNF = &$this->_oConfig->CNF;
  635. $iContentId = $aDataEntry[$CNF['FIELD_ID']];
  636. if(!$this->_oDb->updateEntriesBy([$CNF['FIELD_STATUS'] => 'active'], [$CNF['FIELD_ID'] => $iContentId]))
  637. return false;
  638. $this->checkAllowedHide($aDataEntry, true);
  639. return $this->_bIsApi ? [
  640. 'title' => _t($CNF['T']['menu_item_title_hide']),
  641. 'request_url' => $this->_oConfig->getName() . '/hide/&params[]=' . $iContentId,
  642. ] : true;
  643. }
  644. }
  645. /** @} */