lp_admin_view.php 10 KB

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