123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- 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(["crf/runQuery.js","crf/formGenerator.js","crf/crfRoleSelector.js","crf/crfSetup.js","crf/crfData.js","crf/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;i<formRows.length;i++){
- //dataForms is Forms
- let formEntry=formRows[i];
- let formId=formEntry['Key'];
-
- //set the status for form
- filter.formStatus=formEntry[accessModeColumn];
- this.print(fName+' target formStatus ['+formEntry['formName']+'] '+filter.formStatus);
- //add row for each form
- let row=this.table.rows[i];
- if (!row) row=this.table.insertRow(i);
- let labelCell=row.cells[0];
- let formName=formEntry['formName'];
- if (!labelCell){
- labelCell=row.insertCell();
- labelCell.innerText=formName;
- crfHTML.addStyle(labelCell,'medium');
- crfHTML.addStyle(labelCell,'center');
- //crfHTML.createParagraph(formName,null,labelCell);
- }
- let cell=row.cells[1];
- if (!cell) {
- cell=row.insertCell();
- crfHTML.addStyle(cell,'stretch');
- }
- crfHTML.clear(cell);
- //update the filter with form idr
- filter.form=formId;
-
- let forms=new Array();
- for (let j=0;j<fEntries.length;j++){
- let entry=fEntries[j];
- if (this.filterEntry(entry,filter))
- forms.push(entry);
- }
- this.displayForms(cell,forms,roleAndSite,formId);
- this.print(fName+' finished checking existing forms');
- //only those that are allowed to create forms
- //print('Status: '+qForm[creatorModeColumn]);
-
- }
- if (filter.role=='crfManager')
- //need formGenerator.js
- formGenerator.showFormGenerator(this);
- else
- formGenerator.hideFormGenerator();
- }
- formPortal.displayForms=
- function(el,formList,roleAndSite,formId=null,idLabel=null,elError=null){
- let fName='[displayForms]';
- //formList is a list of crfEntry entries
-
- let table=crfHTML.createTable(null,el);
- let row=table.insertRow();
- let that=this;
- let n=0;
- let deletedForms=new Array();
- for (let i=0;i<formList.length;i++){
- let entry=formList[i];
- let stat=entry['FormStatus'];
- let formStatus=crfSetup.getMap('formStatus')[stat];
- //skip deleted forms
- if (formStatus=='Deleted'){
- deletedForms.push(entry);
- continue;
- }
- let cell=row.insertCell();
- n+=1;
- crfHTML.addStyle(cell,'stretch');
- let fbox=crfHTML.createBox(null,cell);
- //colormap of formStatus to colors
- let style=crfSetup.getEntryMap('formStatus')[stat]['color'];
- if (!style) style='gold';
- crfHTML.addStyle(fbox,style);
- this.print(fName+' setting style ['+i+'] '+style);
- let user=crfSetup.getMap('users')[entry['UserId']];
- let idLabel=crfSetup.getParticipantLabel(entry);
- let text=[entry['entryId'],user,idLabel,formStatus];
- fbox.onclick=function(){that.openForm(entry,roleAndSite,elError);};
- for (let j=0;j<text.length;j++){
- crfHTML.createParagraph(text[j],null,fbox);
- }
- }
- //formId=0 reserverd for forms created when new patients are added
- if (!formId) return table;
- let formEntry=crfSetup.getEntryMap('dataForms')[formId];
- this.print(fName+' unique '+formEntry['unique']);
-
- this.addDeletedForms(row,deletedForms);
- if (n>0 && 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();
- 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.addDeletedForms=
- function(row,deletedForms){
- //display deleted forms
- let fName="[addDeletedForms]";
- this.print(fName+": "+deletedForms.length);
- for (let i=0;i<deletedForms.length;i++){
- let entry=deletedForms[i];
- let stat=entry['FormStatus'];
- let formStatus=crfSetup.getMap('formStatus')[stat];
- let cell=row.insertCell();
- crfHTML.addStyle(cell,'stretch');
- let fbox=crfHTML.createBox(null,cell);
- //colormap of formStatus to colors
- let style=crfSetup.getEntryMap('formStatus')[stat]['color'];
- if (!style) style='gold';
- crfHTML.addStyle(fbox,style);
- this.print(fName+' setting style ['+i+'] '+style);
- let user=crfSetup.getMap('users')[entry['UserId']];
- let idLabel=crfSetup.getParticipantLabel(entry);
- let text=[entry['entryId'],user,idLabel,formStatus];
- //fbox.onclick=function(){that.openForm(entry,roleAndSite,elError);};
- for (let j=0;j<text.length;j++){
- crfHTML.createParagraph(text[j],null,fbox);
- }
- }
-
- }
- 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", 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<varRows.length;i++){
- let varName=varRows[i].staticVariable;
- crfEntry[varName]=studyData[varName];
- }
- crfEntry.UserId=LABKEY.Security.currentUser.id;
- crfEntry.Site=siteNumber;
- this.print(fName+" setting site to id="+crfEntry.Site);
- //from argument list
- crfEntry.Form=formId;
- if (idLabel){
- crfEntry[crfSetup.getLocalIdLabel()]=crfSetup.getLocalId(idLabel);
- crfEntry[crfSetup.getStudyIdLabel()]=crfSetup.getStudyId(idLabel);
- }
- //overload entries
- if (crfEntryOverload){
- for (f in crfEntryOverload){
- crfEntry[f]=crfEntryOverload[f];
- }
- }
- let crfStatus=crfData.createCrfStatus(crfEntry);
- crfStatus.operator=role;
- crfStatus.action='createForm';
- let that=this;
- //let cb=function(data){that.openForm(crfEntry,roleAndSite);}
- this.print(fName+' '+cb);
- let callback=function(data){cb(data,crfEntry);};
- let containerPath=crfSetup.getContainer('data');
- let pass=function(data){runQuery.insertRows('lists','crfStatus',[crfStatus],callback,containerPath);};
- runQuery.insertRows('lists','crfEntry',[crfEntry],pass,containerPath);
- }
|