Browse Source

moving updateRegistration from visit to participantIdManager, allowing readonly to be set either to local or study id, adding automatic localId generation

Andrej Studen 1 year ago
parent
commit
87f403cf03
1 changed files with 142 additions and 60 deletions
  1. 142 60
      web/crf/participantIdManager.js

+ 142 - 60
web/crf/participantIdManager.js

@@ -10,13 +10,13 @@ participantIdManager.init=
 function(cb=null){
    let that=this;
    let action=function(){that.afterScripts(cb);};
-   LABKEY.requiresScript(['crf/crfHTML.js'],action);
+   LABKEY.requiresScript(['crf/crfHTML.js','crf/generateRegistration.js'],action);
 }
 
 participantIdManager.afterScripts=
 function(cb=null){
    crfHTML.init();
-   if (cb) cb();
+   generateRegistration.init(cb);
 }
 
 participantIdManager.set=
@@ -26,6 +26,11 @@ function(setup,data){
    this.updateCrfEntry=function(){;}
    this.mode='STUDY';
    //this.generateTable();
+   if (!("readonly" in this)){
+      this.readonly=new Object();
+      this.readonly['LOCAL']=false;
+      this.readonly['STUDY']=false;
+   }
 }
 
 participantIdManager.setMode=
@@ -40,11 +45,6 @@ function(){
 
 participantIdManager.setReadonly=
 function(mode){
-   if (!("readonly" in this)){
-      this.readonly=new Object();
-      this.readonly['LOCAL']=false;
-      this.readonly['STUDY']=false;
-   }
    if (mode=="LOCAL"){
       this.readonly[mode]=true;
       return;
@@ -52,6 +52,15 @@ function(mode){
    this.readonly['STUDY']=true;
 }
 
+participantIdManager.clearReadonly=
+function(mode){
+   if (mode=='LOCAL'){
+      this.readonly[mode]=false;
+      return;
+   }
+   this.readonly['STUDY']=false;
+}
+
 participantIdManager.isReadonly=
 function(mode){
    if (mode=='LOCAL') return this.readonly['LOCAL'];
@@ -78,15 +87,25 @@ function(mode=null){
    crfHTML.addSelectOptions(input,opts);
 }
 
-participantIdManager.updateElements=
-function(mode=null){
-   let fName='[updateElements]';
-   //reset all values (some might be different depending on the call timing)
+participantIdManager.insertRow
+=function(label){
+	let row=this.table.insertRow();
+	
+   //label for local ID
+   let cell=crfHTML.createTblHeader(null,row);
+	cell.setAttribute("colspan","1");
+	cell.style.fontSize="20px";
+	cell.style.textAlign="left";
+   cell.innerText=label;
 
-   //selector is with study
-   //pM.inputManageStudy=this.parent.getElement(pM.inputManageStudyId);
-}
+   //value
+   row.insertCell();
 
+   //button
+   row.insertCell();
+   
+} 
+ 
 
 participantIdManager.generateTable=
 function(){
@@ -100,44 +119,43 @@ function(){
     
    this.table=crfHTML.createTable(formName);
 	this.table.className='t2';
-	let row=this.table.insertRow();
-	
-   //label for local ID
-   let cell=crfHTML.createTblHeader(null,row);
-	cell.setAttribute("colspan","1");
-	cell.style.fontSize="20px";
-	cell.style.textAlign="left";
-   //Use study coding for participant field
-   cell.innerText='Local ID';
-	//cell.innerText=pM.participantField;
+   this.insertRow('Local ID');
+   this.insertRow('Study ID');
+   this.insertRow('ID generator');
+   this.insertRow('Reset local ID');
 
 
-   //value
-   let cellValue=row.insertCell();
-   //cellValue.id=pM.cellValueId;
-
-   let cellButton=row.insertCell();
    this.getInputButton('LOCAL');
-   
-   //second row for study id
-   let rowStudy=this.table.insertRow();
-
-   //label for study ID
-   let cellStudy=crfHTML.createTblHeader(null,rowStudy);
-	cellStudy.setAttribute("colspan","1");
-	cellStudy.style.fontSize="20px";
-	cellStudy.style.textAlign="left";
-   //Use study coding for participant field
-   cellStudy.innerText='Study ID';
-
-	//selector for study id
-   cellValue=rowStudy.insertCell();
-
-   //manage
-   cellButton=rowStudy.insertCell();
-   this.print(fName+' done');
    this.getInputButton('STUDY');
 
+   let sMap=this.setup.getEntryMap('siteData:siteNumber');
+   let sMapEntry=sMap[this.siteNumber];
+   let qPar=new Object();
+   qPar['queryName']='participantRegistration'+this.siteNumber;
+   qPar['codeBase']=sMapEntry['codeBase'];
+   qPar['schemaName']='lists';
+   qPar['codeField']='registrationCode';
+   let genObj=generateRegistration.getObject(qPar,'participantIdManager_localInput');
+   let that=this;
+   genObj.callback=function(){that.setId('LOCAL');};
+   //attach execute to a button 
+   let cellButton=this.table.rows[2].cells[2];
+   cellButton.style.alignItems='center';
+   this.generateButton=crfHTML.createButton(null,cellButton);
+   this.generateButton.id="participantIdManager_generateIdButton";
+   this.generateButton.onclick=function(){generateRegistration.execute(genObj);};
+   this.generateButton.value="Generate Local ID";
+  
+
+   let cellReset=this.table.rows[3].cells[2];
+   this.resetButton=crfHTML.createButton(null,cellReset);
+   this.resetButton.id='participantIdManager_resetButton';
+   this.resetButton.onclick=function(){that.resetLocal();}
+   this.resetButton.value='Reset Local ID';
+   this.print(fName+' done');
+
+
+
 }
 
 participantIdManager.getRow=
@@ -178,14 +196,17 @@ function(mode=null){
 participantIdManager.createInputElement=
 function(mode=null){
    let elementType=this.getInputElementType(mode);
-   let cell=this.getInputCell(mode);
+   let cell=this.getValueCell(mode);
    
-   if (elementType=="input") el=crfHTML.createTextInput();
+   if (elementType=="input") {
+      el=crfHTML.createTextInput();
+      el.id='participantIdManager_localInput';
+   }
    if (elementType=="select") el=crfHTML.createSelect(new Object());
    this.print(fName+' input '+el);
 
    cell.replaceChildren(el);
-   this.addSelectOptions();
+   this.addSelectOptions(mode);
  
    return el;
 }
@@ -204,7 +225,12 @@ function(mode=null){
    let fName='[getTextElement]';
    let el=this.getValueElement(mode);
    if (el) return el;
-   el=crfHTML.createParagraph('');
+   return createTextElement(mode);
+}
+
+participantIdManager.createTextElement=
+function(mode=null){
+   let el=crfHTML.createParagraph('');
    let cell=this.getValueCell(mode);
    //let oldEl=pM.getInputElement(mode);
    cell.replaceChildren(el);
@@ -270,7 +296,7 @@ function(mode=null){
    this.setParticipantIdToCrfEntry(mode,pId);//no argument (should come from mode)
    this.print(fName+" new value "+pId);
    this.setLabelMode(mode,label);
-   pM.updateCrfEntry();
+   this.updateCrfEntry();
 }
 
 participantIdManager.setLabelMode=
@@ -289,16 +315,16 @@ function(mode,label){
    }
       
 
-   let textValue=this.getTextElement(mode);
+   let textValue=this.createTextElement(mode);
    this.print(fName+' textElement '+textValue);
    textValue.innerText=id;
 
-   if (mode!="LOCAL"){
+   if (mode!="LOCAL" && label!='NOT SET'){
       let loc=crfSetup.getLocalId(label);
       this.print(fName+' setting local id '+loc);
-      if (loc && loc!='null'){
+      if (this.isValid(loc)){
          //pM.getParticipantIdFromCrfEntry('LOCAL');
-         let tValLocal=this.getTextElement('LOCAL');
+         let tValLocal=this.createTextElement('LOCAL');
          tValLocal.innerText=loc;
       }
       //this.setParticipantIdToCrfEntry(pM,loc,'LOCAL');
@@ -311,6 +337,9 @@ function(mode,label){
    }
    x.value="Edit";
 
+   if (mode=='LOCAL')
+      this.generateButton.style.display='none';
+
    
 }
 
@@ -327,11 +356,23 @@ function(mode){
    let fName='[setEditMode1]';
    this.print(fName+' pM '+this+' mode '+mode);
    //input
-   let el=this.getInputElement(mode);
+   let el=this.createInputElement(mode);
 
    let x=this.getInputButton(mode);
    x.value="Set";
 
+   if (mode=='LOCAL'){
+      this.generateButton.style.display='block';
+      x.style.display='block';
+   }
+
+}
+
+participantIdManager.resetLocal=
+function(){
+   this.clearReadonly('LOCAL');
+   this.setEditMode('LOCAL');
+   this.resetButton.style.display='none';
 }
 
 //manage interaction to storage/CRF and study/LabKey
@@ -357,18 +398,59 @@ function(mode=null){
    return this.data.getCrfEntry()[this.getCrfEntryFieldName(mode)];
 }
 
+participantIdManager.isValid=
+function(name){
+   if (!name) return false;
+   if (name=="null") return false;
+   return true;
+}
+
 participantIdManager.verifyCrfStudyId=
 function(){
-
+   let fName='[verifyCrfStatusId]';
    //is studyId already set for the crf
    let studyId=this.getParticipantIdFromCrfEntry('STUDY');
+   let localId=this.getParticipantIdFromCrfEntry('LOCAL');
+   this.print(fName+' studyId '+studyId+' localId '+localId);
+   this.resetButton.style.display='none';
    if (!studyId) return;
+   let originalMode=this.getMode();
+   if (originalMode=='LOCAL') this.resetButton.style.display='block';
    this.setReadonly('STUDY');
    this.setMode("STUDY");
    this.setLabelMode('STUDY',this.setup.getParticipantLabel(this.data.getCrfEntry()));
+   //get registration map to fill local id
+   let that=this;
+   let completeVerification=function(){that.completeVerification(studyId);}
+   this.data.setRegistration(completeVerification);
+}
+   
+participantIdManager.completeVerification=
+function(studyId){
+   let fName='[completeVerification]';
    //subject to content of localId
-   let localId=this.getParticipantIdFromCrfEntry('LOCAL');
-   if (localId) this.setReadonly('LOCAL');
+   let rMapEntry=this.data.getRegistrationEntryMap(this.getCrfEntryFieldName('STUDY'))[studyId];
+    //try to set it from registration
+   let localId=rMapEntry[this.getCrfEntryFieldName('LOCAL')];
+   this.print(fName+' localId '+localId+' isNull '+(localId==null)+' is"null" '+(localId=="null"));
+   
+   let localIdFromEntry=this.getParticipantIdFromCrfEntry('LOCAL');
+   if (localIdFromEntry!=localId){
+      //update mismatches
+      this.setParticipantIdToCrfEntry('LOCAL',localId);
+      this.updateCrfEntry();
+   }
+   //ignore the one set in the crfEntry, studyId prevails
+   //if (!this.isValid(localId)) {
+   //   let localId=this.getParticipantIdFromCrfEntry('LOCAL');
+   //}
+   if (this.isValid(localId)) {
+      this.setReadonly('LOCAL');
+      this.setLabelMode('LOCAL',localId);
+   }
+   else{
+      this.setEditMode('LOCAL');
+   }
    
 }