lp_admin_view.php 11 KB

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