scorm.lib.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // TODO: Migrate this into the scorm.class.php file.
  4. /**
  5. * This file is a container for functions related to SCORM and other
  6. * standard or common course content types. It might later become a class
  7. * instead of a functions library, as several components are likely to be
  8. * re-used for different content types.
  9. * @package chamilo.learnpath.scorm
  10. * @author Yannick Warnier <ywarnier@beeznest.org>
  11. * @author Based on work from Denes NAgy, Isthvan Mandak and Roan Embrechts
  12. */
  13. /**
  14. * Delete a scorm directory (check for imsmanifest and if found, deletes the related rows in scorm tables also)
  15. * @param string Dir path
  16. * @return boolean True on success, false otherwise
  17. */
  18. function removescormDir($dir) {
  19. global $_course;
  20. if(!@$opendir = opendir($dir)) {
  21. return false;
  22. }
  23. while($readdir = readdir($opendir)) {
  24. if($readdir != '..' && $readdir != '.') {
  25. if(is_file($dir.'/'.$readdir)) {
  26. $pos = strpos('/'.$readdir, 'imsmanifest.xml');
  27. if ($pos) { // So we have the imsmanifest in this dir
  28. // from d:/myworks/dokeos/dokeos_cvs/dokeos/dokeos/courses/CVSCODE4/scorm/LP2/LP2
  29. // We have to get /LP2/LP2
  30. $path = api_get_path(SYS_COURSE_PATH).$_course['official_code'].'/scorm';
  31. $pos = strpos($dir, $path);
  32. if ($pos == 0) {
  33. $scormdir = substr($dir, strlen($path), strlen($dir) - strlen($path));
  34. $courseid = $_course['official_code'];
  35. $sql = "SELECT * FROM ".Database::get_scorm_table(TABLE_SCORM_MAIN)." where (contentTitle='$scormdir' and dokeosCourse='$courseid')";
  36. $result = Database::query($sql);
  37. while ($row = Database::fetch_array($result)) {
  38. $c = $row['contentId'];
  39. $sql2 = "DELETE FROM ".Database::get_scorm_table(TABLE_SCORM_SCO_DATA)." where contentId=$c";
  40. $result2 = Database::query($sql2);
  41. }
  42. $sql = "DELETE FROM ".Database::get_scorm_table(TABLE_SCORM_MAIN)." where (contentTitle='$scormdir' and dokeosCourse='$courseid')";
  43. $result = Database::query($sql);
  44. }
  45. }
  46. if (!@unlink($dir.'/'.$readdir)) {
  47. return false;
  48. }
  49. } elseif (is_dir($dir.'/'.$readdir)) {
  50. if(!removescormDir($dir.'/'.$readdir)) {
  51. return false;
  52. }
  53. }
  54. }
  55. }
  56. closedir($opendir);
  57. if (!@rmdir($dir)) {
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * This function removes a directory if it exists
  64. * @param string Dir path
  65. * @return boolean True on success, false otherwise
  66. * @uses removescormDir() to actually remove the directory
  67. */
  68. function scorm_delete($file) {
  69. if (check_name_exist($file)) {
  70. if (is_dir($file)) {
  71. return removescormDir($file);
  72. }
  73. } else {
  74. return false; // No file or directory to delete.
  75. }
  76. }
  77. /**
  78. * This function gets a list of scorm paths located in a given directory
  79. * @param string Base directory path
  80. * @param string Current directory
  81. * @param array Reference to a list of paths that exist in the database
  82. * @return array Array(type=>array(),size=>array(),date=>array())
  83. */
  84. function get_scorm_paths_from_dir($basedir, $curdir, &$attribute){
  85. $scormcontent = false;
  86. $saved_dir = getcwd();
  87. $res = @chdir (realpath($basedir.$curdir));
  88. if ($res === false) { return(null); }
  89. $handle = opendir('.');
  90. define('A_DIRECTORY', 1);
  91. define('A_FILE', 2);
  92. $fileList = array();
  93. // Fill up $fileList for displaying the files list later on.
  94. while ($file = readdir($handle)) {
  95. if ($file == '.' || $file == '..' || $file == '.htaccess') {
  96. continue; // Skip current and parent directories
  97. }
  98. $fileList['name'][] = $file;
  99. //if ($file=='imsmanifest.xml') { $scormcontent=true; }
  100. if(is_dir($file)) {
  101. $fileList['type'][] = A_DIRECTORY;
  102. $fileList['size'][] = false;
  103. $fileList['date'][] = false;
  104. } elseif (is_file($file)) {
  105. $fileList['type'][] = A_FILE;
  106. $fileList['size'][] = filesize($file);
  107. $fileList['date'][] = filectime($file);
  108. }
  109. /*
  110. * Make the correspondance between
  111. * info given by the file system
  112. * and info given by the DB
  113. */
  114. if (is_array($attribute) && count($attribute['path']) > 0) {
  115. $keyAttribute = array_search($curdir.'/'.$file, $attribute['path']);
  116. }
  117. if ($keyAttribute !== false) {
  118. $fileList['comment' ][] = $attribute['comment' ][$keyAttribute];
  119. $fileList['visibility'][] = $attribute['visibility'][$keyAttribute];
  120. unset ($attribute['comment' ][$keyAttribute],
  121. $attribute['visibility'][$keyAttribute],
  122. $attribute['path' ][$keyAttribute]);
  123. } else {
  124. $fileList['comment' ][] = false;
  125. $fileList['visibility'][] = false;
  126. }
  127. }
  128. closedir($handle);
  129. chdir($saved_dir);
  130. return $fileList;
  131. }
  132. /**
  133. * Detects the SCORM version from an imsmanifest.xml file
  134. * @param string Path to imsmanifest.xml
  135. * @return string SCORM version (1.0,1.1,1.2,1.3)
  136. * @todo Implement this function
  137. */
  138. function get_scorm_version($path){
  139. return '1.2';
  140. }