var formPortal={}; formPortal.setDebug= function(debug=null){ if (debug){ this.print=function(msg){debug.print(msg);}; this.clear=function(){debug.clear();} return; } //provide default functions if not debug object is available this.print=function(msg){console.log(msg);} this.clear=function(){;} } formPortal.setDebug(); formPortal.doNothing= function(){ this.print('doNothing called'); } //load runQuery.js formPortal.printMessage= function(msg){ crfHTML.createParagraph(msg,'formDiv') } formPortal.init= function(cb=null){ let that=this; let action=function(){that.scriptsLoaded(cb);}; LABKEY.Utils.requiresScript(["crfDORA/runQuery.js","crfDORA/formGenerator.js","crfDORA/crfRoleSelector.js","crfDORA/crfSetup.js","crfDORA/crfData.js","crfDORA/crfHTML.js"],action); } formPortal.scriptsLoaded= function(cb=null){ crfHTML.init(); crfRoleSelector.set(crfSetup); crfData.setSetup(crfSetup); let initGenerator=function(){formGenerator.init(cb);}; let initData=function(){crfData.init(initGenerator);}; crfSetup.init(initData); } formPortal.generateFormArray= function(){ let fName='[generateFormArray]'; this.print(fName); this.print(fName); let that=this; let action=function(){that.afterSetup();}; let parseSetup=function(){crfSetup.parseSetup(action);}; crfSetup.setContainers(parseSetup); } formPortal.filterEntry= function(entry,filter){ let fName="[filterEntry]"; //this.print(fName+' candidate '+entry.entryId); if (entry.Form!=filter.form) return false; //only select forms where status matches the target status if (entry.FormStatus!=filter.formStatus){ return false; } if (crfSetup.getSettings("filterUser") && filter.role=='crfEditor' && entry.UserId!=filter.userId){ //this.print(fName+' skipping identity mismatch: '+entry.UserId+'/'+filter.userId); return false; } if (crfSetup.getSettings("filterSite") && entry.Site!=filter.siteNumber){ //this.print(fName+' skipping site mismatch: '+entry.Site+'/'+filter.siteNumber); return false; } return true; } formPortal.afterSetup= function(){ let fName='[afterSetup]'; this.print(fName); let that=this; let cb=function(){that.updateForms();}; crfRoleSelector.makePortal(cb); this.table=crfHTML.createTable('formDiv'); } formPortal.updateForms= function(){ //this is normallz callback on role selector roleAndSite=crfRoleSelector.getRoleAndSite(); this.drawForms(roleAndSite); } formPortal.drawForms= function(roleAndSite=null){ let fName="[drawForms]"; if (!roleAndSite){ alert("Please set role and site"); return false; } //set filter let ar=roleAndSite.split(":"); let role=ar[0]; let filter=new Object(); filter.role=role; filter.siteNumber=ar[1]; filter.userId=LABKEY.Security.currentUser.id; let accessModeColumn=filter.role+'Status'; let formRows=crfSetup.getRows('dataForms'); //browse through forms let fEntries=crfSetup.getRows('crfEntries'); this.print(fName+' all entries '+fEntries.length); for (let i=0;i0 && formEntry['unique']=='TRUE') return table; //add empty forms for absent records and non-unique records //should not be allowed to create new forms let regFormId=crfSetup.getSettings('registrationFormId'); if (regFormId==formId) return table; let cell=row.insertCell(n); crfHTML.addStyle(cell,'stretch'); let fbox=crfHTML.createBox(null,cell); crfHTML.addStyle(fbox,'empty'); let cb=function(xdata,xcrfEntry){that.openForm(xcrfEntry,roleAndSite);}; fbox.onclick=function(){that.createForm(formId,roleAndSite,cb,idLabel);}; return table; } formPortal.openForm= function(crfEntry,roleAndSite,elError=null){ let fName="[openForm]"; if (!roleAndSite){ if (elError) elError.innerText="Set role and site"; this.print(fName+' role and site not specified'); return; } let crfRef=crfEntry.entryId; this.print(fName+" clicked for "+crfRef); let ar=roleAndSite.split(':'); let role=ar[0]; let siteNumber=ar[1]; //select between review and view //let formUrl=formEntry["formUrl"]; //if ("reviewMode" in config) formUrl=formEntry["reviewFormUrl"]; //print("Setting url "+formUrl); //direct all to the same html let formUrl="visitNew"; reviewMode="EDIT"; let params = { "name": formUrl, // The destination wiki page. The name of this parameter is not arbitrary. "entryId": crfRef, "role" : role }; //"formSetupQuery":formEntry["setupQuery"], let containerPath= LABKEY.ActionURL.getContainer(); // This changes the page after building the URL. //Note that the wiki page destination name is set in params. var wikiURL = LABKEY.ActionURL.buildURL("crf_dora", formUrl , containerPath, params); this.print(fName+" redirecting to "+wikiURL); window.location = wikiURL; } formPortal.createForm= function(formId,roleAndSite,cb=null,idLabel=null,crfEntryOverload=null){ //cb takes arguments (data,crfEntry) let fName="[createForm]"; let formRow=crfSetup.getEntryMap('dataForms')[formId]; let creator=formRow['creator']; if (!creator) { alert("Creator for form "+formRow['formName']+' not set'); return; } let ar=roleAndSite.split(':'); let role=ar[0]; let siteNumber=ar[1]; if (creator!=role){ alert("Can't create form as "+role+' only allowed for '+creator); return; } this.print(fName+" create form w/id "+formId); let crfEntry=new Object(); crfEntry.entryId=Date.now(); crfEntry["Date"]=new Date(); crfEntry["View"]="[VIEW]"; crfEntry.formStatus=1;//In progress let fStatus=formRow['initialStatus']; if (fStatus) crfEntry.formStatus=fStatus; //set other variables //requires studyData as part of formConfig let studyData=crfSetup.getRows('studyData')[0]; let varRows=crfSetup.getRows('crfStaticVariables'); for (let i=0;i