PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/backup/bb/restore_bb.php

https://bitbucket.org/ceu/moodle_demo
PHP | 123 lines | 78 code | 23 blank | 22 comment | 17 complexity | 8d120919f5722c66ca88b0e1280eb9bc MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php // $Id: restore_bb.php,v 1.7.16.3 2009/09/16 11:18:56 danmarsden Exp $
  2. // This file facilitates the conversion of a Blackboard course export
  3. // into a Moodle course export. It assumes an unzipped directory and makes in-place alterations.
  4. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  5. // Ziba Scott <ziba@linuxbox.com> 10-25-04
  6. require_once($CFG->dirroot.'/backup/bb/xsl_emulate_xslt.inc');
  7. function get_subdirs($directory){
  8. $opendirectory = opendir( $directory );
  9. while(false !== ($filename = readdir($opendirectory))) {
  10. if (is_dir($directory.$filename) and $filename != ".." and $filename != "."){
  11. $subdirs[] = $filename;
  12. }
  13. }
  14. closedir($opendirectory);
  15. return $subdirs;
  16. }
  17. function choose_bb_xsl($manifest){
  18. $f = fopen($manifest,"r");
  19. $buffer = fgets($f, 400);
  20. $buffer = fgets($f, 400);
  21. fclose($f);
  22. if (strstr($buffer,"xmlns:bb=\"http://www.blackboard.com/content-packaging/\"")){
  23. return "bb6_to_moodle.xsl";
  24. }
  25. return "bb5.5_to_moodle.xsl";
  26. }
  27. function blackboard_convert($dir){
  28. global $CFG;
  29. // Check for a Blackboard manifest file
  30. if (is_readable($dir.'/imsmanifest.xml') && !is_readable($dir.'/moodle.xml')){
  31. if (!function_exists('xslt_create')) { // XSLT MUST be installed for this to work
  32. notify('You need the XSLT library installed in PHP to open this Blackboard file');
  33. return false;
  34. }
  35. //Select the proper XSL file
  36. $xslt_file = choose_bb_xsl($dir.'/imsmanifest.xml');
  37. //TODO: Use the get_string function for this
  38. echo "<li>Converting Blackboard export</li>";
  39. // The XSL file must be in the same directory as the Blackboard files when it is processed
  40. if (!copy($CFG->dirroot."/backup/bb/$xslt_file", "$dir/$xslt_file")) {
  41. notify('Could not copy the XSLT file to '."$dir/$xslt_file");
  42. return false;
  43. }
  44. // Change to that directory
  45. $startdir = getcwd();
  46. chdir($dir);
  47. // Process the Blackboard XML files with the chosen XSL file.
  48. // The imsmanifest contains all the XML files and their relationships.
  49. // The XSL processor will open them as needed.
  50. $xsltproc = xslt_create();
  51. if (!xslt_process($xsltproc, 'imsmanifest.xml', "$dir/$xslt_file", "$dir/moodle.xml")) {
  52. notify('Failed writing xml file');
  53. chdir($startdir);
  54. return false;
  55. }
  56. // Copy the Blackboard course files into the moodle course_files structure
  57. $subdirs = get_subdirs($dir."/");
  58. mkdir("$dir/course_files", $CFG->directorypermissions);
  59. foreach ($subdirs as $subdir){
  60. rename($subdir, "course_files/$subdir");
  61. rename_hexfiles($subdir);
  62. }
  63. chdir($startdir);
  64. // Blackboard export successfully converted
  65. return true;
  66. }
  67. // This is not a Blackboard export
  68. return true;
  69. }
  70. /**
  71. * grabs all files in the directory, checks if the filenames start with a ! or @
  72. * then checks to see if the name is a hex - if so, it translates/renames correctly.
  73. *
  74. * @param string $subdir - the directory to parse.
  75. *
  76. */
  77. function rename_hexfiles($subdir) {
  78. //this bit of code grabs all files in the directory, and if they start with ! or @, performs the name conversion
  79. if ($handle = opendir("course_files/$subdir")) {
  80. while ($file = readdir($handle)) {
  81. if ($file == '..' or $file == '.') { //don't bother processing these!
  82. continue;
  83. }
  84. if(substr($file,0,1)=="!" || substr($file,0,1)=="@"){
  85. $outputfilename = "";
  86. $filebase = substr($file,1,strrpos($file,".")-1);
  87. if (ctype_xdigit($filebase)) { //check if this name is a hex - if not, don't bother to rename
  88. $filenamesplit = str_split($filebase,2);
  89. foreach($filenamesplit as $hexvalue){
  90. $outputfilename .= chr(hexdec($hexvalue));
  91. }
  92. $outputfilename .= strrchr($file,".");
  93. rename("course_files/$subdir/$file","course_files/$subdir/$outputfilename");
  94. }
  95. }
  96. }
  97. closedir($handle);
  98. }
  99. }
  100. ?>