lp.ajax.php 7.6 KB

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