lp.ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Responses to AJAX calls
  6. */
  7. api_protect_course_script(true);
  8. $action = $_REQUEST['a'];
  9. $course_id = api_get_course_int_id();
  10. $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
  11. $sessionId = api_get_session_id();
  12. switch ($action) {
  13. case 'get_documents':
  14. $courseInfo = api_get_course_info();
  15. $folderId = isset($_GET['folder_id']) ? $_GET['folder_id'] : null;
  16. if (empty($folderId)) {
  17. exit;
  18. }
  19. $lpId = isset($_GET['lp_id']) ? $_GET['lp_id'] : null;
  20. $url = isset($_GET['url']) ? $_GET['url'] : null;
  21. echo DocumentManager::get_document_preview(
  22. $courseInfo,
  23. $lpId,
  24. null,
  25. api_get_session_id(),
  26. true,
  27. null,
  28. $url,
  29. true,
  30. false,
  31. $folderId
  32. );
  33. break;
  34. case 'add_lp_item':
  35. if (api_is_allowed_to_edit(null, true)) {
  36. $learnPath = learnPath::getCurrentLpFromSession();
  37. if ($learnPath) {
  38. // Updating the lp.modified_on
  39. $learnPath->set_modified_on();
  40. $title = $_REQUEST['title'];
  41. if ($_REQUEST['type'] == TOOL_QUIZ) {
  42. $title = Exercise::format_title_variable($title);
  43. }
  44. $parentId = isset($_REQUEST['parent_id']) ? $_REQUEST['parent_id'] : '';
  45. $previousId = isset($_REQUEST['previous_id']) ? $_REQUEST['previous_id'] : '';
  46. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
  47. $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
  48. echo $learnPath->add_item(
  49. $parentId,
  50. $previousId,
  51. $type,
  52. $id,
  53. $title,
  54. null
  55. );
  56. $learnPath->updateCurrentLpFromSession();
  57. }
  58. }
  59. break;
  60. case 'update_lp_item_order':
  61. if (api_is_allowed_to_edit(null, true)) {
  62. $new_order = $_POST['new_order'];
  63. $sections = explode('^', $new_order);
  64. $new_array = array();
  65. // We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database
  66. $LP_item_list = new LP_item_order_list();
  67. foreach ($sections as $items) {
  68. if (!empty($items)) {
  69. list($id, $parent_id) = explode('|', $items);
  70. $item = new LP_item_order_item($id, $parent_id);
  71. $LP_item_list->add($item);
  72. }
  73. }
  74. $tab_parents_id = $LP_item_list->get_list_of_parents();
  75. foreach ($tab_parents_id as $parent_id) {
  76. $Same_parent_LP_item_list = $LP_item_list->get_item_with_same_parent($parent_id);
  77. $previous_item_id = 0;
  78. for ($i=0; $i < count($Same_parent_LP_item_list->list); $i++) {
  79. $item_id = $Same_parent_LP_item_list->list[$i]->id;
  80. // display_order
  81. $display_order = $i + 1;
  82. $LP_item_list->set_parameters_for_id($item_id, $display_order, "display_order");
  83. // previous_item_id
  84. $LP_item_list->set_parameters_for_id($item_id, $previous_item_id, "previous_item_id");
  85. $previous_item_id = $item_id;
  86. // next_item_id
  87. $next_item_id = 0;
  88. if ($i < count($Same_parent_LP_item_list->list) - 1) {
  89. $next_item_id = $Same_parent_LP_item_list->list[$i+1]->id;
  90. }
  91. $LP_item_list->set_parameters_for_id($item_id, $next_item_id, "next_item_id");
  92. }
  93. }
  94. foreach ($LP_item_list->list as $LP_item) {
  95. $params = array();
  96. $params['display_order'] = $LP_item->display_order;
  97. $params['previous_item_id'] = $LP_item->previous_item_id;
  98. $params['next_item_id'] = $LP_item->next_item_id;
  99. $params['parent_item_id'] = $LP_item->parent_item_id;
  100. Database::update(
  101. $tbl_lp_item,
  102. $params,
  103. array(
  104. 'id = ? AND c_id = ? ' => array(
  105. intval($LP_item->id),
  106. $course_id,
  107. ),
  108. )
  109. );
  110. }
  111. Display::display_confirmation_message(get_lang('Saved'));
  112. }
  113. break;
  114. case 'record_audio':
  115. if (api_is_allowed_to_edit(null, true) == false) {
  116. exit;
  117. }
  118. $learnPath = learnPath::getCurrentLpFromSession();
  119. $course_info = api_get_course_info();
  120. $lpPathInfo = $learnPath->generate_lp_folder($course_info);
  121. if (empty($lpPathInfo)) {
  122. exit;
  123. }
  124. foreach (array('video', 'audio') as $type) {
  125. if (isset($_FILES["${type}-blob"])) {
  126. $fileName = $_POST["${type}-filename"];
  127. //$file = $_FILES["${type}-blob"]["tmp_name"];
  128. $file = $_FILES["${type}-blob"];
  129. $fileInfo = pathinfo($fileName);
  130. $file['name'] = 'rec_'.date('Y-m-d_His').'_'.uniqid().'.'.$fileInfo['extension'];
  131. $file['file'] = $file;
  132. $result = DocumentManager::upload_document(
  133. $file,
  134. '/audio',
  135. $file['name'],
  136. null,
  137. 0,
  138. 'overwrite',
  139. false,
  140. false
  141. );
  142. if (!empty($result) && is_array($result)) {
  143. $newDocId = $result['id'];
  144. $courseId = $result['c_id'];
  145. $learnPath->set_modified_on();
  146. $lpItem = new learnpathItem($_REQUEST['lp_item_id']);
  147. $lpItem->add_audio_from_documents($newDocId);
  148. $data = DocumentManager::get_document_data_by_id($newDocId, $course_info['code']);
  149. echo $data['document_url'];
  150. exit;
  151. }
  152. }
  153. }
  154. break;
  155. case 'get_forum_thread':
  156. $lpId = isset($_GET['lp']) ? intval($_GET['lp']) : 0;
  157. $lpItemId = isset($_GET['lp_item']) ? intval($_GET['lp_item']) : 0;
  158. $sessionId = api_get_session_id();
  159. if (empty($lpId) || empty($lpItemId)) {
  160. echo json_encode([
  161. 'error' => true,
  162. ]);
  163. break;
  164. }
  165. $learningPath = learnpath::getLpFromSession(api_get_course_id(), $lpId, api_get_user_id());
  166. $lpItem = $learningPath->getItem($lpItemId);
  167. if (empty($lpItem)) {
  168. echo json_encode([
  169. 'error' => true,
  170. ]);
  171. break;
  172. }
  173. $lpHasForum = $learningPath->lpHasForum();
  174. if (!$lpHasForum) {
  175. echo json_encode([
  176. 'error' => true
  177. ]);
  178. break;
  179. }
  180. $forum = $learningPath->getForum($sessionId);
  181. if (empty($forum)) {
  182. require_once '../../forum/forumfunction.inc.php';
  183. $forumCategory = getForumCategoryByTitle(
  184. get_lang('LearningPaths'),
  185. $course_id,
  186. $sessionId
  187. );
  188. if (empty($forumCategory)) {
  189. $forumCategoryId = store_forumcategory(
  190. [
  191. 'lp_id' => 0,
  192. 'forum_category_title' => get_lang('LearningPaths'),
  193. 'forum_category_comment' => null
  194. ],
  195. [],
  196. false
  197. );
  198. } else {
  199. $forumCategoryId = $forumCategory['cat_id'];
  200. }
  201. $forumId = $learningPath->createForum($forumCategoryId);
  202. } else {
  203. $forumId = $forum['forum_id'];
  204. }
  205. $lpItemHasThread = $lpItem->lpItemHasThread($course_id);
  206. if (!$lpItemHasThread) {
  207. echo json_encode([
  208. 'error' => true
  209. ]);
  210. break;
  211. }
  212. $forumThread = $lpItem->getForumThread($course_id, $sessionId);
  213. if (empty($forumThread)) {
  214. $lpItem->createForumThread($forumId);
  215. $forumThread = $lpItem->getForumThread($course_id, $sessionId);
  216. }
  217. $forumThreadId = $forumThread['thread_id'];
  218. echo json_encode([
  219. 'error' => false,
  220. 'forumId' => intval($forum['forum_id']),
  221. 'threadId' => intval($forumThreadId)
  222. ]);
  223. break;
  224. case 'update_gamification':
  225. $lp = learnpath::getCurrentLpFromSession();
  226. $jsonGamification = [
  227. 'stars' => 0,
  228. 'score' => 0
  229. ];
  230. if ($lp) {
  231. $score = $lp->getCalculateScore($sessionId);
  232. $jsonGamification['stars'] = $lp->getCalculateStars($sessionId);
  233. $jsonGamification['score'] = sprintf(get_lang('XPoints'), $score);
  234. }
  235. echo json_encode($jsonGamification);
  236. break;
  237. case 'check_item_position':
  238. $lp = Session::read('oLP');
  239. $lpItemId = isset($_GET['lp_item']) ? intval($_GET['lp_item']) : 0;
  240. if ($lp) {
  241. $position = $lp->isFirstOrLastItem($lpItemId);
  242. }
  243. echo json_encode($position);
  244. break;
  245. default:
  246. echo '';
  247. }
  248. exit;
  249. /*
  250. * Classes to create a special data structure to manipulate LP Items
  251. * used only in this file
  252. * @todo move in a file
  253. * @todo use PSR
  254. */
  255. class LP_item_order_list
  256. {
  257. public $list = array();
  258. public function __construct()
  259. {
  260. $this->list = array();
  261. }
  262. public function add($in_LP_item_order_item)
  263. {
  264. $this->list[] = $in_LP_item_order_item;
  265. }
  266. public function get_item_with_same_parent($in_parent_id)
  267. {
  268. $out_res = new LP_item_order_list();
  269. for ($i = 0; $i < count($this->list); $i++) {
  270. if ($this->list[$i]->parent_item_id == $in_parent_id) {
  271. $out_res->add($this->list[$i]);
  272. }
  273. }
  274. return $out_res;
  275. }
  276. public function get_list_of_parents()
  277. {
  278. $tab_out_res = array();
  279. foreach ($this->list as $LP_item) {
  280. if (!in_array($LP_item->parent_item_id, $tab_out_res)) {
  281. $tab_out_res[] = $LP_item->parent_item_id;
  282. }
  283. }
  284. return $tab_out_res;
  285. }
  286. public function set_parameters_for_id($in_id, $in_value, $in_parameters)
  287. {
  288. for ($i = 0; $i < count($this->list); $i++) {
  289. if ($this->list[$i]->id == $in_id) {
  290. $this->list[$i]->$in_parameters = $in_value;
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. class LP_item_order_item
  297. {
  298. public $id = 0;
  299. public $parent_item_id = 0;
  300. public $previous_item_id = 0;
  301. public $next_item_id = 0;
  302. public $display_order = 0;
  303. public function __construct($in_id = 0, $in_parent_id = 0)
  304. {
  305. $this->id = $in_id;
  306. $this->parent_item_id = $in_parent_id;
  307. }
  308. }