lp_admin_view.php 12 KB

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