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