/** * Creates an artefact in the manner required to overwrite existing profile * artefacts * * @param PluginImportLeap $importer The importer * @param string $artefacttype The type of artefact to create * @param string $title The title for the artefact * @param string $content The content for the artefact * @return int The ID of the artefact created */ private static function create_artefact(PluginImportLeap $importer, $artefacttype, $title, $content) { $classname = 'ArtefactType' . ucfirst($artefacttype); $artefact = new $classname(0, array('owner' => $importer->get('usr'))); $artefact->set('title', $title); $artefact->set('description', $content); $artefact->commit(); return $artefact->get('id'); }
/** * Render import entry requests for Mahara plans and their tasks * @param PluginImportLeap $importer * @return HTML code for displaying plans and choosing how to import them */ public static function render_import_entry_requests(PluginImportLeap $importer) { $importid = $importer->get('importertransport')->get('importid'); // Get import entry requests for Mahara plans $entryplans = array(); if ($ierplans = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ?', array($importid, 'plan'))) { foreach ($ierplans as $ierplan) { $plan = unserialize($ierplan->entrycontent); $plan['id'] = $ierplan->id; $plan['decision'] = $ierplan->decision; if (is_string($ierplan->duplicateditemids)) { $ierplan->duplicateditemids = unserialize($ierplan->duplicateditemids); } if (is_string($ierplan->existingitemids)) { $ierplan->existingitemids = unserialize($ierplan->existingitemids); } $plan['disabled'][PluginImport::DECISION_IGNORE] = false; $plan['disabled'][PluginImport::DECISION_ADDNEW] = false; $plan['disabled'][PluginImport::DECISION_APPEND] = true; $plan['disabled'][PluginImport::DECISION_REPLACE] = true; if (!empty($ierplan->duplicateditemids)) { $duplicated_item = artefact_instance_from_id($ierplan->duplicateditemids[0]); $plan['duplicateditem']['id'] = $duplicated_item->get('id'); $plan['duplicateditem']['title'] = $duplicated_item->get('title'); $res = $duplicated_item->render_self(array()); $plan['duplicateditem']['html'] = $res['html']; } else { if (!empty($ierplan->existingitemids)) { foreach ($ierplan->existingitemids as $id) { $existing_item = artefact_instance_from_id($id); $res = $existing_item->render_self(array()); $plan['existingitems'][] = array('id' => $existing_item->get('id'), 'title' => $existing_item->get('title'), 'html' => $res['html']); } } } // Get import entry requests of tasks in the plan $entrytasks = array(); if ($iertasks = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ? AND entryparent = ?', array($importid, 'task', $ierplan->entryid))) { foreach ($iertasks as $iertask) { $task = unserialize($iertask->entrycontent); $task['id'] = $iertask->id; $task['decision'] = $iertask->decision; $task['completiondate'] = format_date($task['completiondate'], 'strftimedate'); $task['disabled'][PluginImport::DECISION_IGNORE] = false; $task['disabled'][PluginImport::DECISION_ADDNEW] = false; $task['disabled'][PluginImport::DECISION_APPEND] = true; $task['disabled'][PluginImport::DECISION_REPLACE] = true; $entrytasks[] = $task; } } $plan['entrytasks'] = $entrytasks; $entryplans[] = $plan; } } $smarty = smarty_core(); $smarty->assign_by_ref('displaydecisions', $importer->get('displaydecisions')); $smarty->assign_by_ref('entryplans', $entryplans); return $smarty->fetch('artefact:plans:import/plans.tpl'); }
/** * Render import entry requests for Mahara user's profile fields * @param PluginImportLeap $importer * @return HTML code for displaying user's profile fields and choosing how to import them */ public static function render_import_entry_requests(PluginImportLeap $importer) { $importid = $importer->get('importertransport')->get('importid'); $profilefields = array('profile' => array('legend' => get_string('aboutme', 'artefact.internal'), 'fields' => array('firstname', 'lastname', 'studentid', 'preferredname', 'introduction')), 'contact' => array('legend' => get_string('contact', 'artefact.internal'), 'fields' => array('email', 'maildisabled', 'officialwebsite', 'personalwebsite', 'blogaddress', 'address', 'town', 'city', 'country', 'homenumber', 'businessnumber', 'mobilenumber', 'faxnumber')), 'messaging' => array('legend' => get_string('messaging', 'artefact.internal'), 'fields' => array('socialprofile')), 'general' => array('legend' => get_string('general'), 'fields' => array('occupation', 'industry'))); // Get import entry requests for Mahara profile fields $profilegroups = array(); foreach ($profilefields as $gr_key => $group) { $profilegroup = array(); $profilegroup['id'] = $gr_key; $profilegroup['legend'] = $group['legend']; foreach ($group['fields'] as $f) { if ($iers = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ?', array($importid, $f))) { $profilefieldvalues = array(); foreach ($iers as $ier) { $profilefieldvalue = unserialize($ier->entrycontent); $profilefieldvalue['id'] = $ier->id; $profilefieldvalue['decision'] = $ier->decision; $classname = generate_artefact_class_name($f); $profilefieldvalue['html'] = $classname::render_import_entry_request($profilefieldvalue); if (is_string($ier->duplicateditemids)) { $ier->duplicateditemids = unserialize($ier->duplicateditemids); } if (is_string($ier->existingitemids)) { $ier->existingitemids = unserialize($ier->existingitemids); } $profilefieldvalue['disabled'][PluginImport::DECISION_IGNORE] = false; if (!empty($ier->duplicateditemids)) { $duplicated_pfield = artefact_instance_from_id($ier->duplicateditemids[0]); $profilefieldvalue['duplicateditem']['id'] = $duplicated_pfield->get('id'); $res = $duplicated_pfield->render_self(array()); $profilefieldvalue['duplicateditem']['html'] = $res['html']; $profilefieldvalue['disabled'][PluginImport::DECISION_ADDNEW] = true; $profilefieldvalue['disabled'][PluginImport::DECISION_APPEND] = true; $profilefieldvalue['disabled'][PluginImport::DECISION_REPLACE] = true; } else { if (!empty($ier->existingitemids)) { foreach ($ier->existingitemids as $id) { $existing_pfield = artefact_instance_from_id($id); $res = $existing_pfield->render_self(array()); $profilefieldvalue['existingitems'][] = array('id' => $existing_pfield->get('id'), 'html' => $res['html']); } if ($f == 'email') { $profilefieldvalue['disabled'][PluginImport::DECISION_ADDNEW] = false; $profilefieldvalue['disabled'][PluginImport::DECISION_APPEND] = true; $profilefieldvalue['disabled'][PluginImport::DECISION_REPLACE] = true; } else { $is_singular = call_static_method(generate_artefact_class_name($ier->entrytype), 'is_singular'); $profilefieldvalue['disabled'][PluginImport::DECISION_ADDNEW] = $is_singular; $profilefieldvalue['disabled'][PluginImport::DECISION_APPEND] = !$is_singular; $profilefieldvalue['disabled'][PluginImport::DECISION_REPLACE] = !$is_singular; } } else { $profilefieldvalue['disabled'][PluginImport::DECISION_ADDNEW] = false; $profilefieldvalue['disabled'][PluginImport::DECISION_APPEND] = true; $profilefieldvalue['disabled'][PluginImport::DECISION_REPLACE] = true; } } $profilefieldvalues[] = $profilefieldvalue; } $profilegroup['fields'][$f] = $profilefieldvalues; } } $profilegroups[] = $profilegroup; } $smarty = smarty_core(); $smarty->assign_by_ref('displaydecisions', $importer->get('displaydecisions')); $smarty->assign_by_ref('profilegroups', $profilegroups); return $smarty->fetch('artefact:internal:import/profilefields.tpl'); }
/** * Attach files to their resume composite * TODO: this is experimental and is not actually working correctly. * This may be due to the structure of the export for resume items or * due to this import function being wrong or both. */ public static function setup_relationships(SimpleXMLElement $entry, PluginImportLeap $importer, $strategy, array $otherentries) { $newartefactmapping = array(); $class = false; switch ($strategy) { case self::STRATEGY_IMPORT_AS_ENTRY: case self::STRATEGY_IMPORT_AS_ABILITY: if ($strategy == self::STRATEGY_IMPORT_AS_ENTRY) { $types = array('careergoal', 'academicgoal', 'personalgoal', 'interest', 'coverletter'); } else { $types = array('workskill', 'academicskill', 'personalskill'); } // do stuff here break; case self::STRATEGY_IMPORT_AS_ACHIEVEMENT: $class = 'ArtefactTypeCertification'; break; case self::STRATEGY_IMPORT_AS_EMPLOYMENT: $class = 'ArtefactTypeEmploymenthistory'; break; case self::STRATEGY_IMPORT_AS_BOOK: $class = 'ArtefactTypeBook'; break; case self::STRATEGY_IMPORT_AS_EDUCATION: $class = 'ArtefactTypeEducationhistory'; break; case self::STRATEGY_IMPORT_AS_MEMBERSHIP: $class = 'ArtefactTypeMembership'; break; case self::STRATEGY_IMPORT_AS_SELECTION: // This space intentionally left blank break; default: throw new ImportException($importer, 'TODO: get_string: unknown strategy chosen for importing entry'); } foreach ($otherentries as $entryid) { $compositeentry = $importer->get_entry_by_id($entryid); $composite = null; foreach ($compositeentry->link as $compositelink) { if (class_exists($class)) { if (!$composite) { $artefactids = $importer->get_artefactids_imported_by_entryid((string) $compositeentry->id); $composite = new $class($artefactids[0], array('owner' => $importer->get('usr'))); } if ($id = $importer->create_attachment($entry, $compositelink, $composite)) { $newartefactmapping[$link['href']][] = $id; } if ($composite) { $composite->commit(); } } } } return $newartefactmapping; }
/** * Render import entry requests for Mahara comments * @param PluginImportLeap $importer * @return HTML code for displaying comments and choosing how to import them */ public static function render_import_entry_requests(PluginImportLeap $importer) { $importid = $importer->get('importertransport')->get('importid'); // Get import entry requests for Mahara comments $entrycomments = array(); if ($iercomments = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ?', array($importid, 'comment'))) { foreach ($iercomments as $iercomment) { $comment = unserialize($iercomment->entrycontent); $comment['id'] = $iercomment->id; $comment['decision'] = $iercomment->decision; $comment['disabled'][PluginImport::DECISION_IGNORE] = false; $comment['disabled'][PluginImport::DECISION_ADDNEW] = false; $comment['disabled'][PluginImport::DECISION_APPEND] = true; $comment['disabled'][PluginImport::DECISION_REPLACE] = true; $entrycomments[] = $comment; } } $smarty = smarty_core(); $smarty->assign_by_ref('displaydecisions', $importer->get('displaydecisions')); $smarty->assign_by_ref('entrycomments', $entrycomments); return $smarty->fetch('artefact:comment:import/comments.tpl'); }
/** * Creates a catch-all blog if one doesn't exist already * * @param PluginImportLeap $importer The importer * @return int The artefact ID of the catch-all blog */ private static function ensure_catchall_blog(PluginImportLeap $importer) { static $blogid = null; if (is_null($blogid)) { $time = time(); // TODO maybe the importer will get a time field to record time of import $blog = new ArtefactTypeBlog(); $title = $importer->get('xml')->xpath('//a:feed/a:title'); $blog->set('title', get_string('dataimportedfrom', 'artefact.blog', (string) $title[0])); $blog->set('description', get_string('entriesimportedfromleapexport', 'artefact.blog')); $blog->set('owner', $importer->get('usr')); $blog->set('ctime', $time); $blog->set('mtime', $time); $blog->commit(); $blogid = $blog->get('id'); } return $blogid; }
/** * 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; }
/** * Render import entry requests for Mahara annotations * @param PluginImportLeap $importer * @return HTML code for displaying annotations and choosing how to import them */ public static function render_import_entry_requests(PluginImportLeap $importer) { $importid = $importer->get('importertransport')->get('importid'); // Get import entry requests for Mahara annotations $entryannotations = array(); if ($ierannotations = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ?', array($importid, 'annotation'))) { foreach ($ierannotations as $ierannotation) { $annotation = unserialize($ierannotation->entrycontent); $annotation['id'] = $ierannotation->id; $annotation['decision'] = $ierannotation->decision; if (is_string($ierannotation->duplicateditemids)) { $ierannotation->duplicateditemids = unserialize($ierannotation->duplicateditemids); } if (is_string($ierannotation->existingitemids)) { $ierannotation->existingitemids = unserialize($ierannotation->existingitemids); } $annotation['disabled'][PluginImport::DECISION_IGNORE] = false; $annotation['disabled'][PluginImport::DECISION_ADDNEW] = false; $annotation['disabled'][PluginImport::DECISION_APPEND] = true; $annotation['disabled'][PluginImport::DECISION_REPLACE] = true; if (!empty($ierannotation->duplicateditemids)) { $duplicated_annotation = artefact_instance_from_id($ierannotation->duplicateditemids[0]); $annotation['duplicateditem']['id'] = $duplicated_annotation->get('id'); $annotation['duplicateditem']['title'] = $duplicated_annotation->get('title'); $res = $duplicated_annotation->render_self(array()); $annotation['duplicateditem']['html'] = $res['html']; } else { if (!empty($ierannotation->existingitemids)) { foreach ($ierannotation->existingitemids as $id) { $existing_annotation = artefact_instance_from_id($id); $res = $existing_annotation->render_self(array()); $annotation['existingitems'][] = array('id' => $existing_annotation->get('id'), 'title' => $existing_annotation->get('title'), 'html' => $res['html']); } } } // Get the feedback. $annotationfeedback = array(); if ($ierannotationfeedback = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ? AND entryparent = ?', array($importid, 'annotationfeedback', $ierannotation->entryid))) { foreach ($ierannotationfeedback as $ierfeedback) { $feedback = unserialize($ierfeedback->entrycontent); $feedback['id'] = $ierfeedback->id; $feedback['decision'] = $ierfeedback->decision; if (is_string($ierfeedback->duplicateditemids)) { $ierfeedback->duplicateditemids = unserialize($ierfeedback->duplicateditemids); } if (is_string($ierfeedback->existingitemids)) { $ierfeedback->existingitemids = unserialize($ierfeedback->existingitemids); } $feedback['disabled'][PluginImport::DECISION_IGNORE] = false; $feedback['disabled'][PluginImport::DECISION_ADDNEW] = false; $feedback['disabled'][PluginImport::DECISION_APPEND] = true; $feedback['disabled'][PluginImport::DECISION_REPLACE] = true; if (!empty($ierfeedback->duplicateditemids)) { $duplicated_feedback = artefact_instance_from_id($ierfeedback->duplicateditemids[0]); $feedback['duplicateditem']['id'] = $duplicated_feedback->get('id'); $feedback['duplicateditem']['title'] = $duplicated_feedback->get('title'); $feedback['duplicateditem']['html'] = $duplicated_feedback->render_self(); } else { if (!empty($ierfeedback->existingitemids)) { foreach ($ierfeedback->existingitemids as $id) { $existing_feedback = artefact_instance_from_id($id); $feedback['existingitems'][] = array('id' => $existing_feedback->get('id'), 'title' => $existing_feedback->get('title'), 'html' => $existing_feedback->render_self()); } } } $annotationfeedback[] = $feedback; } } $annotation['annotationfeedback'] = $annotationfeedback; $entryannotations[] = $annotation; } } $smarty = smarty_core(); $smarty->assign_by_ref('displaydecisions', $importer->get('displaydecisions')); $smarty->assign_by_ref('entryannotations', $entryannotations); return $smarty->fetch('artefact:annotation:import/annotations.tpl'); }
/** * Creates an artefact in the manner required to overwrite existing profile * artefacts * * @param PluginImportLeap $importer The importer * @param string $artefacttype The type of artefact to create * @param string $title The title for the artefact (with profile * fields, this is the main data) * @return int The ID of the artefact created */ private static function create_artefact(PluginImportLeap $importer, $artefacttype, $title) { $classname = generate_artefact_class_name($artefacttype); $artefact = null; if ($artefacttype == 'email') { // this type is a bit special. just check if we have one with this value already if ($a = get_record('artefact', 'artefacttype', 'email', 'owner', $importer->get('usr'), 'title', $title)) { $artefact = new $classname($a->id, $a); } } if (empty($artefact)) { try { $artefact = artefact_instance_from_type($artefacttype, $importer->get('usr')); } catch (Exception $e) { $artefact = new $classname(0, array('owner' => $importer->get('usr'))); } } $artefact->set('title', $title); $artefact->commit(); return $artefact->get('id'); }
/** * Add file attachment information to import entry request for a blogpost * * @param SimpleXMLElement $entry The entry to create the blogpost from * @param PluginImportLeap $importer The importer * @param int $blogentryid The ID of the import entry of the blog in which to put the post */ private static function add_files_to_import_entry_request_blogpost(SimpleXMLElement $entry, PluginImportLeap $importer, $blogentryid) { $files = array(); if (isset($entry->link)) { foreach ($entry->link as $link) { if ($importer->curie_equals($link['rel'], '', 'related') && isset($link['href'])) { if ($has_attachments = get_records_select_array('import_entry_requests', 'importid = ? AND entryid = ?', array($importer->get('importertransport')->get('importid'), (string) $link['href']))) { foreach ($has_attachments as $has_attachment) { $attachment = unserialize($has_attachment->entrycontent); $files[] = array('title' => $attachment['title'], 'description' => $attachment['description']); } } } } } return $files; }
/** * Render import entry requests for Mahara milestones and their facts * @param PluginImportLeap $importer * @return HTML code for displaying milestones and choosing how to import them */ public static function render_import_entry_requests(PluginImportLeap $importer) { $importid = $importer->get('importertransport')->get('importid'); // Get import entry requests for Mahara milestones $entrymilestones = array(); if ($iermilestones = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ?', array($importid, 'milestone'))) { foreach ($iermilestones as $iermilestone) { $milestone = unserialize($iermilestone->entrycontent); $milestone['id'] = $iermilestone->id; $milestone['decision'] = $iermilestone->decision; if (is_string($iermilestone->duplicateditemids)) { $iermilestone->duplicateditemids = unserialize($iermilestone->duplicateditemids); } if (is_string($iermilestone->existingitemids)) { $iermilestone->existingitemids = unserialize($iermilestone->existingitemids); } $milestone['disabled'][PluginImport::DECISION_IGNORE] = false; $milestone['disabled'][PluginImport::DECISION_ADDNEW] = false; $milestone['disabled'][PluginImport::DECISION_APPEND] = true; $milestone['disabled'][PluginImport::DECISION_REPLACE] = true; if (!empty($iermilestone->duplicateditemids)) { $duplicated_item = artefact_instance_from_id($iermilestone->duplicateditemids[0]); $milestone['duplicateditem']['id'] = $duplicated_item->get('id'); $milestone['duplicateditem']['title'] = $duplicated_item->get('title'); $res = $duplicated_item->render_self(array()); $milestone['duplicateditem']['html'] = $res['html']; } else { if (!empty($iermilestone->existingitemids)) { foreach ($iermilestone->existingitemids as $id) { $existing_item = artefact_instance_from_id($id); $res = $existing_item->render_self(array()); $milestone['existingitems'][] = array('id' => $existing_item->get('id'), 'title' => $existing_item->get('title'), 'html' => $res['html']); } } } // Get import entry requests of facts in the milestone $entryfacts = array(); if ($ierfacts = get_records_select_array('import_entry_requests', 'importid = ? AND entrytype = ? AND entryparent = ?', array($importid, 'fact', $iermilestone->entryid))) { foreach ($ierfacts as $ierfact) { $fact = unserialize($ierfact->entrycontent); $fact['id'] = $ierfact->id; $fact['decision'] = $ierfact->decision; $fact['completiondate'] = format_date($fact['completiondate'], 'strftimedate'); $fact['disabled'][PluginImport::DECISION_IGNORE] = false; $fact['disabled'][PluginImport::DECISION_ADDNEW] = false; $fact['disabled'][PluginImport::DECISION_APPEND] = true; $fact['disabled'][PluginImport::DECISION_REPLACE] = true; $entryfacts[] = $fact; } } $milestone['entryfacts'] = $entryfacts; $entrymilestones[] = $milestone; } } $smarty = smarty_core(); $smarty->assign_by_ref('displaydecisions', $importer->get('displaydecisions')); $smarty->assign_by_ref('entrymilestones', $entrymilestones); return $smarty->fetch('artefact:milestones:import/milestones.tpl'); }