formPortalNew.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. var formPortal={};
  2. formPortal.setDebug=
  3. function(debug=null){
  4. if (debug){
  5. this.print=function(msg){debug.print(msg);};
  6. this.clear=function(){debug.clear();}
  7. return;
  8. }
  9. //provide default functions if not debug object is available
  10. this.print=function(msg){console.log(msg);}
  11. this.clear=function(){;}
  12. }
  13. formPortal.setDebug();
  14. formPortal.doNothing=
  15. function(){
  16. this.print('doNothing called');
  17. }
  18. //load runQuery.js
  19. formPortal.printMessage=
  20. function(msg){
  21. crfHTML.createParagraph(msg,'formDiv')
  22. }
  23. formPortal.init=
  24. function(cb=null){
  25. let that=this;
  26. let action=function(){that.scriptsLoaded(cb);};
  27. LABKEY.Utils.requiresScript(["crf/runQuery.js","crf/formGenerator.js","crf/crfRoleSelector.js","crf/crfSetup.js","crf/crfData.js","crf/crfHTML.js"],action);
  28. }
  29. formPortal.scriptsLoaded=
  30. function(cb=null){
  31. crfHTML.init();
  32. crfRoleSelector.set(crfSetup);
  33. crfData.setSetup(crfSetup);
  34. let initGenerator=function(){formGenerator.init(cb);};
  35. let initData=function(){crfData.init(initGenerator);};
  36. crfSetup.init(initData);
  37. }
  38. formPortal.generateFormArray=
  39. function(){
  40. let fName='[generateFormArray]';
  41. this.print(fName);
  42. this.print(fName);
  43. let that=this;
  44. let action=function(){that.afterSetup();};
  45. let parseSetup=function(){crfSetup.parseSetup(action);};
  46. crfSetup.setContainers(parseSetup);
  47. }
  48. formPortal.filterEntry=
  49. function(entry,filter){
  50. let fName="[filterEntry]";
  51. //this.print(fName+' candidate '+entry.entryId);
  52. if (entry.Form!=filter.form)
  53. return false;
  54. //only select forms where status matches the target status
  55. if (entry.FormStatus!=filter.formStatus){
  56. return false;
  57. }
  58. if (crfSetup.getSettings("filterUser") && filter.role=='crfEditor' && entry.UserId!=filter.userId){
  59. //this.print(fName+' skipping identity mismatch: '+entry.UserId+'/'+filter.userId);
  60. return false;
  61. }
  62. if (crfSetup.getSettings("filterSite") && entry.Site!=filter.siteNumber){
  63. //this.print(fName+' skipping site mismatch: '+entry.Site+'/'+filter.siteNumber);
  64. return false;
  65. }
  66. return true;
  67. }
  68. formPortal.afterSetup=
  69. function(){
  70. let fName='[afterSetup]';
  71. this.print(fName);
  72. let that=this;
  73. let cb=function(){that.updateForms();};
  74. crfRoleSelector.makePortal(cb);
  75. this.table=crfHTML.createTable('formDiv');
  76. }
  77. formPortal.updateForms=
  78. function(){
  79. //this is normallz callback on role selector
  80. roleAndSite=crfRoleSelector.getRoleAndSite();
  81. this.drawForms(roleAndSite);
  82. }
  83. formPortal.drawForms=
  84. function(roleAndSite=null){
  85. let fName="[drawForms]";
  86. if (!roleAndSite){
  87. alert("Please set role and site");
  88. return false;
  89. }
  90. //set filter
  91. let ar=roleAndSite.split(":");
  92. let role=ar[0];
  93. let filter=new Object();
  94. filter.role=role;
  95. filter.siteNumber=ar[1];
  96. filter.userId=LABKEY.Security.currentUser.id;
  97. let accessModeColumn=filter.role+'Status';
  98. let formRows=crfSetup.getRows('dataForms');
  99. //browse through forms
  100. let fEntries=crfSetup.getRows('crfEntries');
  101. this.print(fName+' all entries '+fEntries.length);
  102. for (let i=0;i<formRows.length;i++){
  103. //dataForms is Forms
  104. let formEntry=formRows[i];
  105. let formId=formEntry['Key'];
  106. //set the status for form
  107. filter.formStatus=formEntry[accessModeColumn];
  108. this.print(fName+' target formStatus ['+formEntry['formName']+'] '+filter.formStatus);
  109. //add row for each form
  110. let row=this.table.rows[i];
  111. if (!row) row=this.table.insertRow(i);
  112. let labelCell=row.cells[0];
  113. let formName=formEntry['formName'];
  114. if (!labelCell){
  115. labelCell=row.insertCell();
  116. labelCell.innerText=formName;
  117. crfHTML.addStyle(labelCell,'medium');
  118. crfHTML.addStyle(labelCell,'center');
  119. //crfHTML.createParagraph(formName,null,labelCell);
  120. }
  121. let cell=row.cells[1];
  122. if (!cell) {
  123. cell=row.insertCell();
  124. crfHTML.addStyle(cell,'stretch');
  125. }
  126. crfHTML.clear(cell);
  127. //update the filter with form idr
  128. filter.form=formId;
  129. let forms=new Array();
  130. for (let j=0;j<fEntries.length;j++){
  131. let entry=fEntries[j];
  132. if (this.filterEntry(entry,filter))
  133. forms.push(entry);
  134. }
  135. this.displayForms(cell,forms,roleAndSite,formId);
  136. this.print(fName+' finished checking existing forms');
  137. //only those that are allowed to create forms
  138. //print('Status: '+qForm[creatorModeColumn]);
  139. }
  140. if (filter.role=='crfManager')
  141. //need formGenerator.js
  142. formGenerator.showFormGenerator(this);
  143. else
  144. formGenerator.hideFormGenerator();
  145. }
  146. formPortal.displayForms=
  147. function(el,formList,roleAndSite,formId=null,idLabel=null,elError=null){
  148. let fName='[displayForms]';
  149. //formList is a list of crfEntry entries
  150. let table=crfHTML.createTable(null,el);
  151. let row=table.insertRow();
  152. let that=this;
  153. let n=0;
  154. let deletedForms=new Array();
  155. for (let i=0;i<formList.length;i++){
  156. let entry=formList[i];
  157. let stat=entry['FormStatus'];
  158. let formStatus=crfSetup.getMap('formStatus')[stat];
  159. //skip deleted forms
  160. if (formStatus=='Deleted'){
  161. deletedForms.push(entry);
  162. continue;
  163. }
  164. let cell=row.insertCell();
  165. n+=1;
  166. crfHTML.addStyle(cell,'stretch');
  167. let fbox=crfHTML.createBox(null,cell);
  168. //colormap of formStatus to colors
  169. let style=crfSetup.getEntryMap('formStatus')[stat]['color'];
  170. if (!style) style='gold';
  171. crfHTML.addStyle(fbox,style);
  172. this.print(fName+' setting style ['+i+'] '+style);
  173. let user=crfSetup.getMap('users')[entry['UserId']];
  174. let idLabel=crfSetup.getParticipantLabel(entry);
  175. let text=[entry['entryId'],user,idLabel,formStatus];
  176. fbox.onclick=function(){that.openForm(entry,roleAndSite,elError);};
  177. for (let j=0;j<text.length;j++){
  178. crfHTML.createParagraph(text[j],null,fbox);
  179. }
  180. }
  181. //formId=0 reserverd for forms created when new patients are added
  182. if (!formId) return table;
  183. let formEntry=crfSetup.getEntryMap('dataForms')[formId];
  184. this.print(fName+' unique '+formEntry['unique']);
  185. this.addDeletedForms(row,deletedForms);
  186. if (n>0 && formEntry['unique']=='TRUE') return table;
  187. //add empty forms for absent records and non-unique records
  188. //should not be allowed to create new forms
  189. let regFormId=crfSetup.getSettings('registrationFormId');
  190. if (regFormId==formId) return table;
  191. let cell=row.insertCell();
  192. crfHTML.addStyle(cell,'stretch');
  193. let fbox=crfHTML.createBox(null,cell);
  194. crfHTML.addStyle(fbox,'empty');
  195. let cb=function(xdata,xcrfEntry){that.openForm(xcrfEntry,roleAndSite);};
  196. fbox.onclick=function(){that.createForm(formId,roleAndSite,cb,idLabel);};
  197. return table;
  198. }
  199. formPortal.addDeletedForms=
  200. function(row,deletedForms){
  201. //display deleted forms
  202. let fName="[addDeletedForms]";
  203. this.print(fName+": "+deletedForms.length);
  204. for (let i=0;i<deletedForms.length;i++){
  205. let entry=deletedForms[i];
  206. let stat=entry['FormStatus'];
  207. let formStatus=crfSetup.getMap('formStatus')[stat];
  208. let cell=row.insertCell();
  209. crfHTML.addStyle(cell,'stretch');
  210. let fbox=crfHTML.createBox(null,cell);
  211. //colormap of formStatus to colors
  212. let style=crfSetup.getEntryMap('formStatus')[stat]['color'];
  213. if (!style) style='gold';
  214. crfHTML.addStyle(fbox,style);
  215. this.print(fName+' setting style ['+i+'] '+style);
  216. let user=crfSetup.getMap('users')[entry['UserId']];
  217. let idLabel=crfSetup.getParticipantLabel(entry);
  218. let text=[entry['entryId'],user,idLabel,formStatus];
  219. //fbox.onclick=function(){that.openForm(entry,roleAndSite,elError);};
  220. for (let j=0;j<text.length;j++){
  221. crfHTML.createParagraph(text[j],null,fbox);
  222. }
  223. }
  224. }
  225. formPortal.openForm=
  226. function(crfEntry,roleAndSite,elError=null){
  227. let fName="[openForm]";
  228. if (!roleAndSite){
  229. if (elError)
  230. elError.innerText="Set role and site";
  231. this.print(fName+' role and site not specified');
  232. return;
  233. }
  234. let crfRef=crfEntry.entryId;
  235. this.print(fName+" clicked for "+crfRef);
  236. let ar=roleAndSite.split(':');
  237. let role=ar[0];
  238. let siteNumber=ar[1];
  239. //select between review and view
  240. //let formUrl=formEntry["formUrl"];
  241. //if ("reviewMode" in config) formUrl=formEntry["reviewFormUrl"];
  242. //print("Setting url "+formUrl);
  243. //direct all to the same html
  244. let formUrl="visitNew";
  245. reviewMode="EDIT";
  246. let params = {
  247. "name": formUrl,
  248. // The destination wiki page. The name of this parameter is not arbitrary.
  249. "entryId": crfRef,
  250. "role" : role
  251. };
  252. //"formSetupQuery":formEntry["setupQuery"],
  253. let containerPath= LABKEY.ActionURL.getContainer();
  254. // This changes the page after building the URL.
  255. //Note that the wiki page destination name is set in params.
  256. var wikiURL = LABKEY.ActionURL.buildURL("crf", formUrl , containerPath, params);
  257. this.print(fName+" redirecting to "+wikiURL);
  258. window.location = wikiURL;
  259. }
  260. formPortal.createForm=
  261. function(formId,roleAndSite,cb=null,idLabel=null,crfEntryOverload=null){
  262. //cb takes arguments (data,crfEntry)
  263. let fName="[createForm]";
  264. let formRow=crfSetup.getEntryMap('dataForms')[formId];
  265. let creator=formRow['creator'];
  266. if (!creator) {
  267. alert("Creator for form "+formRow['formName']+' not set');
  268. return;
  269. }
  270. let ar=roleAndSite.split(':');
  271. let role=ar[0];
  272. let siteNumber=ar[1];
  273. if (creator!=role){
  274. alert("Can't create form as "+role+' only allowed for '+creator);
  275. return;
  276. }
  277. this.print(fName+" create form w/id "+formId);
  278. let crfEntry=new Object();
  279. crfEntry.entryId=Date.now();
  280. crfEntry["Date"]=new Date();
  281. crfEntry["View"]="[VIEW]";
  282. crfEntry.formStatus=1;//In progress
  283. let fStatus=formRow['initialStatus'];
  284. if (fStatus)
  285. crfEntry.formStatus=fStatus;
  286. //set other variables
  287. //requires studyData as part of formConfig
  288. let studyData=crfSetup.getRows('studyData')[0];
  289. let varRows=crfSetup.getRows('crfStaticVariables');
  290. for (let i=0;i<varRows.length;i++){
  291. let varName=varRows[i].staticVariable;
  292. crfEntry[varName]=studyData[varName];
  293. }
  294. crfEntry.UserId=LABKEY.Security.currentUser.id;
  295. crfEntry.Site=siteNumber;
  296. this.print(fName+" setting site to id="+crfEntry.Site);
  297. //from argument list
  298. crfEntry.Form=formId;
  299. if (idLabel){
  300. crfEntry[crfSetup.getLocalIdLabel()]=crfSetup.getLocalId(idLabel);
  301. crfEntry[crfSetup.getStudyIdLabel()]=crfSetup.getStudyId(idLabel);
  302. }
  303. //overload entries
  304. if (crfEntryOverload){
  305. for (f in crfEntryOverload){
  306. crfEntry[f]=crfEntryOverload[f];
  307. }
  308. }
  309. let crfStatus=crfData.createCrfStatus(crfEntry);
  310. crfStatus.operator=role;
  311. crfStatus.action='createForm';
  312. let that=this;
  313. //let cb=function(data){that.openForm(crfEntry,roleAndSite);}
  314. this.print(fName+' '+cb);
  315. let callback=function(data){cb(data,crfEntry);};
  316. let containerPath=crfSetup.getContainer('data');
  317. let pass=function(data){runQuery.insertRows('lists','crfStatus',[crfStatus],callback,containerPath);};
  318. runQuery.insertRows('lists','crfEntry',[crfEntry],pass,containerPath);
  319. }