lp.ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. require_once '../global.inc.php';
  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. if ($_SESSION['oLP']) {
  37. // Updating the lp.modified_on
  38. $_SESSION['oLP']->set_modified_on();
  39. $title = $_REQUEST['title'];
  40. if ($_REQUEST['type'] == TOOL_QUIZ) {
  41. $title = Exercise::format_title_variable($title);
  42. }
  43. $parentId = isset($_REQUEST['parent_id']) ? $_REQUEST['parent_id'] : '';
  44. $previousId = isset($_REQUEST['previous_id']) ? $_REQUEST['previous_id'] : '';
  45. echo $_SESSION['oLP']->add_item(
  46. $parentId,
  47. $previousId,
  48. $_REQUEST['type'],
  49. $_REQUEST['id'],
  50. $title,
  51. null
  52. );
  53. }
  54. }
  55. break;
  56. case 'update_lp_item_order':
  57. if (api_is_allowed_to_edit(null, true)) {
  58. $new_order = $_POST['new_order'];
  59. $sections = explode('^', $new_order);
  60. $new_array = array();
  61. // We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database
  62. $LP_item_list = new LP_item_order_list();
  63. foreach ($sections as $items) {
  64. if (!empty($items)) {
  65. list($id, $parent_id) = explode('|', $items);
  66. $item = new LP_item_order_item($id, $parent_id);
  67. $LP_item_list->add($item);
  68. }
  69. }
  70. $tab_parents_id = $LP_item_list->get_list_of_parents();
  71. foreach ($tab_parents_id as $parent_id) {
  72. $Same_parent_LP_item_list = $LP_item_list->get_item_with_same_parent($parent_id);
  73. $previous_item_id = 0;
  74. for ($i=0; $i < count($Same_parent_LP_item_list->list);$i++) {
  75. $item_id = $Same_parent_LP_item_list->list[$i]->id;
  76. // display_order
  77. $display_order = $i + 1;
  78. $LP_item_list->set_parameters_for_id($item_id, $display_order, "display_order");
  79. // previous_item_id
  80. $LP_item_list->set_parameters_for_id($item_id, $previous_item_id, "previous_item_id");
  81. $previous_item_id = $item_id;
  82. // next_item_id
  83. $next_item_id = 0;
  84. if ($i < count($Same_parent_LP_item_list->list) - 1) {
  85. $next_item_id = $Same_parent_LP_item_list->list[$i+1]->id;
  86. }
  87. $LP_item_list->set_parameters_for_id($item_id, $next_item_id, "next_item_id");
  88. }
  89. }
  90. foreach ($LP_item_list->list as $LP_item) {
  91. $params = array();
  92. $params['display_order'] = $LP_item->display_order;
  93. $params['previous_item_id'] = $LP_item->previous_item_id;
  94. $params['next_item_id'] = $LP_item->next_item_id;
  95. $params['parent_item_id'] = $LP_item->parent_item_id;
  96. Database::update(
  97. $tbl_lp_item,
  98. $params,
  99. array(
  100. 'id = ? AND c_id = ? ' => array(
  101. intval($LP_item->id),
  102. $course_id,
  103. ),
  104. )
  105. );
  106. }
  107. Display::display_confirmation_message(get_lang('Saved'));
  108. }
  109. break;
  110. case 'record_audio':
  111. if (api_is_allowed_to_edit(null, true) == false) {
  112. exit;
  113. }
  114. /** @var Learnpath $lp */
  115. $lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
  116. $course_info = api_get_course_info();
  117. $lpPathInfo = $lp->generate_lp_folder($course_info);
  118. if (empty($lpPathInfo)) {
  119. exit;
  120. }
  121. foreach (array('video', 'audio') as $type) {
  122. if (isset($_FILES["${type}-blob"])) {
  123. $fileName = $_POST["${type}-filename"];
  124. //$file = $_FILES["${type}-blob"]["tmp_name"];
  125. $file = $_FILES["${type}-blob"];
  126. $fileInfo = pathinfo($fileName);
  127. $file['name'] = 'rec_'.date('Y-m-d_His').'_'.uniqid().'.'.$fileInfo['extension'];
  128. $file['file'] = $file;
  129. $lpPathInfo['dir'] = api_remove_trailing_slash($lpPathInfo['dir']);
  130. $result = DocumentManager::upload_document(
  131. $file,
  132. $lpPathInfo['dir'],
  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 = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
  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. default:
  236. echo '';
  237. }
  238. exit;
  239. /*
  240. * Classes to create a special data structure to manipulate LP Items
  241. * used only in this file
  242. * @todo move in a file
  243. */
  244. class LP_item_order_list {
  245. public $list = array();
  246. public function __construct() {
  247. $this->list = array();
  248. }
  249. public function add($in_LP_item_order_item) {
  250. $this->list[] = $in_LP_item_order_item;
  251. }
  252. public function get_item_with_same_parent($in_parent_id) {
  253. $out_res = new LP_item_order_list();
  254. for ($i=0; $i < count($this->list); $i++) {
  255. if ($this->list[$i]->parent_item_id == $in_parent_id) {
  256. $out_res->add($this->list[$i]);
  257. }
  258. }
  259. return $out_res;
  260. }
  261. public function get_list_of_parents() {
  262. $tab_out_res = array();
  263. foreach ($this->list as $LP_item) {
  264. if (!in_array($LP_item->parent_item_id, $tab_out_res)) {
  265. $tab_out_res[] = $LP_item->parent_item_id;
  266. }
  267. }
  268. return $tab_out_res;
  269. }
  270. public function set_parameters_for_id($in_id, $in_value, $in_parameters) {
  271. for ($i=0; $i < count($this->list); $i++) {
  272. if ($this->list[$i]->id == $in_id) {
  273. $this->list[$i]->$in_parameters = $in_value;
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. class LP_item_order_item {
  280. public $id = 0;
  281. public $parent_item_id = 0;
  282. public $previous_item_id = 0;
  283. public $next_item_id = 0;
  284. public $display_order = 0;
  285. public function __construct($in_id=0, $in_parent_id=0) {
  286. $this->id = $in_id;
  287. $this->parent_item_id = $in_parent_id;
  288. }
  289. }