lp_admin_view.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. // The main_api.lib.php, database.lib.php and display.lib.php
  19. // libraries are included by default.
  20. include 'learnpath_functions.inc.php';
  21. //include '../resourcelinker/resourcelinker.inc.php';
  22. include 'resourcelinker.inc.php';
  23. // Rewrite the language file, sadly overwritten by resourcelinker.inc.php.
  24. $language_file = "learnpath";
  25. /* Constants and variables */
  26. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  27. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  28. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  29. $tbl_lp_view = Database::get_course_table(TABLE_LP_VIEW);
  30. $isStudentView = (int) $_REQUEST['isStudentView'];
  31. $learnpath_id = (int) $_REQUEST['lp_id'];
  32. $submit = $_POST['submit_button'];
  33. /* MAIN CODE */
  34. // Using the resource linker as a tool for adding resources to the learning path.
  35. if ($action == 'add' and $type == 'learnpathitem') {
  36. $htmlHeadXtra[] = "<script language='JavaScript' type='text/javascript'> window.location=\"../resourcelinker/resourcelinker.php?source_id=5&action=$action&learnpath_id=$learnpath_id&chapter_id=$chapter_id&originalresource=no\"; </script>";
  37. }
  38. if ((!$is_allowed_to_edit) || ($isStudentView)) {
  39. error_log('New LP - User not authorized in lp_admin_view.php');
  40. header('location:lp_controller.php?action=view&lp_id='.$learnpath_id);
  41. }
  42. // From here on, we are admin because of the previous condition, so don't check anymore.
  43. $sql_query = "SELECT * FROM $tbl_lp WHERE id = $learnpath_id";
  44. $result = Database::query($sql_query);
  45. $therow = Database::fetch_array($result);
  46. //$admin_output = '';
  47. /*
  48. Course admin section
  49. - all the functions not available for students - always available in this case (page only shown to admin)
  50. */
  51. /* SHOWING THE ADMIN TOOLS */
  52. if (isset($_SESSION['gradebook'])) {
  53. $gradebook = $_SESSION['gradebook'];
  54. }
  55. if (!empty($gradebook) && $gradebook == 'view') {
  56. $interbreadcrumb[] = array (
  57. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  58. 'name' => get_lang('ToolGradebook')
  59. );
  60. }
  61. $interbreadcrumb[] = array('url' => 'lp_controller.php?action=list', 'name' => get_lang('LearningPaths'));
  62. $interbreadcrumb[] = array('url' => api_get_self()."?action=build&lp_id=$learnpath_id", "name" => stripslashes("{$therow['name']}"));
  63. if (isset($_REQUEST['updateaudio'])) {
  64. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('UpdateAllAudioFragments'));
  65. } else {
  66. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('BasicOverview'));
  67. }
  68. // Theme calls.
  69. $show_learn_path = true;
  70. $lp_theme_css = $_SESSION['oLP']->get_theme();
  71. $htmlHeadXtra[] = api_get_jquery_ui_js();
  72. Display::display_header(null, 'Path');
  73. //api_display_tool_title($therow['name']);
  74. $suredel = trim(get_lang('AreYouSureToDelete'));
  75. //$suredelstep = trim(get_lang('AreYouSureToDeleteSteps'));
  76. ?>
  77. <script type='text/javascript'>
  78. var newOrderData= "";
  79. //source code found in http://www.swartzfager.org/blog/dspNestedList.cfm
  80. $(function() {
  81. <?php
  82. if (!isset($_REQUEST['updateaudio'])) {
  83. ?>
  84. $("#lp_item_list").sortable({
  85. items: "li",
  86. handle: ".moved", //only the class "moved"
  87. cursor: "move",
  88. placeholder: "ui-state-highlight", //defines the yellow highlight
  89. });
  90. $("#listSubmit").click(function () {
  91. //Disable the submit button to prevent a double-click
  92. $(this).attr("disabled","disabled");
  93. //Initialize the variable that will contain the data to submit to the form
  94. newOrderData= "";
  95. //All direct descendants of the lp_item_list will have a parentId of 0
  96. var parentId= 0;
  97. //Walk through the direct descendants of the lp_item_list <ul>
  98. $("#lp_item_list").children().each(function () {
  99. /*Only process elements with an id attribute (in order to skip the blank,
  100. unmovable <li> elements.*/
  101. if ($(this).attr("id")) {
  102. /*Build a string of data with the child's ID and parent ID,
  103. using the "|" as a delimiter between the two IDs and the "^"
  104. as a record delimiter (these delimiters were chosen in case the data
  105. involved includes more common delimiters like commas within the content)
  106. */
  107. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  108. //Determine if this child is a containter
  109. if ($(this).is(".container")) {
  110. //Process the child elements of the container
  111. processChildren($(this).attr("id"));
  112. }
  113. }
  114. }); //end of lp_item_list children loop
  115. //Write the newOrderData string out to the listResults form element
  116. //$("#listResults").val(newOrderData);
  117. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  118. $.post("<?php echo api_get_path(WEB_AJAX_PATH)?>lp.ajax.php", order, function(reponse){
  119. $("#message").html(reponse);
  120. });
  121. setTimeout(function() {
  122. $("#message").html('');
  123. }, 3000);
  124. return false;
  125. }); //end of lp_item_list event assignment
  126. <?php } ?>
  127. function processChildren(parentId) {
  128. //Loop through the children of the UL element defined by the parentId
  129. var ulParentID= "UL_" + parentId;
  130. $("#" + ulParentID).children().each(function () {
  131. /*Only process elements with an id attribute (in order to skip the blank,
  132. unmovable <li> elements.*/
  133. if ($(this).attr("id"))
  134. {
  135. /*Build a string of data with the child's ID and parent ID,
  136. using the "|" as a delimiter between the two IDs and the "^"
  137. as a record delimiter (these delimiters were chosen in case the data
  138. involved includes more common delimiters like commas within the content)
  139. */
  140. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  141. //Determine if this child is a containter
  142. if ($(this).is(".container"))
  143. {
  144. //Process the child elements of the container
  145. processChildren($(this).attr("id"));
  146. }
  147. }
  148. }); //end of children loop
  149. } //end of processChildren function
  150. });
  151. /* <![CDATA[ */
  152. function stripslashes(str) {
  153. str=str.replace(/\\'/g,'\'');
  154. str=str.replace(/\\"/g,'"');
  155. str=str.replace(/\\\\/g,'\\');
  156. str=str.replace(/\\0/g,'\0');
  157. return str;
  158. }
  159. function confirmation(name) {
  160. name=stripslashes(name);
  161. if (confirm("<?php echo $suredel; ?> " + name + " ?")) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167. </script>
  168. <?php
  169. /* DISPLAY SECTION */
  170. switch ($_GET['action']) {
  171. case 'edit_item':
  172. if (isset($is_success) && $is_success === true) {
  173. Display::display_confirmation_message(get_lang('_learnpath_item_edited'));
  174. } else {
  175. echo $_SESSION['oLP']->display_edit_item($_GET['id']);
  176. }
  177. break;
  178. case 'delete_item':
  179. if (isset($is_success) && $is_success === true) {
  180. Display::display_confirmation_message(get_lang('_learnpath_item_deleted'));
  181. }
  182. break;
  183. }
  184. // POST action handling (uploading mp3, deleting mp3)
  185. if (isset($_POST['save_audio'])) {
  186. //Updating the lp.modified_on
  187. $_SESSION['oLP']->set_modified_on();
  188. // Deleting the audio fragments.
  189. foreach ($_POST as $key => $value) {
  190. if (substr($key, 0, 9) == 'removemp3') {
  191. $lp_items_to_remove_audio[] = str_ireplace('removemp3', '', $key);
  192. // Removing the audio from the learning path item.
  193. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  194. $in = implode(',', $lp_items_to_remove_audio);
  195. }
  196. }
  197. if (count($lp_items_to_remove_audio)>0) {
  198. $sql = "UPDATE $tbl_lp_item SET audio = '' WHERE id IN (".$in.")";
  199. $result = Database::query($sql);
  200. }
  201. // Uploading the audio files.
  202. foreach ($_FILES as $key => $value) {
  203. if (substr($key, 0, 7) == 'mp3file' AND !empty($_FILES[$key]['tmp_name'])) {
  204. // The id of the learning path item.
  205. $lp_item_id = str_ireplace('mp3file', '', $key);
  206. // Create the audio folder if it does not exist yet.
  207. global $_course;
  208. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  209. if (!is_dir($filepath.'audio')) {
  210. mkdir($filepath.'audio', api_get_permissions_for_new_directories());
  211. $audio_id = add_document($_course, '/audio', 'folder', 0, 'audio');
  212. api_item_property_update($_course, TOOL_DOCUMENT, $audio_id, 'FolderCreated', api_get_user_id(), null, null, null, null, api_get_session_id());
  213. }
  214. // Check if file already exits into document/audio/
  215. $file_name = $_FILES[$key]['name'];
  216. $file_name = stripslashes($file_name);
  217. // Add extension to files without one (if possible).
  218. $file_name = add_ext_on_mime($file_name, $_FILES[$key]['type']);
  219. $clean_name = replace_dangerous_char($file_name);
  220. // No "dangerous" files.
  221. $clean_name = disable_dangerous_file($clean_name);
  222. $check_file_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/audio/'.$clean_name;
  223. // If the file exists we generate a new name.
  224. if (file_exists($check_file_path)) {
  225. $filename_components = explode('.', $clean_name);
  226. // Gettting the extension of the file.
  227. $file_extension = $filename_components[count($filename_components) - 1];
  228. // Adding something random to prevent overwriting.
  229. $filename_components[count($filename_components) - 1] = time();
  230. // Reconstructing the new filename.
  231. $clean_name = implode($filename_components) .'.'.$file_extension;
  232. // Using the new name in the $_FILES superglobal.
  233. $_FILES[$key]['name'] = $clean_name;
  234. }
  235. // Upload the file in the documents tool.
  236. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  237. $file_path = handle_uploaded_document($_course, $_FILES[$key], api_get_path(SYS_COURSE_PATH).$_course['path'].'/document','/audio', api_get_user_id(), '', '', '', '', '', false);
  238. // Getting the filename only.
  239. $file_components = explode('/', $file_path);
  240. $file = $file_components[count($file_components) - 1];
  241. // Store the mp3 file in the lp_item table.
  242. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  243. $sql_insert_audio = "UPDATE $tbl_lp_item SET audio = '".Database::escape_string($file)."' WHERE id = '".Database::escape_string($lp_item_id)."'";
  244. Database::query($sql_insert_audio);
  245. }
  246. }
  247. Display::display_confirmation_message(get_lang('ItemUpdated'));
  248. }
  249. echo $_SESSION['oLP']->overview();
  250. /* FOOTER */
  251. Display::display_footer();