lp_admin_view.php 11 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
  28. WHERE c_id = $course_id AND id = $learnpath_id";
  29. $result = Database::query($sql_query);
  30. $therow = Database::fetch_array($result);
  31. if (api_is_in_gradebook()) {
  32. $interbreadcrumb[] = array(
  33. 'url' => Category::getUrl(),
  34. 'name' => get_lang('ToolGradebook')
  35. );
  36. }
  37. $interbreadcrumb[] = array(
  38. 'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
  39. 'name' => get_lang('LearningPaths')
  40. );
  41. $interbreadcrumb[] = array(
  42. 'url' => api_get_self()."?action=build&lp_id=$learnpath_id&".api_get_cidreq(),
  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' &&
  78. !empty($_FILES[$key]['tmp_name'])
  79. ) {
  80. // The id of the learning path item.
  81. $lp_item_id = str_ireplace('mp3file', '', $key);
  82. // Create the audio folder if it does not exist yet.
  83. DocumentManager::createDefaultAudioFolder($_course);
  84. // Check if file already exits into document/audio/
  85. $file_name = $_FILES[$key]['name'];
  86. $file_name = stripslashes($file_name);
  87. // Add extension to files without one (if possible).
  88. $file_name = add_ext_on_mime($file_name, $_FILES[$key]['type']);
  89. $clean_name = api_replace_dangerous_char($file_name);
  90. // No "dangerous" files.
  91. $clean_name = disable_dangerous_file($clean_name);
  92. $check_file_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/audio/'.$clean_name;
  93. // If the file exists we generate a new name.
  94. if (file_exists($check_file_path)) {
  95. $filename_components = explode('.', $clean_name);
  96. // Gettting the extension of the file.
  97. $file_extension = $filename_components[count($filename_components) - 1];
  98. // Adding something random to prevent overwriting.
  99. $filename_components[count($filename_components) - 1] = time();
  100. // Reconstructing the new filename.
  101. $clean_name = implode($filename_components).'.'.$file_extension;
  102. // Using the new name in the $_FILES superglobal.
  103. $_FILES[$key]['name'] = $clean_name;
  104. }
  105. // Upload the file in the documents tool.
  106. $file_path = handle_uploaded_document(
  107. $_course,
  108. $_FILES[$key],
  109. api_get_path(SYS_COURSE_PATH).$_course['path'].'/document',
  110. '/audio',
  111. api_get_user_id(),
  112. '',
  113. '',
  114. '',
  115. '',
  116. false
  117. );
  118. // Getting the filename only.
  119. $file_components = explode('/', $file_path);
  120. $file = $file_components[count($file_components) - 1];
  121. // Store the mp3 file in the lp_item table.
  122. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  123. $sql = "UPDATE $tbl_lp_item
  124. SET audio = '".Database::escape_string($file)."'
  125. WHERE c_id = $course_id AND id = '".Database::escape_string($lp_item_id)."'";
  126. Database::query($sql);
  127. }
  128. }
  129. //echo Display::return_message(get_lang('ItemUpdated'), 'confirm');
  130. $url = api_get_self().'?action=add_item&type=step&lp_id='.intval($_SESSION['oLP']->lp_id).'&'.api_get_cidreq();
  131. header('Location: '.$url);
  132. exit;
  133. }
  134. Display::display_header(null, 'Path');
  135. $suredel = trim(get_lang('AreYouSureToDeleteJS'));
  136. ?>
  137. <script>
  138. var newOrderData= "";
  139. //source code found in http://www.swartzfager.org/blog/dspNestedList.cfm
  140. $(function() {
  141. <?php
  142. if (!isset($_REQUEST['updateaudio'])) {
  143. ?>
  144. $("#lp_item_list").sortable({
  145. items: "li",
  146. handle: ".moved", //only the class "moved"
  147. cursor: "move",
  148. placeholder: "ui-state-highlight" //defines the yellow highlight
  149. });
  150. $("#listSubmit").click(function () {
  151. //Disable the submit button to prevent a double-click
  152. $(this).attr("disabled","disabled");
  153. //Initialize the variable that will contain the data to submit to the form
  154. newOrderData= "";
  155. //All direct descendants of the lp_item_list will have a parentId of 0
  156. var parentId= 0;
  157. //Walk through the direct descendants of the lp_item_list <ul>
  158. $("#lp_item_list").children().each(function () {
  159. /*Only process elements with an id attribute (in order to skip the blank,
  160. unmovable <li> elements.*/
  161. if ($(this).attr("id")) {
  162. /*Build a string of data with the child's ID and parent ID,
  163. using the "|" as a delimiter between the two IDs and the "^"
  164. as a record delimiter (these delimiters were chosen in case the data
  165. involved includes more common delimiters like commas within the content)
  166. */
  167. newOrderData= newOrderData + $(this).attr("id") + "|" + "0" + "^";
  168. //Determine if this child is a containter
  169. if ($(this).is(".li_container")) {
  170. //Process the child elements of the container
  171. processChildren($(this).attr("id"));
  172. }
  173. }
  174. }); //end of lp_item_list children loop
  175. //Write the newOrderData string out to the listResults form element
  176. //$("#listResults").val(newOrderData);
  177. var order = "new_order="+ newOrderData + "&a=update_lp_item_order";
  178. $.post("<?php echo api_get_path(WEB_AJAX_PATH)?>lp.ajax.php", order, function(reponse) {
  179. $("#message").html(reponse);
  180. });
  181. setTimeout(function() {
  182. $("#message").html('');
  183. }, 3000);
  184. return false;
  185. }); //end of lp_item_list event assignment
  186. <?php } ?>
  187. function processChildren(parentId) {
  188. //Loop through the children of the UL element defined by the parentId
  189. var ulParentID= "UL_" + parentId;
  190. $("#" + ulParentID).children().each(function () {
  191. /*Only process elements with an id attribute (in order to skip the blank,
  192. unmovable <li> elements.*/
  193. if ($(this).attr("id")) {
  194. /*Build a string of data with the child's ID and parent ID,
  195. using the "|" as a delimiter between the two IDs and the "^"
  196. as a record delimiter (these delimiters were chosen in case the data
  197. involved includes more common delimiters like commas within the content)
  198. */
  199. newOrderData= newOrderData + $(this).attr("id") + "|" + parentId + "^";
  200. //Determine if this child is a containter
  201. if ($(this).is(".container")) {
  202. //Process the child elements of the container
  203. processChildren($(this).attr("id"));
  204. }
  205. }
  206. }); //end of children loop
  207. } //end of processChildren function
  208. });
  209. /* <![CDATA[ */
  210. function stripslashes(str) {
  211. str=str.replace(/\\'/g,'\'');
  212. str=str.replace(/\\"/g,'"');
  213. str=str.replace(/\\\\/g,'\\');
  214. str=str.replace(/\\0/g,'\0');
  215. return str;
  216. }
  217. function confirmation(name) {
  218. name=stripslashes(name);
  219. if (confirm("<?php echo $suredel; ?> " + name + " ?")) {
  220. return true;
  221. } else {
  222. return false;
  223. }
  224. }
  225. </script>
  226. <?php
  227. echo $_SESSION['oLP']->build_action_menu();
  228. echo '<div class="row">';
  229. echo '<div class="col-md-4">';
  230. echo $_SESSION['oLP']->return_new_tree(null, true);
  231. echo '</div>';
  232. echo '<div class="col-md-8">';
  233. switch ($_GET['action']) {
  234. case 'edit_item':
  235. if (isset($is_success) && $is_success === true) {
  236. echo Display::return_message(
  237. get_lang('LearnpathItemEdited'),
  238. 'confirm'
  239. );
  240. } else {
  241. echo $_SESSION['oLP']->display_edit_item($_GET['id']);
  242. }
  243. break;
  244. case 'delete_item':
  245. if (isset($is_success) && $is_success === true) {
  246. echo Display::return_message(
  247. get_lang('LearnpathItemDeleted'),
  248. 'confirm'
  249. );
  250. }
  251. break;
  252. }
  253. if (!empty($_GET['updateaudio'])) {
  254. // list of items to add audio files
  255. echo $_SESSION['oLP']->overview();
  256. }
  257. echo '</div>';
  258. echo '</div>';
  259. Display::display_footer();