formGenerator.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. function fgInsertRow(schemaName,queryName,row,cb=null,containerPath=null){
  2. let fName='[fgInsertRow]';
  3. print(fName);
  4. //cb=function(data){....}
  5. let qconfig=new Object();
  6. if (containerPath)
  7. qconfig.containerPath=containerPath;
  8. qconfig.schemaName=schemaName;
  9. qconfig.queryName=queryName;
  10. qconfig.success=cb;
  11. qconfig.rows=[row];
  12. print(fName+' qconfig '+qconfig);
  13. LABKEY.Query.insertRows(qconfig);
  14. }
  15. function addInputRow(table,header,type){
  16. let fName='[addInputRow]';
  17. print(fName);
  18. let row=table.insertRow();
  19. let cell=config.document.createElement('th');
  20. let text=config.document.createTextNode(header);
  21. cell.appendChild(text);
  22. row.appendChild(cell);
  23. let input=null;
  24. if (type=="select")
  25. input=config.document.createElement(type);
  26. if (type=="button"){
  27. input=config.document.createElement("input");
  28. input.type="button";
  29. }
  30. if (type=="text"){
  31. input=config.document.createElement('textarea');
  32. input.cols="65";
  33. input.rows="5";
  34. }
  35. if (type=="label")
  36. input=config.document.createElement(type);
  37. let cell1=row.insertCell();
  38. cell1.appendChild(input);
  39. return input;
  40. }
  41. function getFormName(formId){
  42. let rows=config.formConfig.dataForms.rows;
  43. for (let i=0;i<rows.length;i++){
  44. if (rows[i]['Key']==formId){
  45. return rows[i]['formName'];
  46. }
  47. }
  48. return "NONE";
  49. }
  50. function getQueryName(queryId){
  51. let rows=config.formConfig.inputLists.rows;
  52. for (let i=0;i<rows.length;i++){
  53. if (rows[i]['Key']==queryId){
  54. return rows[i]['queryName'];
  55. }
  56. }
  57. return "NONE";
  58. }
  59. function getGCRow(formId){
  60. let formRows=config.formConfig.generateConfigData.rows;
  61. for (let i=0;i<formRows.length;i++){
  62. if (formRows[i]['formId']==formId){
  63. return formRows[i];
  64. }
  65. }
  66. return Object();
  67. }
  68. function getCrfSelectRow(crfRef){
  69. let rows=config.formConfig.crfSelectRows;
  70. for (let i=0;i<rows.length;i++){
  71. if (rows[i]['crfRef']==crfRef)
  72. return rows[i];
  73. }
  74. return Object();
  75. }
  76. function addOption(input,name,value){
  77. let opt=config.document.createElement("option");
  78. opt.text=name;
  79. opt.value=value;
  80. input.options[input.options.length]=opt;
  81. }
  82. function clearOptions(input){
  83. for(let i = input.options.length; i >= 0; i--) {
  84. input.remove(i);
  85. }
  86. }
  87. function addFormGenerator(){
  88. let fName='[addFormGenerator]';
  89. print(fName);
  90. //layout
  91. let table=config.document.createElement("table");
  92. table.className="t2";
  93. config.document.getElementById(config.div).appendChild(table);
  94. let formGenerator=new Object();
  95. formGenerator.formSelect=addInputRow(table,'Select form',"select");
  96. formGenerator.crfSelect=addInputRow(table,'Select CRF',"select");
  97. formGenerator.comment=addInputRow(table,'Enter comment','text');
  98. formGenerator.details=addInputRow(table,'Details','label');
  99. formGenerator.warnings=addInputRow(table,'Warnings','label');
  100. formGenerator.warnings.innerHTML='None';
  101. addOption(formGenerator.formSelect,'<Select>',-1);
  102. let formRows=config.formConfig.generateConfigData.rows;
  103. for (let i=0;i<formRows.length;i++){
  104. let formId=formRows[i]["formId"];
  105. let formName=getFormName(formId);
  106. print(fName+' '+formRows[i]["formId"]+'/'+formName);
  107. addOption(formGenerator.formSelect,formName,formId);
  108. }
  109. formGenerator.formSelect.onchange=function(){updateIdList(formGenerator);};
  110. formGenerator.crfSelect.onchange=function(){updateLabel(formGenerator);};
  111. formGenerator.generateButton=addInputRow(table,'Generate Form','button');
  112. formGenerator.generateButton.value="Generate Form";
  113. formGenerator.generateButton.onclick=function(){createFormWithId(formGenerator);};
  114. }
  115. function createFormWithId(formGenerator){
  116. //get form id and entry id from select and create form as above
  117. let fName='[createFormWithId]';
  118. print(fName);
  119. let formId=formGenerator.formSelect.options[formGenerator.formSelect.selectedIndex].value;
  120. let crfRef=formGenerator.crfSelect.options[formGenerator.crfSelect.selectedIndex].text;
  121. let configRow=getGCRow(formId);
  122. let crfSelectRow=getCrfSelectRow(crfRef);
  123. let formConfig=config.formConfig;
  124. print("Create form w/id "+formId);
  125. let crfEntry=new Object();
  126. crfEntry.entryId=Date.now();
  127. crfEntry["Date"]=new Date();
  128. crfEntry["View"]="[VIEW]";
  129. crfEntry['participantStudyId']=crfSelectRow['participantStudyId'];
  130. crfEntry['participantLocalId']=crfSelectRow['participantLocalId'];
  131. crfEntry.formStatus=configRow['formStatus'];//In progress
  132. //set other variables
  133. //requires studyData as part of formConfig
  134. let studyData=formConfig.studyData.rows[0];
  135. let varRows=formConfig['crfStaticVariables'].rows;
  136. for (let i=0;i<varRows.length;i++){
  137. let varName=varRows[i].staticVariable;
  138. crfEntry[varName]=studyData[varName];
  139. }
  140. crfEntry.UserId=LABKEY.Security.currentUser.id;
  141. crfEntry.Site=config.formConfig.currentSites[0].siteNumber;
  142. print("Setting site to id="+crfEntry.Site);
  143. //from argument list
  144. crfEntry.Form=formId;
  145. crfEntry.parentCrf=crfRef;
  146. //
  147. //compose a reviewComments entry
  148. let reviewComment=new Object();
  149. reviewComment['submissionDate']=crfEntry['Date'];
  150. reviewComment['crfRef']=crfRef;
  151. //comment length
  152. let x=formGenerator.comment.value;
  153. print(fName+' comment length '+x.length);
  154. if (x.length==0){
  155. formGenerator.warnings.innerHTML='Supply a comment';
  156. return;
  157. }
  158. reviewComment['reviewComment']=formGenerator.comment.value;
  159. reviewComment['queryName']=configRow['queryId'];
  160. let rd=function(data){redirect();};
  161. let pass=function(data){fgInsertRow('lists','reviewComments',reviewComment,rd,getContainer('data'));}
  162. fgInsertRow('lists','crfEntry',crfEntry,pass,getContainer('data'));
  163. }
  164. function updateIdList(formGenerator){
  165. let fName='[updateIdList]';
  166. let formId=formGenerator.formSelect.options[formGenerator.formSelect.selectedIndex].value;
  167. print(fName+' id '+formId);
  168. //remove old options
  169. clearOptions(formGenerator.crfSelect);
  170. print(fName+' options cleared');
  171. //get query associated with form
  172. let configRow=getGCRow(formId);
  173. let queryId=configRow['queryId'];
  174. print(fName+' queryId '+queryId);
  175. if (!queryId || queryId<0)
  176. return;
  177. let qselect=new Object();
  178. qselect.containerPath=getContainer('data');
  179. qselect.schemaName='lists';
  180. qselect.queryName=getQueryName(queryId);
  181. qselect.success=function(data){updateIdListWithData(formGenerator,data);};
  182. LABKEY.Query.selectRows(qselect);
  183. }
  184. function updateIdListWithData(formGenerator,data){
  185. let rows=data.rows;
  186. config.formConfig.crfSelectRows=data.rows;
  187. for (let i=0;i<rows.length;i++){
  188. addOption(formGenerator.crfSelect,rows[i]['crfRef'],i);
  189. }
  190. let event=new Event('change');
  191. formGenerator.crfSelect.dispatchEvent(event);
  192. }
  193. function updateLabel(formGenerator){
  194. let crfRef=formGenerator.crfSelect.options[formGenerator.crfSelect.selectedIndex].text;
  195. let crfSelectRow=getCrfSelectRow(crfRef);
  196. formGenerator.details.innerHTML='Generating for Study:'+crfSelectRow['participantStudyId']+' / Local:'+crfSelectRow['participantLocalId'];
  197. }
  198. function redirect(){
  199. let debug=false;
  200. let formUrl="begin";
  201. let params=new Object();
  202. params.name=formUrl;
  203. params.pageId="CRF";
  204. //points to crf container
  205. let containerPath=getContainer('data');
  206. // This changes the page after building the URL.
  207. //Note that the wiki page destination name is set in params.
  208. var homeURL = LABKEY.ActionURL.buildURL(
  209. "project", formUrl , containerPath, params);
  210. print("Redirecting to "+homeURL);
  211. if (debug) return;
  212. window.location = homeURL;
  213. }