Commit 28f94413 authored by Nigel McNie's avatar Nigel McNie
Browse files

Add new helper method - is_rdf_type - refactors some duplicate code.



Call it everywhere we're looking for rdf types.
Signed-off-by: default avatarNigel McNie <nigel@catalyst.net.nz>
parent f55b45d4
......@@ -53,13 +53,10 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
public static function get_import_strategies_for_entry(SimpleXMLElement $entry, PluginImport $importer) {
$strategies = array();
// TODO: when the xpath has an error in it, count(error) == 1 also.. so should check return type
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'selection') . ']')) == 1;
$correctcategoryscheme = count($entry->xpath('a:category[('
. $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'selection_type#') . ') and @term="Blog"]')) == 1;
if ($correctrdftype && $correctcategoryscheme) {
if (PluginImportLeap::is_rdf_type($entry, $importer, 'selection') && $correctcategoryscheme) {
$otherrequiredentries = array();
// Get entries that this blog feels are a part of it
......
......@@ -104,10 +104,8 @@ class LeapImportFile extends LeapImportArtefactPlugin {
* @return boolean Whether the entry is a file
*/
private static function is_file(SimpleXMLElement $entry, PluginImport $importer) {
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'resource') . ']')) == 1;
$outoflinecontent = isset($entry->content['src']);
return $correctrdftype && $outoflinecontent;
return PluginImportLeap::is_rdf_type($entry, $importer, 'resource') && $outoflinecontent;
}
/**
......@@ -123,11 +121,9 @@ class LeapImportFile extends LeapImportArtefactPlugin {
if (isset($cache[$id])) {
return $cache[$id];
}
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'selection') . ']')) == 1;
$correctcategoryscheme = count($entry->xpath('a:category[('
. $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'selection_type#') . ') and @term="Folder"]')) == 1;
return ($cache[$id] = $correctrdftype && $correctcategoryscheme);
return ($cache[$id] = PluginImportLeap::is_rdf_type($entry, $importer, 'selection') && $correctcategoryscheme);
}
/**
......
......@@ -150,10 +150,8 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
// If it's a raw entry with the right mahara:plugin and mahara:type
// we should be able to import it
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'entry') . ']')) == 1;
$correctplugintype = count($entry->xpath('mahara:artefactplugin[@mahara:plugin="internal"]')) == 1;
if ($correctrdftype && $correctplugintype) {
if (PluginImportLeap::is_rdf_type($entry, $importer, 'entry') && $correctplugintype) {
$strategies[] = array(
'strategy' => self::STRATEGY_IMPORT_AS_PROFILE_FIELD,
'score' => 100,
......
......@@ -79,11 +79,14 @@ class LeapImportResume extends LeapImportArtefactPlugin {
$strategies = array();
$correctplugintype = count($entry->xpath('mahara:artefactplugin[@mahara:plugin="resume"]')) == 1;
$isentry = PluginImportLeap::is_rdf_type($entry, $importer, 'entry');
$isability = PluginImportLeap::is_rdf_type($entry, $importer, 'ability');
$isachievement = PluginImportLeap::is_rdf_type($entry, $importer, 'achievement');
$isactivity = PluginImportLeap::is_rdf_type($entry, $importer, 'activity');
$isresource = PluginImportLeap::is_rdf_type($entry, $importer, 'resource');
// Goals, cover letter & interests
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'entry') . ']')) == 1;
if ($correctrdftype && $correctplugintype) {
if ($isentry && $correctplugintype) {
$strategies[] = array(
'strategy' => self::STRATEGY_IMPORT_AS_ENTRY,
'score' => 100,
......@@ -92,9 +95,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
}
// Skills
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'ability') . ']')) == 1;
if ($correctrdftype && $correctplugintype) {
if ($isability && $correctplugintype) {
$strategies[] = array(
'strategy' => self::STRATEGY_IMPORT_AS_ABILITY,
'score' => 100,
......@@ -103,9 +104,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
}
// Achievements
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'achievement') . ']')) == 1;
if ($correctrdftype && $correctplugintype) {
if ($isachievement && $correctplugintype) {
if (count($entry->xpath('mahara:artefactplugin[@mahara:plugin="resume" and @mahara:type="pseudo:certification"]')) == 1) {
// We know for certain these are meant to be certifications within Mahara
$score = 100;
......@@ -125,11 +124,9 @@ class LeapImportResume extends LeapImportArtefactPlugin {
// Employment
$other_required_entries = array();
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'activity') . ']')) == 1;
$correctcategoryscheme = count($entry->xpath('a:category[('
. $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'life_area#') . ') and @term="Work"]')) == 1;
if ($correctrdftype && $correctcategoryscheme) {
if ($isactivity && $correctcategoryscheme) {
foreach ($entry->link as $link) {
if (!isset($other_required_entries['organisation'])
&& $organisation = self::check_for_supporting_organisation($importer, $link)) {
......@@ -146,19 +143,15 @@ class LeapImportResume extends LeapImportArtefactPlugin {
// Books
$other_required_entries = array();
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'resource') . ']')) == 1;
$correctcategoryscheme = count($entry->xpath('a:category[('
. $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'resource_type#') . ') and @term="Printed"]')) == 1;
if ($correctrdftype && $correctcategoryscheme) {
if ($isresource && $correctcategoryscheme) {
// If it exists, the related achievement will be the user's role in
// relation to the book
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], '', 'relation') && isset($link['href'])) {
if ($potentialrole = $importer->get_entry_by_id((string)$link['href'])) {
$correctrdftype = count($potentialrole->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'achievement') . ']')) == 1;
if ($correctrdftype) {
if (PluginImportLeap::is_rdf_type($potentialrole, $importer, 'achievement')) {
// We have a related achievement!
$other_required_entries[] = (string)$link['href'];
break;
......@@ -176,20 +169,16 @@ class LeapImportResume extends LeapImportArtefactPlugin {
// Education
$other_required_entries = array();
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'activity') . ']')) == 1;
$correctcategoryscheme = count($entry->xpath('a:category[('
. $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'life_area#') . ') and @term="Education"]')) == 1;
if ($correctrdftype && $correctcategoryscheme) {
if ($isactivity && $correctcategoryscheme) {
// If this entry supports an achievement, that achievement will be
// the qualification the user gained in relation to this entry
foreach ($entry->link as $link) {
if (!isset($other_required_entries['achievement'])
&& $importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'supports') && isset($link['href'])) {
if ($potentialqualification = $importer->get_entry_by_id((string)$link['href'])) {
$correctrdftype = count($potentialqualification->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'achievement') . ']')) == 1;
if ($correctrdftype) {
if (PluginImportLeap::is_rdf_type($potentialqualification, $importer, 'achievement')) {
// We have a related achievement!
$other_required_entries['achievement'] = (string)$link['href'];
}
......@@ -209,10 +198,8 @@ class LeapImportResume extends LeapImportArtefactPlugin {
}
// Professional Membership
$correctrdftype = count($entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'activity') . ']')) == 1;
$correctmaharatype = count($entry->xpath('mahara:artefactplugin[@mahara:plugin="resume" and @mahara:type="pseudo:membership"]')) == 1;
if ($correctrdftype && $correctmaharatype) {
if ($isactivity && $correctmaharatype) {
$strategies[] = array(
'strategy' => self::STRATEGY_IMPORT_AS_MEMBERSHIP,
'score' => 100,
......@@ -472,9 +459,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
private static function check_for_supporting_organisation(PluginImport $importer, $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'is_supported_by') && isset($link['href'])) {
if ($potentialorganisation = $importer->get_entry_by_id((string)$link['href'])) {
$correctrdftype = count($potentialorganisation->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'organisation') . ']')) == 1;
if ($correctrdftype) {
if (PluginImportLeap::is_rdf_type($potentialorganisation, $importer, 'organisation')) {
return (string)$link['href'];
}
}
......@@ -509,8 +494,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
$found = true;
}
else if ($potentialselection = $importer->get_entry_by_id($href)) {
if (count($potentialselection->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, 'selection') . ']')) == 1) {
if (PluginImportLeap::is_rdf_type($potentialselection, $importer, 'selection')) {
if (count($potentialselection->xpath('a:category[('
. $importer->curie_xpath('@scheme', PluginImportLeap::NS_CATEGORIES, 'selection_type#') . ') and @term="Grouping"]')) == 1) {
if (count($potentialselection->xpath('mahara:artefactplugin[@mahara:type="' . $selectiontype . '"]')) == 1) {
......
......@@ -485,6 +485,24 @@ class PluginImportLeap extends PluginImport {
return $curie == $namespace . $term;
}
/**
* Returns whether an entry has the given RDF type
*
* This helper method should probably be replaced by the as yet unwritten
* get_rdf_type at some point, as it would be faster to get the type and do
* comparisons than call this multiple times.
*
* @param SimpleXMLElement $entry The entry to check
* $param PluginImportLeap $importer The importer
* @param string $rdftype The type to check for
* @return boolean Whether the entry has the given RDF type
*/
public function is_rdf_type(SimpleXMLElement $entry, PluginImportLeap $importer, $rdftype) {
$result = $entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, $rdftype) . ']');
return isset($result[0]) && $result[0] instanceof SimpleXMLElement;
}
/**
* TODO: document
* TODO: make sure we are rawurlencoding our file paths in our export
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment