Owner groupID change from user area

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
amyat
Veteran Member
Posts: 45
Joined: 2020-06-24 20:39

Owner groupID change from user area

Post by amyat » 2020-08-16 19:22

hi,

https://imgur.com/AIvJWdR

my code is

Code: Select all

 if( $memberInfo['group']!='guest'){	
		ob_start(); ?>
		<script>
			$j(function(){
				<?php if($selectedID){ ?>
					$j('#student_form_dv_action_buttons .btn-toolbar').append(
						'<div class="btn-group-vertical btn-group-lg" style="width: 100%;">' +
						
							'<button type="button" class="btn btn-warning btn-lg" onclick="accept()">' +
							'<i class="glyphicon glyphicon-ok"></i> Accept!</button>' +
						'</div>'
					);
				<?php } ?>
			});
			
			function accept(){
				alert("We're doing something else!");
			}
		</script>
		
		<?php
		 }
i want to change this record owner to another as like as batch transfer
function accept(){
This Record owner groupID(guest) change to another groupID(student) and carry this record
}
can u help me this query
thank you :)

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Owner groupID change from user area

Post by jsetzer » 2020-08-16 19:32

Hi,
I don't know your requirements in detail, but I recommend using the following, helpful AppGini PHP functions. You can then for playing around with record ownership. The function names are quite clear, I think.

Helpful AppGini functions (PHP)
  • getMemberInfo()
  • get_group_id()
  • getMemberInfo("differentMemberID")
  • get_group_id("differentMemberID")
  • set_record_owner("TABLENAME", "primaryKey", "newOwnerMemberID")
Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Owner groupID change from user area

Post by jsetzer » 2020-08-16 19:55

By the way, I'm afraid you will not be able to do ownership changes in pure Javascript (on client side / browser). So the functions I have named before are serverside PHP. You can do those changes on serverside for example in TABLENAME_after_update hook OR you will have to write clientside Javascript code which uses AJAX to call a serverside PHP script.

I guess it will be much easier to just add an additional Checkbox "Accept" (integer, checkox), check the value after update and then do your group-/ownership-changes on serverside.

I don't know if there is a function for changing the group-membership of a member. So maybe you will have to do this by updating groupID in table membership_users using SQL.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

amyat
Veteran Member
Posts: 45
Joined: 2020-06-24 20:39

Re: Owner groupID change from user area

Post by amyat » 2020-08-17 13:35

thank you,
now i'm trying in tablename_after_update().
but Update query is not working in function tablename_after_update()???
my code is
---------------------------------------
function student_form_after_update($data, $memberInfo, &$args) {
$record_t =sqlValue("UPDATE membership_userrecords SET groupID='57' WHERE pkValue='{$selectedID}'");
$ID =sqlValue("UPDATE membership_users SET groupID='57' WHERE memberID=(select memberID from membership_userrecords where pkValue='{$selectedID}')");
return TRUE;
}
---------------------------------------

it is work when i put in
function student_form_dv($selectedID, $memberInfo, &$html, &$args) { }

may be Update query is not working in _after_update()????

if u have another idea, please give me
just only i want to update user groupID as like as batch transfer

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Owner groupID change from user area

Post by jsetzer » 2020-08-17 14:02

1) sqlValue() is a function for getting a value. You should use sql("UPDATE ...", $eo) function instead
2) you have missed membership_userrecords.tableName in your first query. So, your command will set groupID=57 to all records which, by change, have the same pkValue='$selectedID'. This will mess up your ownership in other tables.
3) the same problem (missing `tableName` column) in 2nd query. Your sub-select will return all memberID's from all records having the same $selectedID by chance.

I hope I will find the time to prepare sample code later today.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Owner groupID change from user area

Post by jsetzer » 2020-08-17 15:03

The following code allows you to change a user's group membership by PHP code. Attention: There is a problem probably due to AppGini's caching implementation. See my annotation at the end.

Code

Code: Select all

function TABLENAME_after_update($data, $memberInfo, &$args)
{
	// As recommended, I have added an additional checkbox named "agreement"
	// which seems easiest to me. But you may work out a different solution

	// your specific condition here, for example...
	$user_has_agreed = $data["agreement"] === "1";

	// decision: if agreed, move to group      (1)     else move to group (2)
	$targetGroupName = $user_has_agreed ? "geschäftsführung" : "assistenz";
	$username = $memberInfo["username"];

	// see function below
	return membership_setGroup($username, $targetGroupName, true);
}

