lp.ajax.php 11 KB

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