~intrahealth+informatics/ihris-common/4.3-dev

« back to all changes in this revision

Viewing changes to modules/FHIR/modules/Questionnaire/lib/I2CE_FHIR_Questionnaire.php

  • Committer: Carl Leitner
  • Date: 2016-07-09 16:29:01 UTC
  • mto: This revision was merged to the branch mainline in revision 1454.
  • Revision ID: litlfred@ibiblio.org-20160709162901-bfr3cv3vseb7dfw4
added fhir questionnaire -> form code.   added base fhir helper classes:

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
* © Copyright 2016 IntraHealth International, Inc.
 
4
 
5
* This File is part of I2CE 
 
6
 
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.
 
11
 
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.
 
16
 
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
 
20
* @subpackage FHIR
 
21
* @author Carl Leitner <litlfred@ibiblio.org>
 
22
* @version v4.2.1
 
23
* @since v4.2.1
 
24
* @filesource 
 
25
*/ 
 
26
/** 
 
27
* Class I2CE_Questionnaire
 
28
 
29
* @access public
 
30
*/
 
31
 
 
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;
 
40
 
 
41
class I2CE_FHIR_Questionnaire extends I2CE_FHIR_Base {
 
42
 
 
43
    /* ValueSet handler
 
44
     * @var I2CE_FHIR_ValueSet $valueset_handler;
 
45
     */
 
46
    public $valueset_handlder;
 
47
 
 
48
 
 
49
    public function __construct() {
 
50
        parent::__construct();
 
51
        $this->valueset_handler =new I2CE_FHIR_ValueSet();
 
52
    }
 
53
 
 
54
 
 
55
 
 
56
 
 
57
    /**
 
58
     * Loads a questionniare resource from a content string     
 
59
     *
 
60
     * @param string $conent the contnet of the resource.
 
61
     * @return \FHIR_DSTU_TWO\FHIRQuestionnaire on success
 
62
     */
 
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");
 
67
        }
 
68
        return $this->resource;
 
69
    }
 
70
 
 
71
    /**
 
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.
 
75
     */
 
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)
 
80
            ) {
 
81
            throw new Exception("Invalid questionnaire");
 
82
        }
 
83
        $form_classes = array();
 
84
        $forms = array();
 
85
        $group_queue = array(array($group,$parent_form));
 
86
        $parent_forms = array();
 
87
        $lists = 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)
 
95
                ) {
 
96
                continue;
 
97
            }
 
98
            $fields = array();
 
99
            $formid = $id . '.' . $g_linkId;
 
100
            $form_class = 'Questionnaire-' . $formid;
 
101
            $form_name = 'questionnaire-' . $formid;
 
102
            
 
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)
 
111
                    ) {
 
112
                    continue;
 
113
                }
 
114
                $f_name = $id . '.' . $g_linkId . '.' . $q_linkId;
 
115
                $formfield = false;
 
116
                switch($type) {
 
117
                case 'string':
 
118
                    $formfield = 'STRING_LINE';
 
119
                    break;
 
120
                case 'integer':
 
121
                    $formfield = 'INT';
 
122
                    break;
 
123
                case 'date':
 
124
                    $formfield = 'DATE_TIME';
 
125
                    break;
 
126
                case 'decimal':
 
127
                    $formfield = 'FLOAT';
 
128
                    break;
 
129
                case 'choice':
 
130
                    $formfield = 'MAP';                    
 
131
                    $values = array();
 
132
                    try {
 
133
                        if (! ($reference = $question->options) instanceof FHIRReference
 
134
                            || ! ($valueset = $this->get_referenced_resource($reference)) instanceof FHIRValueSet
 
135
                            ) {
 
136
                            break;
 
137
                        }
 
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);
 
142
                    } 
 
143
                    $forms[$f_name] =
 
144
                        array(
 
145
                            'class'=>'I2CE_SimpleList',
 
146
                            'display'=> $f_title,
 
147
                            'storage'=>'magicadata' 
 
148
                            );
 
149
                    $lists[$f_name] = $values;
 
150
                    break;
 
151
 
 
152
                }
 
153
                if (!$formfield) { continue;}
 
154
 
 
155
                $fields[$f_name] = 
 
156
                    array(
 
157
                        'formfield'=>$formfield,
 
158
                        'headers'=>array('default'=>$f_title),                    
 
159
                        );
 
160
            }
 
161
 
 
162
            $form_classes[$form_class] = 
 
163
                array(
 
164
                    'extends'=>'I2CE_Form',
 
165
                    'fields'=>$fields
 
166
                    );
 
167
            $forms[$form_name] = 
 
168
                array(
 
169
                    'class'=>'Questionnaire-' . $id,
 
170
                    'display'=>$title,                
 
171
                    );
 
172
            if ($p_form) {
 
173
                if (!array_key_exists($form_name,$parent_forms)) {
 
174
                    $parent_forms[$form_name] = array();
 
175
                }
 
176
                $parent_forms[$form_name][] = $p_form;
 
177
            }
 
178
            if ($group->group instanceof  FHIRQuestionnaireGroup) {
 
179
                foreach ($group->group as $c_group) {
 
180
                    $group_queue[] = array($c_group,$form_name);
 
181
                }
 
182
            }
 
183
 
 
184
        }
 
185
 
 
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())
 
190
                ) {
 
191
                continue;
 
192
            }
 
193
            if (!in_array($p,$forms)) {
 
194
                $forms[$p] = array();
 
195
            }
 
196
            if (!array_key_exists('meta',$forms[$p])) {
 
197
                $forms[$p]['meta'] = array();
 
198
            }
 
199
            if (!array_key_exists('child_forms',$forms[$p]['meta'])) {
 
200
                $forms[$p]['meta']['child_forms'] = array();
 
201
            }                  
 
202
            $forms[$p]['meta']['child_forms'][] = $c;
 
203
        }
 
204
        $magicdata = array(
 
205
            'I2CE'=>array(
 
206
                'formsData'=>array(
 
207
                    'forms' => $lists
 
208
                    )
 
209
                ),
 
210
            'modules'=>array(                
 
211
                'forms'=>array(
 
212
                    'formClasses' => $form_classes,
 
213
                    'forms' => $forms          
 
214
                    )
 
215
                )
 
216
            );
 
217
        I2CE::raiseMessage("Creating form from:\n" . print_r($magicdata,true));
 
218
        
 
219
        
 
220
        
 
221
    }
 
222
 
 
223
 
 
224
}
 
225
# Local Variables:
 
226
# mode: php
 
227
# c-default-style: "bsd"
 
228
# indent-tabs-mode: nil
 
229
# c-basic-offset: 4
 
230
# End: