/**
  * Logic shared by create_file_from_request() and create_file(). This actually takes the data and processes it into a file artefact
  * @param object $data
  * @param PluginImportLeap $importer
  * @param int $parent
  * @param SimplexMLElement $entry
  * @param unknown_type $entry_request
  * @return PluginArtefactFile
  * @throws ImportException
  */
 private static function create_file_from_entry_data($data, PluginImportLeap $importer, $entryid, $fromrequest = false)
 {
     global $USER;
     if ($fromrequest) {
         $usr = $USER;
     } else {
         $usr = $importer->get('usrobj');
     }
     $data->oldextension = end(explode('.', $data->title));
     // This API sucks, but that's not my problem
     if (!($id = ArtefactTypeFile::save_file($data->pathname, $data, $usr, true))) {
         $importer->trace("WARNING: the file for entry {$entryid} does not exist in the import (path={$data->pathname})");
         return false;
     }
     $artefact = artefact_instance_from_id($id);
     $artefact->set('tags', $data->tags);
     if ($fromrequest) {
         $artefact->set('mtime', $data->mtime);
     } else {
         // Work around that save_file doesn't let us set the mtime
         $artefact->set('mtime', strtotime($data->mtime));
     }
     // Now that we've actually imported the file, let's check to see whether it was an image, before making it a real profile icon
     $isprofileicon = $data->isprofileicon && $artefact->get('artefacttype') == 'image';
     if ($isprofileicon) {
         $artefact->set('artefacttype', 'profileicon');
         // Put profile pic in 'profile pics' folder
         $artefact->set('parent', ArtefactTypeFolder::get_folder_id(get_string('imagesdir', 'artefact.file'), get_string('imagesdirdesc', 'artefact.file'), null, true, $importer->get('usr')));
         // Sadly the process for creating a profile icon is a bit dumb. To
         // be honest, it shouldn't even be a separate artefact type
         $basedir = get_config('dataroot') . 'artefact/file/';
         $olddir = 'originals/' . $id % 256 . '/';
         $newdir = 'profileicons/originals/' . $id % 256 . '/';
         check_dir_exists($basedir . $newdir);
         if (!rename($basedir . $olddir . $id, $basedir . $newdir . $id)) {
             throw new ImportException($importer, 'TODO: get_string: was unable to move profile icon');
         }
     }
     $artefact->commit();
     return $artefact;
 }
 /**
  * Get name fields from imported namedata
  */
 private static function get_namefields(PluginImportLeap $importer, array $persondata)
 {
     $namefields = array('full_name' => false, 'legal_family_name' => false, 'legal_given_name' => false, 'preferred_family_name' => false, 'preferred_given_name' => false, 'family_name_first' => false, 'name_prefix' => false, 'name_suffix' => false);
     foreach ($persondata as $item) {
         $leapattributes = PluginImportLeap::get_attributes($item, $importer->get_leap2a_namespace());
         if (isset($leapattributes['field'])) {
             if (in_array($leapattributes['field'], array_keys($namefields))) {
                 // legal_given_name is allowed to occur any number of times
                 if ($leapattributes['field'] == 'legal_given_name' && $namefields['legal_given_name'] != '') {
                     $namefields['legal_given_name'] .= ' ' . (string) $item;
                 } else {
                     $namefields[$leapattributes['field']] = (string) $item;
                 }
             }
         } else {
             // 'Field' is required
             // http://wiki.cetis.ac.uk/2009-03/Leap2A_personal_data#field
             $importer->trace('WARNING: persondata element did not have leap:field attribute');
             continue;
         }
     }
     $familynamefirst = $namefields['family_name_first'] == 'yes' ? true : false;
     // Try to guess reasonable values for first/last names if they're not set
     if ($namefields['legal_given_name'] === false && $namefields['preferred_given_name'] !== false) {
         $namefields['legal_given_name'] = $namefields['preferred_given_name'];
     }
     if ($namefields['legal_family_name'] === false && $namefields['preferred_family_name'] !== false) {
         $namefields['legal_family_name'] = $namefields['preferred_family_name'];
     }
     // This is _an_ algorithm for parsing this info, I'm not saying it's the _best_ one ;)
     if ($familynamefirst) {
         $firstname = (string) $namefields['legal_given_name'] . ' ' . (string) $namefields['name_suffix'];
         $lastname = (string) $namefields['name_prefix'] . (string) $namefields['legal_family_name'];
         $preferredname = (string) $namefields['preferred_family_name'] . ' ' . (string) $namefields['preferred_given_name'];
     } else {
         $firstname = (string) $namefields['name_prefix'] . ' ' . (string) $namefields['legal_given_name'];
         $lastname = (string) $namefields['legal_family_name'] . ' ' . (string) $namefields['name_suffix'];
         $preferredname = (string) $namefields['preferred_given_name'] . ' ' . (string) $namefields['preferred_family_name'];
     }
     return array('firstname' => trim($firstname), 'lastname' => trim($lastname), 'preferredname' => trim($preferredname));
 }
 /**
  * Attaches a file to a blogpost entry that was just linked directly, rather than having a Leap2a entry
  * See http://wiki.leapspecs.org/2A/files
  *
  * @param SimpleXMLElement $blogpostentry
  * @param SimpleXMLElement $blogpostlink
  * @param PluginImportLeap $importer
  */
 private static function attach_linked_file($blogpostentry, $blogpostlink, PluginImportLeap $importer)
 {
     $importer->trace($blogpostlink);
     $pathname = urldecode((string) $blogpostlink['href']);
     $dir = dirname($importer->get('filename'));
     $pathname = $dir . '/' . $pathname;
     if (!file_exists($pathname)) {
         return false;
     }
     // Note: this data is passed (eventually) to ArtefactType->__construct,
     // which calls strtotime on the dates for us
     require_once 'file.php';
     $data = (object) array('title' => (string) $blogpostentry->title . ' ' . get_string('attachment', 'artefact.blog'), 'owner' => $importer->get('usr'));
     $data->oldextension = end(explode('.', $data->title));
     return ArtefactTypeFile::save_file($pathname, $data, $importer->get('usrobj'), true);
 }