Commit 82b0028e authored by Lisa Seeto's avatar Lisa Seeto Committed by Robert Lyon

Bug 1855349: Tagged Journal Entries are not importing in Leap2A

- added import class funcs for blogs to add/ extract tag data
read from import file and stored on the block data config
- get rid of warning message by casting to array
- add export/import functionality for old layout export/imports

Change-Id: Iccaa8ee97e0730aebf035e1cbf31749622b28175
Signed-off-by: default avatarLisa Seeto <lisaseeto@catalyst.net.nz>
parent 2c5c70c3
......@@ -88,6 +88,15 @@ class PluginBlocktypeTaggedposts extends MaharaCoreBlocktype {
$configdata = $instance->get('configdata');
$results = array();
// 'tagsin' and 'tagsout' are configdata values brought in by Leap2A export
// but we need to add them in one go
if (isset($configdata['tagsvialeap2a'])) {
self::save_tag_selection($configdata['tagsvialeap2a'], $instance);
unset($configdata['tagsvialeap2a']);
$instance->set('configdata', $configdata);
$instance->commit();
}
$tagsin = $tagsout = array();
if ($versioning) {
$tagrecords = $configdata['tagrecords'];
......@@ -189,6 +198,7 @@ class PluginBlocktypeTaggedposts extends MaharaCoreBlocktype {
// Display all posts, from all blogs, owned by this user
$tagsin = $tagsout = array();
$results = self::get_blog_posts_in_block($instance, $tagsin, $tagsout, $versioning);
if ($tagsin || $tagsout) {
$smarty->assign('blockid', $instance->get('id'));
......
......@@ -54,3 +54,70 @@ class LeapExportElementBlog extends LeapExportElement {
return 'html';
}
}
class LeapExportTaggedPosts {
/**
* This function inserts tag data into a taggedpost's block configdata
* The tag is put into the configdata as an array of ['tagsin'] or ['tagsout']
* based on its type (included: 1 or excluded: 0)
* @param array &$config by reference, the configdata array containing block data
* @return array &$config the configdata array
*/
public function get_blocktype_export_data(&$config, $view, $oldlayout=false) {
if ($oldlayout) {
foreach ($config as &$row) {
foreach($row as $columns => &$column) {
foreach ($column as &$blocks) {
foreach($blocks as &$block) {
if ($block['blocktype'] == 'taggedposts') {
$sql = '
SELECT tag, tagtype
FROM {blocktype_taggedposts_tags}
WHERE block_instance = ?
';
if ($tags = get_records_sql_array($sql, array($block['id']))) {
foreach ($tags as $tag) {
if ($tag->tagtype == PluginBlocktypeTaggedposts::TAGTYPE_INCLUDE) {
isset($block['config']['tagsin']) ? array_push($block['config']['tagsin'], $tag->tag) : $block['config']['tagsin'] = array($tag->tag);
}
else {
$excludedtag = '-' . $tag->tag;
isset($block['config']['tagsout']) ? array_push($block['config']['tagsout'], $excludedtag) : $block['config']['tagsout'] = array($excludedtag);
}
}
}
}
}#end foreach blocks
}#end foreach column
}#end foreach row
}#end foreach config
}
else {
$sql = '
SELECT bid.block, bid.positionx, bid.positiony, tp.tag, tp.tagtype
FROM {block_instance_dimension} AS bid INNER JOIN {block_instance} bi on bid.block = bi.id
INNER JOIN {blocktype_taggedposts_tags} AS tp
ON bid.block = tp.block_instance WHERE bi.view = ?
';
if ($taggedpostplacement = get_records_sql_array($sql, array($view))) {
foreach ($taggedpostplacement as $tpp) {
foreach ($config as &$cg) {
if ($cg['positionx'] == $tpp->positionx && $cg['positiony'] == $tpp->positiony) {
if ($tpp->tagtype == PluginBlocktypeTaggedposts::TAGTYPE_INCLUDE) {
isset($cg['config']['tagsin']) ? array_push($cg['config']['tagsin'], $tpp->tag) : $cg['config']['tagsin'] = array($tpp->tag);
}
else {
$excludedtag = '-' . $tpp->tag;
isset($cg['config']['tagsout']) ? array_push($cg['config']['tagsout'], $excludedtag) : $cg['config']['tagsout'] = array($excludedtag);
}
}
}
}
}
}
return $config;
}
}
......@@ -652,7 +652,7 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
*/
private static function setup_outoflinecontent_relationship(SimpleXMLElement $entry, PluginImportLeap $importer) {
$artefactids = $importer->get_artefactids_imported_by_entryid((string)$entry->id);
if (count($artefactids) == 2) {
if (count((array)$artefactids) == 2) {
// In this case, a file was created as a result of
// importing a blog entry with out-of-line content. We
// attach the file to this post.
......@@ -661,5 +661,35 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
$blogpost->commit();
}
}
}
/**
* This function inserts tag data into a taggedpost's block configdata
* The tag is put into the block['config'] as an array of ['tagsin'] or ['tagsout']
* @param array $blockinstance the leap2a data containing blockinstance information
* @param array &$block by reference, the block['config'] array
* @return array $block the block['config'] array
*/
class LeapImportTaggedPosts {
public function get_blocktype_import_data($blockinstance, &$block) {
$block['tagsvialeap2a'] = array();
if ($tagsin = $blockinstance->xpath('mahara:tagsin')) {
foreach ($tagsin as $tagin) {
$tagin = (string)$tagin;
array_push($block['tagsvialeap2a'], $tagin);
}
}
if ($tagsout = $blockinstance->xpath('mahara:tagsout')) {
foreach ($tagsout as $tagout) {
$tagout = (string)$tagout;
if (substr($tagout, 0, 1) != '-') {
$tagout = '-' . $tagout;
}
array_push($block['tagsvialeap2a'], $tagout);
}
}
return $block;
}
}
......@@ -239,6 +239,29 @@ class PluginExportLeap extends PluginExport {
}
}
/**
* Get any extra configdata that should export to a blockinstance, i.e tags for taggedposts
* by getting all distinct, installed blocktypes on a view then calling the get_blocktype_export_data(...)
* for each block type where necessary.
* @param array &$config by reference, the configdata array containing block data
* @param int $viewid id of the view containing the blocks
*
*/
private function get_block_export_data_for_blocktype(&$config, $viewid, $oldlayout=false) {
$blocktypes = get_records_sql_array('
SELECT DISTINCT bi.blocktype, b.artefactplugin
FROM {block_instance} bi
JOIN {blocktype_installed} b
ON b.name = bi.blocktype
WHERE view = ?',
array($viewid));
foreach ($blocktypes as $blocktype) {
$classname = 'LeapExport' . $blocktype->blocktype;
if (class_exists($classname) && method_exists($classname, 'get_blocktype_export_data')) {
call_user_func_array(array($classname, 'get_blocktype_export_data'), array(&$config, $viewid, $oldlayout));
}
}
}
/**
* Export the views
......@@ -284,7 +307,6 @@ class PluginExportLeap extends PluginExport {
if ($viewcontent = self::parse_xhtmlish_content($view->build_rows(false, true), $view->get('id'))) {
$this->smarty->assign('content', clean_html($viewcontent, true));
}
$this->smarty->assign('viewdata', $config['rows']);
$layout = $view->get_layout();
$widths = '';
foreach ($layout->rows as $row){
......@@ -292,12 +314,16 @@ class PluginExportLeap extends PluginExport {
}
$widths = substr($widths, 0, -1);
$this->smarty->assign('layout', $widths);
$this->get_block_export_data_for_blocktype($config['rows'], $view->get('id'), true);
$this->smarty->assign('viewdata', $config['rows']);
}
else {
if ($viewblocks = self::parse_xhtmlish_content($view->get_blocks(false, true), $view->get('id'))) {
$this->smarty->assign('content', clean_html($viewblocks, true));
$this->smarty->assign('blocks', $config['grid']);
$this->get_block_export_data_for_blocktype($config['grid'], $view->get('id'));
}
$this->smarty->assign('blocks', $config['grid']);
$this->smarty->assign('newlayout', true);
}
......
......@@ -1309,50 +1309,56 @@ class PluginImportLeap extends PluginImport {
$colindex = 1;
foreach ($columns as $column) {
$blockinstances = $column->xpath('mahara:blockinstance');
$order = 1;
$config['grid'][$rowindex][$colindex] = array();
$config['grid'][$rowindex][$colindex]['blocks'] = array();
$config['grid'][$rowindex][$colindex]['width'] = $columnwidths[$rowindex-1][$colindex-1];
foreach ($blockinstances as $blockinstance) {
$attrs = self::get_attributes($blockinstance, PluginImportLeap::NS_MAHARA);
if (!isset($attrs['blocktype'])) {
$this->trace(" No mahara:blocktype attribute set for blockinstance at row $rowindex col $colindex, order $order: skipping");
continue;
}
$this->trace(" Found block with type {$attrs['blocktype']} at [$rowindex][$colindex][$order]", self::LOG_LEVEL_VERBOSE);
if ($blocktypes_installed === null) {
$blocktypes_installed = array_map(function($a) { return $a->name; }, plugins_installed('blocktype'));
}
if (in_array($attrs['blocktype'], $blocktypes_installed)) {
$configelements = $blockinstance->xpath('mahara:*');
$config['grid'][$rowindex][$colindex]['blocks'][$order] = array(
'type' => $attrs['blocktype'],
'title' => $attrs['blocktitle'],
'row' => $rowindex,
'column' => $colindex,
'order' => $order,
'config' => array()
);
foreach ($configelements as $element) {
$value = json_decode((string)$element);
if (is_array($value) && isset($value[0])) {
$config['grid'][$rowindex][$colindex]['blocks'][$order]['config'][$element->getName()] = $value[0];
}
else {
$this->trace(" Value for {$element->getName()} is not an array, ignoring (value follows below)");
$this->trace($value);
$blockinstances = $column->xpath('mahara:blockinstance');
$order = 1;
$config['grid'][$rowindex][$colindex] = array();
$config['grid'][$rowindex][$colindex]['blocks'] = array();
$config['grid'][$rowindex][$colindex]['width'] = $columnwidths[$rowindex-1][$colindex-1];
foreach ($blockinstances as $blockinstance) {
$attrs = self::get_attributes($blockinstance, PluginImportLeap::NS_MAHARA);
if (!isset($attrs['blocktype'])) {
$this->trace(" No mahara:blocktype attribute set for blockinstance at row $rowindex col $colindex, order $order: skipping");
continue;
}
$this->trace(" Found block with type {$attrs['blocktype']} at [$rowindex][$colindex][$order]", self::LOG_LEVEL_VERBOSE);
if ($blocktypes_installed === null) {
$blocktypes_installed = array_map(function($a) { return $a->name; }, plugins_installed('blocktype'));
}
$order++;
}
else {
$this->trace(" Ignoring unknown blocktype {$attrs['blocktype']}");
}
if (in_array($attrs['blocktype'], $blocktypes_installed)) {
$configelements = $blockinstance->xpath('mahara:*');
$config['grid'][$rowindex][$colindex]['blocks'][$order] = array(
'type' => $attrs['blocktype'],
'title' => $attrs['blocktitle'],
'row' => $rowindex,
'column' => $colindex,
'order' => $order,
'config' => array()
);
foreach ($configelements as $element) {
$value = json_decode((string)$element);
if (is_array($value) && isset($value[0])) {
$config['grid'][$rowindex][$colindex]['blocks'][$order]['config'][$element->getName()] = $value[0];
}
else {
$this->trace(" Value for {$element->getName()} is not an array, ignoring (value follows below)");
$this->trace($value);
}
}
// If the block has any extra import data (i.e taggedpost tags data), call its import function
$classname = 'LeapImport' . $attrs['blocktype'];
if (class_exists($classname) && method_exists($classname, 'get_blocktype_import_data')) {
call_user_func_array(array($classname, 'get_blocktype_import_data'), array($blockinstance, & $config['grid'][$rowindex][$colindex]['blocks'][$order]['config']));
}
$order++;
}
else {
$this->trace(" Ignoring unknown blocktype {$attrs['blocktype']}");
}
}
$colindex++;
} // cols
$rowindex++;
......@@ -1396,6 +1402,11 @@ class PluginImportLeap extends PluginImport {
$this->trace($value);
}
}
// If the block has any extra import data (i.e taggedpost tags data), call its import function
$classname = 'LeapImport' . $attrs['blocktype'];
if (class_exists($classname) && method_exists($classname, 'get_blocktype_import_data')) {
call_user_func_array(array($classname, 'get_blocktype_import_data'), array($blockinstance, &$block['config']));
}
$config['grid'][] = $block;
}
else {
......
......@@ -3094,6 +3094,7 @@ class View {
$method .= "_$format";
}
$config['rows'][$rowkey]['columns'][$colkey][] = array(
'id' => $bi->get('id'),
'blocktype' => $bi->get('blocktype'),
'title' => $bi->get('title'),
'config' => call_static_method($classname, $method, $bi),
......
{include file="export:leap:entry.tpl" skipfooter=true}
<mahara:view{if $newlayout} mahara:newlayout="1"{/if}{if $layout} mahara:layout="{$layout}"{/if}{if $type} mahara:type="{$type}"{/if} mahara:ownerformat="{$ownerformat}">
{if !$newlayout}
{foreach from=$viewdata item=row}
<mahara:row>
{foreach from=$row['columns'] item=column}
<mahara:column>
{foreach from=$column item=blockinstance}
<mahara:blockinstance mahara:blocktype="{$blockinstance.blocktype}" mahara:blocktitle="{$blockinstance.title}">
{foreach from=$blockinstance.config key=fieldname item=fieldvalue}
{if !$newlayout}
{foreach from=$viewdata item=row}
<mahara:row>
{foreach from=$row['columns'] item=column}
<mahara:column>
{foreach from=$column item=blockinstance}
<mahara:blockinstance mahara:blocktype="{$blockinstance.blocktype}" mahara:blocktitle="{$blockinstance.title}">
{foreach from=$blockinstance.config key=fieldname item=fieldvalue}
{if $fieldname != 'tagsin' && $fieldname != 'tagsout'}
<mahara:{$fieldname}>{$fieldvalue}</mahara:{$fieldname}>
{/if}
{/foreach}
{if $blockinstance.config.tagsin}
{strip}
{foreach from=$blockinstance.config.tagsin key=fieldname item=fieldvalue}
<mahara:tagsin>{$fieldvalue}</mahara:tagsin>
{/foreach}
{/strip}
{/if}
{if $blockinstance.config.tagsout}
{strip}
{foreach from=$blockinstance.config.tagsout key=fieldname item=fieldvalue}
<mahara:tagsout>{$fieldvalue}</mahara:tagsout>
{/foreach}
{/strip}
{/if}
</mahara:blockinstance>
{/foreach}
</mahara:column>
{/foreach}
</mahara:row>
{/foreach}
{else}
{foreach from=$blocks item=bi}
<mahara:blockinstance mahara:blocktype="{$bi.blocktype}" mahara:blocktitle="{$bi.title}" mahara:positionx="{$bi.positionx}" mahara:positiony="{$bi.positiony}" mahara:height="{$bi.height}" mahara:width="{$bi.width}">
{foreach from=$bi.config key=fieldname item=fieldvalue}
{if $fieldname != 'tagsin' && $fieldname != 'tagsout'}
<mahara:{$fieldname}>{$fieldvalue}</mahara:{$fieldname}>
{/foreach}
</mahara:blockinstance>
{/foreach}
</mahara:column>
{/foreach}
</mahara:row>
{/foreach}
{else}
{foreach from=$blocks item=bi}
<mahara:blockinstance mahara:blocktype="{$bi.blocktype}" mahara:blocktitle="{$bi.title}" mahara:positionx="{$bi.positionx}" mahara:positiony="{$bi.positiony}" mahara:height="{$bi.height}" mahara:width="{$bi.width}">
{foreach from=$bi.config key=fieldname item=fieldvalue}
<mahara:{$fieldname}>{$fieldvalue}</mahara:{$fieldname}>
{/foreach}
</mahara:blockinstance>
{/foreach}
{/if}
{/if}
{/foreach}
{if $bi.config.tagsin}
{foreach from=$bi.config.tagsin key=fieldname item=fieldvalue}
<mahara:tagsin>{$fieldvalue}</mahara:tagsin>
{/foreach}
{/if}
{if $bi.config.tagsout}
{foreach from=$bi.config.tagsout key=fieldname item=fieldvalue}
<mahara:tagsout>{$fieldvalue}</mahara:tagsout>
{/foreach}
{/if}
</mahara:blockinstance>
{/foreach}
{/if}
</mahara:view>
{include file="export:leap:entryfooter.tpl"}
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