lp_admin_view.php 11 KB

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