Include subprocess cases in Participated cases

From pmusers
Jump to: navigation, search

Subprocess cases are considered separate from their parent cases. Users who participated in the parent case will not see the subprocess case when they go to Home > Participated. This programming example shows how to make subprocess case appear in the list of participated cases for users who were assigned to tasks in the parent case. After the subprocess case is created, users who participated in the master case are temporarily assigned to the first task in the subprocess case, so that the subprocess case will appear in the list under Home > Participated.



Subprocess: Participated_Subprocess-1.pmx (right click and select Save Link As)
Parent Process: Parent_process_with_participated_subprocess-1.pmx (right click and select Save Link As)
Author: Amos Batto (amos[at]processmaker[dot]com)
Version: 1.0 (2019-03-13)
Tested in: PM 3.3.4 Enterprise in Debian 9.5 with PHP 5.6.37



Create the following parent process:

ParticipatedParentProcessInDesigner.png

Assign users to "Task 1" and "Task 2".

Then, create the following subprocess:

ParticipatedSubprocessInDesigner.png

All the users, who are assigned to "Task 1" and "Task 2" in the parent parent, should also be assigned Ad Hoc to the "Subordinate Task" in the subprocess. If they aren't assigned, then it won't be possible to temporarily assign these users to the "Subordinate Task" so that they can see the subprocess case under Home > Participated.

AdHocAssignmentSubprocessTask.png


Then, add the following trigger to the parent process:

//look up the new subprocess case in the database:
$parentCaseId = @@APPLICATION;
$sql = "SELECT SA.APP_UID, AD.USR_UID, AD.TAS_UID FROM SUB_APPLICATION SA, APP_DELEGATION AD 
   WHERE SA.APP_PARENT='$parentCaseId' AND SA.APP_UID=AD.APP_UID AND AD.DEL_INDEX=1";
$aResult = executeQuery($sql);
@@aResult = $aResult;

$subcaseId = $aResult[1]['APP_UID'];
$subcaseUserId = $aResult[1]['USR_UID'];
$subcaseIndex = 1;

$currentUserId = @@USER_LOGGED;
$currentIndex = @%INDEX;

//change the logged user to the "admin" who has the PM_REASSIGNCASE permission:
$_SESSION['USER_LOGGED'] = '00000000000000000000000000000001';

$c = new Cases();
$aUsersParticipated = $c->getUsersParticipatedInCase($parentCaseId);

//get users assigned to first task in subprocess
$d = new Derivation();
$aUsersSubtask = $d->getAllUsersFromAnyTask( $aResult[1]['TAS_UID'], true );

foreach ($aUsersParticipated['array'] as $userId => $userInfo) {
   //if the user who participated in the parent case is also in the assignment list of 
   //the suprocess's first task, then temporarily reassign the case to the user so she
   //can see the case under Home > Participated
   if (in_array($userId, $aUsersSubtask)) { 
      $c->reassignCase($subcaseId, $subcaseIndex, $subcaseUserId, $userId);
      $subcaseUserId = $userId;
      $subcaseIndex++;
   }
}

//reassign first task in subprocess case back to the original user:
$c->reassignCase($subcaseId, $subcaseIndex, $subcaseUserId, $aResult[1]['USR_UID']);

//reset session variables back to their original values:
$_SESSION['APPLICATION'] = $parentCaseId;
$_SESSION['INDEX']       = $currentIndex;
$_SESSION['USER_LOGGED'] = $currentUserId;

Set this trigger to fire after routing in "Task 2" of the parent process. At this point, the subprocess case will already have been created, so it can be found with a database query.

First, the trigger queries the SUB_APPLICATION table in the database to determine the ID of the new subprocess case that was just created and the APP_DELEGATION to find out who is assigned to the first task in the subprocess case.

Then, the trigger stores the current case ID, logged-in user and index number in parent case, so that they later restored. Then, the trigger switches the logged-in user to "admin," because she is guaranteed to have the PM_REASSIGNCASE permission in her role. Note that this code can be commented out if all the users assigned to "Task 2" have this permission in their roles.

Then, the trigger calls the Cases::getUsersParticipatedInCase() function to get a list of the users who were assigned to tasks in the parent case and the Derivation::getAllUsersFromAnyTask() to get all the users who are in the assignment list for the first task in the subprocess.

Then, the code loops through the list of users who participated in the parent case. If they are also in the assignment list of the subprocess's first task, then Cases::reassignCase() is called to temporarily assign these users to the task in the subprocess case, so they will see the subprocess case in their list of Home > Participated.

Finally, the trigger code restores the original case ID, index number and logged-in user, in case the Cases::reassignCase() function has changed them.