|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file facilitates the conversion of a Blackboard course export 00003 // into a Moodle course export. It assumes an unzipped directory and makes in-place alterations. 00004 00005 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.'); 00006 00007 // Ziba Scott <ziba@linuxbox.com> 10-25-04 00008 require_once($CFG->dirroot.'/backup/bb/xsl_emulate_xslt.inc'); 00009 00010 function get_subdirs($directory){ 00011 $opendirectory = opendir( $directory ); 00012 while(false !== ($filename = readdir($opendirectory))) { 00013 if (is_dir($directory.$filename) and $filename != ".." and $filename != "."){ 00014 $subdirs[] = $filename; 00015 } 00016 } 00017 closedir($opendirectory); 00018 return $subdirs; 00019 } 00020 00021 00022 function choose_bb_xsl($manifest){ 00023 $f = fopen($manifest,"r"); 00024 $buffer = fgets($f, 400); 00025 $buffer = fgets($f, 400); 00026 fclose($f); 00027 if (strstr($buffer,"xmlns:bb=\"http://www.blackboard.com/content-packaging/\"")){ 00028 return "bb6_to_moodle.xsl"; 00029 } 00030 return "bb5.5_to_moodle.xsl"; 00031 } 00032 00033 00034 function blackboard_convert($dir){ 00035 global $CFG, $OUTPUT; 00036 00037 throw new coding_exception('bb_convert was not converted to new file api yet, sorry'); 00038 00039 // Check for a Blackboard manifest file 00040 if (is_readable($dir.'/imsmanifest.xml') && !is_readable($dir.'/moodle.xml')){ 00041 00042 if (!function_exists('xslt_create')) { // XSLT MUST be installed for this to work 00043 echo $OUTPUT->notification('You need the XSLT library installed in PHP to open this Blackboard file'); 00044 return false; 00045 } 00046 00047 //Select the proper XSL file 00048 $xslt_file = choose_bb_xsl($dir.'/imsmanifest.xml'); 00049 00050 00051 //TODO: Use the get_string function for this 00052 echo "<li>Converting Blackboard export</li>"; 00053 00054 // The XSL file must be in the same directory as the Blackboard files when it is processed 00055 if (!copy($CFG->dirroot."/backup/bb/$xslt_file", "$dir/$xslt_file")) { 00056 echo $OUTPUT->notification('Could not copy the XSLT file to '."$dir/$xslt_file"); 00057 return false; 00058 } 00059 00060 // Change to that directory 00061 $startdir = getcwd(); 00062 chdir($dir); 00063 00064 00065 // Process the Blackboard XML files with the chosen XSL file. 00066 // The imsmanifest contains all the XML files and their relationships. 00067 // The XSL processor will open them as needed. 00068 $xsltproc = xslt_create(); 00069 if (!xslt_process($xsltproc, 'imsmanifest.xml', "$dir/$xslt_file", "$dir/moodle.xml")) { 00070 echo $OUTPUT->notification('Failed writing xml file'); 00071 chdir($startdir); 00072 return false; 00073 } 00074 00075 00076 // Copy the Blackboard course files into the moodle course_files structure 00077 $subdirs = get_subdirs($dir."/"); 00078 mkdir("$dir/course_files", $CFG->directorypermissions); 00079 foreach ($subdirs as $subdir){ 00080 rename($subdir, "course_files/$subdir"); 00081 rename_hexfiles($subdir); 00082 } 00083 00084 chdir($startdir); 00085 00086 // Blackboard export successfully converted 00087 return true; 00088 } 00089 // This is not a Blackboard export 00090 return true; 00091 00092 } 00093 00101 function rename_hexfiles($subdir) { 00102 //this bit of code grabs all files in the directory, and if they start with ! or @, performs the name conversion 00103 if ($handle = opendir("course_files/$subdir")) { 00104 while ($file = readdir($handle)) { 00105 if ($file == '..' or $file == '.') { //don't bother processing these! 00106 continue; 00107 } 00108 if(substr($file,0,1)=="!" || substr($file,0,1)=="@"){ 00109 $outputfilename = ""; 00110 $filebase = substr($file,1,strrpos($file,".")-1); 00111 if (ctype_xdigit($filebase)) { //check if this name is a hex - if not, don't bother to rename 00112 $filenamesplit = str_split($filebase,2); 00113 foreach($filenamesplit as $hexvalue){ 00114 $outputfilename .= chr(hexdec($hexvalue)); 00115 } 00116 $outputfilename .= strrchr($file,"."); 00117 rename("course_files/$subdir/$file","course_files/$subdir/$outputfilename"); 00118 } 00119 } 00120 } 00121 closedir($handle); 00122 } 00123 }