Merge branch 'MDL-37522-published-attachment_24' of git://github.com/mudrd8mz/moodle...
[moodle.git] / backup / bb / restore_bb.php
blob962c46b7efca440a9c80aee9b2ad8c6c5d8a6b73
1 <?php
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.
5 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
7 // Ziba Scott <ziba@linuxbox.com> 10-25-04
8 require_once($CFG->dirroot.'/backup/bb/xsl_emulate_xslt.inc');
10 function get_subdirs($directory){
11 if (!$opendirectory = opendir( $directory )) {
12 return array();
14 while(false !== ($filename = readdir($opendirectory))) {
15 if (is_dir($directory.$filename) and $filename != ".." and $filename != "."){
16 $subdirs[] = $filename;
19 closedir($opendirectory);
20 return $subdirs;
24 function choose_bb_xsl($manifest){
25 $f = fopen($manifest,"r");
26 $buffer = fgets($f, 400);
27 $buffer = fgets($f, 400);
28 fclose($f);
29 if (strstr($buffer,"xmlns:bb=\"http://www.blackboard.com/content-packaging/\"")){
30 return "bb6_to_moodle.xsl";
32 return "bb5.5_to_moodle.xsl";
36 function blackboard_convert($dir){
37 global $CFG, $OUTPUT;
39 throw new coding_exception('bb_convert was not converted to new file api yet, sorry');
41 // Check for a Blackboard manifest file
42 if (is_readable($dir.'/imsmanifest.xml') && !is_readable($dir.'/moodle.xml')){
44 if (!function_exists('xslt_create')) { // XSLT MUST be installed for this to work
45 echo $OUTPUT->notification('You need the XSLT library installed in PHP to open this Blackboard file');
46 return false;
49 //Select the proper XSL file
50 $xslt_file = choose_bb_xsl($dir.'/imsmanifest.xml');
53 //TODO: Use the get_string function for this
54 echo "<li>Converting Blackboard export</li>";
56 // The XSL file must be in the same directory as the Blackboard files when it is processed
57 if (!copy($CFG->dirroot."/backup/bb/$xslt_file", "$dir/$xslt_file")) {
58 echo $OUTPUT->notification('Could not copy the XSLT file to '."$dir/$xslt_file");
59 return false;
62 // Change to that directory
63 $startdir = getcwd();
64 chdir($dir);
67 // Process the Blackboard XML files with the chosen XSL file.
68 // The imsmanifest contains all the XML files and their relationships.
69 // The XSL processor will open them as needed.
70 $xsltproc = xslt_create();
71 if (!xslt_process($xsltproc, 'imsmanifest.xml', "$dir/$xslt_file", "$dir/moodle.xml")) {
72 echo $OUTPUT->notification('Failed writing xml file');
73 chdir($startdir);
74 return false;
78 // Copy the Blackboard course files into the moodle course_files structure
79 $subdirs = get_subdirs($dir."/");
80 mkdir("$dir/course_files", $CFG->directorypermissions);
81 foreach ($subdirs as $subdir){
82 rename($subdir, "course_files/$subdir");
83 rename_hexfiles($subdir);
86 chdir($startdir);
88 // Blackboard export successfully converted
89 return true;
91 // This is not a Blackboard export
92 return true;
96 /**
97 * grabs all files in the directory, checks if the filenames start with a ! or @
98 * then checks to see if the name is a hex - if so, it translates/renames correctly.
100 * @param string $subdir - the directory to parse.
103 function rename_hexfiles($subdir) {
104 //this bit of code grabs all files in the directory, and if they start with ! or @, performs the name conversion
105 if ($handle = opendir("course_files/$subdir")) {
106 while ($file = readdir($handle)) {
107 if ($file == '..' or $file == '.') { //don't bother processing these!
108 continue;
110 if(substr($file,0,1)=="!" || substr($file,0,1)=="@"){
111 $outputfilename = "";
112 $filebase = substr($file,1,strrpos($file,".")-1);
113 if (ctype_xdigit($filebase)) { //check if this name is a hex - if not, don't bother to rename
114 $filenamesplit = str_split($filebase,2);
115 foreach($filenamesplit as $hexvalue){
116 $outputfilename .= chr(hexdec($hexvalue));
118 $outputfilename .= strrchr($file,".");
119 rename("course_files/$subdir/$file","course_files/$subdir/$outputfilename");
123 closedir($handle);