Permalink
Browse files

Added the ability to add trackfields by trackfield code over multiple…

… tracks
  • Loading branch information...
jvangestel committed Dec 14, 2018
1 parent 04e974b commit b4a800fd4b1d19a5ae23aa1f2912527d16b94d0d
@@ -74,6 +74,7 @@ protected function _addExtraDataToExportModel(\MUtil_Model_ModelAbstract $model,
$this->_addExtraGenderAge($model, $data, $prefixes);
$this->_addExtraTokenReceptionCode($model, $data, $prefixes);
$this->_addExtraTrackReceptionCode($model, $data, $prefixes);
$this->addExtraTrackFieldsByCode($model, $data, $prefixes);
}
/**
@@ -176,6 +177,28 @@ protected function _addExtraTrackFields(\MUtil_Model_ModelAbstract $model, array
}
}
/**
* @param MUtil_Model_ModelAbstract $model
* @param array $data
* @param array $prefixes
*/
protected function addExtraTrackFieldsByCode(\MUtil_Model_ModelAbstract $model, array $data, array &$prefixes)
{
if (isset($data['export_trackfield_codes']) && $data['export_trackfield_codes']) {
$includeCodes = array_map('trim', explode(',', $data['export_trackfield_codes']));
if (!empty($includeCodes)) {
foreach ($includeCodes as $name) {
$model->set($name, 'label', $name);
}
$tracker = $this->loader->getTracker();
$transformer = new \Gems\Tracker\Model\AddTrackFieldsByCodeTransformer($tracker, $includeCodes);
$model->addTransformer($transformer);
}
}
}
/**
*
* @param \MUtil_Model_ModelAbstract $model
@@ -87,6 +87,16 @@ protected function getAutoSearchElements(array $data)
$elements[] = null;
$elements['export_trackfield_codes'] = $this->form->createElement('text', 'export_trackfield_codes',
[
'size' => 50,
'label' => 'Export trackfield codes',
'description' => 'Export specific trackfield codes. This works without selecting a track. The columnname will be the code. Use , as a separator between codes.'
]
);
$elements[] = null;
$elements[] = $this->_('Output');
$elements['incomplete'] = $this->_createCheckboxElement(
'incomplete',
@@ -0,0 +1,72 @@
<?php
namespace Gems\Tracker\Model;
class AddTrackFieldsByCodeTransformer extends \MUtil_Model_ModelTransformerAbstract
{
protected $includeCodes;
/**
* @var \Gems_Tracker_TrackerInterface
*/
protected $tracker;
protected $trackFieldsByRespondentTrack;
public function __construct(\Gems_Tracker_TrackerInterface $tracker, array $includeCodes)
{
$this->includeCodes = $includeCodes;
$this->tracker = $tracker;
}
/**
* The transform function performs the actual transformation of the data and is called after
* the loading of the data in the source model.
*
* @param \MUtil_Model_ModelAbstract $model The parent model
* @param array $data Nested array
* @param boolean $new True when loading a new item
* @param boolean $isPostData With post data, unselected multiOptions values are not set so should be added
* @return array Nested array containing (optionally) transformed data
*/
public function transformLoad(\MUtil_Model_ModelAbstract $model, array $data, $new = false, $isPostData = false)
{
foreach($data as $tokenId=>$row) {
if (isset($row['gto_id_respondent_track'])) {
if (!isset($this->trackFieldsByRespondentTrack[$row['gto_id_respondent_track']])) {
$trackData = array_filter($row, function($key) {
return strpos($key, 'gtr_') === 0;
}, ARRAY_FILTER_USE_KEY);
$this->trackFieldsByRespondentTrack[$row['gto_id_respondent_track']] = $this->getTrackFields($trackData, $row['gto_id_respondent_track']);
}
$newData = $this->trackFieldsByRespondentTrack[$row['gto_id_respondent_track']];
$data[$tokenId] = array_merge($data[$tokenId], $newData);
}
}
return $data;
}
protected function getTrackfields(array $trackData, $respondentTrackId)
{
$engine = $this->tracker->getTrackEngine($trackData);
$fieldCodes = $engine->getFieldCodes();
$filteredFieldCodes = array_intersect($fieldCodes, $this->includeCodes);
if (empty($filteredFieldCodes)) {
return [];
}
$fieldData = $engine->getFieldsData($respondentTrackId);
$filteredFieldData = [];
foreach($filteredFieldCodes as $fieldId=>$fieldCode) {
$filteredFieldData[$fieldCode] = $fieldData[$fieldId];
}
return $filteredFieldData;
}
}

1 comment on commit b4a800f

@mennodekker

This comment has been minimized.

Copy link
Member

mennodekker commented on b4a800f Dec 14, 2018

Nice new feature! Please don't forget to create an issue for this so we can link it to the next release and don't forget it for testing and documentation updates.

Please sign in to comment.