Created
June 3, 2016 13:40
-
-
Save woodwardtw/f6ced1565e162c9a5a4f5770017e3ace to your computer and use it in GitHub Desktop.
this is a bit of mess but it'll pull all the dependent pieces from a moodle export and create WP pages -- next step is getting parent/child relationships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: IMPORT MOODLE | |
* Plugin URI: https://github.com/woodwardtw/ | |
* Description: Parses imsmanifest.xml 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/mood/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/mood/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); | |
echo 'counter - ' . $counter . '<br/>'; | |
for ($x = 0; $x < $counter; $x++) { | |
$title = $data->organizations->organization->item->item[$x]->title[0]; | |
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); | |
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']); | |
//echo 'child counter - ' . $childcounter . '<br/>'; | |
// echo $childTitle . ' <br>'; | |
// echo $data->organizations->organization->item->item[$x]->item[$y]->attributes() . ' <br>'; | |
//echo 'identifierref - ' . $identifierref . ' <br>'; //I_219D9605_R | |
$match = array_key_exists($identifierref, $resources); | |
//var_dump($match); | |
if ($match === true) { | |
$result = @$resources[$identifierref] ?: null; | |
$encoded_url = urlencode($result); | |
$after_encoded_url = str_replace("%2F", "/", $encoded_url); //echo 'path--> ' .$result . '<br/><br/>'; | |
$content = file_get_contents('http://192.168.33.10/mood/'.$after_encoded_url); | |
// echo $content; | |
$my_post = array( | |
'post_title' => $childTitle, | |
'post_content' => $content, | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'page', | |
); | |
$the_post_id = wp_insert_post( $my_post ); | |
if ( ! add_post_meta( $the_post_id, 'identifier', $identifier, true ) ) { | |
update_post_meta ( $the_post_id, 'identifier', $identifier ); | |
} | |
if ( ! add_post_meta( $the_post_id, 'identifierref', $identifierref, true ) ) { | |
update_post_meta ( $the_post_id, 'identifierref', $identifierref ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment