participantPortal.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. var participantPortal={};
  2. participantPortal.print=function(msg){
  3. console.log(msg);
  4. }
  5. participantPortal.idField='participantStudyId';
  6. participantPortal.localIdField='participantLocalId';
  7. participantPortal.init=
  8. function(cb=null){
  9. let that=this;
  10. let action=function(){that.scriptsLoaded(cb);};
  11. LABKEY.Utils.requiresScript(["crfDORA/crfSetup.js","crfDORA/crfData.js","crfDORA/crfHTML.js","crfDORA/crfRoleSelector.js","crfDORA/formPortalNew.js"],action);
  12. }
  13. participantPortal.scriptsLoaded=
  14. function(cb=null){
  15. //if other script need init, just stack the init scripts
  16. //let action=function(){runQuery.init(cb);}
  17. crfData.setSetup(crfSetup);
  18. crfRoleSelector.set(crfSetup);
  19. crfHTML.init();
  20. let initFormPortal=function(){formPortal.init(cb);};
  21. let initData=function(){crfData.init(initFormPortal);};
  22. crfSetup.init(initData);
  23. }
  24. participantPortal.getParticipantMap=
  25. function(){
  26. if (!("participantMap" in this)){
  27. this.participantMap=new Object();
  28. this.sortByParticipantId();
  29. }
  30. return this.participantMap;
  31. }
  32. participantPortal.clearKeyMap=
  33. function(){
  34. if (!("participantKeyMap" in this)){
  35. this.participantKeyMap=new Object();
  36. return;
  37. }
  38. Object.keys(this.participantKeyMap).forEach(key => delete this.participantKeyMap[key]);
  39. }
  40. participantPortal.addToKeyMap=
  41. function(key,value){
  42. this.participantKeyMap[value]=key;
  43. }
  44. participantPortal.getFromKeyMap=
  45. function(value){
  46. return this.participantKeyMap[value];
  47. }
  48. participantPortal.getParticipantArray=
  49. function(id,formId){
  50. let fName='[getParticipantArray/'+id+','+formId+']';
  51. //this.print(fName);
  52. let pMap=this.getParticipantMap();
  53. if (!(id in pMap))
  54. pMap[id]=new Object();
  55. if (!(formId in pMap[id]))
  56. pMap[id][formId]=new Array();
  57. return pMap[id][formId];
  58. }
  59. participantPortal.generateFormArray=
  60. function(){
  61. let fName='[generateFormArray]';
  62. this.print(fName);
  63. //gang callbacks (last to first)
  64. let that=this;
  65. let makePortal=function(){that.makePortal();};
  66. let setRegistration=function(){crfData.setRegistration(makePortal);};
  67. let action=function(){crfSetup.parseSetup(setRegistration);}
  68. crfSetup.setContainers(action);
  69. }
  70. participantPortal.sortByParticipantId=
  71. function(){
  72. let fName='[sortByParticipantId]';
  73. //this.print(fName);
  74. //let pMap=this.getParticipantMap();
  75. let rows=crfSetup.getRows('crfEntries');
  76. for (let i=0;i<rows.length;i++){
  77. let entry=rows[i];
  78. let id=crfSetup.getParticipantLabel(entry);
  79. if (!id) id="NONE";
  80. let formId=entry['Form'];
  81. let pArray=this.getParticipantArray(id,formId);
  82. pArray.push(entry);
  83. this.print(fName+' pushing '+id+','+formId);
  84. }
  85. this.printParticipantArray();
  86. }
  87. participantPortal.printParticipantArray=
  88. function(){
  89. let fName='[printParticipantMap]';
  90. this.print(fName);
  91. let pMap=this.getParticipantMap();
  92. for (let q in pMap){
  93. for (let x in pMap[q])
  94. this.print(fName+' ['+q+','+x+'] '+pMap[q][x].length);
  95. }
  96. }
  97. participantPortal.makePortal=
  98. function(){
  99. let that=this;
  100. let updateParticipants=function(){that.updateParticipants();};
  101. crfRoleSelector.makePortal(updateParticipants);
  102. this.roleError=crfHTML.createParagraph('','formDiv');
  103. let txt=crfHTML.createParagraph('Select participant','formDiv');
  104. crfHTML.clearStyle(txt,'center');
  105. this.participantSelect=crfHTML.createSelect(new Object(),'formDiv');
  106. this.displayTable=crfHTML.createTable('formDiv');
  107. this.participantSelect.onchange=function(){that.displayEntries();}
  108. //start with all patients
  109. this.updateParticipants();
  110. if (this.searchParams.get("role") && this.searchParams.get("site")){
  111. let label=this.searchParams.get("role")+':'+this.searchParams.get("site");
  112. crfRoleSelector.setRoleAndSite(label);
  113. crfRoleSelector.onChange(updateParticipants);
  114. }
  115. }
  116. participantPortal.updateParticipants=
  117. function(){
  118. let fName='[participantPortal.updateParticipants]';
  119. let roleAndSite=crfRoleSelector.getRoleAndSite();
  120. //clear error
  121. this.roleError.innerText='';
  122. let site=null;
  123. if (roleAndSite){
  124. let ar=roleAndSite.split(':');
  125. site=ar[1];
  126. this.print(fName+' site '+site);
  127. }
  128. let regMap=crfData.getRegistrationEntryMap();
  129. this.print(fName+' registration '+Object.keys(regMap).length);
  130. //let idMap=crfData.getRegistrationMap(this.idField);
  131. //let idLocalMap=crfData.getRegistrationMap(this.localIdField);
  132. this.clearKeyMap();
  133. let updatedMap=new Object();
  134. for (q in regMap){
  135. let key=regMap[q]['Key'];
  136. let label=crfSetup.getParticipantLabel(regMap[q]);
  137. this.print(fName+' key '+key+' label '+label+' site '+site);
  138. this.addToKeyMap(key,label);
  139. if (site && regMap[q]['site']!=site) continue;
  140. updatedMap[key]=label;
  141. }
  142. updatedMap[1000]='NONE';
  143. updatedMap[1001]='Add new participant';
  144. crfHTML.addSelectOptions(this.participantSelect,updatedMap);
  145. if (this.searchParams.get('participantLabel')){
  146. this.participantSelect.value=this.getFromKeyMap(this.searchParams.get('participantLabel'));
  147. this.displayEntries();
  148. }
  149. }
  150. participantPortal.displayEntries=
  151. function(){
  152. let fName='[displayEntries]';
  153. let roleAndSite=crfRoleSelector.getRoleAndSite();
  154. this.print(fName);
  155. let formRows=crfSetup.getRows('dataForms');
  156. //let idRows=crfData.getRegistration();
  157. let selectIdText=this.participantSelect.options[this.participantSelect.selectedIndex].text;
  158. if (selectIdText=='Add new participant'){
  159. let formId=crfSetup.getSettings('registrationFormId');
  160. if (!formId){
  161. alert('registrationFormId not set in settings!');
  162. return;
  163. }
  164. let cb=function(_data,_crfEntry){formPortal.openForm(_crfEntry,roleAndSite);};
  165. formPortal.createForm(formId,roleAndSite,cb);
  166. return;
  167. }
  168. //let selectId=crfSetup.getStudyId(selectIdText);
  169. let selectId=selectIdText;
  170. //let formId=formIds[Object.keys(formIds)[0]];
  171. //this.printParticipantArray();
  172. this.print(fName+' rows '+this.displayTable.rows.length);
  173. for (let i=0;i<formRows.length;i++){
  174. let formId=formRows[i]['Key'];
  175. let row=this.displayTable.rows[i];
  176. if (!row) row=this.displayTable.insertRow(i);
  177. let labelCell=row.cells[0];
  178. let formName=crfSetup.getMap('dataForms')[formId];
  179. if (!labelCell){
  180. labelCell=row.insertCell();
  181. labelCell.innerText=formName;
  182. crfHTML.addStyle(labelCell,'medium');
  183. crfHTML.addStyle(labelCell,'center');
  184. //crfHTML.createParagraph(formName,null,labelCell);
  185. }
  186. let cell=row.cells[1];
  187. if (!cell) {
  188. cell=row.insertCell();
  189. crfHTML.addStyle(cell,'stretch');
  190. }
  191. crfHTML.clear(cell);
  192. let forms=this.getParticipantArray(selectId,formId);
  193. formPortal.displayForms(cell,forms,roleAndSite,formId,selectIdText,this.roleError);
  194. this.print(fName+' ['+selectId+'/'+formId+'] forms '+forms.length);
  195. }
  196. }