Skip to content
Permalink
Browse files

Fixed #456 Now actually with the survey codebook model

  • Loading branch information...
jvangestel committed Jul 9, 2019
1 parent 876be69 commit a20496f077c200a13e812dab45851ff027ddf93d
Showing with 115 additions and 0 deletions.
  1. +11 −0 classes/Gems/Model.php
  2. +104 −0 classes/Gems/Model/SurveyCodeBookModel.php
@@ -379,6 +379,17 @@ public function getSurveyMaintenanceModel()
return $this->_loadClass('SurveyMaintenanceModel', true);
}
/**
* Get the survey codebook Model
*
* @param $surveyId
* @return \Gems\Model\SurveyCodeBookModel
*/
public function getSurveyCodeBookModel($surveyId)
{
return $this->_loadClass('SurveyCodeBookModel', true, [$surveyId]);
}
/**
* Set a field in this model as a gems unique user id
*
@@ -0,0 +1,104 @@
<?php
namespace Gems\Model;
use MUtil\Translate\TranslateableTrait;
class SurveyCodeBookModel extends \Gems_Model_PlaceholderModel
{
use TranslateableTrait;
protected $data;
/**
* @var array List of field columns
*/
protected $fieldArray;
/**
* @var \Gems_Loader
*/
public $loader;
/**
*
* @var \Zend_Locale
*/
public $locale;
protected $surveyId;
/**
* @var \Gems_Tracker
*/
public $tracker;
public function __construct($surveyId)
{
$this->surveyId = $surveyId;
}
public function afterRegistry()
{
$this->tracker = $this->loader->getTracker();
$this->data = $this->getData($this->surveyId);
$survey = $this->tracker->getSurvey($this->surveyId);
$name = $this->cleanupName($survey->getName()) . '-code-book';
parent::__construct($name, $this->fieldArray, $this->data);
$this->set('id', 'label', $this->_('Survey ID'));
$this->set('title', 'label', $this->_('Question code'));
$this->set('question', 'label', $this->_('Question'));
$this->set('answers', 'label', $this->_('Answers'));
$this->set('answer_codes', 'label', $this->_('Answer codes'));
parent::afterRegistry();
}
/**
* Clean a proposed filename up so it can be used correctly as a filename
* @param string $filename Proposed filename
* @return string filtered filename
*/
protected function cleanupName($filename)
{
$filename = str_replace(array('/', '\\', ':', ' '), '_', $filename);
// Remove dot if it starts with one
$filename = trim($filename, '.');
return \MUtil_File::cleanupName($filename);
}
public function getData($surveyId)
{
$survey = $this->tracker->getSurvey($surveyId);
$questionInformation = $survey->getQuestionInformation($this->locale->getLanguage());
$firstItem = reset($questionInformation);
$this->fieldArray = array_keys($firstItem);
$data = [];
foreach($questionInformation as $questionTitle=>$information) {
$data[$questionTitle]['id'] = $this->surveyId;
$data[$questionTitle]['title'] = $questionTitle;
$data[$questionTitle]['question'] = $information['question'];
$data[$questionTitle]['answers'] = null;
$data[$questionTitle]['answer_codes'] = null;
if (is_array($information['answers'])) {
$firstInfo = reset($information['answers']);
if (count($information['answers']) === 1 && empty($firstInfo)) {
continue;
}
$answerIds = array_keys($information['answers']);
$data[$questionTitle]['answers'] = join('|', $information['answers']);
$data[$questionTitle]['answer_codes'] = join('|', $answerIds);
}
}
return $data;
}
}

0 comments on commit a20496f

Please sign in to comment.
You can’t perform that action at this time.