lp.ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. case 'get_parent_names':
  244. /** @var \learnpath $lp */
  245. $lp = Session::read('oLP');
  246. $parentNames = $lp->getCurrentItemParentNames();
  247. $response = '';
  248. foreach ($parentNames as $parentName) {
  249. $response .= '<p class="h5 hidden-xs hidden-md">'.$parentName.'</p>';
  250. }
  251. echo $response;
  252. break;
  253. default:
  254. echo '';
  255. }
  256. exit;
  257. /*
  258. * Classes to create a special data structure to manipulate LP Items
  259. * used only in this file
  260. * @todo move in a file
  261. * @todo use PSR
  262. */
  263. class LP_item_order_list
  264. {
  265. public $list = array();
  266. public function __construct()
  267. {
  268. $this->list = array();
  269. }
  270. public function add($in_LP_item_order_item)
  271. {
  272. $this->list[] = $in_LP_item_order_item;
  273. }
  274. public function get_item_with_same_parent($in_parent_id)
  275. {
  276. $out_res = new LP_item_order_list();
  277. for ($i = 0; $i < count($this->list); $i++) {
  278. if ($this->list[$i]->parent_item_id == $in_parent_id) {
  279. $out_res->add($this->list[$i]);
  280. }
  281. }
  282. return $out_res;
  283. }
  284. public function get_list_of_parents()
  285. {
  286. $tab_out_res = array();
  287. foreach ($this->list as $LP_item) {
  288. if (!in_array($LP_item->parent_item_id, $tab_out_res)) {
  289. $tab_out_res[] = $LP_item->parent_item_id;
  290. }
  291. }
  292. return $tab_out_res;
  293. }
  294. public function set_parameters_for_id($in_id, $in_value, $in_parameters)
  295. {
  296. for ($i = 0; $i < count($this->list); $i++) {
  297. if ($this->list[$i]->id == $in_id) {
  298. $this->list[$i]->$in_parameters = $in_value;
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. class LP_item_order_item
  305. {
  306. public $id = 0;
  307. public $parent_item_id = 0;
  308. public $previous_item_id = 0;
  309. public $next_item_id = 0;
  310. public $display_order = 0;
  311. public function __construct($in_id = 0, $in_parent_id = 0)
  312. {
  313. $this->id = $in_id;
  314. $this->parent_item_id = $in_parent_id;
  315. }
  316. }