lp_admin_view.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This is a learning path creation and player tool in Chamilo - previously learnpath_handler.php.
  6. *
  7. * @author Patrick Cool
  8. * @author Denes Nagy
  9. * @author Roan Embrechts, refactoring and code cleaning
  10. * @author Yannick Warnier <ywarnier@beeznest.org> - cleaning and update for new SCORM tool
  11. *
  12. * @package chamilo.learnpath
  13. */
  14. $this_section = SECTION_COURSES;
  15. api_protect_course_script();
  16. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  17. /** @var learnpath $learnPath */
  18. $learnPath = Session::read('oLP');
  19. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  20. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  21. $isStudentView = isset($_REQUEST['isStudentView']) ? (int) $_REQUEST['isStudentView'] : null;
  22. $learnpath_id = (int) $_REQUEST['lp_id'];
  23. $submit = isset($_POST['submit_button']) ? $_POST['submit_button'] : null;
  24. $_course = api_get_course_info();
  25. if (!$is_allowed_to_edit || $isStudentView) {
  26. header('location:lp_controller.php?action=view&lp_id='.$learnpath_id);
  27. exit;
  28. }
  29. if (api_is_in_gradebook()) {
  30. $interbreadcrumb[] = [
  31. 'url' => Category::getUrl(),
  32. 'name' => get_lang('ToolGradebook'),
  33. ];
  34. }
  35. $interbreadcrumb[] = [
  36. 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
  37. 'name' => get_lang('LearningPaths'),
  38. ];
  39. $interbreadcrumb[] = [
  40. 'url' => api_get_self()."?action=build&lp_id=$learnpath_id&".api_get_cidreq(),
  41. "name" => Security::remove_XSS($learnPath->getNameNoTags()),
  42. ];
  43. $interbreadcrumb[] = [
  44. 'url' => api_get_self()."?action=add_item&type=step&lp_id=$learnpath_id&".api_get_cidreq(),
  45. 'name' => get_lang('NewStep'),
  46. ];
  47. if (isset($_REQUEST['updateaudio'])) {
  48. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('UpdateAllAudioFragments')];
  49. } else {
  50. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('BasicOverview')];
  51. }
  52. $htmlHeadXtra[] = '<script>'.$learnPath->get_js_dropdown_array().'</script>';
  53. // Theme calls.
  54. $show_learn_path = true;
  55. $lp_theme_css = $learnPath->get_theme();
  56. // POST action handling (uploading mp3, deleting mp3)
  57. if (isset($_POST['save_audio'])) {
  58. //Updating the lp.modified_on
  59. $learnPath->set_modified_on();
  60. $lp_items_to_remove_audio = [];
  61. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  62. // Deleting the audio fragments.
  63. foreach ($_POST as $key => $value) {
  64. if (substr($key, 0, 9) == 'removemp3') {
  65. $lp_items_to_remove_audio[] = str_ireplace('removemp3', '', $key);
  66. // Removing the audio from the learning path item.
  67. $in = implode(',', $lp_items_to_remove_audio);
  68. }
  69. }
  70. if (count($lp_items_to_remove_audio) > 0) {
  71. $sql = "UPDATE $tbl_lp_item SET audio = ''
  72. WHERE iid IN (".$in.")";
  73. Database::query($sql);
  74. }
  75. // Uploading the audio files.
  76. foreach ($_FILES as $key => $value) {
  77. if (substr($key, 0, 7) == 'mp3file' &&
  78. !empty($_FILES[$key]['tmp_name'])
  79. ) {
  80. // The id of the learning path item.
  81. $lp_item_id = str_ireplace('mp3file', '', $key);
  82. // Create the audio folder if it does not exist yet.
  83. DocumentManager::createDefaultAudioFolder($_course);
  84. // Check if file already exits into document/audio/
  85. $file_name = $_FILES[$key]['name'];
  86. $file_name = stripslashes($file_name);
  87. // Add extension to files without one (if possible).
  88. $file_name = add_ext_on_mime($file_name, $_FILES[$key]['type']);
  89. $clean_name = api_replace_dangerous_char($file_name);
  90. // No "dangerous" files.
  91. $clean_name = disable_dangerous_file($clean_name);
  92. $check_file_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/audio/'.$clean_name;
  93. // If the file exists we generate a new name.
  94. if (file_exists($check_file_path)) {
  95. $filename_components = explode('.', $clean_name);
  96. // Gettting the extension of the file.
  97. $file_extension = $filename_components[count($filename_components) - 1];
  98. // Adding something random to prevent overwriting.
  99. $filename_components[count($filename_components) - 1] = time();
  100. // Reconstructing the new filename.
  101. $clean_name = implode($filename_components).'.'.$file_extension;
  102. // Using the new name in the $_FILES superglobal.
  103. $_FILES[$key]['name'] = $clean_name;
  104. }
  105. // Upload the file in the documents tool.
  106. $file_path = handle_uploaded_document(
  107. $_course,
  108. $_FILES[$key],
  109. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  110. '/audio',
  111. api_get_user_id(),
  112. '',
  113. '',
  114. '',
  115. '',
  116. false
  117. );
  118. // Getting the filename only.
  119. $file_components = explode('/', $file_path);
  120. $file = $file_components[count($file_components) - 1];
  121. // Store the mp3 file in the lp_item table.
  122. $sql = "UPDATE $tbl_lp_item
  123. SET audio = '".Database::escape_string($file)."'
  124. WHERE iid = ".(int) $lp_item_id;
  125. Database::query($sql);
  126. }
  127. }
  128. //echo Display::return_message(get_lang('ItemUpdated'), 'confirm');
  129. $url = api_get_self().'?action=add_item&type=step&lp_id='.$learnPath->get_id().'&'.api_get_cidreq();
  130. header('Location: '.$url);
  131. exit;
  132. }
  133. Display::display_header(null, 'Path');
  134. $suredel = trim(get_lang('AreYouSureToDeleteJS'));
  135. ?>
  136. <script>
  137. var newOrderData= "";
  138. //source code found in http://www.swartzfager.org/blog/dspNestedList.cfm
  139. $(function() {
  140. <?php
  141. if (!isset($_REQUEST['updateaudio'])) {
  142. ?>
  143. $("#lp_item_list").sortable({
  144. items: "li",
  145. handle: ".moved", //only the class "moved"
  146. cursor: "move",
  147. placeholder: "ui-state-highlight" //defines the yellow highlight
  148. });
  149. $("#listSubmit").click(function () {
  150. //Disable the submit button to prevent a double-click
  151. $(this).attr("disabled","disabled");
  152. //Initialize the variable that will contain the data to submit to the form
  153. newOrderData= "";
  154. //All direct descendants of the lp_item_list will have a parentId of 0
  155. var parentId= 0;
  156. //Walk through the direct descendants of the lp_item_list <ul>
  157. $("#lp_item_list").children().each(function () {
  158. /*Only process elements with an id attribute (in order to skip the blank,
  159. unmovable <li> elements.*/
  160. if ($(this).attr("id")) {
  161. /*Build a string of data with the child's ID and parent ID,
  162. using the "|" as a delimiter between the two IDs and the "^"
  163. as a record delimiter (these delimiters were chosen in case the data
  164. involved includes more common delimiters like commas within the content)
  165. */
  166. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  167. //Determine if this child is a containter
  168. if ($(this).is(".li_container")) {
  169. //Process the child elements of the container
  170. processChildren($(this).attr("id"));
  171. }
  172. }
  173. }); //end of lp_item_list children loop
  174. //Write the newOrderData string out to the listResults form element
  175. //$("#listResults").val(newOrderData);
  176. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  177. $.post("<?php echo api_get_path(WEB_AJAX_PATH); ?>lp.ajax.php", order, function(reponse) {
  178. $("#message").html(reponse);
  179. });
  180. setTimeout(function() {
  181. $("#message").html('');
  182. }, 3000);
  183. return false;
  184. }); //end of lp_item_list event assignment
  185. <?php
  186. } ?>
  187. function processChildren(parentId) {
  188. //Loop through the children of the UL element defined by the parentId
  189. var ulParentID= "UL_" + parentId;
  190. $("#" + ulParentID).children().each(function () {
  191. /*Only process elements with an id attribute (in order to skip the blank,
  192. unmovable <li> elements.*/
  193. if ($(this).attr("id")) {
  194. /*Build a string of data with the child's ID and parent ID,
  195. using the "|" as a delimiter between the two IDs and the "^"
  196. as a record delimiter (these delimiters were chosen in case the data
  197. involved includes more common delimiters like commas within the content)
  198. */
  199. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  200. //Determine if this child is a containter
  201. if ($(this).is(".container")) {
  202. //Process the child elements of the container
  203. processChildren($(this).attr("id"));
  204. }
  205. }
  206. }); //end of children loop
  207. } //end of processChildren function
  208. });
  209. /* <![CDATA[ */
  210. function stripslashes(str) {
  211. str=str.replace(/\\'/g,'\'');
  212. str=str.replace(/\\"/g,'"');
  213. str=str.replace(/\\\\/g,'\\');
  214. str=str.replace(/\\0/g,'\0');
  215. return str;
  216. }
  217. function confirmation(name) {
  218. name=stripslashes(name);
  219. if (confirm("<?php echo $suredel; ?> " + name + " ?")) {
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. }
  225. </script>
  226. <?php
  227. echo $learnPath->build_action_menu();
  228. echo '<div class="row">';
  229. echo '<div class="col-md-4">';
  230. echo $learnPath->return_new_tree(null, true);
  231. echo '</div>';
  232. echo '<div class="col-md-8">';
  233. switch ($_GET['action']) {
  234. case 'edit_item':
  235. if (isset($is_success) && $is_success === true) {
  236. echo Display::return_message(
  237. get_lang('LearnpathItemEdited'),
  238. 'confirm'
  239. );
  240. } else {
  241. echo $learnPath->display_edit_item($_GET['id']);
  242. }
  243. break;
  244. case 'delete_item':
  245. if (isset($is_success) && $is_success === true) {
  246. echo Display::return_message(
  247. get_lang('LearnpathItemDeleted'),
  248. 'confirm'
  249. );
  250. }
  251. break;
  252. }
  253. if (!empty($_GET['updateaudio'])) {
  254. // list of items to add audio files
  255. echo $learnPath->overview();
  256. }
  257. echo '</div>';
  258. echo '</div>';
  259. Display::display_footer();