lp.ajax.php 13 KB

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