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