Eric,
Here is a process that you can use to reassign cases in batches.
[*] First, create a separate process that will be used to reassign cases:
[attachment=4]ReassignCasesProcess.png[/attachment]
Assign the users who will be able to reassign cases to the first task in this process. These users must have the PM_REASSIGNCASES permission in their roles.
[*] Second, create a form that will be used to select the users and cases to be reassigned:
[attachment=3]ReassignCasesForm.png[/attachment]
This form has the following JavaScript code:
[code]
var nRows = $("#reassignCasesList").getNumberRows();
//if first empty row in grid by default, then delete it:
if (nRows == 1 && $("#reassignCasesList").getValue(1,2) == ''){
$("#reassignCasesList").deleteRow(1);
nRows = 0;
}
else {
//when DynaForm loads, disable cases in the grid which can't be reassigned:
for (var i = 1; i <= nRows; i++) {
// if the "canAssign" field is not true, then disable the "Reassign?" checkbox
// and set the row's color to grey
if ($("#reassignCasesList").getValue(i, 9) != '1') {
var oCheckbox = $("[id='form[reassignCasesList]["+i+"][reassign]']");
oCheckbox.prop("disabled", true);
oCheckbox.closest("div.pmdynaform-grid-row").css("backgroundColor","rgb(220,220,220)")
}
}
}
$("#cancelForm").find("button").click(function() {
$("#userAction").setValue("CANCEL");
$("form").submitForm();
});
$("#form\\[reassignCases\\]").on("click", function() {
$("#userAction").setValue("REASSIGN");
$("form").submitForm();
});
$("#fromUser").setOnchange(function(newVal, oldVal) {
if (newVal != '' && $("#toUser").getValue() != '') {
$("#userAction").setValue("FIND_CASES");
$("form").submitForm();
}
});
$("#toUser").setOnchange(function(newVal, oldVal) {
if (newVal != '' && $("#fromUser").getValue() != '') {
$("#userAction").setValue("FIND_CASES");
$("form").submitForm();
}
});
$("#selectAll").find("button").click(function() {
var label = $(this).find("span").text().trim();
//toggle button's label:
if (label == 'Select All') {
$(this).find("span").text("Unselect All");
}
else {
$(this).find("span").text("Select All");
}
//select/unselect all reassignable cases:
for (var i = 1; i <= nRows; i++) {
// check if the "canAssign" field is true, before select/unselect case
if ($("#reassignCasesList").getValue(i, 9) == '1') {
if (label == 'Select All') {
$("#reassignCasesList").setValue('1', i, 1);
} else {
$("#reassignCasesList").setValue('0', i, 1);
}
}
}
});
[/code]
[*]Then, add the following trigger to the process which is set to fire [i]before assignment[/i] in the first task.
[code=php]
//set to the ID of the DynaForm to select cases to reassign
$dynaformId = '98974920459b87661b4f415036264074';
if (@@userAction == 'CANCEL') {
@@userAction = '';
@=reassignCasesList = null;
@@fromUser = '';
@@toUser = '';
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
elseif (@@userAction == 'REASSIGN') {
@@userAction = ''; //reset for next use of form
$g = new G();
if (empty(@@fromUser)) {
$g->SendMessageText('Please select a "From User".', "INFO");
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
elseif (empty(@@toUser)) {
$g->SendMessageText('Please select a "To User".', "INFO");
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
global $RBAC;
if ($RBAC->userCanAccess('PM_REASSIGNCASE') != 1) {
$g->SendMessageText('Please login as a user who has the PM_REASSSIGNCASE permission in her role',
'WARNING');
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
$oCase = new Cases();
@@feedback = '';
foreach (@=reassignCasesList as $aCase) {
if ($aCase['reassign'] == '1') {
try {
$g->sessionVarSave();
$oCase->reassignCase($aCase['caseId'], $aCase['delIndex'], @@fromUser, @@toUser, 'REASSIGN');
$g->sessionVarRestore();
}
catch (Exception $e) {
$g->sessionVarRestore();
$g->SendMessageText("Unable to reassign case {$aCase['caseNo']} to ".@@toUser_label.
".\n".$e->getMessage()."\n".@@feedback, "ERROR");
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
@@feedback .= (empty(@@feedback) ? '' : "\n") .
"Case {$aCase['caseNo']} in \"{$aCase['process']} / {$aCase['task']}\" reassigned to ".
@@toUser_label;
}
}
//inform user which cases were reassigned:
@@feedback = empty(@@feedback) ? "No cases selected for reassignment" : @@feedback;
$g->SendMessageText(@@feedback, 'INFO');
//clear form:
@=reassignCasesList = null;
@@fromUser = '';
@@toUser = '';
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
//if user selected "From User" or "To User"
elseif (@@userAction == 'FIND_CASES') {
@@userAction = '';
if (empty(@@fromUser)) {
@=reassignCasesList = array();
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
$sql = "SELECT APP_UID AS caseId, DEL_INDEX AS delIndex, APP_TITLE AS caseNo, TAS_UID AS taskId,
APP_PRO_TITLE AS process, APP_TAS_TITLE AS task, APP_UPDATE_DATE AS lastUpdate,
DEL_TASK_DUE_DATE AS dueDate FROM APP_CACHE_VIEW
WHERE USR_UID='".@@fromUser."' AND DEL_THREAD_STATUS='OPEN' AND
(APP_STATUS = 'TO_D0' OR APP_STATUS = 'DRAFT')";
$aActiveCases = executeQuery($sql);
$aReassignables = array();
$aNotReassignables = array();
foreach ($aActiveCases as $aCase) {
$aCase['canReassign'] = 0; //case can't be reassigned
if (!empty(@@toUser)) {
//check whether the To User can be reassigned, i.e. is in the assignment pool for task
require_once 'classes/model/TaskUser.php';
$oTU = new TaskUser();
$aAssignedUsers = $oTU->getAllUsersTask($aCase['taskId']);
foreach ($aAssignedUsers as $aUser) {
//if user is in the assignment pool, then set that the case can be reassigned:
if ($aUser['USR_UID'] == @@toUser) {
$aCase['canReassign'] = 1;
break;
}
}
}
if ($aCase['canReassign']) {
$aReassignables[] = $aCase;
} else {
$aNotReassignables[] = $aCase;
}
}
//add unreassignable cases to the end of the list:
$aReassignables = array_merge($aReassignables, $aNotReassignables);
@=reassignCasesList = array();
$caseCounter = 1;
//change from a normal array to an associative array which starts counting from 1
//so can be used by grid
foreach ($aReassignables as $aCase) {
@=reassignCasesList[$caseCounter++] = $aCase;
}
PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', $dynaformId);
}
[/code]
Note that this trigger code won't work in the ProcessMaker Mobile App because it doesn't support the PMFRedirectToStep() function. If you want to be able to use this process in the mobile app, then you will have create a different trigger. Then, modify the above JavaScript to use the $.ajax() to execute that trigger by calling the REST endpoint PUT case/{app_uid/execute-trigger.
When a case is run, the user will first select the user who currently assigned to the cases in "From User" and the user to reassign to those cases in "To User". The form will be redisplayed showing the available cases. Cases which can't be reassigned to the selected "To User" will be displayed in grey rows below the cases which can be reassigned. Select the cases to reassign and then click on the "Reassign Cases" button at the button of the form.
SelectCasesToReassign.png (51.9 KiB) Viewed 11062 times
After the cases have been reassigned, the form will be redisplayed again with a feedback message at the top in green to let the user know which cases were reassigned.
FeedbackAfterReassigningCase.png (35.9 KiB) Viewed 11062 times
Here is the "Reassign Cases" process to download and import into your installation of ProcessMaker:
(37.33 KiB) Downloaded 401 times
You only have to create one case and then use that same case over and over to do reassignment. If you don't want to run this as a process, then you can adapt this code to use it in a plugin.
Note: If your users can have hundreds of active cases, then you will have to increase the maximum number of allowed fields in your
php.ini file on your server. See:
http://wiki.processmaker.com/3.0/Changi ... ber_fields