formsrv.module 7.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
 * Created by IntelliJ IDEA.
 * User: abuddenberg
 * Date: 8/22/13
 * Time: 11:58 AM
 * To change this template use File | Settings | File Templates.
 */

/**
 * Implementation of hook_help().
 */
function formsrv_help($path, $arg) {
    $output = '';
    switch ($path) {
        case "admin/help#formsrv":
            $output = '<p>'.  t("Provides RESTful endpoint for serving out Webform submissions") .'</p>';
            break;
    }
    return $output;
}

/**
 * Implementation of hook_menu().
 */
function formsrv_menu() {
//    $items['metadata/figures/all'] = array(
//        'page callback' => 'formsrv_figure_metadata_json',
//        'type' => MENU_CALLBACK,
//        'access arguments' => array('access content'),
//    );

    $items['metadata/figures'] = array(
        'page callback' => 'formsrv_figure_metadata_json',
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
    );

    $items['metadata/list'] = array(
        'page callback' => 'formsrv_figure_list_json',
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
    );

    return $items;
}

/**
 * Renders a list of figure node IDs with a basic descriptions in JSON.
 */
function formsrv_figure_list_json(){
    $q = <<<END
        #Query to give brief listing and description of figures in Drupal
        select
          trim(figures.chapter_image_nid) as chapter_image_nid,
          max(
              case c.form_key
                when 'what_is_the_figure_id' then trim(d.data) else NULL
              end
          ) as figure_id,
          max(
              case c.form_key
                when 'what_is_the_chapter_and_figure_number' then trim(d.data) else NULL
              end
          ) as figure_num,
          max(
              case c.form_key
                when 'what_is_the_name_of_the_figure_as_listed_in_the_report' then trim(d.data) else NULL
              end
          ) as figure_name
        from {webform_submitted_data} d
          join {webform_component} c on d.nid = c.nid and d.cid = c.cid
          join (
            select
              d.nid,
              d.sid,
              d.data as chapter_image_nid
            from {webform_submitted_data} d
              join {webform_component} c on d.nid = c.nid and d.cid = c.cid
            where d.nid = (select nid from {node} where title = 'Figure Metadata')
             and c.form_key ='nid'
          ) figures on d.nid = figures.nid and d.sid = figures.sid
        where c.form_key in ('what_is_the_figure_id', 'what_is_the_name_of_the_figure_as_listed_in_the_report', 'what_is_the_chapter_and_figure_number')
        group by figures.chapter_image_nid
        order by figures.chapter_image_nid
END;
    $list = array();

    $result = db_query($q);
    while($row = $result->fetch_assoc()){
        $list[] = array(
            'chapter_image_nid' => $row['chapter_image_nid'],
            'figure_id' => $row['figure_id'],
            'figure_num' => $row['figure_num'],
            'figure_name' => $row['figure_name'],
            'url' => url('metadata/figures/'.$row['chapter_image_nid'])
        );
    }
    drupal_set_header('Content-Type: application/json');
    echo json_encode($list);
//    drupal_json($list);
}

/**
 * Creates and populates a tree-like data structure of figures and images with their related datasources.
 * If the node ID of the figure from trh
 * Renders this data structure as JSON.
 */
function formsrv_figure_metadata_json($image_nid_query = 'all'){
//    drupal_json(node_load(1314));

    $map = formsrv_get_chapter_image_map();

    //Filter out figures we don't care about
    if($image_nid_query != 'all'){
        $map = array_intersect_key($map, array($image_nid_query => 1));
    }

    foreach($map as $chapter_image_nid => $figure){
        foreach($figure as $section => $list_ids){
            foreach($list_ids as $index => $ids){
                $form_node_id = $ids['nid'];
                $submission_id = $ids['sid'];

                $map[$chapter_image_nid][$section][$index] = formsrv_get_form_data($form_node_id , $submission_id);

                //So much for a functional design...
                //Grab the path to the figure in Drupal from its node ID
                if($section == 'figure'){
                    $chapter_image = node_load($chapter_image_nid);
                    $map[$chapter_image_nid][$section][$index]['path'] = url('system/files/'.$chapter_image->field_chapter_image_image[0]['filename']);
                }

                //Add datasource section to images; might need to expand this to figures as well
                if($section == 'images'){
                    $map[$chapter_image_nid][$section][$index]['datasources'] = formsrv_get_datasources_for_image($submission_id);
                }
            }
        }
    }

    drupal_set_header('Content-Type: application/json');
    echo json_encode($map);
//    drupal_json($map);
}


/**
 * Create a basic tree-like structure of figures and images with their identifiers.
 * We'll replace the identifiers with actual data from the database in another function.
 */
function formsrv_get_chapter_image_map(){
    $q = <<<END
        select
          n.nid as form_nid,
          n.title as section,
          d.sid as form_submission_id,
          trim(d.data) as chapter_image_nid
        from (
            select
                nid,
                case title
                    when 'Image Metadata' then 'images'
                    when 'Figure Metadata' then 'figure'
                    end as title
            from {node}
            where title in ('Image Metadata', 'Figure Metadata')
            ) n
          join {webform_submitted_data} d on n.nid = d.nid
          join {webform_component} c on n.nid = c.nid and d.cid = c.cid
        where c.form_key = 'nid'
        order by chapter_image_nid, d.sid
END;

    $map = array();

    $result = db_query($q);
    while($row = $result->fetch_assoc()){
        $map[$row['chapter_image_nid']][$row['section']][] = array('nid' => $row['form_nid'], 'sid' => $row['form_submission_id']);
    }

    return $map;
}


/**
 * Returns a populated list of datasources for the given image.
 */
function formsrv_get_datasources_for_image($image_sid){
    $q = <<<END
        #Query to get datasource information for an image
        select
          d.nid,
          d.sid
        from webform_submitted_data d
          join webform_component c on d.nid = c.nid and d.cid = c.cid
        where d.nid = (select nid from node where title = 'Data Source Metadata')
          and c.form_key = 'image_metadata_sid'
          and d.data = %d;
END;

    $datasources = array();

    $result = db_query($q, $image_sid);
    while($row = $result->fetch_assoc()){
        $datasources[] = formsrv_get_form_data($row['nid'], $row['sid']);
    }

    return $datasources;
}


/**
 * Returns a map of field-value pairs of webform submission data.
 * Parameters:
 * $form_node_id: the node ID of the webform
 * $submission_id: the submission ID within the given webform to get
 */
function formsrv_get_form_data($form_node_id, $submission_id) {
    $q = <<<END
        select
#          d.sid,
          trim(c.form_key) as k,
#          c.name,
          trim(d.data) as v
        from {webform_submitted_data} d
          join {webform_component} c on d.nid = c.nid and d.cid = c.cid
        where d.nid = %d
          and d.sid = %d
          order by c.pid, c.weight
END;

    $form_data = array();

    $result = db_query($q, $form_node_id, $submission_id);
    while($row = $result->fetch_assoc()){
        $form_data[$row['k']] = $row['v'];
    }

    return $form_data;

}