lp.ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Responses to AJAX calls.
  6. */
  7. require_once __DIR__.'/../global.inc.php';
  8. api_protect_course_script(true);
  9. $debug = false;
  10. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
  11. $courseId = api_get_course_int_id();
  12. $sessionId = api_get_session_id();
  13. if ($debug) {
  14. error_log('----------lp.ajax-------------- action '.$action);
  15. }
  16. switch ($action) {
  17. case 'get_documents':
  18. $courseInfo = api_get_course_info();
  19. $folderId = isset($_GET['folder_id']) ? $_GET['folder_id'] : null;
  20. if (empty($folderId)) {
  21. exit;
  22. }
  23. $lpId = isset($_GET['lp_id']) ? $_GET['lp_id'] : false;
  24. $url = isset($_GET['url']) ? $_GET['url'] : '';
  25. $addMove = isset($_GET['add_move_button']) && $_GET['add_move_button'] == 1 ? true : false;
  26. echo DocumentManager::get_document_preview(
  27. $courseInfo,
  28. $lpId,
  29. null,
  30. api_get_session_id(),
  31. $addMove,
  32. null,
  33. $url,
  34. true,
  35. false,
  36. $folderId,
  37. false
  38. );
  39. break;
  40. case 'add_lp_item':
  41. if (api_is_allowed_to_edit(null, true)) {
  42. /** @var learnpath $learningPath */
  43. $learningPath = Session::read('oLP');
  44. if ($learningPath) {
  45. // Updating the lp.modified_on
  46. $learningPath->set_modified_on();
  47. $title = $_REQUEST['title'];
  48. if ($_REQUEST['type'] == TOOL_QUIZ) {
  49. $title = Exercise::format_title_variable($title);
  50. }
  51. $parentId = isset($_REQUEST['parent_id']) ? $_REQUEST['parent_id'] : '';
  52. $previousId = isset($_REQUEST['previous_id']) ? $_REQUEST['previous_id'] : '';
  53. $itemId = $learningPath->add_item(
  54. $parentId,
  55. $previousId,
  56. $_REQUEST['type'],
  57. $_REQUEST['id'],
  58. $title,
  59. null
  60. );
  61. /** @var learnpath $learningPath */
  62. $learningPath = Session::read('oLP');
  63. if ($learningPath) {
  64. echo $learningPath->returnLpItemList(null);
  65. }
  66. }
  67. }
  68. break;
  69. case 'update_lp_item_order':
  70. if (api_is_allowed_to_edit(null, true)) {
  71. $new_order = $_POST['new_order'];
  72. $sections = explode('^', $new_order);
  73. // We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database
  74. $itemList = new LpItemOrderList();
  75. foreach ($sections as $items) {
  76. if (!empty($items)) {
  77. list($id, $parent_id) = explode('|', $items);
  78. $item = new LpOrderItem($id, $parent_id);
  79. $itemList->add($item);
  80. }
  81. }
  82. $parents = $itemList->getListOfParents();
  83. foreach ($parents as $parentId) {
  84. $sameParentLpItemList = $itemList->getItemWithSameParent($parentId);
  85. $previous_item_id = 0;
  86. for ($i = 0; $i < count($sameParentLpItemList->list); $i++) {
  87. $item_id = $sameParentLpItemList->list[$i]->id;
  88. // display_order
  89. $display_order = $i + 1;
  90. $itemList->setParametersForId($item_id, $display_order, 'display_order');
  91. // previous_item_id
  92. $itemList->setParametersForId($item_id, $previous_item_id, 'previous_item_id');
  93. $previous_item_id = $item_id;
  94. // next_item_id
  95. $next_item_id = 0;
  96. if ($i < count($sameParentLpItemList->list) - 1) {
  97. $next_item_id = $sameParentLpItemList->list[$i + 1]->id;
  98. }
  99. $itemList->setParametersForId($item_id, $next_item_id, 'next_item_id');
  100. }
  101. }
  102. $table = Database::get_course_table(TABLE_LP_ITEM);
  103. foreach ($itemList->list as $item) {
  104. $params = [];
  105. $params['display_order'] = $item->display_order;
  106. $params['previous_item_id'] = $item->previous_item_id;
  107. $params['next_item_id'] = $item->next_item_id;
  108. $params['parent_item_id'] = $item->parent_item_id;
  109. Database::update(
  110. $table,
  111. $params,
  112. [
  113. 'iid = ? AND c_id = ? ' => [
  114. intval($item->id),
  115. $courseId,
  116. ],
  117. ]
  118. );
  119. }
  120. echo Display::return_message(get_lang('Saved'), 'confirm');
  121. }
  122. break;
  123. case 'record_audio':
  124. if (api_is_allowed_to_edit(null, true) == false) {
  125. exit;
  126. }
  127. /** @var Learnpath $lp */
  128. $lp = Session::read('oLP');
  129. $course_info = api_get_course_info();
  130. $lpPathInfo = $lp->generate_lp_folder($course_info);
  131. if (empty($lpPathInfo)) {
  132. exit;
  133. }
  134. foreach (['video', 'audio'] as $type) {
  135. if (isset($_FILES["${type}-blob"])) {
  136. $fileName = $_POST["${type}-filename"];
  137. //$file = $_FILES["${type}-blob"]["tmp_name"];
  138. $file = $_FILES["${type}-blob"];
  139. $fileInfo = pathinfo($fileName);
  140. $file['name'] = 'rec_'.date('Y-m-d_His').'_'.uniqid().'.'.$fileInfo['extension'];
  141. $file['file'] = $file;
  142. $result = DocumentManager::upload_document(
  143. $file,
  144. '/audio',
  145. $file['name'],
  146. null,
  147. 0,
  148. 'overwrite',
  149. false,
  150. false
  151. );
  152. if (!empty($result) && is_array($result)) {
  153. $newDocId = $result['id'];
  154. $courseId = $result['c_id'];
  155. $lp->set_modified_on();
  156. $lpItem = new learnpathItem($_REQUEST['lp_item_id']);
  157. $lpItem->add_audio_from_documents($newDocId);
  158. $data = DocumentManager::get_document_data_by_id($newDocId, $course_info['code']);
  159. echo $data['document_url'];
  160. exit;
  161. }
  162. }
  163. }
  164. break;
  165. case 'get_forum_thread':
  166. $lpId = isset($_GET['lp']) ? intval($_GET['lp']) : 0;
  167. $lpItemId = isset($_GET['lp_item']) ? intval($_GET['lp_item']) : 0;
  168. $sessionId = api_get_session_id();
  169. if (empty($lpId) || empty($lpItemId)) {
  170. echo json_encode([
  171. 'error' => true,
  172. ]);
  173. break;
  174. }
  175. $learningPath = learnpath::getLpFromSession(
  176. api_get_course_id(),
  177. $lpId,
  178. api_get_user_id()
  179. );
  180. $lpItem = $learningPath->getItem($lpItemId);
  181. if (empty($lpItem)) {
  182. echo json_encode([
  183. 'error' => true,
  184. ]);
  185. break;
  186. }
  187. $lpHasForum = $learningPath->lpHasForum();
  188. if (!$lpHasForum) {
  189. echo json_encode([
  190. 'error' => true,
  191. ]);
  192. break;
  193. }
  194. $forum = $learningPath->getForum($sessionId);
  195. if (empty($forum)) {
  196. require_once '../../forum/forumfunction.inc.php';
  197. $forumCategory = getForumCategoryByTitle(
  198. get_lang('LearningPaths'),
  199. $courseId,
  200. $sessionId
  201. );
  202. if (empty($forumCategory)) {
  203. $forumCategoryId = store_forumcategory(
  204. [
  205. 'lp_id' => 0,
  206. 'forum_category_title' => get_lang('LearningPaths'),
  207. 'forum_category_comment' => null,
  208. ],
  209. [],
  210. false
  211. );
  212. } else {
  213. $forumCategoryId = $forumCategory['cat_id'];
  214. }
  215. $forumId = $learningPath->createForum($forumCategoryId);
  216. } else {
  217. $forumId = $forum['forum_id'];
  218. }
  219. $lpItemHasThread = $lpItem->lpItemHasThread($courseId);
  220. if (!$lpItemHasThread) {
  221. echo json_encode([
  222. 'error' => true,
  223. ]);
  224. break;
  225. }
  226. $forumThread = $lpItem->getForumThread($courseId, $sessionId);
  227. if (empty($forumThread)) {
  228. $lpItem->createForumThread($forumId);
  229. $forumThread = $lpItem->getForumThread($courseId, $sessionId);
  230. }
  231. $forumThreadId = $forumThread['thread_id'];
  232. echo json_encode([
  233. 'error' => false,
  234. 'forumId' => intval($forum['forum_id']),
  235. 'threadId' => intval($forumThreadId),
  236. ]);
  237. break;
  238. case 'update_gamification':
  239. $lp = Session::read('oLP');
  240. $jsonGamification = [
  241. 'stars' => 0,
  242. 'score' => 0,
  243. ];
  244. if ($lp) {
  245. $score = $lp->getCalculateScore($sessionId);
  246. $jsonGamification['stars'] = $lp->getCalculateStars($sessionId);
  247. $jsonGamification['score'] = sprintf(get_lang('XPoints'), $score);
  248. }
  249. echo json_encode($jsonGamification);
  250. break;
  251. case 'check_item_position':
  252. $lp = Session::read('oLP');
  253. $lpItemId = isset($_GET['lp_item']) ? intval($_GET['lp_item']) : 0;
  254. if ($lp) {
  255. $position = $lp->isFirstOrLastItem($lpItemId);
  256. echo json_encode($position);
  257. }
  258. break;
  259. case 'get_parent_names':
  260. $newItemId = isset($_GET['new_item']) ? intval($_GET['new_item']) : 0;
  261. if (!$newItemId) {
  262. break;
  263. }
  264. /** @var \learnpath $lp */
  265. $lp = Session::read('oLP');
  266. $parentNames = $lp->getCurrentItemParentNames($newItemId);
  267. $response = '';
  268. foreach ($parentNames as $parentName) {
  269. $response .= '<p class="h5 hidden-xs hidden-md">'.$parentName.'</p>';
  270. }
  271. echo $response;
  272. break;
  273. case 'get_item_prerequisites':
  274. /** @var learnpath $lp */
  275. $lp = Session::read('oLP');
  276. $itemId = isset($_GET['item_id']) ? (int) $_GET['item_id'] : 0;
  277. if (empty($lp) || empty($itemId)) {
  278. exit;
  279. }
  280. $result = $lp->prerequisites_match($itemId);
  281. if ($result) {
  282. echo '1';
  283. } else {
  284. if (!empty($lp->error)) {
  285. echo $lp->error;
  286. } else {
  287. echo get_lang('LearnpathPrereqNotCompleted');
  288. }
  289. }
  290. $lp->error = '';
  291. exit;
  292. break;
  293. default:
  294. echo '';
  295. }
  296. exit;
  297. /**
  298. * Class LpItemOrderList
  299. * Classes to create a special data structure to manipulate LP Items
  300. * used only in this file.
  301. *
  302. * @todo move in a file
  303. * @todo use PSR
  304. */
  305. class LpItemOrderList
  306. {
  307. public $list = [];
  308. public function __construct()
  309. {
  310. $this->list = [];
  311. }
  312. /**
  313. * @param array $list
  314. */
  315. public function add($list)
  316. {
  317. $this->list[] = $list;
  318. }
  319. /**
  320. * @param int $parentId
  321. *
  322. * @return LpItemOrderList
  323. */
  324. public function getItemWithSameParent($parentId)
  325. {
  326. $list = new LpItemOrderList();
  327. for ($i = 0; $i < count($this->list); $i++) {
  328. if ($this->list[$i]->parent_item_id == $parentId) {
  329. $list->add($this->list[$i]);
  330. }
  331. }
  332. return $list;
  333. }
  334. /**
  335. * @return array
  336. */
  337. public function getListOfParents()
  338. {
  339. $result = [];
  340. foreach ($this->list as $item) {
  341. if (!in_array($item->parent_item_id, $result)) {
  342. $result[] = $item->parent_item_id;
  343. }
  344. }
  345. return $result;
  346. }
  347. /**
  348. * @param int $id
  349. * @param int $value
  350. * @param string $parameter
  351. */
  352. public function setParametersForId($id, $value, $parameter)
  353. {
  354. for ($i = 0; $i < count($this->list); $i++) {
  355. if ($this->list[$i]->id == $id) {
  356. $this->list[$i]->$parameter = $value;
  357. break;
  358. }
  359. }
  360. }
  361. }
  362. /**
  363. * Class LpOrderItem.
  364. */
  365. class LpOrderItem
  366. {
  367. public $id = 0;
  368. public $parent_item_id = 0;
  369. public $previous_item_id = 0;
  370. public $next_item_id = 0;
  371. public $display_order = 0;
  372. /**
  373. * LpOrderItem constructor.
  374. *
  375. * @param int $id
  376. * @param int $parentId
  377. */
  378. public function __construct($id = 0, $parentId = 0)
  379. {
  380. $this->id = $id;
  381. $this->parent_item_id = $parentId;
  382. }
  383. }