learnpath_functions.inc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a function library for the learning path.
  5. *
  6. * Due to the face that the learning path has been built upon the resoucelinker,
  7. * naming conventions have changed at least 2 times. You can see here in order the :
  8. * 1. name used in the first version of the resourcelinker
  9. * 2. name used in the first version of the LP
  10. * 3. name used in the second (current) version of the LP
  11. *
  12. * 1. 2. 3.
  13. * Category = Chapter = Module
  14. * Item (?) = Item = Step
  15. *
  16. * @author Denes Nagy <darkden@evk.bke.hu>, main author
  17. * @author Roan Embrechts, some code cleaning
  18. * @author Yannick Warnier <yannick.warnier@beeznest.com>, multi-level learnpath behaviour + new SCORM tool
  19. * @access public
  20. * @package chamilo.learnpath
  21. * @todo rename functions to coding conventions: not deleteitem but delete_item, etc
  22. * @todo rewrite functions to comply with phpDocumentor
  23. * @todo remove code duplication
  24. */
  25. /**
  26. * This function returns false if there is at least one item in the path
  27. * @param Learnpath ID
  28. * @return boolean True if nothing was found, false otherwise
  29. */
  30. function is_empty($id) {
  31. $tbl_learnpath_item = Database :: get_course_table(TABLE_LEARNPATH_ITEM);
  32. $tbl_learnpath_chapter = Database :: get_course_table(TABLE_LEARNPATH_CHAPTER);
  33. $course_id = api_get_course_int_id();
  34. $sql = "SELECT * FROM $tbl_learnpath_chapter WHERE c_id = $course_id AND lp_id=$id ORDER BY display_order ASC";
  35. $result = Database::query($sql);
  36. $num_modules = Database::num_rows($result);
  37. $empty = true;
  38. if ($num_modules != 0) {
  39. while ($row = Database::fetch_array($result)) {
  40. $num_items = 0;
  41. $parent_item_id = $row['id'];
  42. $sql2 = "SELECT * FROM $tbl_learnpath_item WHERE c_id = $course_id AND (parent_item_id=$parent_item_id) ORDER BY display_order ASC";
  43. $result2 = Database::query($sql2);
  44. $num_items = Database::num_rows($result2);
  45. if ($num_items > 0) {
  46. $empty = false;
  47. }
  48. }
  49. }
  50. return ($empty);
  51. }
  52. /**
  53. * This function deletes an entire directory
  54. * @param string The directory path
  55. * @return boolean True on success, false on failure
  56. */
  57. function deldir($dir) {
  58. $dh = opendir($dir);
  59. while ($file = readdir($dh)) {
  60. if ($file != '.' && $file != '..') {
  61. $fullpath = $dir.'/'.$file;
  62. if (!is_dir($fullpath)) {
  63. unlink($fullpath);
  64. } else {
  65. deldir($fullpath);
  66. }
  67. }
  68. }
  69. closedir($dh);
  70. if (rmdir($dir)) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. function rcopy($source, $dest) {
  76. //error_log($source." -> ".$dest, 0);
  77. if (!file_exists($source)) {
  78. //error_log($source." does not exist", 0);
  79. return false;
  80. }
  81. if (is_dir($source)) {
  82. //error_log($source." is a dir", 0);
  83. // This is a directory.
  84. // Remove trailing '/'
  85. if (strrpos($source, '/') == sizeof($source) - 1) {
  86. $source = substr($source, 0, size_of($source) - 1);
  87. }
  88. if (strrpos($dest, '/') == sizeof($dest) - 1) {
  89. $dest = substr($dest, 0, size_of($dest) - 1);
  90. }
  91. if (!is_dir($dest)) {
  92. $res = @mkdir($dest, api_get_permissions_for_new_directories());
  93. if ($res !== false) {
  94. return true;
  95. } else {
  96. // Remove latest part of path and try creating that.
  97. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/')))) {
  98. return @mkdir($dest, api_get_permissions_for_new_directories());
  99. } else {
  100. return false;
  101. }
  102. }
  103. }
  104. return true;
  105. } else {
  106. // This is presumably a file.
  107. //error_log($source." is a file", 0);
  108. if (!@ copy($source, $dest)) {
  109. //error_log("Could not simple-copy $source", 0);
  110. $res = rcopy(dirname($source), dirname($dest));
  111. if ($res === true) {
  112. //error_log("Welcome dir created", 0);
  113. return @ copy($source, $dest);
  114. } else {
  115. return false;
  116. //error_log("Error creating path", 0);
  117. }
  118. } else {
  119. //error_log("Could well simple-copy $source", 0);
  120. return true;
  121. }
  122. }
  123. }