123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- //global config variable
- const config=new Object();
- function print(msg){
- config.document.getElementById(config.debugArea).value+="\n"+msg;
- }
- function clear(){
- config.document.getElementById(config.debugArea).value="";
- }
- function getMode(){
- if ("role" in config){
- return config.role;
- }
- return "crfEditor";
- }
- function doNothing(){
- print('doNothing called');
- }
- function makeQuery(containerName,queryName,fieldName,filterArray){
- //queryArray should contain elements with
- //- fieldName to set the data variable
- //- containerName to select container (data,config,CRF)
- //- queryName to select query
- //- filterArray to perform filtering, empty array works
- //- callback cb to be called with no arguments
-
- let e=new Object();
- e.containerName=containerName;
- e.queryName=queryName;
- e.fieldName=fieldName;
- e.filterArray=filterArray;
- return e;
- }
- function getDataFromQueries(queryArray,cb){
- afterQuery(new Object(),-1,queryArray,cb);
- }
- function afterQuery(data,id,queryArray,cb){
- print('afterQuery['+id+']: ');
- if (id>-1){
- let fieldName=queryArray[id].fieldName;
- print('afterQuery['+fieldName+']: '+data.rows.length);
- //uses config.formConfig
- config.formConfig[fieldName]=data;
- }
- id+=1;
- if (id==queryArray.length) {
- cb();
- return;
- }
- let e=queryArray[id];
- let qconfig=new Object();
- qconfig.containerPath=getContainer(e.containerName);
- qconfig.schemaName="lists";
- if ("schemaName" in e){
- print('afterQuery: schemaName='+e.schemaName);
- qconfig.schemaName=e.schemaName;
- }
- if ("columns" in e){
- print('afterQuery: columns='+e.columns);
- qconfig.columns=e.columns;
- }
- qconfig.queryName=e.queryName;
- //this should point to configuration container
- //don't filter -> so we can pick up other forms (say registration) later on
- //qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)];
- if ("filterArray" in e)
- qconfig.filterArray=e.filterArray;
-
- //qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)]
- qconfig.success=function(data){afterQuery(data,id,queryArray,cb);};
- qconfig.failure=doNothing;
- LABKEY.Query.selectRows(qconfig);
- }
- function printMessage(msg){
- let txt=config.document.createElement("p");
- config.document.getElementById(config.div).appendChild(txt);
- txt.innerText=msg;
- }
- function userName(id){
- let formConfig=config.formConfig;
- for (let i=0;i<formConfig.users.rows.length;i++){
- if (formConfig.users.rows[i].UserId!=id)
- continue;
- return formConfig.users.rows[i].DisplayName;
- }
- return "NONE";
- }
- function setContainer(label,container){
- if (!(config.formConfig.hasOwnProperty('container'))){
- config.formConfig.container=new Array();
- }
- config.formConfig.container[label]=container;
- }
- function getContainer(label){
- return config.formConfig.container[label];
- }
- function generateFormArray(){
- print("generateFormArray "+getMode());
-
- config.formConfig=new Object();
- config.formConfig.softwareVersion='0.1.7';
- //report software version
- config.document.getElementById('version').innerText=config.formConfig.softwareVersion;
-
- setContainer('data',LABKEY.ActionURL.getContainer());
- setContainer('config',LABKEY.ActionURL.getContainer());
- setContainer('CRF',LABKEY.ActionURL.getContainer());
- let selectRows=new Object();
- //this is local data
- selectRows.containerPath=getContainer('CRF');
- selectRows.schemaName='lists';
- selectRows.queryName='crfSettings';
- //store form related data to this object
- selectRows.success=afterSettings;
- LABKEY.Query.selectRows(selectRows);
- }
- function afterSettings(data){
- config.formConfig.settings=new Array();
- for (let i=0;i<data.rows.length;i++){
- let n=data.rows[i]['name'];
- let v=data.rows[i]['value'];
- config.formConfig.settings[n]=v;
- }
- let st=config.formConfig.settings;
- print('afterSettings');
- for (let k in st){
- print('\t'+k+'='+st[k]);
- }
- //if ('dataContainer' in st){
- // setContainer('data',st['dataContainer']);
- //}
- let vname='configContainer';
- if (vname in st){
- setContainer('config',st[vname]);
- }
- print('Config: '+getContainer('config'));
- print('Data: '+getContainer('data'));
- //setup queryArray
- let queryArray=new Array();
- //Forms
- queryArray.push(makeQuery('config','Forms','dataForms',[]));
- //users
- queryArray.push(makeQuery('data','users','users',[]));
- queryArray[queryArray.length-1].schemaName='core';
- //inputLists
- queryArray.push(makeQuery('config','inputLists','inputLists',[]));
- //studyData
- queryArray.push(makeQuery('data','StudyProperties','studyData',[]));
- let e=queryArray[queryArray.length-1];
- e.schemaName='study';
- e.columns="StudySponsor,StudyCoordinator,EudraCTNumber";
- e.columns+=',RegulatoryNumber,SubjectColumnName';
- //crfEditors
- queryArray.push(makeQuery('config','crfEditors','crfEditors',[]));
- //crfMonitors
- queryArray.push(makeQuery('config','crfMonitors','crfMonitors',[]));
- //crfSponsors
- queryArray.push(makeQuery('config','crfSponsors','crfSponsors',[]));
- //FormStatus
- queryArray.push(makeQuery('config','FormStatus','formStatusg',[]));
- //site
- queryArray.push(makeQuery('config','site','siteData',[]));
- //crfEntry
- queryArray.push(makeQuery('data','crfEntry','crfEntries',[]));
- getDataFromQueries(queryArray,fcontinue);
- }
- function fcontinue(){
- let formConfig=config.formConfig;
- print("Number of study data entries: "+formConfig.studyData.rows.length);
- print("ParticipantId: "+formConfig.studyData.rows[0].SubjectColumnName);
- let qconfig=new Object();
- qconfig.containerPath=getContainer('data');
- qconfig.schemaName="study";
- let demographicDataId=formConfig.dataForms.rows[0].masterQuery;
- let demographicDataQuery="NONE";
- for (let i=0;i<formConfig.inputLists.rows.length;i++){
- let entry=formConfig.inputLists.rows[i];
- print("inputList ["+i+"] ["+entry.Key+"] "+entry.queryName);
- if (entry.Key==demographicDataId){
- demographicDataQuery=entry.queryName;
- break;
- }
- }
- print('Setting demographic query to '+demographicDataQuery);
- qconfig.queryName=demographicDataQuery;
- //qconfig.queryName="demographicData";
-
- qconfig.success=afterPopulatingDemographicData;
- LABKEY.Query.selectRows(qconfig);
- }
- function afterPopulatingDemographicData(data){
- let formConfig=config.formConfig;
- formConfig.demographicData=data;
- print("afterPopulatingDemographic");
- print("Number of patients: "+formConfig.demographicData.rows.length);
-
- fcontinue2();
- }
- function fcontinue2(){
- let formConfig=config.formConfig;
- let dataForms=formConfig.dataForms.rows;
- formConfig.table=config.document.createElement("table");
- config.document.getElementById(config.div).appendChild(formConfig.table);
- let accessModeColumn=getMode()+'Status';
- print('accessModeColumn '+accessModeColumn);
- let creatorModeColumn=getMode()+'Creator';
- //switch from status based to form based access
- print("Forms: "+dataForms.length);
- print("Entries: "+formConfig.crfEntries.rows.length);
- let fEntries=formConfig.crfEntries.rows;
- let users=formConfig.users.rows;
- let currentUserId=LABKEY.Security.currentUser.id;
- let currentUser=undefined;
- for (let i=0;i<users.length;i++){
- if (users[i].UserId!=currentUserId) continue;
- currentUser=users[i];
- }
- let fList=config.role+'s';
- let fRows=config.formConfig[fList].rows;
- //current user must be in the list
- //
- let currentUserRoles=new Array();
- //the same user can act for multiple sites
- for (let i=0;i<fRows.length;i++){
- if (fRows[i].User!=currentUser.UserId) continue;
- currentUserRoles.push(fRows[i]);
- }
- if (currentUserRoles.length==0){
- printMessage('User '+currentUser.DisplayName+" can't act as "+config.role);
- return;
- }
- let currentSites=new Array();
- let siteRows=config.formConfig.siteData.rows;
- for (let i=0;i<siteRows.length;i++){
- for (let j=0;j<currentUserRoles.length;j++){
- if (siteRows[i].siteNumber!=currentUserRoles[j].Site) continue;
- currentSites.push(siteRows[i]);
- }
- }
- let msg='User '+currentUser.DisplayName+' acting as '+config.role+' for (';
- for (let i=0;i<currentSites.length;i++){
- if (i>0) msg+=', ';
- msg+=currentSites[i].siteName;
- }
- msg+=')';
- printMessage(msg);
- for (let i=0;i<dataForms.length;i++){
- let qForm=dataForms[i];
- let formKey=qForm.Key;
-
- //figure out master query name
- let masterQuery="NONE";
- let mID=qForm.masterQuery;
- for (let i2=0;i2<formConfig.inputLists.rows.length;i2++){
- //queryName
- if (formConfig.inputLists.rows[i2].Key!=mID)
- continue;
-
- masterQuery=formConfig.inputLists.rows[i2].queryName;
- print("Setting master query "+masterQuery);
- break;
- }
- //add row for each form
- let row=formConfig.table.insertRow(i);
- let formName=qForm.formName;
- print("["+i+"/"+formKey+']: '+formName);
- let k=0;
- let formStatus=qForm[accessModeColumn];
- print('target formStatus '+formStatus);
- for (let j=0;j<fEntries.length;j++){
- let entry=fEntries[j];
- let formId=entry.Form;
- //print("Row["+j+"] formId: "+formId);
- if (formId!=formKey)
- continue;
- //should we consider this form
-
- if (entry.FormStatus!=formStatus){
- //print('Form status mismatch :'+entry.FormStatus);
- continue;
- }
- print('Candidate '+entry.entryId);
- //TODO: smart filter on user (now we get to see all)
- //
- //for editors
- if (config.role=='crfEditor' && entry.UserId!=currentUser.UserId){
- print('Skipping identity mismatch: '+entry.UserId+'/'+currentUser.UserId);
- continue;
- }
- let matchingSite=-1;
- let potentialSiteNumbers="[";
- for (let k=0;k<currentSites.length;k++){
- if (k>0) potentialSiteNumbers+=',';
- potentialSiteNumbers+=currentSites[k].siteNumber;
- if (entry.Site!=currentSites[k].siteNumber) continue;
- matchingSite=currentSites[k].siteNumber;
- break;
- }
- potentialSiteNumbers+=']';
- if (matchingSite==-1){
- print('Skipping wrong site: '+entry.Site+'/'+potentialSiteNumbers);
- continue;
- }
- //insert form
- //
- let fbox=config.document.createElement("div");
- fbox.classList.add("box","gold");
- let fp=config.document.createElement("p");
- let id=entry.entryId;
- fp.innerHTML=id;
- //it would be great if this were patientId if available
- //fp.classList.add("large","center");
- fp.classList.add("center");
- fbox.appendChild(fp);
-
- let fp1=config.document.createElement("p");
- let user="NONE";
- for (let ii=0;ii<users.length;ii++){
- if (users[ii].UserId!=entry.UserId)
- continue;
- user=users[ii].DisplayName;
- break;
- }
- fp1.innerHTML=user;
- fp1.classList.add("center");
- fbox.appendChild(fp1);
-
-
- let fp2=config.document.createElement("p");
- fp2.innerHTML=formName;
- fp2.classList.add("center");
- fbox.appendChild(fp2);
-
- let fp3=config.document.createElement("p");
- fp3.id="pid"+id;
- fp3.innerHTML="NONE";
- print('Setting participant id from query: '+masterQuery);
- if (masterQuery!="NONE"){
- let qconfig=new Object();
- qconfig.schemaName='lists';
- qconfig.queryName=masterQuery;
- qconfig.filterArray=[LABKEY.Filter.create('crfRef',id)];
- qconfig.success=function(data){setPatientId(data,fp3.id);}
- LABKEY.Query.selectRows(qconfig);
- }
- fp3.classList.add("center");
- fbox.appendChild(fp3);
-
-
- let cell=row.insertCell(k);
- cell.classList.add("stretch");
- cell.id="box"+entry.crfRef;
- let button=config.document.createElement("button");
- button.appendChild(fbox);
- button.onclick=function(){openForm(entry)};
- cell.appendChild(button);
- k++;
- }
- print('finished checking existing forms');
- //only those that are allowed to create formsa
- if (qForm[creatorModeColumn]!='TRUE') continue;
- let fbox=config.document.createElement("div");
- fbox.classList.add("box","red");
- let fp=config.document.createElement("p");
- fp.innerHTML="Create new";
- fbox.appendChild(fp);
-
-
-
- let fp2=config.document.createElement("p");
- fp2.innerHTML=formName;
- fp2.classList.add("center");
- fbox.appendChild(fp2);
-
- let cell=row.insertCell(k);
- cell.classList.add("stretch");
- let button=config.document.createElement("button");
- button.appendChild(fbox);
- button.onclick=function(){createForm(formKey)};
- cell.appendChild(button);
- }
- }
- function setPatientId(data,id){
- let formConfig=config.formConfig;
- if (data.rows.length==0) return;
- //every masterQuery must have participantCode
- let participantCode=data.rows[0].participantCode;
- let participantField=formConfig.studyData.rows[0].SubjectColumnName;
- print('setPatientId: '+data.queryName);
- print('participantCode: '+participantCode);
- print('participantField: '+participantField);
- //equates to registration
- for (let i=0;i<formConfig.demographicData.rows.length;i++){
- let entry=formConfig.demographicData.rows[i];
- let key=entry.lsid;
- print('Comparing to: '+key);
- if (key!=participantCode) continue;
- print('Setting: ');
- let participantId=entry[participantField];
- config.document.getElementById(id).innerHTML=participantId;
- break;
- }
-
- }
- function openForm(crfEntry){
- let formConfig=config.formConfig;
- let crfRef=crfEntry.entryId;
-
- print("Clicked for "+crfRef);
- let formId=crfEntry.Form;
- for (let i=0;i<formConfig.dataForms.rows.length;i++){
- if (formConfig.dataForms.rows[i].Key!=formId) continue;
- print("Setting form "+formConfig.dataForms.rows[i].formName);
- formEntry=formConfig.dataForms.rows[i];
- break;
- }
- if (formEntry==undefined) return;
- //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="visit";
- reviewMode="EDIT";
- if ("reviewMode" in config) reviewMode=config.reviewMode;
- let params = {
- "name": formUrl,
- // The destination wiki page. The name of this parameter is not arbitrary.
- "entryId": crfRef,
- "formId":formId,
- "role" : config.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);
- print("Redirecting to "+wikiURL);
-
- window.location = wikiURL;
- }
- function createForm(formId){
- let formConfig=config.formConfig;
- print("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
- //set other variables
- //requires studyData as part of formConfig
- let studyData=formConfig.studyData.rows[0];
- crfEntry.EudraCTNumber=studyData.EudraCTNumber;
- crfEntry.StudyCoordinator=studyData.StudyCoordinator;
- crfEntry.StudySponsor=studyData.StudySponsor;
- crfEntry.RegulatoryNumber=studyData.RegulatoryNumber;
- crfEntry.UserId=LABKEY.Security.currentUser.id;
- //requires crfEditors as part of formConfig
- for (let i=0;i<formConfig.crfEditors.rows.length;i++){
- print("Checking user "+formConfig.crfEditors.rows[i].User);
- if (formConfig.crfEditors.rows[i].User!=crfEntry.UserId) continue;
- print("Found user");
- crfEntry.Site=formConfig.crfEditors.rows[i].Site;
- print("Setting site to id="+crfEntry.Site);
- break;
- }
- //from argument list
- crfEntry.Form=formId;
- let qconfig=new Object();
- qconfig.containerPath=getContainer('data');
- qconfig.schemaName='lists';
- qconfig.queryName='crfEntry';
- qconfig.success=function(data){openForm(crfEntry)};
- qconfig.rows=[crfEntry];
- LABKEY.Query.insertRows(qconfig);
- }
|