Code: Select all

// new function
function membership_setGroup($username, $groupName, $skipPerm = false)
{
	if (!$skipPerm && !getLoggedAdmin()) die("Permission denied.");

	$memberInfo = getMemberInfo($username);
	if (!$memberInfo["username"] || !$memberInfo["approved"] || $memberInfo["banned"]) die("Invalid, un-approved or banned user.");

	$memberIDSafe = makeSafe(trim($memberInfo["username"]));
	$currentGroupNameSafe = makeSafe(trim($memberInfo["group"]));
	$currentGroupID = sqlValue("SELECT groupID FROM `membership_groups` WHERE `name` = '{$currentGroupNameSafe}' LIMIT 1");
	$newGroupNameSafe = makeSafe(trim($groupName));
	$newGroupID = sqlValue("SELECT groupID FROM `membership_groups` WHERE `name` = '{$newGroupNameSafe}' LIMIT 1");

	if (!$currentGroupID) die("unknown source group");
	if (!$newGroupID) die("unknown target group");

	$sql = "UPDATE membership_users SET groupID='{$newGroupID}' WHERE memberID='{$memberIDSafe}' LIMIT 1";
	return sql($sql, $eo);
}
(1) user is logged in as "assistenz"
chrome_S28Wq6RaQJ.png
chrome_S28Wq6RaQJ.png (9.33 KiB) Viewed 2685 times

(2) she belongs to group "Assistenz"
chrome_RKLJE7Uyv5.png
chrome_RKLJE7Uyv5.png (2.14 KiB) Viewed 2685 times

(3) check the "agreement" checkbox and save record
chrome_RlygxXIvYv.png
chrome_RlygxXIvYv.png (2.3 KiB) Viewed 2685 times

(4) now she is in the more privileged group
chrome_w6DtxaFlvT.png
chrome_w6DtxaFlvT.png (2.46 KiB) Viewed 2685 times

The database change works fine...
chrome_Q9rjlh5Tm9.png
chrome_Q9rjlh5Tm9.png (5.33 KiB) Viewed 2685 times

...but there is a problem!

Problem

I guess due to AppGini's caching, the membership info will not be reloaded after database change. This means, even if there is the new groupID in the membership_users record, the permission stays the same.

I do not see any way to reset AppGini's cache, because memberinfo-caching has been implemented as a local static variable and therefore I must not touch it from the outside.

So, I'm afraid you will have to force users to log out and log in again.

Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

amyat
Veteran Member
Posts: 45
Joined: 2020-06-24 20:39

Re: Owner groupID change from user area

Post by amyat » 2020-08-20 19:31

Thank you
i got what i wanted
Sorry for delay
------------------------------------------------------
function student_form_after_update($data, $memberInfo, &$args) {
if( $memberInfo['group']!='guest'){
$record_ownerID=sqlValue("SELECT membership_userrecords.memberID from membership_userrecords where pkValue='" . makeSafe($data['stud_reg_id']) . "'");
$owner_groupID=sqlValue("SELECT membership_users.groupID FROM membership_users WHERE memberID='{$record_ownerID}'");
$group_name=sqlValue("SELECT membership_groups.name FROM membership_groups WHERE groupID='{$owner_groupID}'");
$changeGroupID=sqlValue("SELECT membership_groups.groupID FROM membership_groups WHERE name='student'");
if($group_name=='guest'){
$sqli="UPDATE membership_userrecords SET groupID='{$changeGroupID}' WHERE pkValue='" . makeSafe($data['stud_reg_id']) . "'";
$sql = "UPDATE membership_users SET groupID='{$changeGroupID}' WHERE memberID='{$record_ownerID}' LIMIT 1";
$sql_d="DELETE FROM membership_userpermissions WHERE memberID='{$record_ownerID}' AND tableName='student_form'";
sql($sql, $eo);
sql($sqli, $eo);
sql($sql_d, $eo);}
}
return TRUE;
}

-----------------------------------------------------
(Check data)Save update and auto change group
I got idea for your help.
thank you

Post Reply