Commit 14cc4342 authored by abuddenberg's avatar abuddenberg
Browse files

formsrv Drupal module modified to serve Angels survey from resources 1.5 system

parent c51040ae
......@@ -51,48 +51,27 @@ function formsrv_menu() {
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_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')
group by figures.chapter_image_nid
order by figures.chapter_image_nid
SELECT
n.nid,
n.title,
ci.field_metadata_t1_value as t1
FROM {node} n
JOIN {content_type_chapter_image} ci ON n.nid = ci.nid AND n.vid = ci.vid
WHERE n.type = 'chapter_image'
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_name' => $row['figure_name'],
'url' => url('metadata/figures/'.$row['chapter_image_nid'])
'nid' => $row['nid'],
'node_title' => $row['title'],
'figure_name' => json_decode($row['t1'])->{'figure'}->{'graphics_title'},
'url' => url('metadata/figures/'.$row['nid'])
);
}
drupal_set_header('Content-Type: application/json');
echo json_encode($list);
// drupal_json($list);
}
/**
......@@ -100,137 +79,31 @@ END;
* 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(){
function formsrv_2_figure_metadata_json($image_nid_query = ''){
$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
SELECT
n.nid,
ci.field_metadata_t1_value as t1,
ci.field_metadata_t2_value as t2,
f.filepath
FROM {node} n
JOIN {content_type_chapter_image} ci ON n.nid = ci.nid AND n.vid = ci.vid
JOIN {files} f ON f.fid = ci.field_chapter_image_image_fid
WHERE n.nid = %d;
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();
$list = array();
$result = db_query($q, $form_node_id, $submission_id);
$result = db_query($q, $image_nid_query);
while($row = $result->fetch_assoc()){
$form_data[$row['k']] = $row['v'];
$list[] = array(
'nid' => $row['nid'],
't1' => json_decode($row['t1']),
't2' => json_decode($row['t2']),
'filepath' => $row['filepath']
);
}
return $form_data;
}
drupal_set_header('Content-Type: application/json');
echo json_encode($list);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment