crfVisit.js 92 KB

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