3
* © Copyright 2016 IntraHealth International, Inc.
5
* This File is part of I2CE
7
* I2CE is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
* @package ihris-common
21
* @author Carl Leitner <litlfred@ibiblio.org>
27
* Class I2CE_Questionnaire
32
use FHIR_DSTU_TWO\FHIRDomainResource\FHIRQuestionnaire as FHIRQuestionnaire;
33
use FHIR_DSTU_TWO\FHIRResource\FHIRQuestionnaire\FHIRQuestionnaireGroup as FHIRQuestionnaireGroup;
34
use FHIR_DSTU_TWO\FHIRResource\FHIRQuestionnaire\FHIRQuestionnaireQuestion as FHIRQuestionnaireQuestion;
35
use FHIR_DSTU_TWO\PHPFHIRResponseParser as PHPFHIRResponseParser;
36
use FHIR_DSTU_TWO\FHIRElement\FHIRReference as FHIRReference;
37
use FHIR_DSTU_TWO\FHIRElement\FHIRString as FHIRString;
38
use FHIR_DSTU_TWO\FHIRResource\FHIRValueSet as FHIRValueSet;
39
use FHIR_DSTU_TWO\FHIRElement\FHIRAnswerFormat as FHIRAnswerFormat;
41
class I2CE_FHIR_Questionnaire extends I2CE_FHIR_Base {
44
* @var I2CE_FHIR_ValueSet $valueset_handler;
46
public $valueset_handlder;
49
public function __construct() {
50
parent::__construct();
51
$this->valueset_handler =new I2CE_FHIR_ValueSet();
58
* Loads a questionniare resource from a content string
60
* @param string $conent the contnet of the resource.
61
* @return \FHIR_DSTU_TWO\FHIRQuestionnaire on success
63
public function load_resource_content($content) {
64
parent::load_resource_content($content);
65
if (! $this->resource instanceof FHIRQuestionnaire) {
66
throw new Exception("Did not get a DSTU2 Questionnaire resource");
68
return $this->resource;
72
* walk through the questionnaire and create form name with the given name
73
* You should probabl call load_resource() before calling this method.
74
* @param string $parent_form the parent form (if any) we want to attach the questionnaire form to. Defaults to false.
76
public function create_form($parent_form = '') {
77
if (! $this->resource instanceof FHIRQuestionnaire
78
|| (! ($id = $this->resource->id))
79
|| (! ($group = $this->resource->group) instanceof FHIRQuestionnaireGroup)
81
throw new Exception("Invalid questionnaire");
83
$form_classes = array();
85
$group_queue = array(array($group,$parent_form));
86
$parent_forms = array();
88
while (count($group_queue) > 0) {
89
list($group,$p_form) = array_shift($group_queue);
90
if (! $group instanceof FHIRQuestionnaireGroup
91
|| ! ($group->linkId instanceof FHIRString)
92
|| ! ($g_linkId = $group->linkId->value)
93
|| ! ( $group->title instanceof FHIRString)
94
|| ! ( $title = $group->title->value)
99
$formid = $id . '.' . $g_linkId;
100
$form_class = 'Questionnaire-' . $formid;
101
$form_name = 'questionnaire-' . $formid;
103
foreach ($group->question as $question) {
104
if (! $question instanceof FHIRQuestionnaireQuestion
105
|| !( $question->linkId instanceof FHIRString)
106
|| !( $q_linkId = $question->linkId->value)
107
|| !( $question->type instanceof FHIRAnswerFormat)
108
|| !( $type = $question->type->value)
109
|| !( $question->text instanceof FHIRString)
110
|| !( $f_title = $question->text->value)
114
$f_name = $id . '.' . $g_linkId . '.' . $q_linkId;
118
$formfield = 'STRING_LINE';
124
$formfield = 'DATE_TIME';
127
$formfield = 'FLOAT';
133
if (! ($reference = $question->options) instanceof FHIRReference
134
|| ! ($valueset = $this->get_referenced_resource($reference)) instanceof FHIRValueSet
138
$this->valueset_handler->resource = $valueset;
139
$values = $this->valueset_handler->get_simple_list();
140
} catch(Exception $e) {
141
I2CE::raiseMessage("could not get valueset values from " . $reference);
145
'class'=>'I2CE_SimpleList',
146
'display'=> $f_title,
147
'storage'=>'magicadata'
149
$lists[$f_name] = $values;
153
if (!$formfield) { continue;}
157
'formfield'=>$formfield,
158
'headers'=>array('default'=>$f_title),
162
$form_classes[$form_class] =
164
'extends'=>'I2CE_Form',
169
'class'=>'Questionnaire-' . $id,
173
if (!array_key_exists($form_name,$parent_forms)) {
174
$parent_forms[$form_name] = array();
176
$parent_forms[$form_name][] = $p_form;
178
if ($group->group instanceof FHIRQuestionnaireGroup) {
179
foreach ($group->group as $c_group) {
180
$group_queue[] = array($c_group,$form_name);
186
$ff =I2CE_FormFactory::instance();
187
foreach ($parent_forms as $p=>$c) {
188
if ( ($pObj = $ff->createContainer($p)) instanceof I2CE_Form
189
&& in_array($c,$pObj->getChildForms())
193
if (!in_array($p,$forms)) {
194
$forms[$p] = array();
196
if (!array_key_exists('meta',$forms[$p])) {
197
$forms[$p]['meta'] = array();
199
if (!array_key_exists('child_forms',$forms[$p]['meta'])) {
200
$forms[$p]['meta']['child_forms'] = array();
202
$forms[$p]['meta']['child_forms'][] = $c;
212
'formClasses' => $form_classes,
217
I2CE::raiseMessage("Creating form from:\n" . print_r($magicdata,true));
227
# c-default-style: "bsd"
228
# indent-tabs-mode: nil