Browse Source

move regstration to participantIdManager, allow for form reassignment in crfVisitNew.js

Andrej Studen 1 year ago
parent
commit
53960eb00d
1 changed files with 88 additions and 51 deletions
  1. 88 51
      web/crf/crfVisitNew.js

+ 88 - 51
web/crf/crfVisitNew.js

@@ -92,6 +92,7 @@ function (){
 crfVisit.getIdManager=
 function(){
    if (!("masterForm" in participantIdManager)){
+      participantIdManager.siteNumber=crfData.getCrfEntry()['Site'];
       participantIdManager.masterForm=this.masterForm;
       participantIdManager.generateTable();
    }
@@ -1106,7 +1107,6 @@ crfVisit.saveReview=
 function(queryName,elementId,setup){
 	//loads any queryName
 
-	let debug=true;
    let fName='[saveReview/'+queryName+']';
 	this.print(fName+" elementId "+elementId);
 
@@ -1163,9 +1163,18 @@ function(queryName,elementId,setup){
 
 	}
    let that=this;
-	let action=function(data){that.updateLastSavedFlag(data,setup,elementId)};
+	let updateFlag=function(data){that.updateLastSavedFlag(data,setup,elementId)};
+   let cPath=crfSetup.getContainer('data');
+   
+   let action=updateFlag;
+   if (crfSetup.getSettings("updateRegistration")){
+      let pars=variableList.parseVariables(crfSetup.getSettings("updateRegistration"));
+      if (pars['query']==queryName){
+         action=function(data){that.updateRegistration(updateFlag,data);}
+      } 
+   }
 
-   runQuery.modifyRows(mode,'lists',queryName,[entry],action,crfSetup.getContainer('data'));
+   runQuery.modifyRows(mode,'lists',queryName,[entry],action,cPath);
 
 }
 
@@ -1346,17 +1355,17 @@ function(actionName){
 		}
 	}
 	//this is necessary only for Generated to Generation completed step
-	let actionSettings=crfSetup.getActionSettings(actionName);	
-	if (variableList.hasVariable(actionSettings,"updateRegistration")){
+	//let actionSettings=crfSetup.getActionSettings(actionName);	
+	//if (variableList.hasVariable(actionSettings,"updateRegistration")){
       //if updateRegistrationFormId is set, only update when submit is used on that form
-      if (variableList.hasVariable(actionSettings,"updateRegistrationFormId")){
-         let formId=crfData.getCrfEntry()['Form'];
-         if (actionSettings['updateRegistrationFormId']==formId)
-            this.updateRegistration();
-      }
-      else
-		   this.updateRegistration();
-	}
+     // if (variableList.hasVariable(actionSettings,"updateRegistrationFormId")){
+       //  let formId=crfData.getCrfEntry()['Form'];
+         //if (actionSettings['updateRegistrationFormId']==formId)
+           // this.updateRegistration();
+      //}
+      //else
+		  // this.updateRegistration();
+	//}
    this.changeFormStatusAndNotify(actionName);
 
 }
