Skip to content

Commit

Permalink
Added the ability to add trackfields by trackfield code over multiple…
Browse files Browse the repository at this point in the history
… tracks
  • Loading branch information
jvangestel committed Dec 14, 2018
1 parent 04e974b commit b4a800f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
23 changes: 23 additions & 0 deletions classes/Gems/Export/ModelSource/AnswerExportModelSource.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected function _addExtraDataToExportModel(\MUtil_Model_ModelAbstract $model,
$this->_addExtraGenderAge($model, $data, $prefixes); $this->_addExtraGenderAge($model, $data, $prefixes);
$this->_addExtraTokenReceptionCode($model, $data, $prefixes); $this->_addExtraTokenReceptionCode($model, $data, $prefixes);
$this->_addExtraTrackReceptionCode($model, $data, $prefixes); $this->_addExtraTrackReceptionCode($model, $data, $prefixes);
$this->addExtraTrackFieldsByCode($model, $data, $prefixes);
} }


/** /**
Expand Down Expand Up @@ -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 * @param \MUtil_Model_ModelAbstract $model
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ protected function getAutoSearchElements(array $data)


$elements[] = null; $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[] = $this->_('Output');
$elements['incomplete'] = $this->_createCheckboxElement( $elements['incomplete'] = $this->_createCheckboxElement(
'incomplete', 'incomplete',
Expand Down
72 changes: 72 additions & 0 deletions classes/Gems/Tracker/Model/AddTrackFieldsByCodeTransformer.php
Original file line number Original file line Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.