Import Blackboard Common Cartridge into WordPress


flickr photo shared by Internet Archive Book Images with no copyright restriction (Flickr Commons)

It’s far from pretty and I don’t know how well it’ll play with other courses but . . . here’s the plugin I used to import a few Blackboard course exports into WordPress. It’s nothing magical but I think it should get you most of the WordPress kind of content on courses sort of like these. It won’t pull in quizzes or anything like that and I make no attempt to map user/user roles. I just want the page content, discussion prompts, that sort of stuff.

I opted to pull this content in as posts and tag them (as opposed to the Moodle import where I used Pages and parent/child relationships). It just seemed like the right path based on the content.

In any case, here’s the code. It’s super ugly as I adapted some stuff I was playing around with during my Moodle attempts. It loads the xml file twice and probably does other things that will make programmers sad but honestly I was bored with it and didn’t feel it was worth re-writing. I just wanted it to work. It may also be a terrible example to set as I’m not sure I want people importing their Blackboard courses into WordPress.

I do know much, much more about navigating xml files in php than I did previously.

Also take note that the URL are hardcoded in the code below. If you actually want to run this for some reason, you’ll want to switch them. This script is triggered by visiting http://theurl.com/?run_my_script.

<?php
/**
 * Plugin Name: IMPORT bb 
 * Plugin URI: https://github.com/woodwardtw/
 * Description: Parses cc export to create WP pages
 * Version: 1.1
 * Author: Tom Woodward
 * Author URI: http://bionicteaching.com
 * License: GPL2
 */
 
 /*   2016 Tom  (email : bionicteaching@gmail.com)
 
 	This program is free software; you can redistribute it and/or modify
 	it under the terms of the GNU General Public License, version 2, as 
 	published by the Free Software Foundation.
 
 	This program is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 	GNU General Public License for more details.
 
 	You should have received a copy of the GNU General Public License
 	along with this program; if not, write to the Free Software
 	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

if ( isset($_GET['run_my_script']) ) {
    add_action('init', 'makePages', 10);
    add_action('init', 'script_finished', 20);
}
 
//function my_script_function() {
    // this is where your custom code goes
//}
 
function script_finished() {
    add_option('my_script_complete', 1);
    die("Script finished.");
}


function makePages(){

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load('http://192.168.33.10/bbcc/imsmanifest.xml');

$resourcesList = $dom->getElementsByTagName('resources');

foreach ($resourcesList as $resourcesListRow) {

  $resourceList = $resourcesListRow->getElementsByTagName('resource');
    $resources = array();

  foreach ($resourceList as $resourceListRow) {

    // decode the attributes
    // e.g. <resource identifier="A001" type="webcontent" adlcp:scormtype="sco" href="a001index.html">
    $identifier = $resourceListRow->getAttribute('identifier');
    $type = $resourceListRow->getAttribute('type');
    $scormtype = $resourceListRow->getAttribute('adlcp:scormtype');
    $href = $resourceListRow->getAttribute('href');
    $fileList = $resourceListRow->getElementsByTagName('file');
    $resources[$identifier] = $href;
    //echo $identifier . ' - href ' . $href . '<br/>';
  }
}
//var_dump($resources);



$map_url = 'http://192.168.33.10/bbcc/imsmanifest.xml';

if (($response_xml_data = file_get_contents($map_url))===false){
    echo "Error fetching XML\n";
  } else {
   libxml_use_internal_errors(true);
   $data = simplexml_load_string($response_xml_data);
   if (!$data) {
       echo "Error loading XML\n";
       foreach(libxml_get_errors() as $error) {
           echo "\t", $error->message;
       }
   } else {

       $counter = count($data->organizations->organization[0]->item->item);//Count parents
      //echo 'counter - ' . $counter . '<br/>';      

      for ($x = 0; $x < $counter; $x++) {
          $title = $data->organizations->organization->item->item[$x]->title[0]; //get parent titles
          //echo '<h2>' . $title . ' - ' . $x . '</h2>';
          //var_dump( $data->organizations->organization->item->item[$x]['identifier']) . ' <br>';

          $childcounter = count($data->organizations->organization[0]->item->item[$x]->item); //count children
          
          echo 'childcounter- ' . $childcounter . '<br/>';       
          for ($y = 0; $y < $childcounter; $y++) {
              $childTitle = $data->organizations->organization->item->item[$x]->item[$y]->title;
              $identifierref = strval($data->organizations->organization->item->item[$x]->item[$y]['identifierref']);
              $childTitleItems = count($data->organizations->organization->item->item[$x]->item[$y]->item);              

              //echo 'child counter - ' . $childcounter . '<br/>';
              echo '<strong>childTitle- ' . $childTitle . ' </strong><br>';
              //echo 'childTitleItems- ' . $childTitleItems . '<br/>';
              echo $data->organizations->organization->item->item[$x]->item[$y]->attributes() . ' <br>';
              echo 'identifierref - ' . $identifierref . ' <br>';  //I_219D9605_R   
                for ($z = 0; $z < $childTitleItems; $z++){
                  $childTitleItemsTitles = $data->organizations->organization->item->item[$x]->item[$y]->item[$z]->title;
                  $childidentifierref = strval($data->organizations->organization->item->item[$x]->item[$y]->item[$z]['identifierref']);
                  echo $childTitleItemsTitles . '<br/>';
                  echo $childidentifierref . '<br/>';

                  $match = array_key_exists($childidentifierref, $resources);
                  //var_dump($match);
                  if ($match === true) {
                    $result = @$resources[$childidentifierref] ?: null;;
                  echo 'path--> ' .$result . '<br/><br/>';  
                  $content = file_get_contents('http://192.168.33.10/bbcc/' . $result);
                  echo $content;

                }
                
                $my_post = array(
                'post_title' => $childTitle,
                'post_content' => $content,
                'post_status' => 'publish',
                'post_author'   => 1,
                'tags_input' => $title,
                );
                     
                 $the_post_id = wp_insert_post( $my_post );   
            }
              $match = array_key_exists($identifierref, $resources);
              //var_dump($match);
              if ($match === true) {
                $result = @$resources[$identifierref] ?: null;;
              echo 'path--> ' .$result . '<br/><br/>';  
              $content = file_get_contents('http://192.168.33.10/bbcc/' . $result);
              echo $content;


                $my_post = array(
                'post_title' => $childTitle,
                'post_content' => $content,
                'post_status' => 'publish',
                'post_author'   => 1,
                'tags_input' => $title,
                );
                     
                 $the_post_id = wp_insert_post( $my_post );              
          }                             


        }
      }     
   }
}
        
        }
     

2 thoughts on “Import Blackboard Common Cartridge into WordPress

Comments are closed.