lp.ajax.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. switch ($action) {
  12. case 'get_documents';
  13. $courseInfo = api_get_course_info();
  14. $folderId = isset($_GET['folder_id']) ? $_GET['folder_id'] : null;
  15. if (empty($folderId)) {
  16. exit;
  17. }
  18. $lpId = isset($_GET['lp_id']) ? $_GET['lp_id'] : null;
  19. $url = isset($_GET['url']) ? $_GET['url'] : null;
  20. echo DocumentManager::get_document_preview(
  21. $courseInfo,
  22. $lpId,
  23. null,
  24. api_get_session_id(),
  25. true,
  26. null,
  27. $url,
  28. true,
  29. false,
  30. $folderId
  31. );
  32. break;
  33. case 'add_lp_item':
  34. if (api_is_allowed_to_edit(null, true)) {
  35. if ($_SESSION['oLP']) {
  36. //Updating the lp.modified_on
  37. $_SESSION['oLP']->set_modified_on();
  38. $title = $_REQUEST['title'];
  39. if ($_REQUEST['type'] == TOOL_QUIZ) {
  40. $title = Exercise::format_title_variable($title);
  41. }
  42. echo $_SESSION['oLP']->add_item($_REQUEST['parent_id'], $_REQUEST['previous_id'], $_REQUEST['type'], $_REQUEST['id'], $title, null);
  43. }
  44. }
  45. break;
  46. case 'update_lp_item_order':
  47. if (api_is_allowed_to_edit(null, true)) {
  48. $new_order = $_POST['new_order'];
  49. $sections = explode('^', $new_order);
  50. $new_array = array();
  51. // We have to update parent_item_id, previous_item_id, next_item_id, display_order in the database
  52. $LP_item_list = new LP_item_order_list();
  53. foreach ($sections as $items) {
  54. if (!empty($items)) {
  55. list($id, $parent_id) = explode('|', $items);
  56. $item = new LP_item_order_item($id, $parent_id);
  57. $LP_item_list->add($item);
  58. }
  59. }
  60. $tab_parents_id = $LP_item_list->get_list_of_parents();
  61. foreach ($tab_parents_id as $parent_id) {
  62. $Same_parent_LP_item_list = $LP_item_list->get_item_with_same_parent($parent_id);
  63. $previous_item_id = 0;
  64. for ($i=0; $i < count($Same_parent_LP_item_list->list);$i++) {
  65. $item_id = $Same_parent_LP_item_list->list[$i]->id;
  66. // display_order
  67. $display_order = $i + 1;
  68. $LP_item_list->set_parameters_for_id($item_id, $display_order, "display_order");
  69. // previous_item_id
  70. $LP_item_list->set_parameters_for_id($item_id, $previous_item_id, "previous_item_id");
  71. $previous_item_id = $item_id;
  72. // next_item_id
  73. $next_item_id = 0;
  74. if ($i < count($Same_parent_LP_item_list->list) - 1) {
  75. $next_item_id = $Same_parent_LP_item_list->list[$i+1]->id;
  76. }
  77. $LP_item_list->set_parameters_for_id($item_id, $next_item_id, "next_item_id");
  78. }
  79. }
  80. foreach ($LP_item_list->list as $LP_item) {
  81. $params = array();
  82. $params['display_order'] = $LP_item->display_order;
  83. $params['previous_item_id'] = $LP_item->previous_item_id;
  84. $params['next_item_id'] = $LP_item->next_item_id;
  85. $params['parent_item_id'] = $LP_item->parent_item_id;
  86. Database::update($tbl_lp_item, $params, array('id = ? AND c_id = ? '=> array(intval($LP_item->id), $course_id)));
  87. }
  88. Display::display_confirmation_message(get_lang('Saved'));
  89. }
  90. break;
  91. case 'record_audio':
  92. if (api_is_allowed_to_edit(null, true) == false) {
  93. exit;
  94. }
  95. /** @var Learnpath $lp */
  96. $lp = isset($_SESSION['oLP']) ? $_SESSION['oLP'] : null;
  97. $course_info = api_get_course_info();
  98. $lpPathInfo = $lp->generate_lp_folder($course_info);
  99. if (empty($lpPathInfo)) {
  100. exit;
  101. }
  102. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
  103. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  104. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  105. foreach (array('video', 'audio') as $type) {
  106. if (isset($_FILES["${type}-blob"])) {
  107. $fileName = $_POST["${type}-filename"];
  108. //$file = $_FILES["${type}-blob"]["tmp_name"];
  109. $file = $_FILES["${type}-blob"];
  110. $fileInfo = pathinfo($fileName);
  111. $file['name'] = 'rec_'.date('Y-m-d_His').'_'.uniqid().'.'.$fileInfo['extension'];
  112. $file['file'] = $file;
  113. $lpPathInfo['dir'] = api_remove_trailing_slash($lpPathInfo['dir']);
  114. $result = DocumentManager::upload_document(
  115. $file,
  116. $lpPathInfo['dir'],
  117. $file['name'],
  118. null,
  119. 0,
  120. 'overwrite',
  121. false,
  122. false
  123. );
  124. if (!empty($result) && is_array($result)) {
  125. $newDocId = $result['id'];
  126. $courseId = $result['c_id'];
  127. $lp->set_modified_on();
  128. $lpItem = new learnpathItem($_REQUEST['lp_item_id']);
  129. $lpItem->add_audio_from_documents($newDocId);
  130. $data = DocumentManager::get_document_data_by_id($newDocId, $course_info['code']);
  131. echo $data['document_url'];
  132. exit;
  133. }
  134. }
  135. }
  136. break;
  137. default:
  138. echo '';
  139. }
  140. exit;
  141. /*
  142. * Classes to create a special data structure to manipulate LP Items
  143. * used only in this file
  144. * @todo move in a file
  145. */
  146. class LP_item_order_list {
  147. public $list = array();
  148. public function __construct() {
  149. $this->list = array();
  150. }
  151. public function add($in_LP_item_order_item) {
  152. $this->list[] = $in_LP_item_order_item;
  153. }
  154. public function get_item_with_same_parent($in_parent_id) {
  155. $out_res = new LP_item_order_list();
  156. for ($i=0; $i < count($this->list); $i++) {
  157. if ($this->list[$i]->parent_item_id == $in_parent_id) {
  158. $out_res->add($this->list[$i]);
  159. }
  160. }
  161. return $out_res;
  162. }
  163. public function get_list_of_parents() {
  164. $tab_out_res = array();
  165. foreach ($this->list as $LP_item) {
  166. if (!in_array($LP_item->parent_item_id, $tab_out_res)) {
  167. $tab_out_res[] = $LP_item->parent_item_id;
  168. }
  169. }
  170. return $tab_out_res;
  171. }
  172. public function set_parameters_for_id($in_id, $in_value, $in_parameters) {
  173. for ($i=0; $i < count($this->list); $i++) {
  174. if ($this->list[$i]->id == $in_id) {
  175. $this->list[$i]->$in_parameters = $in_value;
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. class LP_item_order_item {
  182. public $id = 0;
  183. public $parent_item_id = 0;
  184. public $previous_item_id = 0;
  185. public $next_item_id = 0;
  186. public $display_order = 0;
  187. public function __construct($in_id=0, $in_parent_id=0) {
  188. $this->id = $in_id;
  189. $this->parent_item_id = $in_parent_id;
  190. }
  191. }