@@ -1527,6 +1536,12 @@ function(){
 	let formUrl="participantPortal";
 	let params=new Object();
 	params.name=formUrl;
+   params.role=this.role;
+   let crfEntry=crfData.getCrfEntry();
+   params.site=crfEntry['Site'];
+   let participantLabel=crfSetup.getParticipantLabel(crfEntry);
+   params['participantLabel']=participantLabel;
+
 	//params.pageId="CRF";
 
 	//points to crf container
@@ -1626,39 +1641,60 @@ function(){
 	let formCreatorId=formCreator.UserId;
 
 
-
-	//let formSite=config.formConfig.crfEntry.Site;
+   //get list of sites the user can work on
 	let fList=this.role+'s';
 	let fRows=crfSetup.getRows(fList);
-	//let currentSiteId=-1;
+   let operatorSites=new Array();
+	for (let i=0;i<fRows.length;i++){
+		if (fRows[i].User!=currentUser.UserId) continue;
+		operatorSites.push(fRows[i].Site);
+	}
+	this.print('operator Site: '+operatorSites.length);
+	if (operatorSites.length==0){
+      let msg='User '+currentUser.DisplayName;
+      msg+=' is not a '+operator;
+      this.generateErrorMsg(msg);
+      return false;
+   }
+
 	
 	//depending on operator mode, we should decide what is right
 	let operator=this.role;
 	if (this.role=='crfEditor'){
 		//editor can only edit its own forms
-		if (currentUser.UserId!=formCreatorId){
-			let msg='User '+currentUser.DisplayName;
-			msg+=' has no permission on this form';
-			this.generateErrorMsg(msg);
-			return false;
-		}
+		if (currentUser.UserId==formCreatorId){
+         return true;
+      }
+      //here we are if UserId and formCreatorId are not equal
+      let allowR=crfSetup.getSettings("allowFormReassignment");
+      //form reassignment is not allowed
+      if (!allowR){
+         let msg='User '+currentUser.DisplayName;
+         msg+=' has no permission on this form';
+         this.generateErrorMsg(msg);
+         return false;
+      }
+      //this is for allowed form reassignments
+      //site should still match
+      if (!operatorSites.includes(currentSite.siteNumber)){
+         let msg='User '+currentUser.DisplayName;
+         msg+=' has no permission for site '+currentSite.siteName;
+         this.generateErrorMsg(msg);
+         return false;
+      }
+      //re-assign form, leave a trace in crfStatus and change creator in crfEntry
+      let that=this;
+      let action=new Object();
+      action.name="formReassignement";
+      action.cb=function(){that.doNothing();}
+      crfData.getCrfEntry()['UserId']=currentUser.UserId;
+      let status=crfData.getCrfEntry()['FormStatus'];
+      this.updateFlag(status,action);
       return true;
 	}
 	if (operator=='crfMonitor' || operator=='crfSponsor'){
 		//monitor can look at forms based on his site
 		//find monitor line
-		let operatorSites=new Array();
-		for (let i=0;i<fRows.length;i++){
-			if (fRows[i].User!=currentUser.UserId) continue;
-			operatorSites.push(fRows[i].Site);
-		}
-		this.print('operator Site: '+operatorSites.length);
-		if (operatorSites.length==0){
-			let msg='User '+currentUser.DisplayName;
-			msg+=' is not a '+operator;
-			this.generateErrorMsg(msg);
-			return false;
-		}
       //implementation of currentSite in operatorSites
       if (!operatorSites.includes(currentSite.siteNumber)){
 
@@ -1743,28 +1779,22 @@ function(){
 }
 
 crfVisit.updateRegistration=
-function(){
+function(cb=null,data){
+   //data is just inserted/updated registration row
+   
 	let fName="[updateRegistration]";
 	this.print(fName);
    let pM=this.getIdManager();
-	let idFieldName=pM.getCrfEntryFieldName(pM.getMode());
+	let idFieldName=pM.getCrfEntryFieldName('STUDY');
 	//have to reload query data
-	let regQueryPars=variableList.parseVariables(crfSetup.getSettings('registrationQuery'));
-   let regQuery=regQueryPars['query'];
-	let fQuery=crfData.getQuerySnapshot(regQuery);
-
-	if (fQuery.rows.length==0) {
-		this.print(fName+" registration is empty");
-		return; //registration is empty
-	}
-	let regEntry=fQuery.rows[0];
+	let regEntry=data.rows[0];
 
 	for (x in regEntry){
 		this.print(fName+" ["+x+"] "+regEntry[x]);
 	}
 
 
-	let studyId=fQuery.rows[0][idFieldName];
+	let studyId=data.rows[0][idFieldName];
 	if (!studyId) {
 		this.print(fName+" study id not set ("+idFieldName+'/'+studyId+")");
 		return; //study id not set
@@ -1772,6 +1802,7 @@ function(){
 	
 	//set 
 	pM.setParticipantIdToCrfEntry("STUDY",studyId);
+   pM.updateCrfEntry();
 	//this will only update crfEntry in memory, but not on LabKey, 
 	//we are counting on updateFlag to follow updateRegistration
 
@@ -1780,11 +1811,12 @@ function(){
 		let parentCrfEntry=crfSetup.getRows('parentCrfData')[0];
 		parentCrfEntry[idFieldName]=studyId;
       let that=this;
-      let action={name:"updateRegistration",cb:function(){that.doNothing();}};
-		let cb=function(data){that.completeWithFlag(data,action);};
-      this.modifyRows('update','lists','crfEntry',[parentCrfEntry],cb,crfSetup.getContainer('CRF'));
+      let doNothing=function(){that.doNothing();};
+      let action={name:"updateRegistration",cb:doNothing};
+		let complete=function(data){that.completeWithFlag(data,action);};
+      this.modifyRows('update','lists','crfEntry',[parentCrfEntry],complete,crfSetup.getContainer('CRF'));
 	}
-  
+   if (cb) cb(data);
 
 }
 
@@ -1798,6 +1830,7 @@ function(){
 
 crfVisit.configureIdManager=
 function(){
+   let fName='[configureIdManager]';
    let idMode=this.formEntry['idMode'];
    //set default value if no value is in the list (read value is null)
    if (!idMode) idMode="STUDY:EDIT";
@@ -1827,7 +1860,11 @@ function(){
          let id=pM.getParticipantIdFromCrfEntry(pM.getMode());
          pM.setLabelMode(pM.getMode(),id);
       }
-      pM.setEditMode(pM.getMode());
+
+      let localId=pM.getParticipantIdFromCrfEntry('LOCAL');
+      if (localId) pM.setLabelMode('LOCAL',localId);
+      else pM.setEditMode(pM.getMode());
+      pM.setReadonly('STUDY');
       pM.setLabelMode('STUDY','NOT SET');
    }