Assign record to other user

9 posts by 3 authors in: Forums > CMS Builder
Last Post: June 14, 2022   (RSS)

By andreasml - June 5, 2022

Hi,

I use CSMB to record medical records. Various users have access to their personal data only by using their own username/password to login and by having "Author" privileges. Thus, each user logs in, inserts data of his/her patients and he/she is the only user that can have access (modify, delete etc) to these data.

I wonder whether it was possible to let a user, inserting data of a patient and assign them to another user. For example, I would like the user A to log in as Author, create a record and then assign this record to user B. From now on, this record would be seen only by user B and not user A. 

I suspect that a way to solve it could be if user A was able to change the "Created by" field on the top of each record, in this case to user B. Thus, this new patient's record would be visible (and editable) by the user B only. Unfortunately, this is not allowed to be done by a user who has access privileges as an "Author". I wonder whether it would be possible to leave this "Created by" field free to be changed by those who have "Author" privileges apart from those who have "Editor" privileges.

Of course I am free of any other way to get around this issue.

Kind regards and thank you for your great support.

Andreas Lazaris 

By pgplast - June 6, 2022

This can be done with a simple plugin.

First you include a user list in your section (e.g. 'new_owner') that pulls the nums and names of all your users from your accounts.

When the person creates a new section record, he selects from this list the person to whom he wishes the ownership re-assigned.

On save of the record, a 'post_save' plugin then captures the record number of the section record and the 'new_owner' value and updates the value of createByUseNum  (only if a value for 'new_owner' has been saved).

By andreasml - June 7, 2022

Hi

Thank you for your reply. 

On save of the record, a 'post_save' plugin then captures the record number of the section record and the 'new_owner' value and updates the value of createByUseNum  (only if a value for 'new_owner' has been saved).

How can I create this? Could you please give an example?

Regards, 

Andreas

By pgplast - June 8, 2022

Hi:

Create a field of list type in your section called (e.g.), ‘new_owner.’

Under ‘list’ type select ‘get options from MYSQL query’ from the list options

Put into the code field

SELECT num,CONCAT_WS(", ",last_name,first_name)
FROM `<?php echo $TABLE_PREFIX ?>accounts`
ORDER BY last_name

——

Create plugin called 'new_record_owner.php,'  a la:

<?php
/*
Plugin Name: Reassign record ownership
Description: Plugin file: new_record_owner.php, table: 'your_section_name'
Version: #####
Requires at least: 3.0
*/

addAction('record_postsave', 'new_record_owner', null, 2);

//
function new_record_owner($tableName,$recordNum) {
global $TABLE_PREFIX,$CURRENT_USER, $SETTINGS;

if($tableName!="new_owner") {
return;
}

if (@$_REQUEST['new_owner']) {

$new_owner_num = $_REQUEST['new_owner'];
$record_num = $_REQUEST['num'];

require_once("lib/viewer_functions.php");

mysqlStrictMode(false);


$query = "UPDATE 'your_section_name' SET

createdByUserNum = '".$new_owner_num."',
updatedByUserNum = '".$new_owner_num."',

WHERE num = '".$record_num."'";
mysqli()->query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysqli()->error) . "\n");


}// end if

}// end function
?>

upload and activate the plugin.

Create a dummy record in your section and select a new owner from the 'new_owner" list field and then check back to see if it now belongs to your newly selected owner.

Note that you have to put your section name in the appropriate areas above.

By pgplast - June 9, 2022

Yes, sorry.

First, add a checkbox field to your section called 'assign_new_owner.'

(Leave the default as 0 (false).)

Before saving a record, check the 'assign_new_owner.' checkbox and select a new owner from the list.

When the record is saved, the ownership will be changed and the 'assign_new_owner.' and 'new_owner' valses will be resret so another save of the record will have no effect.

Use the plugin code below

This works fine on my server.

<?php
/*
Plugin Name: Reassign record ownership
Description: Plugin file: new_record_owner.php, table: 'test_update_owner'
Version: #####
Requires at least: 3.0
*/

addAction('record_postsave', 'new_record_owner', null, 2);

//
function new_record_owner($tableName,$recordNum) {
global $TABLE_PREFIX,$CURRENT_USER, $SETTINGS;

if($tableName!="test_update_owner") {
return;
}

if (@$_REQUEST['assign_new_owner'] && @$_REQUEST['new_owner']) {

$new_owner_num = $_REQUEST['new_owner'];
$record_num = $_REQUEST['num'];

require_once("lib/viewer_functions.php");

mysqlStrictMode(false);

$query = "UPDATE cmsb_test_update_owner SET

assign_new_owner = '0',
new_owner = NULL,
updatedDate = NOW(),
createdByUserNum = '".$new_owner_num."',
updatedByUserNum = '".$new_owner_num."'
WHERE num = '".mysql_escape( $record_num )."'";
mysqli()->query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysqli()->error) . "\n");

}// end if

}// end function
?>

By andreasml - June 9, 2022

Hi 

Thanks again!! It seems to work for me also (with some very mild modifications). I will let you know in more detail as long as I try it a bit more.

Kind regards,

Andreas

By andreasml - June 10, 2022

Hi

It does work!! 

I wonder if there is a way to apply this to more than one sections, or you need to create a different plugin for every different section. 

Again, thanks for your assistance, 

Kind regards, 

Andreas

By daniel - June 14, 2022

Hey Andreas,

Glad you were able to get this sorted out!

In regards to getting it working for different sections, the key is the "$tableName" variable and this portion of the code:

if($tableName!="test_update_owner") {
return;
}

As it stands, this checks the current section and halts if it's not "test_update_owner" - it could be modified to permit multiple sections:

if($tableName!="test_update_owner" && $tableName != "other_permitted_table") {
return;
}

It could also be updated to take different actions depending on the section with some moderate modifications:

if($tableName == "test_update_owner") {

 // run code here

} elseif ($tableName == "other_permitted_table") {

 // run alternate code here

}

Hope that helps!

Daniel
Technical Lead
interactivetools.com