crfVisit.js 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250
  1. var crfVisit={};
  2. crfVisit.config=new Object();
  3. crfVisit.setDebug=
  4. function(debug=null){
  5. if (debug){
  6. this.print=function(msg){debug.this.print(msg);};
  7. this.clear=function(){debug.clear();}
  8. return;
  9. }
  10. //provide default functions if not debug object is available
  11. this.print=function(msg){console.log(msg);}
  12. this.clear=function(){;}
  13. }
  14. crfVisit.setDebug();
  15. //harmonize signature
  16. //(schema,query,row,action=cvDoNothing,container=null
  17. crfVisit.insertRows=
  18. function(schema,query,rows,action=null,container=null,failure=null){
  19. this.modifyRows('insert',schema,query,rows,action,container);
  20. }
  21. crfVisit.deleteRows=
  22. function(schema,query,rows,action=null,container=null,failure=null){
  23. this.modifyRows('delete',schema,query,rows,action,container);
  24. }
  25. crfVisit.modifyRows=
  26. function(mode,schema,query,rows,action=null,container=null,failure=null){
  27. //insert rows to container/schema/query and return with action
  28. let fName="[cvModifyRows/"+mode+"]";
  29. this.print(fName+' '+schema+'/'+query);
  30. let qconfig=new Object();
  31. qconfig.schemaName=schema;
  32. qconfig.queryName=query;
  33. if (container) qconfig.containerPath=container;
  34. if (!rows) {
  35. this.print(fName+' rows '+rows);
  36. return;
  37. }
  38. qconfig.rows=rows;
  39. qconfig.success=function(data){;};
  40. if (action) qconfig.success=action;
  41. if (mode=='insert') LABKEY.Query.insertRows(qconfig);
  42. if (mode=='update') LABKEY.Query.updateRows(qconfig);
  43. if (mode=='delete') LABKEY.Query.deleteRows(qconfig);
  44. this.print(fName+" done");
  45. }
  46. crfVisit.selectRows=
  47. function(schema,query,filters=[],action=null, container=null, failure=null, columns=null){
  48. let fName="[cvSelectRows]";
  49. this.print(fName+' '+schema+' '+query+' '+container);
  50. let qconfig=new Object();
  51. qconfig.schemaName=schema;
  52. qconfig.queryName=query;
  53. if (container) qconfig.containerPath=container;
  54. qconfig.filterArray=filters;
  55. qconfig.success=function(data){;};
  56. if (action) qconfig.success=action;
  57. if (failure) qconfig.failure=failure;
  58. if (columns) qconfig.columns=columns;
  59. LABKEY.Query.selectRows(qconfig);
  60. this.print(fName+" done");
  61. }
  62. crfVisit.createCrfStatus=
  63. function(crfEntry){
  64. let crfStatus=new Object();
  65. crfStatus.entryId=crfEntry.entryId;
  66. crfStatus.submissionDate=new Date();
  67. crfStatus.FormStatus=crfEntry.FormStatus;
  68. crfStatus.User=crfEntry.UserId;
  69. crfStatus.Form=crfEntry.Form;
  70. return crfStatus;
  71. }
  72. crfVisit.init=
  73. function(cb=null){
  74. let that=this;
  75. let action=function(){that.scriptsLoaded(cb);};
  76. LABKEY.Utils.requiresScript(["crfTecant/runQuery.js","crfTecant/crfReviewSection.js","crfTecant/participantIdManager.js","crfTecant/variableList.js","crfTecant/webdav.js","crfTecant/crfPrint.js"],action);
  77. }
  78. crfVisit.scriptsLoaded=
  79. function(cb=null){
  80. participantIdManager.set(this);
  81. webdav.set(this);
  82. crfReviewSection.set(this);
  83. crfPrint.set(this);
  84. if (cb) cb();
  85. }
  86. crfVisit.getElement=
  87. function(id){
  88. return this.config.document.getElementById(id);
  89. }
  90. crfVisit.setContainer=
  91. function(label,container){
  92. let config=this.config;
  93. if (!(config.formConfig.hasOwnProperty('container'))){
  94. config.formConfig.container=new Array();
  95. }
  96. config.formConfig.container[label]=container;
  97. }
  98. crfVisit.getContainer=
  99. function(label){
  100. return this.config.formConfig.container[label];
  101. }
  102. crfVisit.getCRFrefFirst=
  103. function(){
  104. //crfRef is part of html call and gets stored in the page
  105. return this.getElement(this.config.crfRefId).innerHTML;
  106. }
  107. crfVisit.getCRFref=
  108. function (){
  109. //'crfRefId'
  110. return this.config.formConfig.crfEntry['entryId'];
  111. }
  112. crfVisit.getCRFrefData=
  113. function(){
  114. let parentCrf=this.config.formConfig.crfEntry['parentCrf'];
  115. if (parentCrf!=undefined) return parentCrf;
  116. return this.getCRFref();
  117. }
  118. crfVisit.onFailure=
  119. function(errorInfo, options, responseObj){
  120. if (errorInfo && errorInfo.exception)
  121. alert("Failure: " + errorInfo.exception);
  122. else
  123. alert("Failure: " + responseObj.statusText);
  124. }
  125. crfVisit.doNothing=
  126. function (){
  127. this.print('doNothing called');
  128. }
  129. crfVisit.getSnapshotObject=
  130. function(){
  131. if (!("dataQueriesSnapshot" in this.config.formConfig))
  132. this.config.formConfig.dataQueriesSnapshot=new Object();
  133. return this.config.formConfig.dataQueriesSnapshot;
  134. }
  135. crfVisit.getQuerySnapshot=
  136. function(queryName){
  137. //check whether queryName is in snapshotObject?
  138. return this.getSnapshotObject()[queryName];
  139. }
  140. crfVisit.getLayoutObject=
  141. function(){
  142. if (!("dataQueriesLayout" in this.config.formConfig))
  143. this.config.formConfig.dataQueriesLayout=new Object();
  144. return this.config.formConfig.dataQueriesLayout;
  145. }
  146. crfVisit.getQueryLayout=
  147. function(queryName){
  148. //check whether queryName is in snapshotObject?
  149. return this.getLayoutObject()[queryName];
  150. }
  151. crfVisit.getLookupObject=
  152. function(){
  153. if (!("lookup" in this.config.formConfig))
  154. this.config.formConfig.lookup=new Object();
  155. return this.config.formConfig.lookup;
  156. }
  157. crfVisit.getLookup=
  158. function(queryName){
  159. let x=this.getLookupObject();
  160. if (queryName in x) return x[queryName];
  161. return null;
  162. }
  163. crfVisit.getQueryList=
  164. function(){
  165. if (!("queryList" in this.config.formConfig))
  166. this.config.formConfig.queryList=new Object();
  167. return this.config.formConfig.queryList;
  168. }
  169. crfVisit.getIdManager=
  170. function(){
  171. if (!("idManager" in this.config.formConfig))
  172. this.config.formConfig.idManager=participantIdManager.getObject();
  173. return this.config.formConfig.idManager;
  174. }
  175. crfVisit.getAdditionalData=
  176. function(formSetupEntry){
  177. //return information on additional data associated with the form
  178. //additionalData is a sub-list with multiple entries per patient/visit
  179. let config=this.config;
  180. //argument is the row of the formSetup setup list
  181. let queryName=config.formConfig.queryMap[formSetupEntry['queryName']];
  182. let fName='[getAdditionalData/'+queryName+']';
  183. this.print(fName);
  184. //additionalData holds a reference to all queries already parsed
  185. //this helps in reducing number of calls to the database (I assume)
  186. if (queryName in config.formConfig.additionalData){
  187. this.print(fName+': Returning preset value');
  188. return config.formConfig.additionalData[queryName];
  189. }
  190. //first time we see this query, so we have to do the setup
  191. this.print(fName+': generating');
  192. config.formConfig.additionalData[queryName]=new Object();
  193. //takes address, so further changes will be to the newly created object
  194. //in fact, ad is just a short alias of the long variable name on the right
  195. let ad=config.formConfig.additionalData[queryName];
  196. //no additional data
  197. if (formSetupEntry["showFlag"]==="NONE") {
  198. this.print(fName+": empty");
  199. return ad;
  200. }
  201. //use showFlag to setup report section of the CRF list
  202. if (formSetupEntry["showFlag"]==="REVIEW") {
  203. //abuse additionalData to signal different segment
  204. this.print(fName+": generateReport");
  205. ad.isReview=true;
  206. return ad;
  207. }
  208. //setup the additionalData memory object
  209. this.print(fName+': setting values');
  210. ad.showFlag=formSetupEntry["showFlag"];
  211. ad.showFlagValue=formSetupEntry["showFlagValue"];
  212. ad.queryName=formSetupEntry["showQuery"];
  213. //for data queries, limit to present CRF only
  214. ad.filters=new Object();
  215. ad.filters['crfRef']=this.getCRFref();
  216. //compose a long debug message
  217. let msg=fName+": flag "+ad.showFlag;
  218. msg+=" value "+ad.showFlagValue;
  219. msg+=" query "+ad.queryName;
  220. this.print(msg);
  221. return ad;
  222. }
  223. crfVisit.selectFormSetupRows=
  224. function(formId){
  225. let formSetupRows=new Array();
  226. let config=this.config;
  227. let allRows=config.formConfig.formSetup.rows;
  228. for (let i=0;i<allRows.length;i++){
  229. let formEntry=allRows[i];
  230. if (formEntry.formName==formId)
  231. formSetupRows.push(formEntry);
  232. }
  233. return formSetupRows;
  234. }
  235. crfVisit.findTitle=
  236. function(queryName){
  237. //find by name from formDatasets
  238. //and set associated title as title
  239. let config=this.config;
  240. let rows=config.formConfig.formDatasets.rows;
  241. for (let i=0;i<rows.length;i++){
  242. let entry=rows[i];
  243. if (entry['queryName']!=queryName) continue;
  244. return entry['title'];
  245. }
  246. return "NONE";
  247. }
  248. crfVisit.makeSetup=
  249. function(listName){
  250. //generate setup object whcih should contain fields:
  251. //readonlyFlag - whether the dataset is writeable
  252. //filters - selection fields that allow creation of LABKEY.Filter.create()
  253. //getInputId - formating of unique ids for html elements
  254. let fName='[Setup]';
  255. this.print(fName+' '+listName);
  256. let setup=new Object();
  257. setup.queryName=listName;
  258. setup.readonlyFlag=function(vName){return false};
  259. setup.filters=new Object();
  260. setup.filters['crfRef']=this.getCRFref();
  261. setup.getInputId=function(vName){return listName+"_"+vName;}
  262. setup.isReview=false;
  263. return setup;
  264. }
  265. crfVisit.getFullAccessSetup=
  266. function(listName){
  267. //addApply - whether a submit/Save button is generated
  268. let setup=this.makeSetup(listName);
  269. setup.addApply="Save";
  270. return setup;
  271. }
  272. crfVisit.getReadonlySetup=
  273. function(listName){
  274. let setup=this.makeSetup(listName);
  275. //see definition of setup object above, change readonly flag
  276. setup.readonlyFlag=function(vName){return true};
  277. return setup;
  278. }
  279. crfVisit.getSetup=
  280. function(listName,writeAccess=true){
  281. //change to section granulated permission of type EDIT, COMMENT, READ
  282. //let formStatus=config.formConfig.formStatus;
  283. //equivalent to READ
  284. if (!writeAccess)
  285. //if (formStatus=="Submitted")
  286. return this.getReadonlySetup(listName);
  287. //if (formStatus=="Approved")
  288. // return readonlySetup(listName);
  289. return this.getFullAccessSetup(listName);
  290. }
  291. crfVisit.generateSection=
  292. function(formSetupEntry){
  293. let config=this.config;
  294. let that=this;
  295. let listName=config.formConfig.queryMap[formSetupEntry['queryName']];
  296. //if (!listName) is for debugSection
  297. if (!listName){
  298. listName="debugSection";
  299. }
  300. let fName='[generateSection/'+listName+']';
  301. let sectionTitle=formSetupEntry['title'];
  302. let accessModeColumn=config.formConfig.operator+'Mode';
  303. let accessMode=formSetupEntry[accessModeColumn];
  304. //this will fix it for later use as well
  305. let additionalData=this.getAdditionalData(formSetupEntry);
  306. this.print(fName);
  307. let formName=config.masterForm;//this is HTML designator of area on page
  308. let debug=true;
  309. let tb=config.document.createElement('table');
  310. tb.className='t2';
  311. let row=tb.insertRow();
  312. let cell=config.document.createElement('th');
  313. row.appendChild(cell);
  314. cell.setAttribute("colspan","4");
  315. cell.style.fontSize="20px";
  316. cell.style.textAlign="center";
  317. let cellData=config.document.createTextNode(sectionTitle);
  318. cell.appendChild(cellData);
  319. cell=row.insertCell();
  320. let input=config.document.createElement("input");
  321. input.type="button";
  322. input.value="Show";
  323. input.id="toggle"+listName+"VisbilityButton";
  324. input.onclick=function(){that.toggleVisibility(listName,input.id)};
  325. cell.appendChild(input);
  326. this.getElement(formName).appendChild(tb);
  327. let div=config.document.createElement('div');
  328. div.id=listName;
  329. div.style.display="none";
  330. this.getElement(formName).appendChild(div);
  331. //here divert for debugArea
  332. if (listName=="debugSection"){
  333. let debugArea=config.document.createElement('textarea');
  334. debugArea.rows=10;
  335. debugArea.cols=95;
  336. debugArea.id=config.debugId;
  337. div.appendChild(debugArea);
  338. return;
  339. }
  340. let divTable=config.document.createElement('div');
  341. divTable.id=listName+"Table";
  342. div.appendChild(divTable);
  343. if ("showFlag" in additionalData) {
  344. additionalData.divName=listName+"SubDiv";
  345. additionalData.divQueryName=listName+"SubDivList";
  346. let div1=config.document.createElement('div');
  347. div1.id=additionalData.divName;
  348. div1.style.display="none";
  349. div.appendChild(div1);
  350. let div2=config.document.createElement('div');
  351. div2.id=additionalData.divQueryName;
  352. div1.appendChild(div2);
  353. }
  354. this.print(fName+" generate master table");
  355. let writeMode=accessMode=="EDIT";
  356. let setup=this.getSetup(listName,writeMode);
  357. if ("isReview" in additionalData){
  358. crfReviewSection.set(this);
  359. let action=function(){crfReviewSection.CB();};
  360. crfReviewSection.generateSection(listName,div.id,action);
  361. return;
  362. }
  363. //master table is unique per visit
  364. setup.unique=true;
  365. this.generateTable(listName,divTable.id,additionalData,setup);
  366. if (debug) this.print("generate master table: done");
  367. let generateSubTable=true;
  368. //generateSubTable equivalent to read/write access to section
  369. if (accessMode != "EDIT")
  370. generateSubTable=false;
  371. if (! ("showFlag" in additionalData) ) generateSubTable=false;
  372. if (generateSubTable){
  373. let qName=additionalData.queryName;
  374. let dName=additionalData.divName;
  375. let xsetup=this.getFullAccessSetup(qName);
  376. //only set master query for additionalData
  377. xsetup.masterQuery=listName;
  378. //if (readonly) setup=readonlySetup(config);
  379. xsetup.subTable=true;
  380. this.generateTable(qName,dName,additionalData,xsetup);
  381. //generateTable(formSetupEntry,qName,dName,additionalData,setup);
  382. }
  383. this.print("generate review");
  384. let divReviewList=config.document.createElement('div');
  385. divReviewList.id=listName+"ReviewList";
  386. div.appendChild(divReviewList);
  387. let divReview=config.document.createElement('div');
  388. divReview.id=listName+"Review";
  389. div.appendChild(divReview);
  390. //assume we already have listId (content of config.setupQueryName is listId)
  391. //we need listName also
  392. //qconfig.queryName=config.setupQueryName;
  393. this.generateReview(divReview.id,divReviewList.id,listName,accessMode);
  394. if (accessMode!='GENERATE') return;
  395. this.print('Adding generate button');
  396. //add generateButton
  397. let divGenerateButton=config.document.createElement('div');
  398. divGenerateButton.id=listName+'GenerateButton';
  399. div.appendChild(divGenerateButton);
  400. this.print('Adding generate button completed to here');
  401. let cb=function(){that.onGenerateQuery(listName);};
  402. this.generateButton(divGenerateButton.id,'Generate','Generate '+listName,'onGenerateQuery',cb);
  403. this.print('Adding generate button completed');
  404. }
  405. crfVisit.generateReview=
  406. function(divReviewId,divReviewListId, listName, accessMode){
  407. let config=this.config;
  408. let listId=config.formConfig.fields[listName].queryId;
  409. //listId is a number->should it be queryName?
  410. let fName='[generateReview]';
  411. this.print(fName+" list "+listId+'/'+listName);
  412. let reviewSetup=new Object();
  413. reviewSetup.readonlyFlag=function(vName){
  414. if (vName=="queryName") return true;
  415. if (vName=="queryname") return true;
  416. if (vName=="ModifiedBy") return true;
  417. return false;};
  418. reviewSetup.addApply="Add Review";
  419. reviewSetup.reviewTable=true;
  420. let generateTableFlag=true;
  421. let formStatus=config.formConfig.formStatus;
  422. //COMMENTS allowed or not
  423. //three levels of access: EDIT, COMMENT, READ
  424. if (accessMode == "READ"){
  425. //if (formStatus == "Approved" ){
  426. delete reviewSetup.addApply;
  427. reviewSetup.readonlyFlag=function(vName){return false;}
  428. generateTableFlag=false;
  429. }
  430. reviewSetup.filters=new Object();
  431. reviewSetup.filters["crfRef"]=this.getCRFref();
  432. if (config.formConfig.crfEntry.parentCrf){
  433. reviewSetup.filters["crfRef"]=this.getCRFref()+";"+config.formConfig.crfEntry.parentCrf;
  434. }
  435. reviewSetup.filters["queryName"]=listId;//entry in reviewComments list is queryname, all in small caps
  436. //needs listName, in argument
  437. reviewSetup.getInputId=function(vName){return listName+"_add"+vName};
  438. reviewSetup.divReviewListId=divReviewListId;
  439. reviewSetup.isReview=true;
  440. let msg="Review: divId: "+divReviewId;
  441. msg+=" inputId: "+reviewSetup.getInputId;
  442. this.print(msg);
  443. this.updateListDisplay(divReviewListId,"reviewComments",reviewSetup.filters,true);
  444. if (! generateTableFlag) return;
  445. this.generateTable("reviewComments",divReviewId,new Object(),reviewSetup);
  446. }
  447. //>>>>>>>>>>trigger visibility of additional lists
  448. crfVisit.setListVisibility=
  449. function(input,setup,readonlyFlag){
  450. let config=this.config;
  451. let fName="[setListVisibility/"+setup.queryName+"]";
  452. this.print(fName);
  453. let additionalData=config.formConfig.additionalData[setup.queryName];
  454. let x = this.getElement(additionalData.divName);
  455. this.print(fName+": Div: "+x);
  456. x.style.display="none";
  457. let sText;
  458. if (readonlyFlag) sText=input.innerText;
  459. else sText=input.options[input.selectedIndex].text;
  460. this.print(fName+": Selected option text: "+sText);
  461. if (sText == additionalData.showFlagValue){
  462. let filters=new Object();
  463. if ("filters" in additionalData) filters=additionalData.filters;
  464. x.style.display = "block";
  465. this.updateListDisplay(additionalData.divQueryName,
  466. additionalData.queryName,filters,readonlyFlag);
  467. }
  468. }
  469. //>>have list refresh when data is added (not optimal yet)
  470. //
  471. crfVisit.updateListDisplay=
  472. function(divName,queryName,filters,readonlyFlag){
  473. //use Labkey.QueryWebPart to show list
  474. let fName="[updateListDisplay]";
  475. this.print(fName+": UpdateListDisplay: Query - "+queryName
  476. +" div - "+divName);
  477. if (divName=="NONE") return;
  478. let crfRef=this.getCRFref();
  479. let div=this.getElement(divName);
  480. this.print(fName+": generating WebPart: "+queryName);
  481. var qconfig=new Object();
  482. qconfig.renderTo=divName;
  483. //point to data container
  484. qconfig.containerPath=this.getContainer('data');
  485. qconfig.schemaName='lists';
  486. qconfig.queryName=queryName;
  487. qconfig.buttonBarPosition='top';
  488. qconfig.filters=[];
  489. for (f in filters){
  490. let fType=LABKEY.Filter.Types.EQUAL;
  491. this.print(fName+' filter ['+f+'] '+filters[f]+'/'+typeof(filters[f])+' ['+fType+']');
  492. if (variableList.isFilterList(filters[f])){
  493. fType=LABKEY.Filter.Types.IN;
  494. }
  495. qconfig.filters.push(LABKEY.Filter.create(f, filters[f],fType));
  496. }
  497. let that=this;
  498. qconfig.success=function(data){that.updateSuccess(data);};
  499. qconfig.failure=function(errorInfo,options,responseObj){that.onFailure(errorInfo,options,responseObj);};
  500. //show only print button
  501. if (readonlyFlag){
  502. qconfig.buttonBar=new Object();
  503. qconfig.buttonBar.items=["print"];
  504. }
  505. qconfig.showUpdateColumn=false;
  506. LABKEY.QueryWebPart(qconfig);
  507. }
  508. crfVisit.updateSuccess=
  509. function(data){
  510. this.print("Update success");
  511. }
  512. //TODO: this should trigger a data refresh on section, ie populateData(field)
  513. crfVisit.toggleVisibility=
  514. function(divName,buttonName){
  515. let fName='[toggleVisibility/'+divName+']';
  516. this.print(fName);
  517. let config=this.config;
  518. let x = this.getElement(divName);
  519. if (x.style.display === "none") {
  520. //exclude non data sections (like debug)...
  521. this.print(fName+': issuing setData(populateSection)');
  522. x.style.display = "block";
  523. this.getElement(buttonName).value="Hide";
  524. let that=this;
  525. let cb=function(){that.populateSection(divName);};
  526. this.setData(cb);
  527. } else {
  528. x.style.display = "none";
  529. this.getElement(buttonName).value="Show";
  530. }
  531. }
  532. crfVisit.generateButton=
  533. function(divName,caption,label,callbackLabel,callback=null){
  534. this.print("generateButtonX");
  535. let config=this.config;
  536. let tb=config.document.createElement('table');
  537. tb.className="t2";
  538. let r1=tb.insertRow();
  539. th=config.document.createElement('th');
  540. r1.appendChild(th);
  541. th.innerHTML=caption;
  542. //*!*
  543. let c2=r1.insertCell();
  544. let i1=config.document.createElement("input");
  545. i1.type="button";
  546. i1.value=label;
  547. i1.style.fontSize="20px";
  548. let that=this;
  549. if (callback)
  550. i1.onclick=callback;
  551. else
  552. i1.onclick=function(){that[callbackLabel]();};
  553. i1.id='button_'+callbackLabel;
  554. c2.appendChild(i1);
  555. let c1=r1.insertCell();
  556. c1.setAttribute("colspan","1");
  557. //this is only for saveReview?
  558. c1.id=divName+'_reportField';
  559. //c1.id=config.submitReportId;
  560. let el=this.getElement(divName);
  561. this.print("generateButton: element["+divName+"]: "+el);
  562. el.appendChild(tb);
  563. }
  564. crfVisit.generateSubQuery=
  565. function(input, setup, readonlyFlag){
  566. let fName="[generateSubQuery]";
  567. let config=this.config;
  568. if (setup.isReview) return;
  569. if (!(setup.queryName in config.formConfig.additionalData)){
  570. this.print(fName+': no additionalData entry (probably a subquery)');
  571. return;
  572. }
  573. let additionalData=config.formConfig.additionalData[setup.queryName];
  574. if (!("showFlag" in additionalData))
  575. return;
  576. this.print(fName);
  577. let expId=setup.getInputId(additionalData.showFlag);
  578. if (expId!=input.id) {
  579. this.print(fName+": ignoring field "+input.id+"/"+expId);
  580. return;
  581. }
  582. this.print(fName+": Setting onChange to "+input.id);
  583. if (readonlyFlag)
  584. return;
  585. let that=this;
  586. input.onchange=function(){that.setListVisibility(input,setup,readonlyFlag)};
  587. }
  588. //>>populate fields
  589. //
  590. //
  591. //split to field generation and field population
  592. //
  593. crfVisit.addFieldRow=
  594. function(tb,field,setup,additionalData){
  595. let fName="[addFieldRow/"+setup.queryName+':'+field.name+']';
  596. let config=this.config;
  597. let vName=field.name;
  598. let vType=field.type;
  599. let isLookup=("lookup" in field);
  600. this.print(fName+": ["+vName+"/"+vType+'/'+isLookup+"]");
  601. let row=tb.insertRow();
  602. let cell=config.document.createElement('th');
  603. cell.style.width='300px';
  604. row.appendChild(cell);
  605. let text = config.document.createTextNode(field.shortCaption);
  606. cell.appendChild(text);
  607. let input=null;
  608. let colSpan="3";
  609. let cell1=row.insertCell();
  610. cell1.colSpan=colSpan;
  611. let readonlyFlag=setup.readonlyFlag(vName);
  612. //set the html input object
  613. while (1){
  614. if (readonlyFlag){
  615. input=config.document.createElement('label');
  616. input.innerText='Loading';
  617. break;
  618. }
  619. //lookup
  620. if (isLookup){
  621. input = config.document.createElement("select");
  622. break;
  623. }
  624. //date
  625. if (vType=="date"){
  626. input = config.document.createElement("input");
  627. input.type="date";
  628. break;
  629. }
  630. //string
  631. if (vType=="string"){
  632. //we have to make sure UNDEF is carried to below
  633. //since we are adapting file to either show
  634. //current file or allow user to select a file
  635. //
  636. //TODO change this so one can always select file
  637. //but also show the selected file
  638. if(vName.search("reviewComment")>-1){
  639. input = config.document.createElement("textarea");
  640. input.cols="65";
  641. input.rows="5";
  642. break;
  643. }
  644. input=config.document.createElement('input');
  645. input.type="text";
  646. if (vName.search('_file_')<0) break;
  647. cell1.setAttribute('colspan',"1");
  648. let cell2=row.insertCell();
  649. cell2.setAttribute('colspan',"2");
  650. let input1=config.document.createElement('input');
  651. input1.type="file";
  652. input1.id=setup.getInputId(vName)+'_file_';
  653. cell2.appendChild(input1);
  654. break;
  655. }
  656. if (vType=="float"){
  657. input = config.document.createElement("input");
  658. input.type="text";
  659. break;
  660. }
  661. if (vType=="boolean"){
  662. input = config.document.createElement("input");
  663. input.type="checkbox";
  664. this.print("Creating checkbox");
  665. break;
  666. }
  667. break;
  668. }
  669. input.id=setup.getInputId(vName);
  670. cell1.appendChild(input);
  671. this.print(fName+': adding element '+input.id);
  672. this.print(fName+': listing element '+this.getElement(input.id));
  673. //connect associated list
  674. this.generateSubQuery(input,setup,readonlyFlag);
  675. if (readonlyFlag) {
  676. this.print(fName+': exiting(readonlyFlag)');
  677. return;
  678. }
  679. if (!isLookup) {
  680. this.print(fName+': exiting (not lookup)');
  681. return;
  682. }
  683. let lookup=field["lookup"];
  684. //get all values from config.formConfig.lookup[X]
  685. let lObject=config.formConfig.lookup[lookup.queryName];
  686. //debug
  687. this.print(fName+": query: "+lookup.queryName);
  688. this.print(fName+": ElementId: "+input.id);
  689. this.print(fName+": No of options: " +lObject.LUT.length);
  690. this.print(fName+": Element: "+input);
  691. //set the lut value (input is text label) for readonly
  692. //clear existing fields from input
  693. for(let i = input.options.length; i >= 0; i--) {
  694. input.remove(i);
  695. }
  696. //create option -1
  697. let opt = config.document.createElement("option");
  698. opt.text = "<Select>";
  699. opt.value = -1;
  700. input.options[0] = opt;
  701. this.print(fName+": Adding <Select>");
  702. //add other, label them with LUT
  703. for (let v in lObject.LUT) {
  704. this.print(fName+': populating '+v+': '+lObject.LUT[v]);
  705. let opt = config.document.createElement("option");
  706. opt.text = lObject.LUT[v];
  707. opt.value = v;
  708. input.options[input.options.length] = opt;
  709. }
  710. input.selectedIndex=0;
  711. }
  712. crfVisit.addSpecialFieldRows=
  713. function(tb,specFieldSetup,setup){
  714. //tb is the table, specFieldSetup is a row from the table where special fields are being setup
  715. //the first column is fieldUID, which is a colon joined amalgation of queryName:fieldName
  716. let fieldUID=specFieldSetup["fieldUID"];
  717. let x=fieldUID.split(':');
  718. let fieldName=x[1];
  719. let fName="[addSpecialFieldRow/"+fieldUID+"]";
  720. let q=variableList.parseVariables(specFieldSetup['actionParameters']);
  721. let config=this.config;//for add data
  722. this.print(fName);
  723. if (specFieldSetup['actionType']=='textArea'){
  724. let row=tb.insertRow();
  725. let cell1=row.insertCell();
  726. cell1.colSpan="4";
  727. cell1.style.textAlign="justify";
  728. cell1.style.padding="10px";
  729. cell1.style.backgroundColor="#e0e0e0";
  730. cell1.innerText=q['description'];
  731. return;
  732. }
  733. if (specFieldSetup['actionType']=='generationObject'){
  734. //only in EDIT mode!!
  735. let ro=setup.readonlyFlag(fieldName);
  736. if (ro) return;
  737. generateRegistration.set(this);
  738. let gc=generateRegistration.getObject(q,setup.getInputId(fieldName));
  739. let that=this;
  740. let action=function(){that.doNothing();};
  741. if ('mailRecipient' in q){
  742. gc.callback=function(data){that.sendEmail(data,q['mailRecipient'],action,q['subject']);};
  743. }
  744. else
  745. gc.callback=function(data){that.doNothing();};
  746. if ("addData" in q){
  747. vars=q["addData"].split(',');
  748. gc.addData=new Array();
  749. for (let v in vars){
  750. let s=vars[v]
  751. //variable name can be written as A/B where A is the name in addData and B is the variable name in crfEntry
  752. //useful for mocking up crfId from daughter crf-s such as registration
  753. let sArray=s.split('/');
  754. let sTarget=sArray[0];
  755. let sSource=sArray[sArray.length-1];
  756. gc.addData[sTarget]=config.formConfig.crfEntry[sSource];
  757. this.print(fName+" addData ["+sTarget+"]: "+gc.addData[sTarget]);
  758. }
  759. }
  760. let row=tb.insertRow();
  761. let cell=config.document.createElement('th');
  762. row.appendChild(cell);
  763. let text = config.document.createTextNode("Automatic ID generator");
  764. cell.appendChild(text);
  765. let cell1=row.insertCell();
  766. cell1.colSpan="3";
  767. let b=config.document.createElement("input");
  768. b.type="button";
  769. b.id="generateIdButton";
  770. b.onclick=function(){generateRegistration.execute(gc);};
  771. b.value="Generate ID";
  772. cell1.appendChild(b);
  773. }
  774. }
  775. crfVisit.populateFieldRow=
  776. function(entry,field,setup){
  777. this.populateField(entry,field,setup);
  778. this.populateSubQuery(entry,field,setup);
  779. }
  780. crfVisit.populateSubQuery=
  781. function(entry,field,setup){
  782. let fName='[populateSubQuery/'+setup.queryName+':'+field.name+']';
  783. let config=this.config;
  784. if (setup.isReview) return;
  785. if (!(setup.queryName in config.formConfig.additionalData)){
  786. let msg=fName+': no additionalData entry for '+setup.queryName;
  787. msg+=' (probably a subquery)';
  788. this.print(msg);
  789. return;
  790. }
  791. //find if field is connected to a sub array
  792. //find queryName
  793. //
  794. let additionalData=config.formConfig.additionalData[setup.queryName];
  795. this.print(fName);
  796. //let flag=additionalData.showFlag;
  797. if (!("showFlag" in additionalData)) return;
  798. let eId=setup.getInputId(additionalData.showFlag);
  799. let id=setup.getInputId(field.name);
  800. if (eId!=id) {
  801. this.print(fName+": ignoring field "+id+"/"+eId);
  802. return;
  803. }
  804. this.print(fName+': id '+id);
  805. //hard to estimate readonlyFlag
  806. //
  807. let input=this.getElement(id);
  808. let eType=input.nodeName.toLowerCase();
  809. let readonlyFlag=eType!="select";
  810. this.setListVisibility(input,setup,readonlyFlag);
  811. }
  812. crfVisit.clearField=
  813. function(field,setup){
  814. let foo=new Object();
  815. this.populateField(foo,field,setup);
  816. }
  817. crfVisit.populateField=
  818. function(entry,field,setup){
  819. let vName=field.name;
  820. let fName='[populateFieldName/'+vName+']';
  821. let config=this.config;
  822. let varValue="UNDEF";
  823. //if (vName in setup.filters) varValue=setup.filters[vName];
  824. if (vName in entry) varValue=entry[vName];
  825. //if part of the filter, set it to value
  826. if (vName in setup.filters) varValue=setup.filters[vName];
  827. let isLookup=("lookup" in field);
  828. this.print(fName+' v='+varValue+'/'+isLookup+' ['+
  829. setup.getInputId(field.name)+']');
  830. let vType=field.type;
  831. let id=setup.getInputId(vName);
  832. let input=this.getElement(id);
  833. //date
  834. if (vType=="date"){
  835. if (varValue=="UNDEF") varValue=new Date();
  836. else varValue=new Date(varValue);
  837. }
  838. //lookup for readonly
  839. if (isLookup && varValue!="UNDEF"){
  840. let lookup=field["lookup"];
  841. //get all values from config.formConfig.lookup[X]
  842. let lObject=config.formConfig.lookup[lookup.queryName];
  843. varValue=lObject.LUT[varValue];
  844. }
  845. this.print('Element: '+input);
  846. //figure out the element type
  847. let eType=input.nodeName.toLowerCase();
  848. this.print('Element type: '+eType);
  849. //change varValue for printing
  850. if (varValue=="UNDEF") varValue="";
  851. //HTMLTextArea, createElement(textArea)
  852. if (eType==="textarea"){
  853. input.value=varValue;
  854. return;
  855. }
  856. //Text, createTextNode
  857. if (eType==="#text"){
  858. input.nodeValue=varValue;
  859. return;
  860. }
  861. //HTMLLabelElement, createElement('label')
  862. if (eType==="label"){
  863. input.innerText=varValue;
  864. return;
  865. }
  866. //HTMLSelectElement, createElement('select')
  867. if (eType==="select"){
  868. input.selectedIndex=0;
  869. for (let i=0;i<input.options.length;i++){
  870. let v=input.options[i].text;
  871. if (v!=varValue) continue;
  872. input.selectedIndex=i;
  873. break;
  874. }
  875. return;
  876. }
  877. if (eType!="input"){
  878. this.print('Unknown type: '+eType+' encountered, igonring');
  879. return;
  880. }
  881. //HTMLInputElement
  882. let type=input.type;
  883. if (type=="date"){
  884. input.valueAsDate=varValue;
  885. return;
  886. }
  887. //string,float
  888. if (type=="text"){
  889. input.value=varValue;
  890. return;
  891. }
  892. //boolean
  893. if (type=="checkbox"){
  894. input.checked=varValue;
  895. return;
  896. }
  897. this.print('Unknown input type: '+type+'. Ignoring.');
  898. }
  899. crfVisit.populateTable=
  900. function(listName,writeMode){
  901. //function populateTable(formSetupEntry){
  902. //let listName=config.formConfig.queryMap[formSetupEntry['queryName']];
  903. //let accessMode=config.formConfig.operator+'Mode';
  904. //let writeMode=formSetupEntry[accessMode]=='EDIT';
  905. let fName='[populateTable/'+listName+']';
  906. let setup=this.getSetup(listName,writeMode);
  907. let entry=new Object();
  908. //data snapshot
  909. let fQuery=this.getQuerySnapshot(listName);
  910. let queryLayout=this.getQueryLayout(listName);
  911. //here I assume that listName was parsed during setDataLayout and setData
  912. //so that rows was set (even if they are empty)
  913. this.print(fName+"]: nrows "+fQuery.rows.length);
  914. if (fQuery.rows.length>0)
  915. entry=fQuery.rows[0];
  916. let fields=queryLayout.fields;
  917. for (f in fields){
  918. let field=fields[f];
  919. //each field is a new row
  920. this.print(fName+": Adding field: "+f+'/'+field.name+' hidden: '+field.hidden+' type:'+field.type);
  921. if (field.hidden) continue;
  922. if (field.name=="crfRef") continue;
  923. this.populateFieldRow(entry,field,setup);
  924. }
  925. }
  926. crfVisit.generateTable=
  927. function(listName,divName,additionalData,setup){
  928. let fName="[generateTable/"+listName+"]";
  929. let config=this.config;
  930. this.print(fName);
  931. //is listName and setup.queryName a duplicate of the same value
  932. this.print(fName+': setup.queryName '+setup.queryName);
  933. //assume data is set in config.formConfig.dataQueries[data.queryName].rows;
  934. let populateData=true;
  935. if ("subTable" in setup){
  936. this.print(fName+" is subTable");
  937. populateData=false;
  938. }
  939. let entry=new Object();
  940. //data snapshot
  941. let fQuerySnapshot=this.getQuerySnapshot(listName);
  942. let queryLayout=this.getQueryLayout(listName);
  943. //here I assume that listName was parsed during setDataLayout and setData
  944. //so that rows was set (even if they are empty)
  945. this.print(fName+": Nrows "+fQuerySnapshot.rows.length);
  946. if (fQuerySnapshot.rows.length>0)
  947. entry=fQuerySnapshot.rows[0];
  948. if ("reviewTable" in setup){
  949. entry['reviewComment']='';
  950. delete entry["ModifiedBy"];
  951. }
  952. let tb=config.document.createElement('table');
  953. tb.className="t2";
  954. this.getElement(divName).appendChild(tb);
  955. //this are the fields (probably constant)
  956. let fields=queryLayout.fields;
  957. for (f in fields){
  958. let field=fields[f];
  959. let fieldUID=listName+":"+field.name;
  960. //each field is a new row
  961. this.print(fName+": Adding field: "+f+'/'+field.name+' ('+fieldUID+').');
  962. //unique name
  963. if (field.hidden) continue;
  964. if (field.name=="crfRef") continue;
  965. this.addFieldRow(tb,field,setup,additionalData);
  966. if (populateData) this.populateFieldRow(entry,field,setup);
  967. if (fieldUID in config.formConfig["specialFields"]){
  968. let specFieldSetup=config.formConfig["specialFields"][fieldUID];
  969. this.addSpecialFieldRows(tb,specFieldSetup,setup);
  970. }
  971. }
  972. //finish of if apply button is not required
  973. if (!("addApply" in setup)) {
  974. this.print(fName+"populateTable: done");
  975. return;
  976. }
  977. let row=tb.insertRow();
  978. let th=config.document.createElement('th');
  979. row.appendChild(th);
  980. th.innerHTML=setup.addApply;
  981. let cell=row.insertCell();
  982. //cell.setAttribute("colspan","2");
  983. let input=config.document.createElement("input");
  984. input.type="button";
  985. input.value=setup.addApply;
  986. cell.appendChild(input);
  987. let cell1=row.insertCell();
  988. cell1.setAttribute("colspan","2");
  989. cell1.id=setup.getInputId("rerviewLastSave");
  990. cell1.innerHTML="No recent update";
  991. //saveReview is a generic name for saving content of the html page to a list entry
  992. let that=this;
  993. input.onclick=function(){that.saveReview(listName,cell1.id,setup)};
  994. }
  995. crfVisit.setEntryFromElement=
  996. function(entry,elementId, field){
  997. //set value to entry from element using representation (field) from labkey
  998. //
  999. //
  1000. let fName='setEntryFromElement';
  1001. let config=this.config;
  1002. let el=this.getElement(elementId);
  1003. if (!el) {
  1004. this.print(fName+" element: "+elementId+" not found");
  1005. return;
  1006. }
  1007. this.print(fName+" element: "+elementId);
  1008. let vName=field.name;
  1009. let vType=field.type;
  1010. let eType=el.nodeName.toLowerCase();
  1011. if (eType==="select"){
  1012. entry[vName]=el.options[el.selectedIndex].value;
  1013. return;
  1014. }
  1015. if (eType==="td"){
  1016. entry[vName]=el.innerText;
  1017. return;
  1018. }
  1019. if (vType=="date"){
  1020. let date=el.valueAsDate;
  1021. if (!date) return;
  1022. date.setUTCHours(12);
  1023. entry[vName]=date.toString();
  1024. this.print(fName+" setting date to "+entry[vName]);
  1025. return;
  1026. }
  1027. if (vType=="string"){
  1028. entry[vName]=el.value;
  1029. if (vName.search('_file_')<0)
  1030. return;
  1031. //upload file
  1032. let id1=elementId+'_file_';
  1033. let input1=this.getElement(id1);
  1034. this.print(fName+' attachment field: '+input1.value);
  1035. //entry[vName]=el.files[0].stream();
  1036. let ctx=new Object();
  1037. ctx['dirName']='consent';
  1038. ctx['ID']=entry['crfRef'];
  1039. //should point to data container
  1040. ctx['project']=getContainer('data');
  1041. //need ID->crf!
  1042. //assume crfRef will get set before this
  1043. //element is encountered
  1044. this.uploadFile(input1,ctx);
  1045. let fv=el.value;
  1046. let suf=fv.split('.').pop();
  1047. entry[vName]=entry['crfRef']+'.'+suf;
  1048. return;
  1049. }
  1050. if (vType=="float" || vType=="int"){
  1051. entry[vName]=el.value;
  1052. if (vName=="queryName") {
  1053. this.print(fName+' parsing queryName: '+el.innerText);
  1054. entry[vName]=config.formConfig.fields[el.innerText].queryId;
  1055. //use queryMap lookup
  1056. }
  1057. return;
  1058. }
  1059. if (vType=="boolean"){
  1060. entry[vName]=el.checked;
  1061. return;
  1062. }
  1063. return;
  1064. }
  1065. crfVisit.saveReview=
  1066. function(queryName,elementId,setup){
  1067. //loads any queryName
  1068. let debug=true;
  1069. let fName='[saveReview/'+queryName+']';
  1070. this.print(fName+" elementId "+elementId);
  1071. let unique=("unique" in setup);
  1072. //data snapshot
  1073. let fQuerySnapshot=this.getQuerySnapshot(queryName);
  1074. let nRows=fQuerySnapshot.rows.length;
  1075. let mode='insert';
  1076. //data layout
  1077. let queryLayout=this.getQueryLayout(queryName);
  1078. let entry=new Object();
  1079. //determine mode based on entry uniqueness and presence of data
  1080. if (unique && nRows>0){
  1081. entry=fQuerySnapshot.rows[0];
  1082. mode='update';
  1083. }
  1084. this.print(fName+' unique '+unique+' mode '+mode+' nRows '+nRows);
  1085. entry.crfRef=this.getCRFrefData();
  1086. this.print(fName+" set crfRef="+entry.crfRef);
  1087. let fields=queryLayout.fields;
  1088. for (f in fields){
  1089. let field=fields[f];
  1090. this.print(fName+" saveReview field: "+field.name);
  1091. if (field.hidden) continue;
  1092. let vName=field.name;
  1093. let vType=field.type;
  1094. this.print(fName+" vType: "+vType);
  1095. if (vName=="crfRef") continue;
  1096. //need to save queryName for reviewComments
  1097. let eId=setup.getInputId(vName);
  1098. //copy values from form to entry
  1099. this.setEntryFromElement(entry,eId,field);
  1100. //clear field value
  1101. if (!unique) this.clearField(field,setup);
  1102. }
  1103. let that=this;
  1104. let action=function(data){that.updateLastSavedFlag(data,setup,elementId)};
  1105. this.modifyRows(mode,'lists',queryName,[entry],action,this.getContainer('data'));
  1106. }
  1107. crfVisit.updateLastSavedFlag=
  1108. function(data,setup,elementId){
  1109. let fName='[updateLastSavedFlag]';
  1110. let config=this.config;
  1111. this.print(fName+" update last saved flag to "+elementId);
  1112. let el=this.getElement(elementId);
  1113. let dt=new Date();
  1114. el.innerHTML="Last saved "+dt.toString();
  1115. if (data.queryName=="reviewComments"){
  1116. this.updateListDisplay(setup.divReviewListId,"reviewComments",setup.filters,true);
  1117. }
  1118. //refresh stored data!
  1119. let writeMode=!setup.readonlyFlag();
  1120. let that=this;
  1121. if ("unique" in setup)
  1122. this.setData(function (){that.populateTable(data.queryName,writeMode);});
  1123. if ("masterQuery" in setup){
  1124. let ad=config.formConfig.additionalData[setup.masterQuery];
  1125. this.print('Updating list display: '+setup.queryName+'/'+ad.queryName);
  1126. this.updateListDisplay(ad.divQueryName,ad.queryName,ad.filters,false);
  1127. }
  1128. }
  1129. //******************************************upload to database *********************
  1130. crfVisit.onDatabaseUpload=
  1131. function(){
  1132. let fName='[onDatabaseUpload]';
  1133. this.print(fName);
  1134. let config=this.config;
  1135. config.upload=new Object();
  1136. let fc=new Object();
  1137. let pM=this.getIdManager();
  1138. fc.participantId=participantIdManager.getParticipantIdFromCrfEntry(pM);
  1139. this.print(fName+' id '+fc.participantId);
  1140. this.afterParticipantId(fc);
  1141. }
  1142. crfVisit.afterParticipantId=
  1143. function(fc){
  1144. this.print("Setting participantId to "+fc.participantId);
  1145. let config=this.config;
  1146. config.upload.participantId=fc.participantId;
  1147. //another select rows to update all queries from setup
  1148. //just use registration for test
  1149. let formSetupRows=config.formConfig.formSetupRows;
  1150. config.upload.queries=new Array();
  1151. this.print("Form rows: "+formSetupRows.length);
  1152. for (let i=0;i<formSetupRows.length;i++){
  1153. let entry=formSetupRows[i];
  1154. //skip reviews
  1155. if (entry.showFlag=="REVIEW") continue;
  1156. //use lookup table to convert from id to name
  1157. let queryName=config.formConfig.queryMap[entry.queryName];
  1158. config.upload.queries.push({queryName:queryName,queryStatus:"QUEUED"});
  1159. this.print('form ['+i+']='+queryName+' '+entry.showFlag+'/'+entry.showQuery);
  1160. if (entry.showQuery=="NONE")
  1161. continue;
  1162. config.upload.queries.push({queryName:entry.showQuery,queryStatus:"QUEUED"});
  1163. }
  1164. //add reviews
  1165. config.upload.queries.push({queryName:"reviewComments",queryStatus:"QUEUED"});
  1166. config.upload.queryId=0;
  1167. this.copyToDataset();
  1168. }
  1169. crfVisit.copyToDataset=
  1170. function(){
  1171. let fName='[copyToDataset]: ';
  1172. let config=this.config;
  1173. this.print(fName+'['+config.upload.queryId+'/'+config.upload.queries.length+']');
  1174. //watch dog + scheduler
  1175. //
  1176. let that=this;
  1177. //watchdog part
  1178. if (config.upload.queryId==config.upload.queries.length) {
  1179. this.print(fName+'completing');
  1180. let targetStatus=config.formConfig.targetStatus['onDatabaseUpload'];
  1181. let targetRecipient=config.formConfig.targetRecipient['onDatabaseUpload'];
  1182. let action=new Object();
  1183. action.name='onDatabaseUpload';
  1184. let redirect=function(){that.redirect();};
  1185. action.cb=function(data){that.sendEmail(data,targetRecipient,redirect,'Form uploaded');}
  1186. this.updateFlag(targetStatus,action);//Approved
  1187. return;
  1188. }
  1189. //scheduler
  1190. let queryName=config.upload.queries[config.upload.queryId].queryName;
  1191. this.print("copyToDataset["+config.upload.queryId+"/"+
  1192. config.upload.queries.length+"]: "+queryName);
  1193. let filters=[LABKEY.Filter.create('crfRef',this.getCRFref())];
  1194. let action=function(data){that.afterListData(data);};
  1195. this.selectRows('lists',queryName,filters,action,this.getContainer('data'));
  1196. }
  1197. crfVisit.afterListData=
  1198. function(data){
  1199. let fName='[afterListData]: ';
  1200. let config=this.config;
  1201. let queryName=config.upload.queries[config.upload.queryId].queryName;
  1202. this.print(fName+" ["+queryName+"/list]: "+data.rows.length+" entries");
  1203. config.upload.queries[config.upload.queryId].listData=data;
  1204. let id=config.upload.participantId;
  1205. let filters=[LABKEY.Filter.create('crfRef',this.getCRFref()),LABKEY.Filter.create('ParticipantId',id)];
  1206. let that=this;
  1207. let action=function(data){that.afterStudyData(data);};
  1208. this.selectRows('study',queryName,filters,action,this.getContainer('data'));
  1209. }
  1210. crfVisit.afterStudyData=
  1211. function(data){
  1212. let fName='[afterStudyData]: ';
  1213. let config=this.config;
  1214. let queryObj=config.upload.queries[config.upload.queryId];
  1215. queryObj.studyData=data;
  1216. let msg=fName+"["+queryObj.queryName+"/study]: "+data.rows.length+" entries";
  1217. this.print(msg);
  1218. let listRows=queryObj.listData.rows;
  1219. //skip uploading an empty set
  1220. if (listRows.length==0){
  1221. this.printErr("List "+queryObj.queryName+" empty.");
  1222. queryObj.queryStatus="DONE";
  1223. config.upload.queryId+=1;
  1224. //back to watchdog
  1225. this.copyToDataset();
  1226. return;
  1227. }
  1228. let studyRows=queryObj.studyData.rows;
  1229. for (let i=0;i<studyRows.length;i++){
  1230. let entry=studyRows[i];
  1231. //
  1232. if (! (i<listRows.length) ) continue;
  1233. let entryList=listRows[i];
  1234. //keeps study only variables (ParticipantId, SequenceNum)
  1235. for (let f in entryList) {
  1236. entry[f]=entryList[f];
  1237. this.print(fName+"Copying ["+f+"]: "+entry[f]+"/"+entryList[f]);
  1238. }
  1239. }
  1240. this.print(fName+' copying completed');
  1241. if (studyRows.length>0) {
  1242. let that=this;
  1243. let action=function(data){that.afterStudyUpload(data);};
  1244. this.modifyRows('update','study',queryObj.queryName,studyRows,action,this.getContainer('data'));
  1245. this.print(fName+'updateRows sent');
  1246. }
  1247. else{
  1248. let data=new Object();
  1249. data.rows=new Array();
  1250. this.afterStudyUpload(data);
  1251. }
  1252. }
  1253. crfVisit.afterStudyUpload=
  1254. function(data){
  1255. let fName='[afterStudyUpload] ';
  1256. let config=this.config;
  1257. let that=this;
  1258. this.print(fName);
  1259. //let participantField=config.participantField;
  1260. let participantField=config.formConfig.studyData["SubjectColumnName"];
  1261. this.print(fName+' participantField: '+participantField);
  1262. let queryObj=config.upload.queries[config.upload.queryId];
  1263. let queryName=queryObj.queryName;
  1264. this.printErr("Updated "+data.rows.length+" rows to "+queryName);
  1265. let studyRows=queryObj.studyData.rows;
  1266. let listRows=queryObj.listData.rows;
  1267. let rows=new Array();
  1268. //also updating existing rows, if they exist
  1269. for (let i=studyRows.length;i<listRows.length;i++){
  1270. let entry=listRows[i];
  1271. //make sure you have the participantField right
  1272. //
  1273. entry[participantField]=config.upload.participantId;
  1274. entry.crfRef=this.getCRFref();
  1275. entry.SequenceNum=this.getCRFref();
  1276. entry.SequenceNum=entry.SequenceNum % 1000000000;
  1277. if (listRows.length>1){
  1278. entry.SequenceNum+=i/100;
  1279. }
  1280. this.print( "Adding sequence number "+entry.SequenceNum);
  1281. rows.push(entry);
  1282. }
  1283. if (rows.length>0){
  1284. let action=function(data){that.afterListUpload(data);};
  1285. this.insertRows('study',queryName,rows,action,this.getContainer('data'));
  1286. }
  1287. else{
  1288. let data=new Object();
  1289. data.rows=rows;
  1290. this.afterListUpload(data);
  1291. }
  1292. }
  1293. crfVisit.afterListUpload=
  1294. function(data){
  1295. let config=this.config;
  1296. let queryObj=config.upload.queries[config.upload.queryId];
  1297. let queryName=queryObj.queryName;
  1298. this.printErr("Inserted "+data.rows.length+" rows to "+queryName);
  1299. queryObj.queryStatus="DONE";
  1300. config.upload.queryId+=1;
  1301. this.copyToDataset();
  1302. }
  1303. //*************************update for further review *************************
  1304. crfVisit.onUpdateForReview=
  1305. function(){
  1306. let config=this.config;
  1307. let targetStatus=config.formConfig.targetStatus['onUpdateForReview'];
  1308. let targetRecipient=config.formConfig.targetRecipient['onUpdateForReview'];
  1309. let action=new Object();
  1310. action.name='onUpdateForReview';
  1311. let that=this;
  1312. let redirect=function(){that.redirect();};
  1313. action.cb=function(data){that.sendEmail(data,targetRecipient,redirect,'Form updated for review');};
  1314. this.updateFlag(targetStatus,action);
  1315. }
  1316. crfVisit.updateFlag=
  1317. function(flag,action){
  1318. let fName='[updateFlag 1]';
  1319. let config=this.config;
  1320. let entry=config.formConfig.crfEntry;
  1321. entry.FormStatus=flag;
  1322. let uId=config.formConfig.currentUser.UserId;
  1323. entry[config.formConfig.operator]=uId;
  1324. this.print(fName+': Form: '+entry.Form);
  1325. this.print(fName+": set form status to "+entry.FormStatus);
  1326. let that=this;
  1327. let cb=function(data){that.completeWithFlag(data,action);};
  1328. this.modifyRows('update','lists','crfEntry',[entry],cb,this.getContainer('data'));
  1329. }
  1330. crfVisit.completeWithFlag=
  1331. function(data,action){
  1332. let fName='[completeWithFlag]';
  1333. this.print(fName+': nrows '+data.rows.length);
  1334. let fentry=data.rows[0];
  1335. this.print(fName+': form status '+fentry.FormStatus);
  1336. this.print(fName+': form '+fentry.Form);
  1337. let crfStatus=this.createCrfStatus(fentry);
  1338. let config=this.config;
  1339. crfStatus.operator=config.formConfig.operator;
  1340. crfStatus.action=action.name;
  1341. let that=this;
  1342. let cb=function(){that.doNothing();};
  1343. if (action.cb) cb=action.cb;
  1344. this.insertRows('lists','crfStatus',[crfStatus],cb,this.getContainer('data'));
  1345. }
  1346. //************************************************ submit *******************************************
  1347. crfVisit.onSubmit=
  1348. function(){
  1349. //update list storage and change status
  1350. this.hideErr();
  1351. this.clearErr();
  1352. this.printErr("onSubmit");
  1353. let that=this;
  1354. let action=function(){that.verifyData();};
  1355. this.setData(action);
  1356. }
  1357. crfVisit.verifyData=
  1358. function(){
  1359. let fName='[verifyData]';
  1360. let config=this.config;
  1361. let qList=this.getQueryList();
  1362. let that=this;
  1363. let doNothing=function(data){that.doNothing();};
  1364. for (q in qList){
  1365. let qData=this.getQuerySnapshot(q);
  1366. if (q=="reviewComments") continue;
  1367. //copy snapshot to history
  1368. if (qData.rows.length==0){
  1369. this.print(fName+' no rows for '+q);
  1370. }
  1371. else
  1372. this.insertRows('lists',q+'History',qData.rows,doNothing,this.getContainer('data'));
  1373. //if it doesn't have additionalData, it is a sub query
  1374. if (!(q in config.formConfig.additionalData)){
  1375. continue;
  1376. }
  1377. if (qData.rows.length<1){
  1378. this.printErr('Missing entry for query '+q);
  1379. return false;
  1380. }
  1381. }
  1382. //this is necessary only for Generated to Generation completed step
  1383. let actionSettings=config.formConfig.actionSettings['onSubmit'];
  1384. if (variableList.hasVariable(actionSettings,"updateRegistration")){
  1385. this.updateRegistration();
  1386. }
  1387. let targetStatus=config.formConfig.targetStatus['onSubmit'];
  1388. let targetRecipient=config.formConfig.targetRecipient['onSubmit'];
  1389. this.print(fName+' targetStatus: '+targetStatus);
  1390. let finalStep=function(){that.redirect();};
  1391. if (variableList.hasVariable(actionSettings,"finalStep")){
  1392. //set to doNothing to remain on submit window
  1393. if (actionSettings.finalStep=="doNothing"){
  1394. finalStep=doNothing;
  1395. }
  1396. }
  1397. let action=new Object();
  1398. action.name='onSubmit';
  1399. action.cb=function(data){that.sendEmail(data,targetRecipient,finalStep,'Form sumbitted');};
  1400. this.updateFlag(targetStatus,action);
  1401. }
  1402. crfVisit.getEmail=
  1403. function(recipientCode){
  1404. this.print('getEmail w/'+recipientCode);
  1405. let config=this.config;
  1406. let recipients=new Array();
  1407. let typeTo=LABKEY.Message.recipientType.to;
  1408. let create=LABKEY.Message.createRecipient;
  1409. let currentUser=config.formConfig.currentUser;
  1410. let formCreator=config.formConfig.formCreator;
  1411. let currentSite=config.formConfig.currentSite;
  1412. let userRows=config.formConfig.userRows;
  1413. let parentUser=undefined;
  1414. if ("parentCrfData" in config.formConfig){
  1415. let parentCrf=config.formConfig.parentCrfData;
  1416. parentUser=this.getUser(parentCrf.rows[0].UserId,'parentUser');
  1417. }
  1418. let recipientCategories=recipientCode.split(',');
  1419. for (let i=0;i<recipientCategories.length;i++){
  1420. let recipient=recipientCategories[i];
  1421. this.print('Checking '+recipient);
  1422. if (recipient=='crfEditor'){
  1423. this.print('Adding :'+formCreator.Email);
  1424. recipients.push(create(typeTo,formCreator.Email));
  1425. if (parentUser==undefined) continue;
  1426. this.print('Adding :'+parentUser.Email);
  1427. recipients.push(create(typeTo,parentUser.Email));
  1428. continue;
  1429. }
  1430. //Monitor or Sponsor
  1431. let fList=recipient+'s';
  1432. let fRows=config.formConfig[fList];
  1433. for (let i=0;i<fRows.length;i++){
  1434. this.print('Checking '+fRows[i].User+'/'+fRows[i].Site);
  1435. if (fRows[i].Site!=currentSite.siteNumber) continue;
  1436. for (let j=0;j<userRows.length;j++){
  1437. if (userRows[j].UserId!=fRows[i].User) continue;
  1438. this.print('Adding :'+userRows[j].Email);
  1439. recipients.push(create(typeTo,userRows[j].Email));
  1440. break;
  1441. }
  1442. }
  1443. }
  1444. return recipients;
  1445. }
  1446. crfVisit.sendEmail=
  1447. function(data,recipient='crfEditor',cb=null,subj='Form submitted'){
  1448. this.print('sendEmail; recipient: '+recipient);
  1449. let config=this.config;
  1450. let that=this;
  1451. if (!cb)
  1452. cb=function(){that.redirect();};
  1453. let st=config.formConfig.settings;
  1454. let cvar='sendEmail';
  1455. if (cvar in st){
  1456. this.print(cvar+' set to '+st[cvar]);
  1457. if (st[cvar]=='FALSE'){
  1458. this.print('Skipping sending emails');
  1459. cb();
  1460. return;
  1461. }
  1462. }
  1463. if (recipient==null){
  1464. this.print('Skipping sending emails w/ no recipients');
  1465. cb();
  1466. return;
  1467. }
  1468. this.print('send email '+data.rows.length);
  1469. let crf=data.rows[0]['entryId'];
  1470. let formId=data.rows[0]['Form'];
  1471. let link=LABKEY.ActionURL.getBaseURL();
  1472. link+=LABKEY.ActionURL.getContainer();
  1473. link+='/crf_tecant-visit.view?';
  1474. link+='entryId='+crf;
  1475. link+='&formId='+formId;
  1476. link+='&role='+recipient;
  1477. //debug
  1478. let recipients=this.getEmail(recipient);
  1479. //from crfManagers list
  1480. let typeHtml=LABKEY.Message.msgType.html;
  1481. let typePlain=LABKEY.Message.msgType.plain;
  1482. let msg1=LABKEY.Message.createMsgContent(typePlain,link);
  1483. //let cb=doNothing;
  1484. //let cb=redirect;
  1485. LABKEY.Message.sendMessage({
  1486. msgFrom:'labkey@fmf.uni-lj.si',
  1487. msgSubject:subj,
  1488. msgRecipients:recipients,
  1489. msgContent:[msg1],
  1490. success: cb
  1491. });
  1492. }
  1493. crfVisit.hideErr=
  1494. function(){
  1495. let el=this.getElement("errorDiv");
  1496. el.style.display="none";
  1497. }
  1498. crfVisit.clearErr=
  1499. function(){
  1500. let el=this.getElement("errorTxt");
  1501. el.value="";
  1502. }
  1503. crfVisit.showErr=
  1504. function(){
  1505. let el=this.getElement("errorDiv");
  1506. el.style.display="block";
  1507. }
  1508. crfVisit.printErr=
  1509. function(msg){
  1510. this.showErr();
  1511. el=this.getElement("errorTxt");
  1512. el.style.color="red";
  1513. el.value+="\n"+msg;
  1514. }
  1515. //**************************************************
  1516. //
  1517. crfVisit.onRemoveCRF=
  1518. function(){
  1519. let fName='[onRemoveCRF]';
  1520. let config=this.config;
  1521. config.inputListsIterator=0;
  1522. this.print(fName+' starting loop');
  1523. //let rd=function(data){redirect();};
  1524. //let cb=function(){cvInsertRows('lists','crfStatus',[crfStatus],rd,getContainer('data'));};
  1525. let that=this;
  1526. let action=function(){that.redirect();};
  1527. let cb=function(){that.removeCrfEntries(action);};
  1528. this.removeCRFLoop(cb);
  1529. }
  1530. crfVisit.removeCRFLoop=
  1531. function(cb){
  1532. let fName='[removeCRFLoop()]';
  1533. let config=this.config;
  1534. let that=this;
  1535. let i=config.inputListsIterator;
  1536. let iMax=config.formConfig.inputLists.rows.length;
  1537. //in fact, we are adding two additional passages of the loop, one for
  1538. //crfEntry, the second for the same query, but using parentCrf as the
  1539. //selection variable
  1540. //let iTotal=iMax+1;
  1541. //let iTotal=iMax+1;
  1542. //
  1543. let actionSettings=config.formConfig.actionSettings['onRemoveCRF'];
  1544. let queryNameDeleteWithParentCrf='NONE';
  1545. if (variableList.hasVariable(actionSettings,'removeWithParentCrf')){
  1546. queryNameDeleteWithParentCrf=actionSettings['removeWithParentCrf'];
  1547. }
  1548. this.print(fName+" ["+i+"/"+iMax+"]");
  1549. if (!(i<iMax)){
  1550. cb();
  1551. return;
  1552. }
  1553. //in all but crfEntry, variable is called crfRef
  1554. let queryName=config.formConfig.inputLists.rows[i].queryName;
  1555. let idVar="crfRef";
  1556. let idValue=config.formConfig.crfEntry['entryId'];
  1557. //delete also crfEntries where parentCrf is set to crf that we are deleting
  1558. if (queryNameDeleteWithParentCrf==queryName){
  1559. idValue=config.formConfig.crfEntry['parentCrf'];
  1560. }
  1561. this.print(fName+" ["+i+"/"+iMax+"] "+queryName+":"+idVar+'/'+idValue);
  1562. let filters=[LABKEY.Filter.create(idVar,idValue)];
  1563. let action=function(data){that.removeListCRF(data,cb);};
  1564. let failure=function(errorInfo){that.skipListCRF(errorInfo,cb);};
  1565. this.selectRows('lists',queryName,filters,action,this.getContainer('data'),failure);
  1566. //selectRows.failure=skipListCRF;
  1567. }
  1568. crfVisit.removeListCRF=
  1569. function(data,cb){
  1570. let fName="[removeListCRF]";
  1571. let config=this.config;
  1572. let that=this;
  1573. this.print(fName+" "+data.queryName+": "+data.rows.length);
  1574. config.inputListsIterator+=1;
  1575. if (data.rows.length==0){
  1576. this.removeCRFLoop(cb);
  1577. return;
  1578. }
  1579. let action=function(data){that.removeCRFLoop(cb)};
  1580. this.deleteRows(data.schemaName,data.queryName,data.rows,action,this.getContainer('data'));
  1581. }
  1582. crfVisit.skipListCRF=
  1583. function(errorInfo,cb){
  1584. let fName='[skipListCRF]';
  1585. this.print(fName+" error in removeCRF: "+errorInfo.exception);
  1586. let config=this.config;
  1587. config.inputListsIterator+=1;
  1588. this.removeCRFLoop(cb);
  1589. }
  1590. crfVisit.removeCrfEntries=
  1591. function(cb){
  1592. let queryName="crfEntry";
  1593. let idVar="entryId";
  1594. let crfRef=this.getCRFref();
  1595. let that=this;
  1596. let filters=[LABKEY.Filter.create('entryId',crfRef)];
  1597. let action=function(data){that.deleteAndUpdateCrfStatus(data,null);}
  1598. this.selectRows('lists',queryName,filters,action,this.getContainer('CRF'));
  1599. let action1=function(data){that.deleteAndUpdateCrfStatus(data,cb);}
  1600. let filters1=[LABKEY.Filter.create('parentCrf',crfRef)];
  1601. this.selectRows('lists',queryName,filters1,action1,this.getContainer('CRF'));
  1602. }
  1603. crfVisit.deleteAndUpdateCrfStatus=
  1604. function(data,cb){
  1605. let fName='[deleteAndUpdateCrfStatus]';
  1606. let config=this.config;
  1607. let that=this;
  1608. let rows=data.rows;
  1609. let stack=new Array();
  1610. stack.push(cb);
  1611. for (let i=0;i<rows.length;i++){
  1612. //generate crfStatus entry out of crfEntry
  1613. let crfStatus=this.createCrfStatus(rows[i]);
  1614. crfStatus.action='onRemoveCRF';
  1615. crfStatus.FormStatus=config.formConfig.targetStatus[crfStatus.action];
  1616. this.print(fName+' status '+crfStatus.FormStatus);
  1617. crfStatus.operator=config.formConfig.operator;
  1618. let k=stack.length-1;
  1619. let containerPath=this.getContainer('CRF');
  1620. stack.push(function(){that.insertRows('lists','crfStatus',[crfStatus],stack[k],containerPath);});
  1621. let k1=k+1;
  1622. stack.push(function(){that.deleteRows('lists','crfEntry',[rows[i]],stack[k1],containerPath);});
  1623. }
  1624. //execute the whole stack
  1625. let m=stack.length-1;
  1626. stack[m]();
  1627. }
  1628. crfVisit.redirect=
  1629. function(){
  1630. let debug=false;
  1631. let formUrl="begin";
  1632. let params=new Object();
  1633. params.name=formUrl;
  1634. params.pageId="CRF";
  1635. //points to crf container
  1636. let containerPath=this.getContainer('CRF');
  1637. // This changes the page after building the URL.
  1638. //Note that the wiki page destination name is set in params.
  1639. var homeURL = LABKEY.ActionURL.buildURL(
  1640. "project", formUrl , containerPath, params);
  1641. this.print("Redirecting to "+homeURL);
  1642. if (debug) return;
  1643. window.location = homeURL;
  1644. }
  1645. //master section, entry point from html files
  1646. crfVisit.generateMasterForm=
  1647. function(){
  1648. let that=this;
  1649. let action=function(){that.setFormConfig();}
  1650. this.init(action);
  1651. }
  1652. //helper function to set basic parameters on web page
  1653. //(fields defined in html file)
  1654. crfVisit.populateBasicData=
  1655. function(){
  1656. let staticData=new Object();
  1657. let titles=new Object();
  1658. let config=this.config;
  1659. staticData['version']=config.formConfig.softwareVersion;
  1660. titles['version']='Software version';
  1661. let varRows=config.formConfig['crfStaticVariables'].rows;
  1662. for (let i=0;i<varRows.length;i++){
  1663. let vName=varRows[i].staticVariable;
  1664. let val=config.formConfig.crfEntry[vName];
  1665. if (val==undefined) continue;
  1666. staticData[vName]=val;
  1667. titles[vName]=varRows[i].Title;
  1668. }
  1669. staticData['investigatorName']=config.formConfig.user['DisplayName'];
  1670. titles['investigatorName']='Investigator';
  1671. staticData['email']=config.formConfig.user['Email'];
  1672. titles['email']='Email';
  1673. staticData['siteName']=config.formConfig.currentSite['siteName'];
  1674. titles['siteName']='Site';
  1675. staticData['sitePhone']=config.formConfig.currentSite['sitePhone'];
  1676. titles['sitePhone']='Telephone(site)';
  1677. for (f in staticData){
  1678. this.addStaticData(f,titles[f],staticData[f]);
  1679. }
  1680. }
  1681. crfVisit.addStaticData=
  1682. function(f,title,value){
  1683. let el=this.getElement(f);
  1684. //populate only
  1685. if (el!=undefined){
  1686. el.innerText=value;
  1687. return;
  1688. }
  1689. //add row to table if element cannot be found
  1690. let table=this.getElement('staticTable');
  1691. let row=table.insertRow();
  1692. let cell=row.insertCell();
  1693. cell.innerText=title;
  1694. let cell1=row.insertCell();
  1695. cell1.id=f;
  1696. cell1.style.fontWeight='bold';
  1697. //populate
  1698. cell1.innerText=value;
  1699. }
  1700. //come here after the layout is read from labkey page
  1701. //
  1702. crfVisit.generateErrorMsg=
  1703. function(msg){
  1704. let config=this.config;
  1705. let txt=config.document.createElement('p');
  1706. txt.innerText=msg;
  1707. this.getElement(config.masterForm).appendChild(txt);
  1708. this.generateButton("submitDiv",'Exit','Exit','redirect');
  1709. }
  1710. crfVisit.getUser=
  1711. function(id,field){
  1712. let config=this.config;
  1713. if (field in config.formConfig) return config.formConfig[field];
  1714. let uRows=config.formConfig.userRows;
  1715. for (let i=0;i<uRows.length;i++){
  1716. let userId=uRows[i].UserId;
  1717. if (userId!=id) continue;
  1718. config.formConfig[field]=uRows[i];
  1719. return config.formConfig[field];
  1720. }
  1721. return null;
  1722. }
  1723. crfVisit.afterConfig=
  1724. function(){
  1725. let fName='[afterConfig]';
  1726. let config=this.config;
  1727. this.print(fName);
  1728. this.populateBasicData();
  1729. //check if user has permission on the form
  1730. let currentUser=this.getUser(LABKEY.Security.currentUser.id,'currentUser');
  1731. let currentSite=config.formConfig.currentSite;
  1732. let formCreator=this.getUser(config.formConfig.crfEntry.UserId,'formCreator');
  1733. let formCreatorId=formCreator.UserId;
  1734. //let formSite=config.formConfig.crfEntry.Site;
  1735. let fList=config.formConfig.operator+'s';
  1736. let fRows=config.formConfig[fList];
  1737. //let currentSiteId=-1;
  1738. let operatorSites=new Array();
  1739. for (let i=0;i<fRows.length;i++){
  1740. if (fRows[i].User!=currentUser.UserId) continue;
  1741. operatorSites.push(fRows[i].Site);
  1742. }
  1743. //depending on operator mode, we should decide what is right
  1744. let operator=config.formConfig.operator;
  1745. if (operator=='crfEditor'){
  1746. //editor can only edit its own forms
  1747. if (currentUser.UserId!=formCreatorId){
  1748. if ("allowFormReassignment" in config.formConfig.settings){
  1749. if (!operatorSites.includes(currentSite.siteNumber)){
  1750. let msg='User '+currentUser.DisplayName;
  1751. msg+=' has no permission for site '+currentSite.siteName;
  1752. this.generateErrorMsg(msg);
  1753. return;
  1754. }
  1755. let that=this;
  1756. let action=new Object();
  1757. action.name="formReassignement";
  1758. action.cb=function(){that.doNothing();}
  1759. config.formConfig.crfEntry['UserId']=currentUser.UserId;
  1760. let status=config.formConfig.crfEntry['FormStatus'];
  1761. this.updateFlag(status,action);
  1762. }
  1763. else{
  1764. let msg='User '+currentUser.DisplayName;
  1765. msg+=' has no permission on this form';
  1766. this.generateErrorMsg(msg);
  1767. return;
  1768. }
  1769. }
  1770. }
  1771. if (operator=='crfMonitor' || operator=='crfSponsor'){
  1772. //monitor can look at forms based on his site
  1773. //find monitor line
  1774. this.print('operator Site: '+operatorSites.length);
  1775. if (operatorSites.length==0){
  1776. let msg='User '+currentUser.DisplayName;
  1777. msg+=' is not a '+operator;
  1778. this.generateErrorMsg(msg);
  1779. return;
  1780. }
  1781. let selectedSite=-1;
  1782. let siteCandidates="[";
  1783. for (let i=0;i<operatorSites.length;i++){
  1784. if (i>0) siteCandidates+=", ";
  1785. siteCandidates+=operatorSites[i];
  1786. if (operatorSites[i]!=currentSite.siteNumber) continue;
  1787. selectedSite=currentSite.siteNumber;
  1788. break;
  1789. }
  1790. siteCandidates+="]";
  1791. if (selectedSite==-1){
  1792. let msg='User '+currentUser.DisplayName;
  1793. msg+=' is not a '+operator+' for site ';
  1794. msg+=currentSite.siteName+'('+currentSite.siteNumber+')';
  1795. msg+='/'+siteCandidates;
  1796. this.generateErrorMsg(msg);
  1797. return;
  1798. }
  1799. }
  1800. this.print('User '+currentUser.DisplayName+'/'+
  1801. config.formConfig.currentSite['siteName']+
  1802. ' acting as '+config.formConfig.operator);
  1803. let rows=config.formConfig.crfButtons.rows;
  1804. config.formConfig.targetStatus=new Array();
  1805. config.formConfig.targetRecipient=new Array();
  1806. config.formConfig.actionSettings=new Array();
  1807. for (let i=0; i<rows.length; i++){
  1808. let action=rows[i].action;//String
  1809. let tstatus=rows[i].targetFormStatus;
  1810. let trecip=rows[i].targetRecipient;
  1811. config.formConfig.targetStatus[action]=tstatus;
  1812. config.formConfig.targetRecipient[action]=trecip;
  1813. //allow for settings to be promoted with each action (and potentially parsed and acted upon)
  1814. config.formConfig.actionSettings[action]=undefined;
  1815. let aSet=rows[i].actionSettings;
  1816. if (aSet){
  1817. config.formConfig.actionSettings[action]=variableList.parseVariables(aSet);
  1818. variableList.printVariables(this,config.formConfig.actionSettings[action]);
  1819. }
  1820. }
  1821. let formStatus=config.formConfig.formStatus;
  1822. //let functionArray=new Array();
  1823. this.print("Generating buttons for formStatus \""+ formStatus+"\"");
  1824. let allButtonRows=config.formConfig.crfButtons.rows;
  1825. let buttonRows=new Array();
  1826. //specifying role=X in actionSettings will limit button to that role
  1827. for (let i=0;i<allButtonRows.length;i++){
  1828. let action=allButtonRows[i]['action'];
  1829. //filter on actionSettings
  1830. let as=config.formConfig.actionSettings[action];
  1831. if (variableList.hasVariable(as,'role')){
  1832. this.print('Role['+config.formConfig.operator+'/'+as['role']+'] limited for action '+action);
  1833. //mismatch skips addition of button to buttonRows
  1834. if (config.formConfig.operator!=as['role']) continue;
  1835. }
  1836. buttonRows.push(allButtonRows[i]);
  1837. }
  1838. for (let i=0;i<buttonRows.length;i++){
  1839. let bt=buttonRows[i];
  1840. //if (typeof window[bt.action]==="function"){
  1841. this.generateButton("submitDiv",bt.caption,bt.label,bt.action,null);
  1842. //}
  1843. //else{
  1844. // this.print('No match for function :'+bt.action+
  1845. // ' obj: '+window[bt.action]);
  1846. //}
  1847. }
  1848. this.print('Here');
  1849. //here we should get data. For now, just initialize objects that will hold data
  1850. let that=this;
  1851. let action=function(){that.afterDataLayout();};
  1852. this.setDataLayout(action);//callback is afterDataLayout
  1853. }
  1854. crfVisit.afterDataLayout=
  1855. function(){
  1856. let that=this;
  1857. let action=function(){that.afterData();};
  1858. //let action=function(){that.doNothing();};
  1859. this.setData(action);//callback is afterData
  1860. }
  1861. crfVisit.updateRegistration=
  1862. function(){
  1863. let fName="[updateRegistration]";
  1864. let config=this.config;
  1865. this.print(fName);
  1866. let pM=this.getIdManager();
  1867. let idFieldName=participantIdManager.getCrfEntryFieldName(pM,"STUDY");
  1868. //have to reload query data
  1869. let regQueryPars=variableList.parseVariables(config.formConfig.settings['registrationQuery']);
  1870. let regQuery=regQueryPars['query'];
  1871. let fQuery=this.getQuerySnapshot(regQuery);
  1872. if (fQuery.rows.length==0) {
  1873. this.print(fName+" registration is empty");
  1874. return; //registration is empty
  1875. }
  1876. let regEntry=fQuery.rows[0];
  1877. for (x in regEntry){
  1878. this.print(fName+" ["+x+"] "+regEntry[x]);
  1879. }
  1880. let studyId=fQuery.rows[0][idFieldName];
  1881. if (!studyId) {
  1882. this.print(fName+" study id not set ("+idFieldName+'/'+studyId+")");
  1883. return; //study id not set
  1884. }
  1885. //set
  1886. participantIdManager.setParticipantIdToCrfEntry(pM,studyId,"STUDY");
  1887. //this will only update crfEntry in memory, but not on LabKey,
  1888. //we are counting on updateFlag to follow updateRegistration
  1889. //update parentCRF as well, here we schedule update of data entry as well
  1890. if ("parentCrfData" in config.formConfig){
  1891. let parentCrfEntry=config.formConfig.parentCrfData.rows[0];
  1892. parentCrfEntry[idFieldName]=studyId;
  1893. let that=this;
  1894. let action={name:"updateRegistration",cb:function(){that.doNothing();}};
  1895. let cb=function(data){that.completeWithFlag(data,action);};
  1896. this.modifyRows('update','lists','crfEntry',[parentCrfEntry],cb,this.getContainer('CRF'));
  1897. }
  1898. }
  1899. crfVisit.afterData=
  1900. function(){
  1901. let fName='afterData';
  1902. let config=this.config;
  1903. //operatorBasedAccessMode
  1904. let accessMode=config.formConfig.operator+'Mode';
  1905. let rowsSetup=config.formConfig.formSetupRows;
  1906. let idMode=config.formConfig.form['idMode'];
  1907. //set default value if no value is in the list (read value is null)
  1908. if (!idMode) idMode="STUDY:EDIT";
  1909. this.print(fName+': idMode '+idMode);
  1910. //add print to config so participantManager can use it
  1911. let pM=this.getIdManager();
  1912. //extend object
  1913. let that=this;
  1914. let action=new Object();
  1915. action.name='updateCrfEntry';
  1916. action.cb=function(){that.doNothing();};
  1917. pM.updateCrfEntry=function(){that.updateFlag(config.formConfig.crfEntry['FormStatus'],action);};
  1918. let idModeArray=idMode.split(':');
  1919. pM.mode="STUDY";
  1920. if (idModeArray.includes("LOCAL")) {
  1921. pM.mode="LOCAL";
  1922. //OK, but check if CRF or registration indicate that study id is already set
  1923. participantIdManager.verifyCrfStudyId(pM);
  1924. //study id should already be set by updateRegistration
  1925. //verifyRegistration(pM);
  1926. }
  1927. if (idModeArray.includes("READONLY")){
  1928. pM.readOnly="TRUE";
  1929. }
  1930. let pId=participantIdManager.getParticipantIdFromCrfEntry(pM);
  1931. if (!pId){
  1932. participantIdManager.setEditMode(pM);
  1933. }
  1934. else{
  1935. let label=pId;
  1936. if (pM.mode=="STUDY"){
  1937. let loc=participantIdManager.getParticipantIdFromCrfEntry(pM,'LOCAL');
  1938. label=pId+':'+loc;
  1939. pM.readOnly="true";
  1940. }
  1941. participantIdManager.setLabelMode(pM,label);
  1942. //in STUDY mode also change LOCAL ID from crfEntry
  1943. }
  1944. for (let i=0;i<rowsSetup.length;i++){
  1945. let entry=rowsSetup[i];
  1946. let queryName=config.formConfig.queryMap[entry['queryName']];
  1947. this.print(fName+" ["+queryName+"]: showFlag: "+entry["showFlag"]);
  1948. this.print(fName+" ["+queryName+"]: accessMode: "+entry[accessMode]);
  1949. const nData=this.getQuerySnapshot(queryName).rows.length;
  1950. this.print(fName+" ["+queryName+"]: nData: "+nData);
  1951. //skip sections
  1952. //also from fields
  1953. if (entry[accessMode]=="NONE") continue;
  1954. //skip readonly empty records
  1955. //if (entry[accessMode]=="READ" && nData==0) continue;
  1956. //let additionalData=new Object();
  1957. //setAdditionalData(additionalData,entry);
  1958. //section fits one dataset/list
  1959. this.generateSection(entry);
  1960. //generateSection(queryName,entry["title"],entry[accessMode],
  1961. // additionalData);
  1962. }
  1963. }
  1964. crfVisit.findSetupRow=
  1965. function(queryName,formId){
  1966. let config=this.config;
  1967. let rowsSetup=config.formConfig.formSetupRows;
  1968. for (let i=0;i<rowsSetup.length;i++){
  1969. let e=rowsSetup[i];
  1970. let queryName1=config.formConfig.queryMap[e['queryName']];
  1971. if (e.formName!=formId) continue;
  1972. if (queryName1!=queryName) continue;
  1973. return e;
  1974. }
  1975. return null;
  1976. }
  1977. crfVisit.populateSection=
  1978. function(queryName){
  1979. let fName='[populateSection/'+queryName+']';
  1980. let config=this.config;
  1981. this.print(fName);
  1982. //old setting
  1983. let formId=config.formId;
  1984. //new setting
  1985. formId=config.formConfig.formId;
  1986. let entry=this.findSetupRow(queryName,formId);
  1987. //ignore names without associated entry in formSetup
  1988. if (entry==undefined){
  1989. this.print(fName+': no matching FormSetup entry found');
  1990. return;
  1991. }
  1992. //populate comes after generate, we should be pretty safe in taking
  1993. //already generated additionalData
  1994. if (!(queryName in config.formConfig.additionalData)){
  1995. this.print(fName+': no additionalData generated for '+queryName);
  1996. return;
  1997. }
  1998. let additionalData=config.formConfig.additionalData[queryName];
  1999. this.print(fName+': using additionalData '+additionalData);
  2000. if ("isReview" in additionalData){
  2001. let action=function(){crfReviewSection.CB();};
  2002. crfReviewSection.generateSection(queryName,queryName,action);
  2003. return;
  2004. }
  2005. let accessMode=config.formConfig.operator+'Mode';
  2006. let aM=entry[accessMode];
  2007. this.print(fName+': accessMode '+aM);
  2008. if (aM!='GENERATE'){
  2009. let writeMode=entry[accessMode]=='EDIT';
  2010. this.print(fName+': mode='+writeMode);
  2011. this.populateTable(queryName,writeMode);
  2012. return;
  2013. }
  2014. //deal with generate
  2015. //
  2016. //already available -> shift to READ mode
  2017. let divTable=queryName+'Table';
  2018. let divObj=this.getElement(divTable);
  2019. let divRev=this.getElement(queryName+'Review');
  2020. let divRLi=this.getElement(queryName+'ReviewList');
  2021. let divGBu=this.getElement(queryName+'GenerateButton');
  2022. this.print('div GBU: '+divGBu);
  2023. divObj.style.display="block";
  2024. divRev.style.display="block";
  2025. divRLi.style.display="block";
  2026. if (divGBu!=undefined) divGBu.style.display="none";
  2027. let nData=this.getQuerySnapshot(queryName).rows.length;
  2028. this.print('['+queryName+']: nrows '+nData);
  2029. if (nData>0){
  2030. this.populateTable(queryName,0);
  2031. return;
  2032. }
  2033. //hide table
  2034. divObj.style.display="none";
  2035. divRev.style.display="none";
  2036. divRLi.style.display="none";
  2037. if (divGBu!=undefined) divGBu.style.display="block";
  2038. //add buttons?
  2039. //is button already generated?
  2040. //populateTable(entry);
  2041. }
  2042. //******* generateQuery infrastructure *********************
  2043. crfVisit.onGenerateQuery=
  2044. function(queryName){
  2045. let fName='[onGenerateQuery]';
  2046. this.print(fName+' '+queryName);
  2047. //
  2048. let config=this.config;
  2049. let cfgRows=config.formConfig.generateConfigData.rows;
  2050. // //queryName to queryId?
  2051. let queryId=config.formConfig.fields[queryName].queryId;
  2052. let cfgRow=undefined;
  2053. for (let i=0;i<cfgRows.length;i++){
  2054. if (cfgRows[i].queryId!=queryId) continue;
  2055. cfgRow=cfgRows[i];
  2056. break;
  2057. }
  2058. if (cfgRow==undefined){
  2059. this.print('generateConfig for queryName['+queryId+']='+queryName+' not found');
  2060. return;
  2061. }
  2062. //add config to the list
  2063. if (!("generateConfig" in config.formConfig)){
  2064. config.formConfig.generateConfig=new Object();
  2065. }
  2066. config.formConfig.generateConfig[queryName]=cfgRow;
  2067. if (!("generateForm" in config.formConfig)){
  2068. config.formConfig.generateForm=new Object();
  2069. }
  2070. //
  2071. let formRows=config.formConfig.formRows;
  2072. let formId=cfgRow.formId;
  2073. for (let i=0;i<formRows.length;i++){
  2074. if (formRows[i].Key==formId) {
  2075. config.formConfig.generateForm[queryName]=formRows[i];
  2076. break;
  2077. }
  2078. }
  2079. //this.print('XcfgRow '+config.formConfig.generateForm[queryName]);
  2080. //
  2081. // //check if all required datasets were at least saved
  2082. this.checkGenerationFields(queryName);
  2083. }
  2084. crfVisit.checkGenerationFields=
  2085. function(queryName){
  2086. let fName='[checkGenerationFields]';
  2087. let config=this.config;
  2088. let genForm=config.formConfig.generateForm[queryName];
  2089. let genCfg=config.formConfig.generateConfig[queryName];
  2090. let mailRecipient=genCfg.emailRecipient;
  2091. //list of queries that are part of Registration form
  2092. this.print(fName);
  2093. this.print(fName+' setRecipient: '+mailRecipient);
  2094. let formId=genForm.Key;
  2095. this.print(fName+" Checking form w/id "+formId);
  2096. let selectGenerationRows=this.selectFormSetupRows(formId);
  2097. //registration rows
  2098. for (let i=0;i<selectGenerationRows.length;i++){
  2099. let row=selectGenerationRows[i];
  2100. let queryId=row.queryName;
  2101. let fQueryName=config.formConfig.queryMap[queryId];
  2102. if (fQueryName==queryName) continue;
  2103. let fQuery=this.getQuerySnapshot(fQueryName);
  2104. this.print('Checking '+fQueryName+' nrows: '+fQuery.rows.length);
  2105. if (fQuery.rows.length==0){
  2106. this.generateError(queryName,fQueryName);
  2107. return;
  2108. }
  2109. }
  2110. this.generateMessage(queryName,'Vailidation OK');
  2111. this.print('callback: set recipient: '+mailRecipient);
  2112. let that=this;
  2113. let cb=function(){that.prepareForm(queryName,formId,mailRecipient);};
  2114. this.generateListEntry(formId,queryName,cb);
  2115. }
  2116. crfVisit.prepareForm=
  2117. function(queryName,formId,mailRecipient){
  2118. let fName="[prepareForm]";
  2119. this.print(fName+' recipient '+mailRecipient);
  2120. //look for existing registration entry
  2121. let that=this;
  2122. let action=function(data){that.generateForm(data,queryName,mailRecipient);};
  2123. let formFilter=LABKEY.Filter.create('Form',formId);
  2124. let parentCrfFilter=LABKEY.Filter.create('parentCrf',this.getCRFref());
  2125. let filters=[formFilter,parentCrfFilter];
  2126. this.selectRows('lists','crfEntry',filters,action,this.getContainer('data'));
  2127. }
  2128. crfVisit.generateError=
  2129. function(queryName,fQueryName){
  2130. let elName=queryName+'GenerateButton'+'_reportField';
  2131. let el=this.getElement(elName);
  2132. el.innerText='Error: '+fQueryName+' was not set';
  2133. el.style.color='red';
  2134. }
  2135. crfVisit.generateMessage=
  2136. function(queryName,msg){
  2137. let elName=queryName+'GenerateButton'+'_reportField';
  2138. let el=this.getElement(elName);
  2139. el.innerText=msg;
  2140. el.style.color='green';
  2141. }
  2142. crfVisit.generateForm=
  2143. function(data,queryName,mailRecipient){
  2144. let fName='[generateForm]';
  2145. this.print(fName+' recipient: '+mailRecipient);
  2146. //
  2147. const nData=data.rows.length;
  2148. this.print(fName+' Registration: '+nData+' rows');
  2149. let config=this.config;
  2150. let formRow=config.formConfig.generateForm[queryName];
  2151. let formCfg=config.formConfig.generateConfig[queryName];
  2152. //we have to generate masterQuery with parentCrf and crfRef
  2153. //and crfEntry with new entryId and parentCrf equal to crfRef
  2154. if (nData>0) {
  2155. this.generateMessage(queryName,'Registration already generated.');
  2156. return;
  2157. }
  2158. let formId=formRow.Key;
  2159. let formName=formRow.formName;
  2160. let crfBase=config.formConfig.crfEntry;
  2161. let crfEntry=new Object();
  2162. //add new reference
  2163. crfEntry.entryId=Date.now();
  2164. crfEntry.parentCrf=this.getCRFref();
  2165. crfEntry["Date"]=new Date();
  2166. crfEntry["View"]="[VIEW]";
  2167. crfEntry.formStatus=1;//In progress
  2168. //checks for both field presence (if not in query, undefined) and field value (if not set, null)
  2169. this.print(fName+' setup status: '+formCfg.formStatus);
  2170. if (formCfg.formStatus){
  2171. crfEntry.formStatus=formCfg.formStatus;
  2172. }
  2173. //get local Id
  2174. let pM=this.getIdManager();
  2175. crfEntry[participantIdManager.getCrfEntryFieldName(pM)]=participantIdManager.getParticipantIdFromCrfEntry(pM);
  2176. // //set other variables
  2177. //requires studyData as part of formConfig
  2178. // let studyData=config.formConfig.studyData;
  2179. this.print('Adding study: '+crfBase.EudraCTNumber);
  2180. crfEntry.EudraCTNumber=crfBase.EudraCTNumber;
  2181. crfEntry.StudyCoordinator=crfBase.StudyCoordinator;
  2182. crfEntry.StudySponsor=crfBase.StudySponsor;
  2183. crfEntry.RegulatoryNumber=crfBase.RegulatoryNumber;
  2184. //
  2185. // //find sponsor for site
  2186. let site=crfBase.Site;
  2187. let crfSponsors=config.formConfig.crfSponsors;
  2188. let users=config.formConfig.userRows;
  2189. for (let i=0;i<crfSponsors.length;i++){
  2190. //this.print('Checking for site '+crfSponsors[i].Site);
  2191. if (crfSponsors[i].Site!=site) continue;
  2192. config.formConfig.sponsorId=crfSponsors[i].User;
  2193. //this.print('Setting id '+config.formConfig.sponsorId);
  2194. //finds first
  2195. break;
  2196. }
  2197. for (let j=0;j<users.length;j++){
  2198. if (config.formConfig.sponsorId!=users[j].UserId) continue;
  2199. config.formConfig.sponsor=users[j];
  2200. //finds first (should be unique)
  2201. break;
  2202. }
  2203. this.print('Selecting '+config.formConfig.sponsor.DisplayName+' as sponsor');
  2204. //different user than the original form...
  2205. //should be set to the study sponsor
  2206. crfEntry.UserId=config.formConfig.sponsor.UserId;
  2207. crfEntry.Site=site;
  2208. // //set formId to one found through registration search
  2209. crfEntry.Form=formId;
  2210. ////
  2211. let crfStatus=this.createCrfStatus(crfEntry);
  2212. crfStatus.operator=config.formConfig.operator;
  2213. crfStatus.action='generateForm';
  2214. let that=this;
  2215. let action=function(){that.doNothing();};
  2216. let cb=function(data){that.sendEmail(data,mailRecipient,action,formName+' generated');}
  2217. let containerPath=this.getContainer('data');
  2218. let pass=function(data){that.insertRows('lists','crfStatus',[crfStatus],cb,containerPath);};
  2219. this.insertRows('lists','crfEntry',[crfEntry],pass,this.getContainer('data'));
  2220. }
  2221. crfVisit.generateListEntry=
  2222. function(formId,queryName,cb){
  2223. //check if registration was already generated
  2224. let config=this.config;
  2225. let formRows=config.formConfig.formRows;
  2226. let qForm=undefined;
  2227. for (let i=0;i<formRows.length;i++){
  2228. if (formRows[i].Key!=formId) continue;
  2229. qForm=formRows[i];
  2230. }
  2231. let nData=this.getQuerySnapshot(queryName).rows.length;
  2232. if (nData>0) return;
  2233. //create new list entry
  2234. let pM=this.getIdManager();
  2235. let e2=new Object();
  2236. e2.crfRef=this.getCRFref();
  2237. e2.registrationStatus=0;
  2238. e2.submissionDate=new Date();
  2239. e2[participantIdManager.getCrfEntryFieldName(pM)]=participantIdManager.getParticipantIdFromCrfEntry(pM);
  2240. this.print('set values');
  2241. this.insertRows('lists',queryName,[e2],cb,this.getContainer('data'));
  2242. }
  2243. // ******************** end form generator (Registration) ********************
  2244. //jump to populate table/generate review, etc defined at the begining of the file
  2245. //entry point from generateMasterForm
  2246. crfVisit.setFormConfig=
  2247. function(){
  2248. let fName="[setFormConfig]";
  2249. let config=this.config;
  2250. //add object to store form related data
  2251. config.formConfig=new Object();
  2252. config.formConfig.softwareVersion='T.15.68';
  2253. this.print(fName+" generateMasterForm");
  2254. //set containers for data and configuration
  2255. //TODO: set this from a query
  2256. //
  2257. this.setContainer('data',LABKEY.ActionURL.getContainer());
  2258. this.setContainer('config',LABKEY.ActionURL.getContainer());
  2259. this.setContainer('CRF',LABKEY.ActionURL.getContainer());
  2260. //this is local data
  2261. let that=this;
  2262. let action=function(data){that.afterSettings(data);};
  2263. this.selectRows('lists','crfSettings',[],action,this.getContainer('CRF'));
  2264. //store form related data to this object
  2265. }
  2266. crfVisit.afterSettings=
  2267. function(data){
  2268. let fName='[afterSettings]';
  2269. let config=this.config;
  2270. config.formConfig.settings=variableList.convertToDictionary(data.rows);
  2271. let st=config.formConfig.settings;
  2272. this.print('afterSettings');
  2273. for (let k in st){
  2274. this.print(fName+'\t'+k+'='+st[k]);
  2275. }
  2276. //if ('dataContainer' in st){
  2277. // setContainer('data',st['dataContainer']);
  2278. //}
  2279. let vname='configContainer';
  2280. if (vname in st){
  2281. this.setContainer('config',st[vname]);
  2282. }
  2283. this.print('Config: '+this.getContainer('config'));
  2284. this.print('Data: '+this.getContainer('data'));
  2285. //use first-> we must first establish link to the rigth crf entry
  2286. let filters=[LABKEY.Filter.create('entryId',this.getCRFrefFirst())];
  2287. let that=this;
  2288. let action=function(data){that.afterCRFEntry(data);};
  2289. this.selectRows('lists','crfEntry',filters,action,this.getContainer('data'));
  2290. }
  2291. crfVisit.afterCRFEntry=
  2292. function(data){
  2293. let config=this.config;
  2294. let fName='[afterCRFEntry]';
  2295. config.formConfig.crfEntry=data.rows[0];
  2296. this.print("Setting crfEntry (x) to "+config.formConfig.crfEntry["entryId"]);
  2297. //for empty records or those with parentCrf not set, parentCrf comes up as null
  2298. //nevertheless, with two equal signs, check against undefined also works
  2299. this.print('parentCrf set to '+config.formConfig.crfEntry.parentCrf);
  2300. this.collectData();
  2301. }
  2302. crfVisit.collectData=
  2303. function(){
  2304. let config=this.config;
  2305. let targetObject=config.formConfig;
  2306. let queryArray=new Array();
  2307. //k
  2308. //site
  2309. queryArray.push(runQuery.makeQuery(targetObject,'config','site','siteData',[]));
  2310. //users
  2311. queryArray.push(runQuery.makeQuery(targetObject,'CRF','users','userData',[]));
  2312. queryArray[queryArray.length-1].schemaName='core';
  2313. //crfEditors
  2314. queryArray.push(runQuery.makeQuery(targetObject,'config','crfEditors','crfEditorsData',[]));
  2315. //crfMonitors
  2316. queryArray.push(runQuery.makeQuery(targetObject,'config','crfMonitors','crfMonitorsData',[]));
  2317. //crfSponsors
  2318. queryArray.push(runQuery.makeQuery(targetObject,'config','crfSponsors','crfSponsorsData',[]));
  2319. //crfManagers
  2320. queryArray.push(runQuery.makeQuery(targetObject,'config','crfManagers','crfManagersData',[]));
  2321. //study static data
  2322. queryArray.push(
  2323. runQuery.makeQuery(targetObject,'data','crfStaticVariables','crfStaticVariables',[]));
  2324. queryArray.push(runQuery.makeQuery(targetObject,'data','specialFields','specialFieldsQuery',[]));
  2325. //study
  2326. queryArray.push(runQuery.makeQuery(targetObject,'data','Study','studyDataAll1',[]));
  2327. let e=queryArray[queryArray.length-1];
  2328. //overload schema name
  2329. e.schemaName='study';
  2330. //make sure variables not part of default view are loaded
  2331. //here we should already have read crfStaticVariables table
  2332. e.columns="SubjectColumnName,EudraCTNumber,StudySponsor";
  2333. e.columns+=",StudyCoordinator,RegulatoryNumber";
  2334. //formStatus
  2335. let varLabel='sourceFormStatus';
  2336. let formStatus=config.formConfig.crfEntry['FormStatus'];
  2337. let formFilter=LABKEY.Filter.create('Key',formStatus);
  2338. queryArray.push(
  2339. runQuery.makeQuery(targetObject,'config','FormStatus','formStatusData',[formFilter]));
  2340. //crfButtons
  2341. let statusFilter=LABKEY.Filter.create(varLabel,formStatus);
  2342. queryArray.push(
  2343. runQuery.makeQuery(targetObject,'config','crfButtons','crfButtons',[statusFilter]));
  2344. //Forms
  2345. queryArray.push(runQuery.makeQuery(targetObject,'config','Forms','formData',[]));
  2346. //FormSetup
  2347. queryArray.push(runQuery.makeQuery(targetObject,'config','FormSetup','formSetup',[]));
  2348. //generateConfig
  2349. queryArray.push(
  2350. runQuery.makeQuery(targetObject,'config','generateConfig','generateConfigData',[]));
  2351. //inputLists
  2352. queryArray.push(
  2353. runQuery.makeQuery(targetObject,'config','inputLists','inputLists',[]));
  2354. //parentCrf
  2355. let parentCrf=config.formConfig.crfEntry['parentCrf'];
  2356. if (parentCrf!=undefined){
  2357. let crfFilter=LABKEY.Filter.create('entryId',parentCrf);
  2358. queryArray.push(runQuery.makeQuery(targetObject,'data','crfEntry','parentCrfData',[crfFilter]));
  2359. }
  2360. this.print('running getDataFromQueries');
  2361. let that=this;
  2362. //let action=function(data){that.doNothing();};
  2363. let action=function(){that.addStudyData();};
  2364. runQuery.getDataFromQueries(this,queryArray,action);
  2365. }
  2366. crfVisit.addStudyData=
  2367. function(){
  2368. let fName='addStudyData';
  2369. let config=this.config;
  2370. //convert specialFields to array
  2371. let q=config.formConfig["specialFieldsQuery"].rows;
  2372. config.formConfig.specialFields=variableList.convertToAssociatedArray(q,"fieldUID");
  2373. this.print(fName);
  2374. let queryArray=new Array();
  2375. let targetObject=config.formConfig;
  2376. //study
  2377. queryArray.push(runQuery.makeQuery(targetObject,'data','Study','studyDataAll',[]));
  2378. //queryArray.push(runQuery.makeQuery('data','Study','studyDataAll',[]));
  2379. let e=queryArray[queryArray.length-1];
  2380. //overload schema name
  2381. e.schemaName='study';
  2382. //make sure variables not part of default view are loaded
  2383. //here we should already have read crfStaticVariables table
  2384. let staticVarRows=config.formConfig['crfStaticVariables'].rows;
  2385. let columnModel=""
  2386. for (let i=0;i<staticVarRows.length;i++){
  2387. if (i>0) columnModel+=',';
  2388. columnModel+=staticVarRows[i]['staticVariable'];
  2389. }
  2390. e.columns=columnModel;
  2391. //also collect ids already in study
  2392. //registrationQuery should be a dataset
  2393. //since monitors can review late, it might be profitable to use lists
  2394. //rather than study
  2395. let regQueryPars=variableList.parseVariables(config.formConfig.settings['registrationQuery']);
  2396. let regQuery=regQueryPars['query'];
  2397. let regSchema='study';
  2398. if ('schema' in regQueryPars){
  2399. regSchema=regQueryPars['schema'];
  2400. }
  2401. queryArray.push(runQuery.makeQuery(targetObject,'data',regQuery,'registrationData',[]));
  2402. queryArray[queryArray.length-1].schemaName=regSchema;
  2403. let that=this;
  2404. let action=function(){that.fcontinue();};
  2405. runQuery.getDataFromQueries(this,queryArray,action);
  2406. }
  2407. crfVisit.fcontinue=
  2408. function(){
  2409. //debug
  2410. let fName='[fcontinue]';
  2411. let config=this.config;
  2412. let varRows=config.formConfig['crfStaticVariables'].rows;
  2413. let studyVars=config.formConfig['studyDataAll'].rows[0];
  2414. for (let i=0;i<varRows.length;i++){
  2415. let vName=varRows[i].staticVariable;
  2416. this.print(fName+' '+vName+': '+studyVars[vName]);
  2417. }
  2418. //parse site
  2419. config.formConfig.siteRows=config.formConfig.siteData.rows;
  2420. let sRows=config.formConfig.siteRows;
  2421. for (let i=0;i<sRows.length;i++){
  2422. let siteId=sRows[i].siteNumber;
  2423. this.print('site '+siteId);
  2424. if (siteId==config.formConfig.crfEntry.Site){
  2425. config.formConfig.currentSite=sRows[i];
  2426. break;
  2427. }
  2428. }
  2429. //config.formConfig.site=data.rows[0];
  2430. this.print("Setting site name to "+config.formConfig.currentSite.siteName);
  2431. //study
  2432. config.formConfig.studyData=config.formConfig.studyDataAll.rows[0];
  2433. this.print("XSetting participantField to "+
  2434. config.formConfig.studyData["SubjectColumnName"]);
  2435. config.formConfig.crfEditors=config.formConfig.crfEditorsData.rows;
  2436. config.formConfig.crfMonitors=config.formConfig.crfMonitorsData.rows;
  2437. config.formConfig.crfSponsors=config.formConfig.crfSponsorsData.rows;
  2438. config.formConfig.crfManagers=config.formConfig.crfManagersData.rows;
  2439. config.formConfig.userRows=config.formConfig.userData.rows;
  2440. let uRows=config.formConfig.userRows;
  2441. for (let i=0;i<uRows.length;i++){
  2442. let userId=uRows[i].UserId;
  2443. if (userId==config.formConfig.crfEntry.UserId){
  2444. config.formConfig.user=uRows[i];
  2445. break;
  2446. }
  2447. }
  2448. //config.formConfig.user=data.rows[0];
  2449. this.print("Setting user to "+config.formConfig.user["DisplayName"]);
  2450. let fsRows=config.formConfig.formStatusData.rows;
  2451. config.formConfig.formStatus=fsRows[0].formStatus;
  2452. config.formConfig.operator=config.role;
  2453. //config.formConfig.operator=fsRows[0].operator;
  2454. this.print('Setting operator to: '+config.formConfig.operator);
  2455. config.formConfig.formRows=config.formConfig.formData.rows;
  2456. //point formId to point to form set in crfEntry
  2457. config.formConfig.formId=config.formConfig.crfEntry['Form'];
  2458. //old setting, set from URL in visit.html
  2459. let formId=config.formId;
  2460. //new setting, set from crfEntry
  2461. formId=config.formConfig.formId;
  2462. let formRows=config.formConfig.formRows;
  2463. //filter out the current form
  2464. for (let i=0;i<formRows.length;i++){
  2465. if (formRows[i].Key==formId){
  2466. config.formConfig.form=formRows[i];
  2467. break;
  2468. }
  2469. }
  2470. config.formConfig.formSetupRows=this.selectFormSetupRows(formId);
  2471. this.print("Number of datasets for form ["+formId+"]: "+
  2472. config.formConfig.formSetupRows.length);
  2473. let fields=config.formConfig.formSetup.metaData.fields;
  2474. //get the lookup for queryName column
  2475. let formQueryName='queryName';
  2476. let field="NONE";
  2477. for (f in fields){
  2478. if (fields[f]['name']!=formQueryName) continue;
  2479. field=fields[f];
  2480. break;
  2481. }
  2482. let lookup=field.lookup;
  2483. this.print("Getting dataset names from "+lookup.queryName);
  2484. //inputLists should be in configuration container
  2485. let that=this;
  2486. let action=function(data){that.afterFormDatasets(data);};
  2487. //let action=function(data){that.doNothing();};
  2488. this.selectRows(lookup.schemaName,lookup.queryName,[],action,this.getContainer('config'));
  2489. }
  2490. crfVisit.afterFormDatasets=
  2491. function(data){
  2492. let fName='[afterFormDatasets]';
  2493. this.print(fName+' nrows '+data.rows.length);
  2494. let config=this.config;
  2495. config.formConfig.formDatasets=data;//inputLists
  2496. config.formConfig.fields=new Object();
  2497. config.formConfig.queryMap=new Object();
  2498. config.formConfig.additionalData=new Object();
  2499. let rows=config.formConfig.formSetupRows;
  2500. //should skip report only rows
  2501. for (let i=0;i<rows.length;i++){
  2502. let entry=rows[i];
  2503. let reviewField=(entry['showFlag']=='REVIEW');
  2504. //is the operator set yet?
  2505. let accessMode=config.formConfig.operator+'Mode';
  2506. let skipField=(entry[accessMode]=="NONE");
  2507. let queryId=entry['queryName'];
  2508. let lookupRows=config.formConfig.formDatasets.rows;
  2509. this.print('QueryID['+i+']='+queryId);
  2510. let dentry;
  2511. for (let j=0;j<lookupRows.length;j++){
  2512. if (queryId!=lookupRows[j]['Key']) continue;
  2513. dentry=lookupRows[j];
  2514. break;
  2515. }
  2516. let qName=dentry['queryName'];
  2517. //update list of dataset formConfig is observing (fields/queryMap)
  2518. while (1){
  2519. //review contains no data
  2520. if (reviewField) break;
  2521. if (skipField) break;
  2522. //already in fields
  2523. if (qName in config.formConfig.fields) break;
  2524. config.formConfig.fields[qName]=new Object();
  2525. break;
  2526. }
  2527. while(1){
  2528. //already done
  2529. if (queryId in config.formConfig.queryMap) break;
  2530. config.formConfig.queryMap[queryId]=qName;
  2531. break;
  2532. }
  2533. if (reviewField) continue;
  2534. if (skipField) continue;
  2535. //only do this for real lists
  2536. let field=config.formConfig.fields[qName];
  2537. field.title=entry['title'];
  2538. field.queryId=queryId;
  2539. }
  2540. this.print("List of datasets in form : ");
  2541. for (f in config.formConfig.fields){
  2542. let field=config.formConfig.fields[f];
  2543. this.print("\t"+f+" ID: "+field.queryId+' title '+field.title);
  2544. }
  2545. this.afterConfig();
  2546. }
  2547. //>>>>>>>>>>>>>>>>>new>>>>>>>>>>>>
  2548. crfVisit.setDataLayout=
  2549. function(cb){
  2550. let fName='[setDataLayout]';
  2551. let config=this.config;
  2552. this.print(fName);
  2553. let rowsSetup=config.formConfig.formSetupRows;
  2554. let queryArray=new Array();
  2555. let dS=this.getLayoutObject();//reference only
  2556. let qList=this.getQueryList();
  2557. let qMap=config.formConfig.queryMap;
  2558. //config.formConfig.lookup=new Object();
  2559. for (let i=0;i<rowsSetup.length;i++){
  2560. let entry=rowsSetup[i];
  2561. //skip review rows
  2562. if (entry['showFlag']=='REVIEW')
  2563. continue;
  2564. let queryId=entry['queryName'];
  2565. let q=qMap[queryId];
  2566. queryArray.push(runQuery.makeQuery(dS,'data',q,q,[]));
  2567. qList[q]=0;
  2568. this.print(fName+' adding '+q);
  2569. if (entry['showQuery']!="NONE"){
  2570. let sq=entry['showQuery'];
  2571. queryArray.push(runQuery.makeQuery(dS,'data',sq,sq,[]));
  2572. qList[sq]=0;
  2573. this.print(fName+' adding '+sq);
  2574. }
  2575. }
  2576. //always add reviews
  2577. let q='reviewComments';
  2578. queryArray.push(runQuery.makeQuery(dS,'data',q,q,[]));
  2579. qList[q]=0;
  2580. let that=this;
  2581. let action=function(){that.processLayout(cb);};
  2582. runQuery.getDataFromQueries(this,queryArray,action);
  2583. }
  2584. //this happens after the for loop, so all dataQueries objects are set
  2585. crfVisit.processLayout=
  2586. function(cb){
  2587. let fName='[processLayout]';
  2588. let qList=this.getQueryList();
  2589. //for layouts
  2590. let queryArray=new Array();
  2591. let targetObject=this.getLookupObject();
  2592. let lookupSet=new Object();
  2593. for (let q in qList){
  2594. let qobject=this.getQueryLayout(q);
  2595. this.print(fName+" inspecting layout for "+q+" "+qobject);
  2596. qobject.fields=qobject.metaData.fields;
  2597. qobject.title=this.findTitle(q);
  2598. //check for lookups
  2599. for (let f in qobject.fields){
  2600. //anything else is simple but lookup
  2601. let field=qobject.fields[f];
  2602. if (!("lookup" in field)) continue;
  2603. let lookup=field.lookup;
  2604. let qObject=this.getLookup(lookup.queryName);
  2605. if (qObject) continue;
  2606. //add to list
  2607. let qName=lookup.queryName;
  2608. let qCode=qName+':'+lookup.keyColumn+':'+lookup.displayColumn;
  2609. let e=runQuery.makeQuery(targetObject,'data',qName,qCode,[]);
  2610. //adjust minor settings
  2611. if (lookup.containerPath) e.containerPath=lookup.containerPath;
  2612. e.schemaName=lookup.schemaName;
  2613. e.columns=lookup.keyColumn+','+lookup.displayColumn;
  2614. lookupSet[qCode]=e;
  2615. this.print(fName+' inserting '+qCode);
  2616. }
  2617. }
  2618. for (let x in lookupSet){
  2619. queryArray.push(lookupSet[x]);
  2620. this.print(fName+' adding '+x);
  2621. for (let v in lookupSet[x]){
  2622. this.print(fName+' value ['+v+'] '+lookupSet[x][v]);
  2623. }
  2624. }
  2625. //this.print(fName+' print '+targetObject.print);
  2626. let that=this;
  2627. let action=function(){that.processLookup(cb);};
  2628. this.print(fName+' getDataFromQueries');
  2629. runQuery.getDataFromQueries(this,queryArray,action);
  2630. this.print(fName+' getDataFromQueries done');
  2631. }
  2632. crfVisit.processLookup=
  2633. function(cb){
  2634. let fName="[processLookup]";
  2635. let obj=this.getLookupObject();
  2636. for (let q in obj){
  2637. this.print(fName+" "+q);
  2638. let a=q.split(':');
  2639. if (a.length<3) continue;
  2640. let lookupName=a[0];
  2641. let key=a[1];
  2642. let val=a[2];
  2643. obj[lookupName]=new Object();
  2644. this.print(fName+' adding ['+lookupName+'] '+key+'/'+val);
  2645. let lObject=obj[lookupName];
  2646. lObject.LUT=new Array();//key to value
  2647. lObject.ValToKey=new Array();//value to key
  2648. lObject.keyColumn=key
  2649. lObject.displayColumn=val;
  2650. let qRows=obj[q].rows;
  2651. for (let i=0;i<qRows.length;i++){
  2652. let r=qRows[i];
  2653. this.print(fName+' LUT ['+r[key]+'] '+r[val]);
  2654. lObject.LUT[r[key]]=r[val];
  2655. lObject.ValToKey[r[val]]=r[key];
  2656. }
  2657. }
  2658. cb();
  2659. }
  2660. crfVisit.setData=
  2661. function(cb){
  2662. fName='[setData]';
  2663. let crfMatch=this.getCRFref();
  2664. let config=this.config;
  2665. let parentCrf=config.formConfig.crfEntry['parentCrf'];
  2666. if (parentCrf!=undefined) crfMatch=parentCrf;
  2667. this.print(fName+' form crf ['+this.getCRFref()+'] matching for crfRef='+crfMatch);
  2668. let queryArray=new Array();
  2669. let targetObject=this.getSnapshotObject();
  2670. //collect data and execute callback cb for queries in cb.queryList
  2671. let qList=this.getQueryList();
  2672. for (q in qList){
  2673. let filters=[LABKEY.Filter.create("crfRef",crfMatch)];
  2674. queryArray.push(runQuery.makeQuery(targetObject,'data',q,q,filters));
  2675. }
  2676. runQuery.getDataFromQueries(this,queryArray,cb);
  2677. }
  2678. crfVisit.uploadFile=
  2679. function(inputElement,context){
  2680. //context should have ID and dirName attributes;
  2681. //path will be dirName/ID/fieldName_ID.suf
  2682. //where suf is identical to localPath content picked from
  2683. //inputElement
  2684. this.print('uploadFile: '+inputElement.value+'/');
  2685. if (inputElement.type=="text") return;
  2686. this.print('uploadFile: '+inputElement.files+'/');
  2687. this.print('uploadFile: '+inputElement.files.length+'/');
  2688. if (inputElement.files.length>0){
  2689. let file=inputElement.files[0];
  2690. this.print('uploadFile: '+inputElement.value+'/'+file.size);
  2691. webdav.uploadFile(file,context);
  2692. }
  2693. }
  2694. crfVisit.printForm=
  2695. function(){
  2696. crfPrint.printForm();
  2697. }