lp_admin_view.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a learning path creation and player tool in Chamilo - previously learnpath_handler.php
  5. *
  6. * @author Patrick Cool
  7. * @author Denes Nagy
  8. * @author Roan Embrechts, refactoring and code cleaning
  9. * @author Yannick Warnier <ywarnier@beeznest.org> - cleaning and update for new SCORM tool
  10. * @package chamilo.learnpath
  11. */
  12. /**
  13. * INIT SECTION
  14. */
  15. $this_section = SECTION_COURSES;
  16. api_protect_course_script();
  17. /* Libraries */
  18. include 'learnpath_functions.inc.php';
  19. /* Constants and variables */
  20. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  21. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  22. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  23. $isStudentView = isset($_REQUEST['isStudentView']) ? (int) $_REQUEST['isStudentView'] : null;
  24. $learnpath_id = (int) $_REQUEST['lp_id'];
  25. $submit = isset($_POST['submit_button']) ? $_POST['submit_button'] : null;
  26. $_course = api_get_course_info();
  27. /* MAIN CODE */
  28. // Using the resource linker as a tool for adding resources to the learning path.
  29. if ($action == 'add' and $type == 'learnpathitem') {
  30. $htmlHeadXtra[] = "<script> window.location=\"../resourcelinker/resourcelinker.php?source_id=5&action=$action&learnpath_id=$learnpath_id&chapter_id=$chapter_id&originalresource=no\"; </script>";
  31. }
  32. if ((!$is_allowed_to_edit) || ($isStudentView)) {
  33. error_log('New LP - User not authorized in lp_admin_view.php');
  34. header('location:lp_controller.php?action=view&lp_id='.$learnpath_id);
  35. }
  36. // From here on, we are admin because of the previous condition, so don't check anymore.
  37. $course_id = api_get_course_int_id();
  38. $sql_query = "SELECT * FROM $tbl_lp WHERE c_id = $course_id AND id = $learnpath_id";
  39. $result = Database::query($sql_query);
  40. $therow = Database::fetch_array($result);
  41. /* SHOWING THE ADMIN TOOLS */
  42. if (api_is_in_gradebook()) {
  43. $interbreadcrumb[]= array(
  44. 'url' => api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq(),
  45. 'name' => get_lang('ToolGradebook')
  46. );
  47. }
  48. $interbreadcrumb[] = array(
  49. 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
  50. 'name' => get_lang('LearningPaths'),
  51. );
  52. $interbreadcrumb[] = array(
  53. 'url' => api_get_self()."?action=build&lp_id=$learnpath_id&".api_get_cidreq(),
  54. "name" => stripslashes("{$therow['name']}")
  55. );
  56. $interbreadcrumb[] = array(
  57. 'url' => api_get_self()."?action=add_item&type=step&lp_id=$learnpath_id&".api_get_cidreq(),
  58. 'name' => get_lang('NewStep')
  59. );
  60. if (isset($_REQUEST['updateaudio'])) {
  61. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('UpdateAllAudioFragments'));
  62. } else {
  63. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('BasicOverview'));
  64. }
  65. $learnpath = learnpath::getCurrentLpFromSession();
  66. // Theme calls.
  67. $show_learn_path = true;
  68. $lp_theme_css = $learnpath->get_theme();
  69. /* DISPLAY SECTION */
  70. // POST action handling (uploading mp3, deleting mp3)
  71. if (isset($_POST['save_audio'])) {
  72. //Updating the lp.modified_on
  73. $learnpath->set_modified_on();
  74. // Deleting the audio fragments.
  75. foreach ($_POST as $key => $value) {
  76. if (substr($key, 0, 9) == 'removemp3') {
  77. $lp_items_to_remove_audio[] = str_ireplace('removemp3', '', $key);
  78. // Removing the audio from the learning path item.
  79. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  80. $in = implode(',', $lp_items_to_remove_audio);
  81. }
  82. }
  83. if (count($lp_items_to_remove_audio)>0) {
  84. $sql = "UPDATE $tbl_lp_item SET audio = '' WHERE c_id = $course_id AND id IN (".$in.")";
  85. $result = Database::query($sql);
  86. }
  87. // Uploading the audio files.
  88. foreach ($_FILES as $key => $value) {
  89. if (substr($key, 0, 7) == 'mp3file' AND !empty($_FILES[$key]['tmp_name'])) {
  90. // The id of the learning path item.
  91. $lp_item_id = str_ireplace('mp3file', '', $key);
  92. // Create the audio folder if it does not exist yet.
  93. DocumentManager::createDefaultAudioFolder($_course);
  94. // Check if file already exits into document/audio/
  95. $file_name = $_FILES[$key]['name'];
  96. $file_name = stripslashes($file_name);
  97. // Add extension to files without one (if possible).
  98. $file_name = add_ext_on_mime($file_name, $_FILES[$key]['type']);
  99. $clean_name = api_replace_dangerous_char($file_name);
  100. // No "dangerous" files.
  101. $clean_name = disable_dangerous_file($clean_name);
  102. $check_file_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/audio/'.$clean_name;
  103. // If the file exists we generate a new name.
  104. if (file_exists($check_file_path)) {
  105. $filename_components = explode('.', $clean_name);
  106. // Gettting the extension of the file.
  107. $file_extension = $filename_components[count($filename_components) - 1];
  108. // Adding something random to prevent overwriting.
  109. $filename_components[count($filename_components) - 1] = time();
  110. // Reconstructing the new filename.
  111. $clean_name = implode($filename_components) .'.'.$file_extension;
  112. // Using the new name in the $_FILES superglobal.
  113. $_FILES[$key]['name'] = $clean_name;
  114. }
  115. // Upload the file in the documents tool.
  116. $file_path = handle_uploaded_document($_course, $_FILES[$key], api_get_path(SYS_COURSE_PATH).$_course['path'].'/document','/audio', api_get_user_id(), '', '', '', '', false);
  117. // Getting the filename only.
  118. $file_components = explode('/', $file_path);
  119. $file = $file_components[count($file_components) - 1];
  120. // Store the mp3 file in the lp_item table.
  121. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  122. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string($file)."'
  123. WHERE c_id = $course_id AND id = '".Database::escape_string($lp_item_id)."'";
  124. Database::query($sql_insert_audio);
  125. }
  126. }
  127. //Display::display_confirmation_message(get_lang('ItemUpdated'));
  128. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval(
  129. $learnpath->lp_id
  130. ).'&'.api_get_cidreq();
  131. header('Location: '.$url);
  132. exit;
  133. }
  134. Display::display_header(null, 'Path');
  135. $suredel = trim(get_lang('AreYouSureToDeleteJS'));
  136. ?>
  137. <script>
  138. var newOrderData= "";
  139. //source code found in http://www.swartzfager.org/blog/dspNestedList.cfm
  140. $(function() {
  141. <?php
  142. if (!isset($_REQUEST['updateaudio'])) {
  143. ?>
  144. $("#lp_item_list").sortable({
  145. items: "li",
  146. handle: ".moved", //only the class "moved"
  147. cursor: "move",
  148. placeholder: "ui-state-highlight" //defines the yellow highlight
  149. });
  150. $("#listSubmit").click(function () {
  151. //Disable the submit button to prevent a double-click
  152. $(this).attr("disabled","disabled");
  153. //Initialize the variable that will contain the data to submit to the form
  154. newOrderData= "";
  155. //All direct descendants of the lp_item_list will have a parentId of 0
  156. var parentId= 0;
  157. //Walk through the direct descendants of the lp_item_list <ul>
  158. $("#lp_item_list").children().each(function () {
  159. /*Only process elements with an id attribute (in order to skip the blank,
  160. unmovable <li> elements.*/
  161. if ($(this).attr("id")) {
  162. /*Build a string of data with the child's ID and parent ID,
  163. using the "|" as a delimiter between the two IDs and the "^"
  164. as a record delimiter (these delimiters were chosen in case the data
  165. involved includes more common delimiters like commas within the content)
  166. */
  167. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  168. //Determine if this child is a containter
  169. if ($(this).is(".li_container")) {
  170. //Process the child elements of the container
  171. processChildren($(this).attr("id"));
  172. }
  173. }
  174. }); //end of lp_item_list children loop
  175. //Write the newOrderData string out to the listResults form element
  176. //$("#listResults").val(newOrderData);
  177. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  178. $.post("<?php echo api_get_path(WEB_AJAX_PATH)?>lp.ajax.php?<?php echo api_get_cidreq() ?>", order, function (reponse) {
  179. $("#message").html(reponse);
  180. });
  181. setTimeout(function() {
  182. $("#message").html('');
  183. }, 3000);
  184. return false;
  185. }); //end of lp_item_list event assignment
  186. <?php } ?>
  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. Display::display_confirmation_message(get_lang('LearnpathItemEdited'));
  237. } else {
  238. echo $learnpath->display_edit_item($_GET['id']);
  239. }
  240. break;
  241. case 'delete_item':
  242. if (isset($is_success) && $is_success === true) {
  243. Display::display_confirmation_message(get_lang('LearnpathItemDeleted'));
  244. }
  245. break;
  246. }
  247. if (!empty($_GET['updateaudio'])) {
  248. // list of items to add audio files
  249. echo $learnpath->overview();
  250. }
  251. echo '</div>';
  252. echo '</div>';
  253. $learnpath->updateCurrentLpFromSession();