MDL-28284 don't load filepicker module for every page
[moodle.git] / backup / bb / restore_bb.php
blob3e01adfd85938a07d941be8ee752da86ecf284b5
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 $opendirectory = opendir( $directory );
12 while(false !== ($filename = readdir($opendirectory))) {
13 if (is_dir($directory.$filename) and $filename != ".." and $filename != "."){
14 $subdirs[] = $filename;
17 closedir($opendirectory);
18 return $subdirs;
22 function choose_bb_xsl($manifest){
23 $f = fopen($manifest,"r");
24 $buffer = fgets($f, 400);
25 $buffer = fgets($f, 400);
26 fclose($f);
27 if (strstr($buffer,"xmlns:bb=\"http://www.blackboard.com/content-packaging/\"")){
28 return "bb6_to_moodle.xsl";
30 return "bb5.5_to_moodle.xsl";
34 function blackboard_convert($dir){
35 global $CFG, $OUTPUT;
37 throw new coding_exception('bb_convert was not converted to new file api yet, sorry');
39 // Check for a Blackboard manifest file
40 if (is_readable($dir.'/imsmanifest.xml') && !is_readable($dir.'/moodle.xml')){
42 if (!function_exists('xslt_create')) { // XSLT MUST be installed for this to work
43 echo $OUTPUT->notification('You need the XSLT library installed in PHP to open this Blackboard file');
44 return false;
47 //Select the proper XSL file
48 $xslt_file = choose_bb_xsl($dir.'/imsmanifest.xml');
51 //TODO: Use the get_string function for this
52 echo "<li>Converting Blackboard export</li>";
54 // The XSL file must be in the same directory as the Blackboard files when it is processed
55 if (!copy($CFG->dirroot."/backup/bb/$xslt_file", "$dir/$xslt_file")) {
56 echo $OUTPUT->notification('Could not copy the XSLT file to '."$dir/$xslt_file");
57 return false;
60 // Change to that directory
61 $startdir = getcwd();
62 chdir($dir);
65 // Process the Blackboard XML files with the chosen XSL file.
66 // The imsmanifest contains all the XML files and their relationships.
67 // The XSL processor will open them as needed.
68 $xsltproc = xslt_create();
69 if (!xslt_process($xsltproc, 'imsmanifest.xml', "$dir/$xslt_file", "$dir/moodle.xml")) {
70 echo $OUTPUT->notification('Failed writing xml file');
71 chdir($startdir);
72 return false;
76 // Copy the Blackboard course files into the moodle course_files structure
77 $subdirs = get_subdirs($dir."/");
78 mkdir("$dir/course_files", $CFG->directorypermissions);
79 foreach ($subdirs as $subdir){
80 rename($subdir, "course_files/$subdir");
81 rename_hexfiles($subdir);
84 chdir($startdir);
86 // Blackboard export successfully converted
87 return true;
89 // This is not a Blackboard export
90 return true;
94 /**
95 * grabs all files in the directory, checks if the filenames start with a ! or @
96 * then checks to see if the name is a hex - if so, it translates/renames correctly.
98 * @param string $subdir - the directory to parse.
101 function rename_hexfiles($subdir) {
102 //this bit of code grabs all files in the directory, and if they start with ! or @, performs the name conversion
103 if ($handle = opendir("course_files/$subdir")) {
104 while ($file = readdir($handle)) {
105 if ($file == '..' or $file == '.') { //don't bother processing these!
106 continue;
108 if(substr($file,0,1)=="!" || substr($file,0,1)=="@"){
109 $outputfilename = "";
110 $filebase = substr($file,1,strrpos($file,".")-1);
111 if (ctype_xdigit($filebase)) { //check if this name is a hex - if not, don't bother to rename
112 $filenamesplit = str_split($filebase,2);
113 foreach($filenamesplit as $hexvalue){
114 $outputfilename .= chr(hexdec($hexvalue));
116 $outputfilename .= strrchr($file,".");
117 rename("course_files/$subdir/$file","course_files/$subdir/$outputfilename");
121 closedir($handle);