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